blob: bcf3b272f29310804a46ec2a1a2808dee43cca34 [file] [log] [blame]
peah522d71b2017-02-23 05:16:26 -08001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/aec3/subtractor.h"
peah522d71b2017-02-23 05:16:26 -080012
13#include <algorithm>
14#include <numeric>
15#include <string>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/audio_processing/aec3/aec_state.h"
Per Åhgren8ba58612017-12-01 23:01:44 +010018#include "modules/audio_processing/aec3/render_delay_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/audio_processing/test/echo_canceller_test_tools.h"
20#include "rtc_base/random.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020021#include "rtc_base/strings/string_builder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "test/gtest.h"
peah522d71b2017-02-23 05:16:26 -080023
24namespace webrtc {
25namespace {
26
27float RunSubtractorTest(int num_blocks_to_process,
28 int delay_samples,
Per Åhgrenee8ad5f2018-08-10 21:15:48 +020029 int main_filter_length_blocks,
30 int shadow_filter_length_blocks,
peah522d71b2017-02-23 05:16:26 -080031 bool uncorrelated_inputs,
32 const std::vector<int>& blocks_with_echo_path_changes) {
33 ApmDataDumper data_dumper(42);
Per Åhgren09a718a2017-12-11 22:28:45 +010034 EchoCanceller3Config config;
Per Åhgrenee8ad5f2018-08-10 21:15:48 +020035 config.filter.main.length_blocks = main_filter_length_blocks;
36 config.filter.shadow.length_blocks = shadow_filter_length_blocks;
37
Per Åhgren09a718a2017-12-11 22:28:45 +010038 Subtractor subtractor(config, &data_dumper, DetectOptimization());
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020039 absl::optional<DelayEstimate> delay_estimate;
Per Åhgrend112c752019-09-02 13:56:56 +000040 std::vector<std::vector<float>> x(3, std::vector<float>(kBlockSize, 0.f));
peah522d71b2017-02-23 05:16:26 -080041 std::vector<float> y(kBlockSize, 0.f);
42 std::array<float, kBlockSize> x_old;
43 SubtractorOutput output;
Per Åhgrenc59a5762017-12-11 21:34:19 +010044 config.delay.default_delay = 1;
Per Åhgren8ba58612017-12-01 23:01:44 +010045 std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
Per Åhgrend112c752019-09-02 13:56:56 +000046 RenderDelayBuffer::Create(config, 48000));
Per Åhgren971de072018-03-14 23:23:47 +010047 RenderSignalAnalyzer render_signal_analyzer(config);
peah522d71b2017-02-23 05:16:26 -080048 Random random_generator(42U);
49 Aec3Fft fft;
peah522d71b2017-02-23 05:16:26 -080050 std::array<float, kFftLengthBy2Plus1> Y2;
51 std::array<float, kFftLengthBy2Plus1> E2_main;
52 std::array<float, kFftLengthBy2Plus1> E2_shadow;
Per Åhgren09a718a2017-12-11 22:28:45 +010053 AecState aec_state(config);
peah522d71b2017-02-23 05:16:26 -080054 x_old.fill(0.f);
55 Y2.fill(0.f);
56 E2_main.fill(0.f);
57 E2_shadow.fill(0.f);
58
59 DelayBuffer<float> delay_buffer(delay_samples);
60 for (int k = 0; k < num_blocks_to_process; ++k) {
Per Åhgrend112c752019-09-02 13:56:56 +000061 RandomizeSampleVector(&random_generator, x[0]);
peah522d71b2017-02-23 05:16:26 -080062 if (uncorrelated_inputs) {
63 RandomizeSampleVector(&random_generator, y);
64 } else {
Per Åhgrend112c752019-09-02 13:56:56 +000065 delay_buffer.Delay(x[0], y);
peah522d71b2017-02-23 05:16:26 -080066 }
Per Åhgren8ba58612017-12-01 23:01:44 +010067 render_delay_buffer->Insert(x);
68 if (k == 0) {
69 render_delay_buffer->Reset();
70 }
Per Åhgrenc59a5762017-12-11 21:34:19 +010071 render_delay_buffer->PrepareCaptureProcessing();
Per Åhgrenc59a5762017-12-11 21:34:19 +010072 render_signal_analyzer.Update(*render_delay_buffer->GetRenderBuffer(),
Per Åhgren5c532d32018-03-22 00:29:25 +010073 aec_state.FilterDelayBlocks());
peah522d71b2017-02-23 05:16:26 -080074
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()) {
Per Åhgren8ba58612017-12-01 23:01:44 +010079 subtractor.HandleEchoPathChange(EchoPathVariability(
80 true, EchoPathVariability::DelayAdjustment::kNewDetectedDelay,
81 false));
peah522d71b2017-02-23 05:16:26 -080082 }
Per Åhgrenc59a5762017-12-11 21:34:19 +010083 subtractor.Process(*render_delay_buffer->GetRenderBuffer(), y,
Per Åhgren8ba58612017-12-01 23:01:44 +010084 render_signal_analyzer, aec_state, &output);
peah522d71b2017-02-23 05:16:26 -080085
Per Åhgren8ba58612017-12-01 23:01:44 +010086 aec_state.HandleEchoPathChange(EchoPathVariability(
87 false, EchoPathVariability::DelayAdjustment::kNone, false));
Per Åhgren3ab308f2018-02-21 08:46:03 +010088 aec_state.Update(delay_estimate, subtractor.FilterFrequencyResponse(),
peah29103572017-07-11 02:54:02 -070089 subtractor.FilterImpulseResponse(),
Per Åhgren0e6d2f52017-12-20 22:19:56 +010090 *render_delay_buffer->GetRenderBuffer(), E2_main, Y2,
Per Åhgrenb20b9372018-07-13 00:22:54 +020091 output, y);
peah522d71b2017-02-23 05:16:26 -080092 }
93
94 const float output_power = std::inner_product(
95 output.e_main.begin(), output.e_main.end(), output.e_main.begin(), 0.f);
96 const float y_power = std::inner_product(y.begin(), y.end(), y.begin(), 0.f);
97 if (y_power == 0.f) {
98 ADD_FAILURE();
99 return -1.0;
100 }
101 return output_power / y_power;
102}
103
Per Åhgren09a718a2017-12-11 22:28:45 +0100104std::string ProduceDebugText(size_t delay, int filter_length_blocks) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200105 rtc::StringBuilder ss;
Per Åhgren09a718a2017-12-11 22:28:45 +0100106 ss << "Delay: " << delay << ", ";
107 ss << "Length: " << filter_length_blocks;
Jonas Olsson84df1c72018-09-14 16:59:32 +0200108 return ss.Release();
peah522d71b2017-02-23 05:16:26 -0800109}
110
111} // namespace
112
113#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
114
115// Verifies that the check for non data dumper works.
116TEST(Subtractor, NullDataDumper) {
Per Åhgren09a718a2017-12-11 22:28:45 +0100117 EXPECT_DEATH(
118 Subtractor(EchoCanceller3Config(), nullptr, DetectOptimization()), "");
peah522d71b2017-02-23 05:16:26 -0800119}
120
121// Verifies the check for null subtractor output.
122// TODO(peah): Re-enable the test once the issue with memory leaks during DEATH
123// tests on test bots has been fixed.
124TEST(Subtractor, DISABLED_NullOutput) {
125 ApmDataDumper data_dumper(42);
Per Åhgren09a718a2017-12-11 22:28:45 +0100126 EchoCanceller3Config config;
127 Subtractor subtractor(config, &data_dumper, DetectOptimization());
Per Åhgren8ba58612017-12-01 23:01:44 +0100128 std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
Per Åhgrend112c752019-09-02 13:56:56 +0000129 RenderDelayBuffer::Create(config, 48000));
Per Åhgren971de072018-03-14 23:23:47 +0100130 RenderSignalAnalyzer render_signal_analyzer(config);
peah522d71b2017-02-23 05:16:26 -0800131 std::vector<float> y(kBlockSize, 0.f);
132
Per Åhgren09a718a2017-12-11 22:28:45 +0100133 EXPECT_DEATH(
134 subtractor.Process(*render_delay_buffer->GetRenderBuffer(), y,
135 render_signal_analyzer, AecState(config), nullptr),
136 "");
peah522d71b2017-02-23 05:16:26 -0800137}
138
139// Verifies the check for the capture signal size.
140TEST(Subtractor, WrongCaptureSize) {
141 ApmDataDumper data_dumper(42);
Per Åhgren09a718a2017-12-11 22:28:45 +0100142 EchoCanceller3Config config;
143 Subtractor subtractor(config, &data_dumper, DetectOptimization());
Per Åhgren8ba58612017-12-01 23:01:44 +0100144 std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
Per Åhgrend112c752019-09-02 13:56:56 +0000145 RenderDelayBuffer::Create(config, 48000));
Per Åhgren971de072018-03-14 23:23:47 +0100146 RenderSignalAnalyzer render_signal_analyzer(config);
peah522d71b2017-02-23 05:16:26 -0800147 std::vector<float> y(kBlockSize - 1, 0.f);
148 SubtractorOutput output;
149
Per Åhgren09a718a2017-12-11 22:28:45 +0100150 EXPECT_DEATH(
151 subtractor.Process(*render_delay_buffer->GetRenderBuffer(), y,
152 render_signal_analyzer, AecState(config), &output),
153 "");
peah522d71b2017-02-23 05:16:26 -0800154}
155
156#endif
157
158// Verifies that the subtractor is able to converge on correlated data.
159TEST(Subtractor, Convergence) {
160 std::vector<int> blocks_with_echo_path_changes;
Per Åhgren09a718a2017-12-11 22:28:45 +0100161 for (size_t filter_length_blocks : {12, 20, 30}) {
162 for (size_t delay_samples : {0, 64, 150, 200, 301}) {
163 SCOPED_TRACE(ProduceDebugText(delay_samples, filter_length_blocks));
peah522d71b2017-02-23 05:16:26 -0800164
Per Åhgrenee8ad5f2018-08-10 21:15:48 +0200165 float echo_to_nearend_power = RunSubtractorTest(
166 400, delay_samples, filter_length_blocks, filter_length_blocks, false,
167 blocks_with_echo_path_changes);
Per Åhgren019008b2017-12-18 11:38:39 +0100168
169 // Use different criteria to take overmodelling into account.
170 if (filter_length_blocks == 12) {
171 EXPECT_GT(0.1f, echo_to_nearend_power);
172 } else {
173 EXPECT_GT(1.f, echo_to_nearend_power);
174 }
Per Åhgren09a718a2017-12-11 22:28:45 +0100175 }
peah522d71b2017-02-23 05:16:26 -0800176 }
177}
178
Per Åhgrenee8ad5f2018-08-10 21:15:48 +0200179// Verifies that the subtractor is able to handle the case when the main filter
180// is longer than the shadow filter.
181TEST(Subtractor, MainFilterLongerThanShadowFilter) {
182 std::vector<int> blocks_with_echo_path_changes;
183 float echo_to_nearend_power =
184 RunSubtractorTest(400, 64, 20, 15, false, blocks_with_echo_path_changes);
185 EXPECT_GT(0.5f, echo_to_nearend_power);
186}
187
188// Verifies that the subtractor is able to handle the case when the shadow
189// filter is longer than the main filter.
190TEST(Subtractor, ShadowFilterLongerThanMainFilter) {
191 std::vector<int> blocks_with_echo_path_changes;
192 float echo_to_nearend_power =
193 RunSubtractorTest(400, 64, 15, 20, false, blocks_with_echo_path_changes);
194 EXPECT_GT(0.5f, echo_to_nearend_power);
195}
196
peah522d71b2017-02-23 05:16:26 -0800197// Verifies that the subtractor does not converge on uncorrelated signals.
198TEST(Subtractor, NonConvergenceOnUncorrelatedSignals) {
199 std::vector<int> blocks_with_echo_path_changes;
Per Åhgren09a718a2017-12-11 22:28:45 +0100200 for (size_t filter_length_blocks : {12, 20, 30}) {
201 for (size_t delay_samples : {0, 64, 150, 200, 301}) {
202 SCOPED_TRACE(ProduceDebugText(delay_samples, filter_length_blocks));
peah522d71b2017-02-23 05:16:26 -0800203
Per Åhgrenee8ad5f2018-08-10 21:15:48 +0200204 float echo_to_nearend_power = RunSubtractorTest(
205 300, delay_samples, filter_length_blocks, filter_length_blocks, true,
206 blocks_with_echo_path_changes);
Per Åhgren019008b2017-12-18 11:38:39 +0100207 EXPECT_NEAR(1.f, echo_to_nearend_power, 0.1);
Per Åhgren09a718a2017-12-11 22:28:45 +0100208 }
peah522d71b2017-02-23 05:16:26 -0800209 }
210}
211
peah522d71b2017-02-23 05:16:26 -0800212} // namespace webrtc