blob: 07b121337a93fa9277f35973ff3db78fa8f63394 [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
11#include "webrtc/modules/audio_processing/aec3/subtractor.h"
12
13#include <algorithm>
14#include <numeric>
15#include <string>
16
peah522d71b2017-02-23 05:16:26 -080017#include "webrtc/modules/audio_processing/aec3/aec_state.h"
18#include "webrtc/modules/audio_processing/test/echo_canceller_test_tools.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020019#include "webrtc/rtc_base/random.h"
peah522d71b2017-02-23 05:16:26 -080020#include "webrtc/test/gtest.h"
21
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(),
peah522d71b2017-02-23 05:16:26 -080072 rtc::Optional<size_t>(delay_samples / kBlockSize),
peah29103572017-07-11 02:54:02 -070073 render_buffer, E2_main, Y2, x[0], output.s_main, false);
peah522d71b2017-02-23 05:16:26 -080074 }
75
76 const float output_power = std::inner_product(
77 output.e_main.begin(), output.e_main.end(), output.e_main.begin(), 0.f);
78 const float y_power = std::inner_product(y.begin(), y.end(), y.begin(), 0.f);
79 if (y_power == 0.f) {
80 ADD_FAILURE();
81 return -1.0;
82 }
83 return output_power / y_power;
84}
85
86std::string ProduceDebugText(size_t delay) {
87 std::ostringstream ss;
88 ss << "Delay: " << delay;
89 return ss.str();
90}
91
92} // namespace
93
94#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
95
96// Verifies that the check for non data dumper works.
97TEST(Subtractor, NullDataDumper) {
98 EXPECT_DEATH(Subtractor(nullptr, DetectOptimization()), "");
99}
100
101// Verifies the check for null subtractor output.
102// TODO(peah): Re-enable the test once the issue with memory leaks during DEATH
103// tests on test bots has been fixed.
104TEST(Subtractor, DISABLED_NullOutput) {
105 ApmDataDumper data_dumper(42);
106 Subtractor subtractor(&data_dumper, DetectOptimization());
peah86afe9d2017-04-06 15:45:32 -0700107 RenderBuffer render_buffer(Aec3Optimization::kNone, 3, kAdaptiveFilterLength,
108 std::vector<size_t>(1, kAdaptiveFilterLength));
peah522d71b2017-02-23 05:16:26 -0800109 RenderSignalAnalyzer render_signal_analyzer;
110 std::vector<float> y(kBlockSize, 0.f);
111
peah8cee56f2017-08-24 22:36:53 -0700112 EXPECT_DEATH(
113 subtractor.Process(render_buffer, y, render_signal_analyzer,
114 AecState(AudioProcessing::Config::EchoCanceller3{}),
115 nullptr),
116 "");
peah522d71b2017-02-23 05:16:26 -0800117}
118
119// Verifies the check for the capture signal size.
120TEST(Subtractor, WrongCaptureSize) {
121 ApmDataDumper data_dumper(42);
122 Subtractor subtractor(&data_dumper, DetectOptimization());
peah86afe9d2017-04-06 15:45:32 -0700123 RenderBuffer render_buffer(Aec3Optimization::kNone, 3, kAdaptiveFilterLength,
124 std::vector<size_t>(1, kAdaptiveFilterLength));
peah522d71b2017-02-23 05:16:26 -0800125 RenderSignalAnalyzer render_signal_analyzer;
126 std::vector<float> y(kBlockSize - 1, 0.f);
127 SubtractorOutput output;
128
peah8cee56f2017-08-24 22:36:53 -0700129 EXPECT_DEATH(
130 subtractor.Process(render_buffer, y, render_signal_analyzer,
131 AecState(AudioProcessing::Config::EchoCanceller3{}),
132 &output),
133 "");
peah522d71b2017-02-23 05:16:26 -0800134}
135
136#endif
137
138// Verifies that the subtractor is able to converge on correlated data.
139TEST(Subtractor, Convergence) {
140 std::vector<int> blocks_with_echo_path_changes;
141 for (size_t delay_samples : {0, 64, 150, 200, 301}) {
142 SCOPED_TRACE(ProduceDebugText(delay_samples));
143
144 float echo_to_nearend_power = RunSubtractorTest(
145 100, delay_samples, false, blocks_with_echo_path_changes);
146 EXPECT_GT(0.1f, echo_to_nearend_power);
147 }
148}
149
150// Verifies that the subtractor does not converge on uncorrelated signals.
151TEST(Subtractor, NonConvergenceOnUncorrelatedSignals) {
152 std::vector<int> blocks_with_echo_path_changes;
153 for (size_t delay_samples : {0, 64, 150, 200, 301}) {
154 SCOPED_TRACE(ProduceDebugText(delay_samples));
155
156 float echo_to_nearend_power = RunSubtractorTest(
157 100, delay_samples, true, blocks_with_echo_path_changes);
158 EXPECT_NEAR(1.f, echo_to_nearend_power, 0.05);
159 }
160}
161
162// Verifies that the subtractor is properly reset when there is an echo path
163// change.
164TEST(Subtractor, EchoPathChangeReset) {
165 std::vector<int> blocks_with_echo_path_changes;
166 blocks_with_echo_path_changes.push_back(99);
167 for (size_t delay_samples : {0, 64, 150, 200, 301}) {
168 SCOPED_TRACE(ProduceDebugText(delay_samples));
169
170 float echo_to_nearend_power = RunSubtractorTest(
171 100, delay_samples, false, blocks_with_echo_path_changes);
172 EXPECT_NEAR(1.f, echo_to_nearend_power, 0.0000001f);
173 }
174}
175
176} // namespace webrtc