OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ |
| 10 |
| 11 #include "webrtc/modules/audio_processing/level_controller/noise_spectrum_estima
tor.h" |
| 12 |
| 13 #include <string.h> |
| 14 #include <algorithm> |
| 15 |
| 16 #include "webrtc/base/array_view.h" |
| 17 #include "webrtc/base/arraysize.h" |
| 18 #include "webrtc/modules/audio_processing/logging/apm_data_dumper.h" |
| 19 |
| 20 namespace webrtc { |
| 21 namespace { |
| 22 float kMinNoisePower = 100.f; |
| 23 } // namespace |
| 24 |
| 25 NoiseSpectrumEstimator::NoiseSpectrumEstimator(ApmDataDumper* data_dumper) |
| 26 : data_dumper_(data_dumper) { |
| 27 Initialize(); |
| 28 } |
| 29 |
| 30 void NoiseSpectrumEstimator::Initialize() { |
| 31 std::fill(noise_spectrum_, noise_spectrum_ + arraysize(noise_spectrum_), |
| 32 kMinNoisePower); |
| 33 } |
| 34 |
| 35 void NoiseSpectrumEstimator::Update(rtc::ArrayView<const float> spectrum, |
| 36 bool first_update) { |
| 37 RTC_DCHECK_EQ(65u, spectrum.size()); |
| 38 |
| 39 if (first_update) { |
| 40 // Initialize the noise spectral estimate with the signal spectrum. |
| 41 std::copy(spectrum.data(), spectrum.data() + spectrum.size(), |
| 42 noise_spectrum_); |
| 43 } else { |
| 44 // Smoothly update the noise spectral estimate towards the signal spectrum |
| 45 // such that the magnitude of the updates are limited. |
| 46 for (size_t k = 0; k < spectrum.size(); ++k) { |
| 47 if (noise_spectrum_[k] < spectrum[k]) { |
| 48 noise_spectrum_[k] = std::min( |
| 49 1.01f * noise_spectrum_[k], |
| 50 noise_spectrum_[k] + 0.05f * (spectrum[k] - noise_spectrum_[k])); |
| 51 } else { |
| 52 noise_spectrum_[k] = std::max( |
| 53 0.99f * noise_spectrum_[k], |
| 54 noise_spectrum_[k] + 0.05f * (spectrum[k] - noise_spectrum_[k])); |
| 55 } |
| 56 } |
| 57 } |
| 58 |
| 59 // Ensure that the noise spectal estimate does not become too low. |
| 60 for (auto& v : noise_spectrum_) { |
| 61 v = std::max(v, kMinNoisePower); |
| 62 } |
| 63 |
| 64 data_dumper_->DumpRaw("lc_noise_spectrum", 65, noise_spectrum_); |
| 65 data_dumper_->DumpRaw("lc_signal_spectrum", spectrum); |
| 66 } |
| 67 |
| 68 } // namespace webrtc |
OLD | NEW |