blob: 0c296a7c184077cba68b26fc0805743130a33b46 [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;
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020043 AecState aec_state(EchoCanceller3Config{});
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
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200113 EXPECT_DEATH(subtractor.Process(render_buffer, y, render_signal_analyzer,
114 AecState(EchoCanceller3Config{}), nullptr),
115 "");
peah522d71b2017-02-23 05:16:26 -0800116}
117
118// Verifies the check for the capture signal size.
119TEST(Subtractor, WrongCaptureSize) {
120 ApmDataDumper data_dumper(42);
121 Subtractor subtractor(&data_dumper, DetectOptimization());
peah86afe9d2017-04-06 15:45:32 -0700122 RenderBuffer render_buffer(Aec3Optimization::kNone, 3, kAdaptiveFilterLength,
123 std::vector<size_t>(1, kAdaptiveFilterLength));
peah522d71b2017-02-23 05:16:26 -0800124 RenderSignalAnalyzer render_signal_analyzer;
125 std::vector<float> y(kBlockSize - 1, 0.f);
126 SubtractorOutput output;
127
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200128 EXPECT_DEATH(subtractor.Process(render_buffer, y, render_signal_analyzer,
129 AecState(EchoCanceller3Config{}), &output),
130 "");
peah522d71b2017-02-23 05:16:26 -0800131}
132
133#endif
134
135// Verifies that the subtractor is able to converge on correlated data.
136TEST(Subtractor, Convergence) {
137 std::vector<int> blocks_with_echo_path_changes;
138 for (size_t delay_samples : {0, 64, 150, 200, 301}) {
139 SCOPED_TRACE(ProduceDebugText(delay_samples));
140
141 float echo_to_nearend_power = RunSubtractorTest(
142 100, delay_samples, false, blocks_with_echo_path_changes);
143 EXPECT_GT(0.1f, echo_to_nearend_power);
144 }
145}
146
147// Verifies that the subtractor does not converge on uncorrelated signals.
148TEST(Subtractor, NonConvergenceOnUncorrelatedSignals) {
149 std::vector<int> blocks_with_echo_path_changes;
150 for (size_t delay_samples : {0, 64, 150, 200, 301}) {
151 SCOPED_TRACE(ProduceDebugText(delay_samples));
152
153 float echo_to_nearend_power = RunSubtractorTest(
154 100, delay_samples, true, blocks_with_echo_path_changes);
155 EXPECT_NEAR(1.f, echo_to_nearend_power, 0.05);
156 }
157}
158
159// Verifies that the subtractor is properly reset when there is an echo path
160// change.
161TEST(Subtractor, EchoPathChangeReset) {
162 std::vector<int> blocks_with_echo_path_changes;
163 blocks_with_echo_path_changes.push_back(99);
164 for (size_t delay_samples : {0, 64, 150, 200, 301}) {
165 SCOPED_TRACE(ProduceDebugText(delay_samples));
166
167 float echo_to_nearend_power = RunSubtractorTest(
168 100, delay_samples, false, blocks_with_echo_path_changes);
169 EXPECT_NEAR(1.f, echo_to_nearend_power, 0.0000001f);
170 }
171}
172
173} // namespace webrtc