blob: 0450b6ccd49033a582d78ca44611dddfc93603c5 [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"
18#include "modules/audio_processing/test/echo_canceller_test_tools.h"
19#include "rtc_base/random.h"
20#include "test/gtest.h"
peah522d71b2017-02-23 05:16:26 -080021
22namespace webrtc {
23namespace {
24
25float RunSubtractorTest(int num_blocks_to_process,
26 int delay_samples,
27 bool uncorrelated_inputs,
28 const std::vector<int>& blocks_with_echo_path_changes) {
29 ApmDataDumper data_dumper(42);
30 Subtractor subtractor(&data_dumper, DetectOptimization());
peah86afe9d2017-04-06 15:45:32 -070031 std::vector<std::vector<float>> x(3, std::vector<float>(kBlockSize, 0.f));
peah522d71b2017-02-23 05:16:26 -080032 std::vector<float> y(kBlockSize, 0.f);
33 std::array<float, kBlockSize> x_old;
34 SubtractorOutput output;
peah86afe9d2017-04-06 15:45:32 -070035 RenderBuffer render_buffer(Aec3Optimization::kNone, 3, kAdaptiveFilterLength,
36 std::vector<size_t>(1, kAdaptiveFilterLength));
peah522d71b2017-02-23 05:16:26 -080037 RenderSignalAnalyzer render_signal_analyzer;
38 Random random_generator(42U);
39 Aec3Fft fft;
peah522d71b2017-02-23 05:16:26 -080040 std::array<float, kFftLengthBy2Plus1> Y2;
41 std::array<float, kFftLengthBy2Plus1> E2_main;
42 std::array<float, kFftLengthBy2Plus1> E2_shadow;
peah8cee56f2017-08-24 22:36:53 -070043 AecState aec_state(AudioProcessing::Config::EchoCanceller3{});
peah522d71b2017-02-23 05:16:26 -080044 x_old.fill(0.f);
45 Y2.fill(0.f);
46 E2_main.fill(0.f);
47 E2_shadow.fill(0.f);
48
49 DelayBuffer<float> delay_buffer(delay_samples);
50 for (int k = 0; k < num_blocks_to_process; ++k) {
peah86afe9d2017-04-06 15:45:32 -070051 RandomizeSampleVector(&random_generator, x[0]);
peah522d71b2017-02-23 05:16:26 -080052 if (uncorrelated_inputs) {
53 RandomizeSampleVector(&random_generator, y);
54 } else {
peah86afe9d2017-04-06 15:45:32 -070055 delay_buffer.Delay(x[0], y);
peah522d71b2017-02-23 05:16:26 -080056 }
peah86afe9d2017-04-06 15:45:32 -070057 render_buffer.Insert(x);
58 render_signal_analyzer.Update(render_buffer, aec_state.FilterDelay());
peah522d71b2017-02-23 05:16:26 -080059
60 // Handle echo path changes.
61 if (std::find(blocks_with_echo_path_changes.begin(),
62 blocks_with_echo_path_changes.end(),
63 k) != blocks_with_echo_path_changes.end()) {
64 subtractor.HandleEchoPathChange(EchoPathVariability(true, true));
65 }
peah86afe9d2017-04-06 15:45:32 -070066 subtractor.Process(render_buffer, y, render_signal_analyzer, aec_state,
67 &output);
peah522d71b2017-02-23 05:16:26 -080068
peah86afe9d2017-04-06 15:45:32 -070069 aec_state.HandleEchoPathChange(EchoPathVariability(false, false));
peah522d71b2017-02-23 05:16:26 -080070 aec_state.Update(subtractor.FilterFrequencyResponse(),
peah29103572017-07-11 02:54:02 -070071 subtractor.FilterImpulseResponse(),
Per Ã…hgren1b4059e2017-10-15 20:19:21 +020072 subtractor.ConvergedFilter(),
peah522d71b2017-02-23 05:16:26 -080073 rtc::Optional<size_t>(delay_samples / kBlockSize),
peah29103572017-07-11 02:54:02 -070074 render_buffer, E2_main, Y2, x[0], output.s_main, false);
peah522d71b2017-02-23 05:16:26 -080075 }
76
77 const float output_power = std::inner_product(
78 output.e_main.begin(), output.e_main.end(), output.e_main.begin(), 0.f);
79 const float y_power = std::inner_product(y.begin(), y.end(), y.begin(), 0.f);
80 if (y_power == 0.f) {
81 ADD_FAILURE();
82 return -1.0;
83 }
84 return output_power / y_power;
85}
86
87std::string ProduceDebugText(size_t delay) {
88 std::ostringstream ss;
89 ss << "Delay: " << delay;
90 return ss.str();
91}
92
93} // namespace
94
95#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
96
97// Verifies that the check for non data dumper works.
98TEST(Subtractor, NullDataDumper) {
99 EXPECT_DEATH(Subtractor(nullptr, DetectOptimization()), "");
100}
101
102// Verifies the check for null subtractor output.
103// TODO(peah): Re-enable the test once the issue with memory leaks during DEATH
104// tests on test bots has been fixed.
105TEST(Subtractor, DISABLED_NullOutput) {
106 ApmDataDumper data_dumper(42);
107 Subtractor subtractor(&data_dumper, DetectOptimization());
peah86afe9d2017-04-06 15:45:32 -0700108 RenderBuffer render_buffer(Aec3Optimization::kNone, 3, kAdaptiveFilterLength,
109 std::vector<size_t>(1, kAdaptiveFilterLength));
peah522d71b2017-02-23 05:16:26 -0800110 RenderSignalAnalyzer render_signal_analyzer;
111 std::vector<float> y(kBlockSize, 0.f);
112
peah8cee56f2017-08-24 22:36:53 -0700113 EXPECT_DEATH(
114 subtractor.Process(render_buffer, y, render_signal_analyzer,
115 AecState(AudioProcessing::Config::EchoCanceller3{}),
116 nullptr),
117 "");
peah522d71b2017-02-23 05:16:26 -0800118}
119
120// Verifies the check for the capture signal size.
121TEST(Subtractor, WrongCaptureSize) {
122 ApmDataDumper data_dumper(42);
123 Subtractor subtractor(&data_dumper, DetectOptimization());
peah86afe9d2017-04-06 15:45:32 -0700124 RenderBuffer render_buffer(Aec3Optimization::kNone, 3, kAdaptiveFilterLength,
125 std::vector<size_t>(1, kAdaptiveFilterLength));
peah522d71b2017-02-23 05:16:26 -0800126 RenderSignalAnalyzer render_signal_analyzer;
127 std::vector<float> y(kBlockSize - 1, 0.f);
128 SubtractorOutput output;
129
peah8cee56f2017-08-24 22:36:53 -0700130 EXPECT_DEATH(
131 subtractor.Process(render_buffer, y, render_signal_analyzer,
132 AecState(AudioProcessing::Config::EchoCanceller3{}),
133 &output),
134 "");
peah522d71b2017-02-23 05:16:26 -0800135}
136
137#endif
138
139// Verifies that the subtractor is able to converge on correlated data.
140TEST(Subtractor, Convergence) {
141 std::vector<int> blocks_with_echo_path_changes;
142 for (size_t delay_samples : {0, 64, 150, 200, 301}) {
143 SCOPED_TRACE(ProduceDebugText(delay_samples));
144
145 float echo_to_nearend_power = RunSubtractorTest(
146 100, delay_samples, false, blocks_with_echo_path_changes);
147 EXPECT_GT(0.1f, echo_to_nearend_power);
148 }
149}
150
151// Verifies that the subtractor does not converge on uncorrelated signals.
152TEST(Subtractor, NonConvergenceOnUncorrelatedSignals) {
153 std::vector<int> blocks_with_echo_path_changes;
154 for (size_t delay_samples : {0, 64, 150, 200, 301}) {
155 SCOPED_TRACE(ProduceDebugText(delay_samples));
156
157 float echo_to_nearend_power = RunSubtractorTest(
158 100, delay_samples, true, blocks_with_echo_path_changes);
159 EXPECT_NEAR(1.f, echo_to_nearend_power, 0.05);
160 }
161}
162
163// Verifies that the subtractor is properly reset when there is an echo path
164// change.
165TEST(Subtractor, EchoPathChangeReset) {
166 std::vector<int> blocks_with_echo_path_changes;
167 blocks_with_echo_path_changes.push_back(99);
168 for (size_t delay_samples : {0, 64, 150, 200, 301}) {
169 SCOPED_TRACE(ProduceDebugText(delay_samples));
170
171 float echo_to_nearend_power = RunSubtractorTest(
172 100, delay_samples, false, blocks_with_echo_path_changes);
173 EXPECT_NEAR(1.f, echo_to_nearend_power, 0.0000001f);
174 }
175}
176
177} // namespace webrtc