• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2019 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 "modules/audio_processing/ns/quantile_noise_estimator.h"
12 
13 #include <algorithm>
14 
15 #include "modules/audio_processing/ns/fast_math.h"
16 
17 namespace webrtc {
18 
QuantileNoiseEstimator()19 QuantileNoiseEstimator::QuantileNoiseEstimator() {
20   quantile_.fill(0.f);
21   density_.fill(0.3f);
22   log_quantile_.fill(8.f);
23 
24   constexpr float kOneBySimult = 1.f / kSimult;
25   for (size_t i = 0; i < kSimult; ++i) {
26     counter_[i] = floor(kLongStartupPhaseBlocks * (i + 1.f) * kOneBySimult);
27   }
28 }
29 
Estimate(rtc::ArrayView<const float,kFftSizeBy2Plus1> signal_spectrum,rtc::ArrayView<float,kFftSizeBy2Plus1> noise_spectrum)30 void QuantileNoiseEstimator::Estimate(
31     rtc::ArrayView<const float, kFftSizeBy2Plus1> signal_spectrum,
32     rtc::ArrayView<float, kFftSizeBy2Plus1> noise_spectrum) {
33   std::array<float, kFftSizeBy2Plus1> log_spectrum;
34   LogApproximation(signal_spectrum, log_spectrum);
35 
36   int quantile_index_to_return = -1;
37   // Loop over simultaneous estimates.
38   for (int s = 0, k = 0; s < kSimult;
39        ++s, k += static_cast<int>(kFftSizeBy2Plus1)) {
40     const float one_by_counter_plus_1 = 1.f / (counter_[s] + 1.f);
41     for (int i = 0, j = k; i < static_cast<int>(kFftSizeBy2Plus1); ++i, ++j) {
42       // Update log quantile estimate.
43       const float delta = density_[j] > 1.f ? 40.f / density_[j] : 40.f;
44 
45       const float multiplier = delta * one_by_counter_plus_1;
46       if (log_spectrum[i] > log_quantile_[j]) {
47         log_quantile_[j] += 0.25f * multiplier;
48       } else {
49         log_quantile_[j] -= 0.75f * multiplier;
50       }
51 
52       // Update density estimate.
53       constexpr float kWidth = 0.01f;
54       constexpr float kOneByWidthPlus2 = 1.f / (2.f * kWidth);
55       if (fabs(log_spectrum[i] - log_quantile_[j]) < kWidth) {
56         density_[j] = (counter_[s] * density_[j] + kOneByWidthPlus2) *
57                       one_by_counter_plus_1;
58       }
59     }
60 
61     if (counter_[s] >= kLongStartupPhaseBlocks) {
62       counter_[s] = 0;
63       if (num_updates_ >= kLongStartupPhaseBlocks) {
64         quantile_index_to_return = k;
65       }
66     }
67 
68     ++counter_[s];
69   }
70 
71   // Sequentially update the noise during startup.
72   if (num_updates_ < kLongStartupPhaseBlocks) {
73     // Use the last "s" to get noise during startup that differ from zero.
74     quantile_index_to_return = kFftSizeBy2Plus1 * (kSimult - 1);
75     ++num_updates_;
76   }
77 
78   if (quantile_index_to_return >= 0) {
79     ExpApproximation(
80         rtc::ArrayView<const float>(&log_quantile_[quantile_index_to_return],
81                                     kFftSizeBy2Plus1),
82         quantile_);
83   }
84 
85   std::copy(quantile_.begin(), quantile_.end(), noise_spectrum.begin());
86 }
87 
88 }  // namespace webrtc
89