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