1 /*
2  *  Copyright (c) 2017 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/main_filter_update_gain.h"
12 
13 #include <algorithm>
14 #include <numeric>
15 #include <string>
16 
17 #include "modules/audio_processing/aec3/adaptive_fir_filter.h"
18 #include "modules/audio_processing/aec3/aec_state.h"
19 #include "modules/audio_processing/aec3/render_buffer.h"
20 #include "modules/audio_processing/aec3/render_signal_analyzer.h"
21 #include "modules/audio_processing/aec3/shadow_filter_update_gain.h"
22 #include "modules/audio_processing/aec3/subtractor_output.h"
23 #include "modules/audio_processing/logging/apm_data_dumper.h"
24 #include "modules/audio_processing/test/echo_canceller_test_tools.h"
25 #include "rtc_base/numerics/safe_minmax.h"
26 #include "rtc_base/random.h"
27 #include "test/gtest.h"
28 
29 namespace webrtc {
30 namespace {
31 
32 // Method for performing the simulations needed to test the main filter update
33 // gain functionality.
RunFilterUpdateTest(int num_blocks_to_process,size_t delay_samples,const std::vector<int> & blocks_with_echo_path_changes,const std::vector<int> & blocks_with_saturation,bool use_silent_render_in_second_half,std::array<float,kBlockSize> * e_last_block,std::array<float,kBlockSize> * y_last_block,FftData * G_last_block)34 void RunFilterUpdateTest(int num_blocks_to_process,
35                          size_t delay_samples,
36                          const std::vector<int>& blocks_with_echo_path_changes,
37                          const std::vector<int>& blocks_with_saturation,
38                          bool use_silent_render_in_second_half,
39                          std::array<float, kBlockSize>* e_last_block,
40                          std::array<float, kBlockSize>* y_last_block,
41                          FftData* G_last_block) {
42   ApmDataDumper data_dumper(42);
43   AdaptiveFirFilter main_filter(9, DetectOptimization(), &data_dumper);
44   AdaptiveFirFilter shadow_filter(9, DetectOptimization(), &data_dumper);
45   Aec3Fft fft;
46   RenderBuffer render_buffer(
47       Aec3Optimization::kNone, 3, main_filter.SizePartitions(),
48       std::vector<size_t>(1, main_filter.SizePartitions()));
49   std::array<float, kBlockSize> x_old;
50   x_old.fill(0.f);
51   ShadowFilterUpdateGain shadow_gain;
52   MainFilterUpdateGain main_gain;
53   Random random_generator(42U);
54   std::vector<std::vector<float>> x(3, std::vector<float>(kBlockSize, 0.f));
55   std::vector<float> y(kBlockSize, 0.f);
56   AecState aec_state(EchoCanceller3Config{});
57   RenderSignalAnalyzer render_signal_analyzer;
58   std::array<float, kFftLength> s_scratch;
59   std::array<float, kBlockSize> s;
60   FftData S;
61   FftData G;
62   SubtractorOutput output;
63   output.Reset();
64   FftData& E_main = output.E_main;
65   FftData E_shadow;
66   std::array<float, kFftLengthBy2Plus1> Y2;
67   std::array<float, kFftLengthBy2Plus1>& E2_main = output.E2_main;
68   std::array<float, kBlockSize>& e_main = output.e_main;
69   std::array<float, kBlockSize>& e_shadow = output.e_shadow;
70   Y2.fill(0.f);
71 
72   constexpr float kScale = 1.0f / kFftLengthBy2;
73 
74   DelayBuffer<float> delay_buffer(delay_samples);
75   for (int k = 0; k < num_blocks_to_process; ++k) {
76     // Handle echo path changes.
77     if (std::find(blocks_with_echo_path_changes.begin(),
78                   blocks_with_echo_path_changes.end(),
79                   k) != blocks_with_echo_path_changes.end()) {
80       main_filter.HandleEchoPathChange();
81     }
82 
83     // Handle saturation.
84     const bool saturation =
85         std::find(blocks_with_saturation.begin(), blocks_with_saturation.end(),
86                   k) != blocks_with_saturation.end();
87 
88     // Create the render signal.
89     if (use_silent_render_in_second_half && k > num_blocks_to_process / 2) {
90       std::fill(x[0].begin(), x[0].end(), 0.f);
91     } else {
92       RandomizeSampleVector(&random_generator, x[0]);
93     }
94     delay_buffer.Delay(x[0], y);
95     render_buffer.Insert(x);
96     render_signal_analyzer.Update(render_buffer, aec_state.FilterDelay());
97 
98     // Apply the main filter.
99     main_filter.Filter(render_buffer, &S);
100     fft.Ifft(S, &s_scratch);
101     std::transform(y.begin(), y.end(), s_scratch.begin() + kFftLengthBy2,
102                    e_main.begin(),
103                    [&](float a, float b) { return a - b * kScale; });
104     std::for_each(e_main.begin(), e_main.end(),
105                   [](float& a) { a = rtc::SafeClamp(a, -32768.f, 32767.f); });
106     fft.ZeroPaddedFft(e_main, &E_main);
107     for (size_t k = 0; k < kBlockSize; ++k) {
108       s[k] = kScale * s_scratch[k + kFftLengthBy2];
109     }
110 
111     // Apply the shadow filter.
112     shadow_filter.Filter(render_buffer, &S);
113     fft.Ifft(S, &s_scratch);
114     std::transform(y.begin(), y.end(), s_scratch.begin() + kFftLengthBy2,
115                    e_shadow.begin(),
116                    [&](float a, float b) { return a - b * kScale; });
117     std::for_each(e_shadow.begin(), e_shadow.end(),
118                   [](float& a) { a = rtc::SafeClamp(a, -32768.f, 32767.f); });
119     fft.ZeroPaddedFft(e_shadow, &E_shadow);
120 
121     // Compute spectra for future use.
122     E_main.Spectrum(Aec3Optimization::kNone, &output.E2_main);
123     E_shadow.Spectrum(Aec3Optimization::kNone, &output.E2_shadow);
124 
125     // Adapt the shadow filter.
126     shadow_gain.Compute(render_buffer, render_signal_analyzer, E_shadow,
127                         shadow_filter.SizePartitions(), saturation, &G);
128     shadow_filter.Adapt(render_buffer, G);
129 
130     // Adapt the main filter
131     main_gain.Compute(render_buffer, render_signal_analyzer, output,
132                       main_filter, saturation, &G);
133     main_filter.Adapt(render_buffer, G);
134 
135     // Update the delay.
136     aec_state.HandleEchoPathChange(EchoPathVariability(false, false));
137     aec_state.Update(main_filter.FilterFrequencyResponse(),
138                      main_filter.FilterImpulseResponse(), true, rtc::nullopt,
139                      render_buffer, E2_main, Y2, x[0], s, false);
140   }
141 
142   std::copy(e_main.begin(), e_main.end(), e_last_block->begin());
143   std::copy(y.begin(), y.end(), y_last_block->begin());
144   std::copy(G.re.begin(), G.re.end(), G_last_block->re.begin());
145   std::copy(G.im.begin(), G.im.end(), G_last_block->im.begin());
146 }
147 
ProduceDebugText(size_t delay)148 std::string ProduceDebugText(size_t delay) {
149   std::ostringstream ss;
150   ss << "Delay: " << delay;
151   return ss.str();
152 }
153 
154 }  // namespace
155 
156 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
157 
158 // Verifies that the check for non-null output gain parameter works.
TEST(MainFilterUpdateGain,NullDataOutputGain)159 TEST(MainFilterUpdateGain, NullDataOutputGain) {
160   ApmDataDumper data_dumper(42);
161   AdaptiveFirFilter filter(9, DetectOptimization(), &data_dumper);
162   RenderBuffer render_buffer(Aec3Optimization::kNone, 3,
163                              filter.SizePartitions(),
164                              std::vector<size_t>(1, filter.SizePartitions()));
165   RenderSignalAnalyzer analyzer;
166   SubtractorOutput output;
167   MainFilterUpdateGain gain;
168   EXPECT_DEATH(
169       gain.Compute(render_buffer, analyzer, output, filter, false, nullptr),
170       "");
171 }
172 
173 #endif
174 
175 // Verifies that the gain formed causes the filter using it to converge.
TEST(MainFilterUpdateGain,GainCausesFilterToConverge)176 TEST(MainFilterUpdateGain, GainCausesFilterToConverge) {
177   std::vector<int> blocks_with_echo_path_changes;
178   std::vector<int> blocks_with_saturation;
179   for (size_t delay_samples : {0, 64, 150, 200, 301}) {
180     SCOPED_TRACE(ProduceDebugText(delay_samples));
181 
182     std::array<float, kBlockSize> e;
183     std::array<float, kBlockSize> y;
184     FftData G;
185 
186     RunFilterUpdateTest(500, delay_samples, blocks_with_echo_path_changes,
187                         blocks_with_saturation, false, &e, &y, &G);
188 
189     // Verify that the main filter is able to perform well.
190     EXPECT_LT(1000 * std::inner_product(e.begin(), e.end(), e.begin(), 0.f),
191               std::inner_product(y.begin(), y.end(), y.begin(), 0.f));
192   }
193 }
194 
195 // Verifies that the magnitude of the gain on average decreases for a
196 // persistently exciting signal.
TEST(MainFilterUpdateGain,DecreasingGain)197 TEST(MainFilterUpdateGain, DecreasingGain) {
198   std::vector<int> blocks_with_echo_path_changes;
199   std::vector<int> blocks_with_saturation;
200 
201   std::array<float, kBlockSize> e;
202   std::array<float, kBlockSize> y;
203   FftData G_a;
204   FftData G_b;
205   FftData G_c;
206   std::array<float, kFftLengthBy2Plus1> G_a_power;
207   std::array<float, kFftLengthBy2Plus1> G_b_power;
208   std::array<float, kFftLengthBy2Plus1> G_c_power;
209 
210   RunFilterUpdateTest(100, 65, blocks_with_echo_path_changes,
211                       blocks_with_saturation, false, &e, &y, &G_a);
212   RunFilterUpdateTest(200, 65, blocks_with_echo_path_changes,
213                       blocks_with_saturation, false, &e, &y, &G_b);
214   RunFilterUpdateTest(300, 65, blocks_with_echo_path_changes,
215                       blocks_with_saturation, false, &e, &y, &G_c);
216 
217   G_a.Spectrum(Aec3Optimization::kNone, &G_a_power);
218   G_b.Spectrum(Aec3Optimization::kNone, &G_b_power);
219   G_c.Spectrum(Aec3Optimization::kNone, &G_c_power);
220 
221   EXPECT_GT(std::accumulate(G_a_power.begin(), G_a_power.end(), 0.),
222             std::accumulate(G_b_power.begin(), G_b_power.end(), 0.));
223 
224   EXPECT_GT(std::accumulate(G_b_power.begin(), G_b_power.end(), 0.),
225             std::accumulate(G_c_power.begin(), G_c_power.end(), 0.));
226 }
227 
228 // Verifies that the gain is zero when there is saturation and that the internal
229 // error estimates cause the gain to increase after a period of saturation.
TEST(MainFilterUpdateGain,SaturationBehavior)230 TEST(MainFilterUpdateGain, SaturationBehavior) {
231   std::vector<int> blocks_with_echo_path_changes;
232   std::vector<int> blocks_with_saturation;
233   for (int k = 99; k < 200; ++k) {
234     blocks_with_saturation.push_back(k);
235   }
236 
237   std::array<float, kBlockSize> e;
238   std::array<float, kBlockSize> y;
239   FftData G_a;
240   FftData G_b;
241   FftData G_a_ref;
242   G_a_ref.re.fill(0.f);
243   G_a_ref.im.fill(0.f);
244 
245   std::array<float, kFftLengthBy2Plus1> G_a_power;
246   std::array<float, kFftLengthBy2Plus1> G_b_power;
247 
248   RunFilterUpdateTest(100, 65, blocks_with_echo_path_changes,
249                       blocks_with_saturation, false, &e, &y, &G_a);
250 
251   EXPECT_EQ(G_a_ref.re, G_a.re);
252   EXPECT_EQ(G_a_ref.im, G_a.im);
253 
254   RunFilterUpdateTest(99, 65, blocks_with_echo_path_changes,
255                       blocks_with_saturation, false, &e, &y, &G_a);
256   RunFilterUpdateTest(201, 65, blocks_with_echo_path_changes,
257                       blocks_with_saturation, false, &e, &y, &G_b);
258 
259   G_a.Spectrum(Aec3Optimization::kNone, &G_a_power);
260   G_b.Spectrum(Aec3Optimization::kNone, &G_b_power);
261 
262   EXPECT_LT(std::accumulate(G_a_power.begin(), G_a_power.end(), 0.),
263             std::accumulate(G_b_power.begin(), G_b_power.end(), 0.));
264 }
265 
266 // Verifies that the gain increases after an echo path change.
TEST(MainFilterUpdateGain,EchoPathChangeBehavior)267 TEST(MainFilterUpdateGain, EchoPathChangeBehavior) {
268   std::vector<int> blocks_with_echo_path_changes;
269   std::vector<int> blocks_with_saturation;
270   blocks_with_echo_path_changes.push_back(99);
271 
272   std::array<float, kBlockSize> e;
273   std::array<float, kBlockSize> y;
274   FftData G_a;
275   FftData G_b;
276   std::array<float, kFftLengthBy2Plus1> G_a_power;
277   std::array<float, kFftLengthBy2Plus1> G_b_power;
278 
279   RunFilterUpdateTest(99, 65, blocks_with_echo_path_changes,
280                       blocks_with_saturation, false, &e, &y, &G_a);
281   RunFilterUpdateTest(100, 65, blocks_with_echo_path_changes,
282                       blocks_with_saturation, false, &e, &y, &G_b);
283 
284   G_a.Spectrum(Aec3Optimization::kNone, &G_a_power);
285   G_b.Spectrum(Aec3Optimization::kNone, &G_b_power);
286 
287   EXPECT_LT(std::accumulate(G_a_power.begin(), G_a_power.end(), 0.),
288             std::accumulate(G_b_power.begin(), G_b_power.end(), 0.));
289 }
290 
291 }  // namespace webrtc
292