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/shadow_filter_update_gain.h" |
| 12 |
| 13 #include <algorithm> |
| 14 #include <numeric> |
| 15 #include <string> |
| 16 #include <vector> |
| 17 |
| 18 #include "webrtc/base/random.h" |
| 19 #include "webrtc/modules/audio_processing/aec3/adaptive_fir_filter.h" |
| 20 #include "webrtc/modules/audio_processing/aec3/aec_state.h" |
| 21 #include "webrtc/modules/audio_processing/aec3/aec3_common.h" |
| 22 #include "webrtc/modules/audio_processing/test/echo_canceller_test_tools.h" |
| 23 #include "webrtc/test/gtest.h" |
| 24 |
| 25 namespace webrtc { |
| 26 namespace { |
| 27 |
| 28 // Method for performing the simulations needed to test the main filter update |
| 29 // gain functionality. |
| 30 void RunFilterUpdateTest(int num_blocks_to_process, |
| 31 size_t delay_samples, |
| 32 const std::vector<int>& blocks_with_saturation, |
| 33 std::array<float, kBlockSize>* e_last_block, |
| 34 std::array<float, kBlockSize>* y_last_block, |
| 35 FftData* G_last_block) { |
| 36 ApmDataDumper data_dumper(42); |
| 37 AdaptiveFirFilter main_filter(9, true, DetectOptimization(), &data_dumper); |
| 38 AdaptiveFirFilter shadow_filter(9, true, DetectOptimization(), &data_dumper); |
| 39 Aec3Fft fft; |
| 40 FftBuffer X_buffer(Aec3Optimization::kNone, main_filter.SizePartitions(), |
| 41 std::vector<size_t>(1, main_filter.SizePartitions())); |
| 42 std::array<float, kBlockSize> x_old; |
| 43 x_old.fill(0.f); |
| 44 ShadowFilterUpdateGain shadow_gain; |
| 45 Random random_generator(42U); |
| 46 std::vector<float> x(kBlockSize, 0.f); |
| 47 std::vector<float> y(kBlockSize, 0.f); |
| 48 AecState aec_state; |
| 49 RenderSignalAnalyzer render_signal_analyzer; |
| 50 FftData X; |
| 51 std::array<float, kFftLength> s; |
| 52 FftData S; |
| 53 FftData G; |
| 54 FftData E_shadow; |
| 55 std::array<float, kBlockSize> e_shadow; |
| 56 |
| 57 constexpr float kScale = 1.0f / kFftLengthBy2; |
| 58 |
| 59 DelayBuffer<float> delay_buffer(delay_samples); |
| 60 for (int k = 0; k < num_blocks_to_process; ++k) { |
| 61 // Handle saturation. |
| 62 bool saturation = |
| 63 std::find(blocks_with_saturation.begin(), blocks_with_saturation.end(), |
| 64 k) != blocks_with_saturation.end(); |
| 65 |
| 66 // Create the render signal. |
| 67 RandomizeSampleVector(&random_generator, x); |
| 68 delay_buffer.Delay(x, y); |
| 69 fft.PaddedFft(x, x_old, &X); |
| 70 X_buffer.Insert(X); |
| 71 render_signal_analyzer.Update( |
| 72 X_buffer, rtc::Optional<size_t>(delay_samples / kBlockSize)); |
| 73 |
| 74 shadow_filter.Filter(X_buffer, &S); |
| 75 fft.Ifft(S, &s); |
| 76 std::transform(y.begin(), y.end(), s.begin() + kFftLengthBy2, |
| 77 e_shadow.begin(), |
| 78 [&](float a, float b) { return a - b * kScale; }); |
| 79 std::for_each(e_shadow.begin(), e_shadow.end(), [](float& a) { |
| 80 a = std::max(std::min(a, 32767.0f), -32768.0f); |
| 81 }); |
| 82 fft.ZeroPaddedFft(e_shadow, &E_shadow); |
| 83 |
| 84 shadow_gain.Compute(X_buffer, render_signal_analyzer, E_shadow, |
| 85 shadow_filter.SizePartitions(), saturation, &G); |
| 86 shadow_filter.Adapt(X_buffer, G); |
| 87 } |
| 88 |
| 89 std::copy(e_shadow.begin(), e_shadow.end(), e_last_block->begin()); |
| 90 std::copy(y.begin(), y.end(), y_last_block->begin()); |
| 91 std::copy(G.re.begin(), G.re.end(), G_last_block->re.begin()); |
| 92 std::copy(G.im.begin(), G.im.end(), G_last_block->im.begin()); |
| 93 } |
| 94 |
| 95 std::string ProduceDebugText(size_t delay) { |
| 96 std::ostringstream ss; |
| 97 ss << ", Delay: " << delay; |
| 98 return ss.str(); |
| 99 } |
| 100 |
| 101 } // namespace |
| 102 |
| 103 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
| 104 |
| 105 // Verifies that the check for non-null output gain parameter works. |
| 106 TEST(ShadowFilterUpdateGain, NullDataOutputGain) { |
| 107 ApmDataDumper data_dumper(42); |
| 108 FftBuffer X_buffer(Aec3Optimization::kNone, 1, std::vector<size_t>(1, 1)); |
| 109 RenderSignalAnalyzer analyzer; |
| 110 FftData E; |
| 111 ShadowFilterUpdateGain gain; |
| 112 EXPECT_DEATH(gain.Compute(X_buffer, analyzer, E, 1, false, nullptr), ""); |
| 113 } |
| 114 |
| 115 #endif |
| 116 |
| 117 // Verifies that the gain formed causes the filter using it to converge. |
| 118 TEST(ShadowFilterUpdateGain, GainCausesFilterToConverge) { |
| 119 std::vector<int> blocks_with_echo_path_changes; |
| 120 std::vector<int> blocks_with_saturation; |
| 121 for (size_t delay_samples : {0, 64, 150, 200, 301}) { |
| 122 SCOPED_TRACE(ProduceDebugText(delay_samples)); |
| 123 |
| 124 std::array<float, kBlockSize> e; |
| 125 std::array<float, kBlockSize> y; |
| 126 FftData G; |
| 127 |
| 128 RunFilterUpdateTest(500, delay_samples, blocks_with_saturation, &e, &y, &G); |
| 129 |
| 130 // Verify that the main filter is able to perform well. |
| 131 EXPECT_LT(1000 * std::inner_product(e.begin(), e.end(), e.begin(), 0.f), |
| 132 std::inner_product(y.begin(), y.end(), y.begin(), 0.f)); |
| 133 } |
| 134 } |
| 135 |
| 136 // Verifies that the magnitude of the gain on average decreases for a |
| 137 // persistently exciting signal. |
| 138 TEST(ShadowFilterUpdateGain, DecreasingGain) { |
| 139 std::vector<int> blocks_with_echo_path_changes; |
| 140 std::vector<int> blocks_with_saturation; |
| 141 |
| 142 std::array<float, kBlockSize> e; |
| 143 std::array<float, kBlockSize> y; |
| 144 FftData G_a; |
| 145 FftData G_b; |
| 146 FftData G_c; |
| 147 std::array<float, kFftLengthBy2Plus1> G_a_power; |
| 148 std::array<float, kFftLengthBy2Plus1> G_b_power; |
| 149 std::array<float, kFftLengthBy2Plus1> G_c_power; |
| 150 |
| 151 RunFilterUpdateTest(100, 65, blocks_with_saturation, &e, &y, &G_a); |
| 152 RunFilterUpdateTest(200, 65, blocks_with_saturation, &e, &y, &G_b); |
| 153 RunFilterUpdateTest(300, 65, blocks_with_saturation, &e, &y, &G_c); |
| 154 |
| 155 G_a.Spectrum(Aec3Optimization::kNone, &G_a_power); |
| 156 G_b.Spectrum(Aec3Optimization::kNone, &G_b_power); |
| 157 G_c.Spectrum(Aec3Optimization::kNone, &G_c_power); |
| 158 |
| 159 EXPECT_GT(std::accumulate(G_a_power.begin(), G_a_power.end(), 0.), |
| 160 std::accumulate(G_b_power.begin(), G_b_power.end(), 0.)); |
| 161 |
| 162 EXPECT_GT(std::accumulate(G_b_power.begin(), G_b_power.end(), 0.), |
| 163 std::accumulate(G_c_power.begin(), G_c_power.end(), 0.)); |
| 164 } |
| 165 |
| 166 // Verifies that the gain is zero when there is saturation. |
| 167 TEST(ShadowFilterUpdateGain, SaturationBehavior) { |
| 168 std::vector<int> blocks_with_echo_path_changes; |
| 169 std::vector<int> blocks_with_saturation; |
| 170 for (int k = 99; k < 200; ++k) { |
| 171 blocks_with_saturation.push_back(k); |
| 172 } |
| 173 |
| 174 std::array<float, kBlockSize> e; |
| 175 std::array<float, kBlockSize> y; |
| 176 FftData G_a; |
| 177 FftData G_a_ref; |
| 178 G_a_ref.re.fill(0.f); |
| 179 G_a_ref.im.fill(0.f); |
| 180 |
| 181 RunFilterUpdateTest(100, 65, blocks_with_saturation, &e, &y, &G_a); |
| 182 |
| 183 EXPECT_EQ(G_a_ref.re, G_a.re); |
| 184 EXPECT_EQ(G_a_ref.im, G_a.im); |
| 185 } |
| 186 |
| 187 } // namespace webrtc |
OLD | NEW |