xref: /aosp_15_r20/external/webrtc/modules/audio_processing/aec3/stationarity_estimator.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2018 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/aec3/stationarity_estimator.h"
12 
13 #include <algorithm>
14 #include <array>
15 
16 #include "api/array_view.h"
17 #include "modules/audio_processing/aec3/aec3_common.h"
18 #include "modules/audio_processing/aec3/spectrum_buffer.h"
19 #include "modules/audio_processing/logging/apm_data_dumper.h"
20 
21 namespace webrtc {
22 
23 namespace {
24 constexpr float kMinNoisePower = 10.f;
25 constexpr int kHangoverBlocks = kNumBlocksPerSecond / 20;
26 constexpr int kNBlocksAverageInitPhase = 20;
27 constexpr int kNBlocksInitialPhase = kNumBlocksPerSecond * 2.;
28 }  // namespace
29 
StationarityEstimator()30 StationarityEstimator::StationarityEstimator()
31     : data_dumper_(new ApmDataDumper(instance_count_.fetch_add(1) + 1)) {
32   Reset();
33 }
34 
35 StationarityEstimator::~StationarityEstimator() = default;
36 
Reset()37 void StationarityEstimator::Reset() {
38   noise_.Reset();
39   hangovers_.fill(0);
40   stationarity_flags_.fill(false);
41 }
42 
43 // Update just the noise estimator. Usefull until the delay is known
UpdateNoiseEstimator(rtc::ArrayView<const std::array<float,kFftLengthBy2Plus1>> spectrum)44 void StationarityEstimator::UpdateNoiseEstimator(
45     rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> spectrum) {
46   noise_.Update(spectrum);
47   data_dumper_->DumpRaw("aec3_stationarity_noise_spectrum", noise_.Spectrum());
48   data_dumper_->DumpRaw("aec3_stationarity_is_block_stationary",
49                         IsBlockStationary());
50 }
51 
UpdateStationarityFlags(const SpectrumBuffer & spectrum_buffer,rtc::ArrayView<const float> render_reverb_contribution_spectrum,int idx_current,int num_lookahead)52 void StationarityEstimator::UpdateStationarityFlags(
53     const SpectrumBuffer& spectrum_buffer,
54     rtc::ArrayView<const float> render_reverb_contribution_spectrum,
55     int idx_current,
56     int num_lookahead) {
57   std::array<int, kWindowLength> indexes;
58   int num_lookahead_bounded = std::min(num_lookahead, kWindowLength - 1);
59   int idx = idx_current;
60 
61   if (num_lookahead_bounded < kWindowLength - 1) {
62     int num_lookback = (kWindowLength - 1) - num_lookahead_bounded;
63     idx = spectrum_buffer.OffsetIndex(idx_current, num_lookback);
64   }
65   // For estimating the stationarity properties of the current frame, the
66   // power for each band is accumulated for several consecutive spectra in the
67   // method EstimateBandStationarity.
68   // In order to avoid getting the indexes of the spectra for every band with
69   // its associated overhead, those indexes are stored in an array and then use
70   // when the estimation is done.
71   indexes[0] = idx;
72   for (size_t k = 1; k < indexes.size(); ++k) {
73     indexes[k] = spectrum_buffer.DecIndex(indexes[k - 1]);
74   }
75   RTC_DCHECK_EQ(
76       spectrum_buffer.DecIndex(indexes[kWindowLength - 1]),
77       spectrum_buffer.OffsetIndex(idx_current, -(num_lookahead_bounded + 1)));
78 
79   for (size_t k = 0; k < stationarity_flags_.size(); ++k) {
80     stationarity_flags_[k] = EstimateBandStationarity(
81         spectrum_buffer, render_reverb_contribution_spectrum, indexes, k);
82   }
83   UpdateHangover();
84   SmoothStationaryPerFreq();
85 }
86 
IsBlockStationary() const87 bool StationarityEstimator::IsBlockStationary() const {
88   float acum_stationarity = 0.f;
89   RTC_DCHECK_EQ(stationarity_flags_.size(), kFftLengthBy2Plus1);
90   for (size_t band = 0; band < stationarity_flags_.size(); ++band) {
91     bool st = IsBandStationary(band);
92     acum_stationarity += static_cast<float>(st);
93   }
94   return ((acum_stationarity * (1.f / kFftLengthBy2Plus1)) > 0.75f);
95 }
96 
EstimateBandStationarity(const SpectrumBuffer & spectrum_buffer,rtc::ArrayView<const float> average_reverb,const std::array<int,kWindowLength> & indexes,size_t band) const97 bool StationarityEstimator::EstimateBandStationarity(
98     const SpectrumBuffer& spectrum_buffer,
99     rtc::ArrayView<const float> average_reverb,
100     const std::array<int, kWindowLength>& indexes,
101     size_t band) const {
102   constexpr float kThrStationarity = 10.f;
103   float acum_power = 0.f;
104   const int num_render_channels =
105       static_cast<int>(spectrum_buffer.buffer[0].size());
106   const float one_by_num_channels = 1.f / num_render_channels;
107   for (auto idx : indexes) {
108     for (int ch = 0; ch < num_render_channels; ++ch) {
109       acum_power += spectrum_buffer.buffer[idx][ch][band] * one_by_num_channels;
110     }
111   }
112   acum_power += average_reverb[band];
113   float noise = kWindowLength * GetStationarityPowerBand(band);
114   RTC_CHECK_LT(0.f, noise);
115   bool stationary = acum_power < kThrStationarity * noise;
116   data_dumper_->DumpRaw("aec3_stationarity_long_ratio", acum_power / noise);
117   return stationary;
118 }
119 
AreAllBandsStationary()120 bool StationarityEstimator::AreAllBandsStationary() {
121   for (auto b : stationarity_flags_) {
122     if (!b)
123       return false;
124   }
125   return true;
126 }
127 
UpdateHangover()128 void StationarityEstimator::UpdateHangover() {
129   bool reduce_hangover = AreAllBandsStationary();
130   for (size_t k = 0; k < stationarity_flags_.size(); ++k) {
131     if (!stationarity_flags_[k]) {
132       hangovers_[k] = kHangoverBlocks;
133     } else if (reduce_hangover) {
134       hangovers_[k] = std::max(hangovers_[k] - 1, 0);
135     }
136   }
137 }
138 
SmoothStationaryPerFreq()139 void StationarityEstimator::SmoothStationaryPerFreq() {
140   std::array<bool, kFftLengthBy2Plus1> all_ahead_stationary_smooth;
141   for (size_t k = 1; k < kFftLengthBy2Plus1 - 1; ++k) {
142     all_ahead_stationary_smooth[k] = stationarity_flags_[k - 1] &&
143                                      stationarity_flags_[k] &&
144                                      stationarity_flags_[k + 1];
145   }
146 
147   all_ahead_stationary_smooth[0] = all_ahead_stationary_smooth[1];
148   all_ahead_stationary_smooth[kFftLengthBy2Plus1 - 1] =
149       all_ahead_stationary_smooth[kFftLengthBy2Plus1 - 2];
150 
151   stationarity_flags_ = all_ahead_stationary_smooth;
152 }
153 
154 std::atomic<int> StationarityEstimator::instance_count_(0);
155 
NoiseSpectrum()156 StationarityEstimator::NoiseSpectrum::NoiseSpectrum() {
157   Reset();
158 }
159 
160 StationarityEstimator::NoiseSpectrum::~NoiseSpectrum() = default;
161 
Reset()162 void StationarityEstimator::NoiseSpectrum::Reset() {
163   block_counter_ = 0;
164   noise_spectrum_.fill(kMinNoisePower);
165 }
166 
Update(rtc::ArrayView<const std::array<float,kFftLengthBy2Plus1>> spectrum)167 void StationarityEstimator::NoiseSpectrum::Update(
168     rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> spectrum) {
169   RTC_DCHECK_LE(1, spectrum[0].size());
170   const int num_render_channels = static_cast<int>(spectrum.size());
171 
172   std::array<float, kFftLengthBy2Plus1> avg_spectrum_data;
173   rtc::ArrayView<const float> avg_spectrum;
174   if (num_render_channels == 1) {
175     avg_spectrum = spectrum[0];
176   } else {
177     // For multiple channels, average the channel spectra before passing to the
178     // noise spectrum estimator.
179     avg_spectrum = avg_spectrum_data;
180     std::copy(spectrum[0].begin(), spectrum[0].end(),
181               avg_spectrum_data.begin());
182     for (int ch = 1; ch < num_render_channels; ++ch) {
183       for (size_t k = 1; k < kFftLengthBy2Plus1; ++k) {
184         avg_spectrum_data[k] += spectrum[ch][k];
185       }
186     }
187 
188     const float one_by_num_channels = 1.f / num_render_channels;
189     for (size_t k = 1; k < kFftLengthBy2Plus1; ++k) {
190       avg_spectrum_data[k] *= one_by_num_channels;
191     }
192   }
193 
194   ++block_counter_;
195   float alpha = GetAlpha();
196   for (size_t k = 0; k < kFftLengthBy2Plus1; ++k) {
197     if (block_counter_ <= kNBlocksAverageInitPhase) {
198       noise_spectrum_[k] += (1.f / kNBlocksAverageInitPhase) * avg_spectrum[k];
199     } else {
200       noise_spectrum_[k] =
201           UpdateBandBySmoothing(avg_spectrum[k], noise_spectrum_[k], alpha);
202     }
203   }
204 }
205 
GetAlpha() const206 float StationarityEstimator::NoiseSpectrum::GetAlpha() const {
207   constexpr float kAlpha = 0.004f;
208   constexpr float kAlphaInit = 0.04f;
209   constexpr float kTiltAlpha = (kAlphaInit - kAlpha) / kNBlocksInitialPhase;
210 
211   if (block_counter_ > (kNBlocksInitialPhase + kNBlocksAverageInitPhase)) {
212     return kAlpha;
213   } else {
214     return kAlphaInit -
215            kTiltAlpha * (block_counter_ - kNBlocksAverageInitPhase);
216   }
217 }
218 
UpdateBandBySmoothing(float power_band,float power_band_noise,float alpha) const219 float StationarityEstimator::NoiseSpectrum::UpdateBandBySmoothing(
220     float power_band,
221     float power_band_noise,
222     float alpha) const {
223   float power_band_noise_updated = power_band_noise;
224   if (power_band_noise < power_band) {
225     RTC_DCHECK_GT(power_band, 0.f);
226     float alpha_inc = alpha * (power_band_noise / power_band);
227     if (block_counter_ > kNBlocksInitialPhase) {
228       if (10.f * power_band_noise < power_band) {
229         alpha_inc *= 0.1f;
230       }
231     }
232     power_band_noise_updated += alpha_inc * (power_band - power_band_noise);
233   } else {
234     power_band_noise_updated += alpha * (power_band - power_band_noise);
235     power_band_noise_updated =
236         std::max(power_band_noise_updated, kMinNoisePower);
237   }
238   return power_band_noise_updated;
239 }
240 
241 }  // namespace webrtc
242