Line data Source code
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_estimator.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 0 : NoiseSpectrumEstimator::NoiseSpectrumEstimator(ApmDataDumper* data_dumper)
26 0 : : data_dumper_(data_dumper) {
27 0 : Initialize();
28 0 : }
29 :
30 0 : void NoiseSpectrumEstimator::Initialize() {
31 0 : std::fill(noise_spectrum_, noise_spectrum_ + arraysize(noise_spectrum_),
32 0 : kMinNoisePower);
33 0 : }
34 :
35 0 : void NoiseSpectrumEstimator::Update(rtc::ArrayView<const float> spectrum,
36 : bool first_update) {
37 0 : RTC_DCHECK_EQ(65, spectrum.size());
38 :
39 0 : if (first_update) {
40 : // Initialize the noise spectral estimate with the signal spectrum.
41 0 : std::copy(spectrum.data(), spectrum.data() + spectrum.size(),
42 0 : 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 0 : for (size_t k = 0; k < spectrum.size(); ++k) {
47 0 : if (noise_spectrum_[k] < spectrum[k]) {
48 0 : noise_spectrum_[k] = std::min(
49 0 : 1.01f * noise_spectrum_[k],
50 0 : noise_spectrum_[k] + 0.05f * (spectrum[k] - noise_spectrum_[k]));
51 : } else {
52 0 : noise_spectrum_[k] = std::max(
53 0 : 0.99f * noise_spectrum_[k],
54 0 : 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 0 : for (auto& v : noise_spectrum_) {
61 0 : v = std::max(v, kMinNoisePower);
62 : }
63 :
64 0 : data_dumper_->DumpRaw("lc_noise_spectrum", 65, noise_spectrum_);
65 0 : data_dumper_->DumpRaw("lc_signal_spectrum", spectrum);
66 0 : }
67 :
68 : } // namespace webrtc
|