xref: /aosp_15_r20/external/webrtc/modules/audio_processing/aec3/erl_estimator_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 "modules/audio_processing/aec3/erl_estimator.h"
12 
13 #include "rtc_base/strings/string_builder.h"
14 #include "test/gtest.h"
15 
16 namespace webrtc {
17 
18 namespace {
ProduceDebugText(size_t num_render_channels,size_t num_capture_channels)19 std::string ProduceDebugText(size_t num_render_channels,
20                              size_t num_capture_channels) {
21   rtc::StringBuilder ss;
22   ss << "Render channels: " << num_render_channels;
23   ss << ", Capture channels: " << num_capture_channels;
24   return ss.Release();
25 }
26 
VerifyErl(const std::array<float,kFftLengthBy2Plus1> & erl,float erl_time_domain,float reference)27 void VerifyErl(const std::array<float, kFftLengthBy2Plus1>& erl,
28                float erl_time_domain,
29                float reference) {
30   std::for_each(erl.begin(), erl.end(),
31                 [reference](float a) { EXPECT_NEAR(reference, a, 0.001); });
32   EXPECT_NEAR(reference, erl_time_domain, 0.001);
33 }
34 
35 }  // namespace
36 
37 class ErlEstimatorMultiChannel
38     : public ::testing::Test,
39       public ::testing::WithParamInterface<std::tuple<size_t, size_t>> {};
40 
41 INSTANTIATE_TEST_SUITE_P(MultiChannel,
42                          ErlEstimatorMultiChannel,
43                          ::testing::Combine(::testing::Values(1, 2, 8),
44                                             ::testing::Values(1, 2, 8)));
45 
46 // Verifies that the correct ERL estimates are achieved.
TEST_P(ErlEstimatorMultiChannel,Estimates)47 TEST_P(ErlEstimatorMultiChannel, Estimates) {
48   const size_t num_render_channels = std::get<0>(GetParam());
49   const size_t num_capture_channels = std::get<1>(GetParam());
50   SCOPED_TRACE(ProduceDebugText(num_render_channels, num_capture_channels));
51   std::vector<std::array<float, kFftLengthBy2Plus1>> X2(num_render_channels);
52   for (auto& X2_ch : X2) {
53     X2_ch.fill(0.f);
54   }
55   std::vector<std::array<float, kFftLengthBy2Plus1>> Y2(num_capture_channels);
56   for (auto& Y2_ch : Y2) {
57     Y2_ch.fill(0.f);
58   }
59   std::vector<bool> converged_filters(num_capture_channels, false);
60   const size_t converged_idx = num_capture_channels - 1;
61   converged_filters[converged_idx] = true;
62 
63   ErlEstimator estimator(0);
64 
65   // Verifies that the ERL estimate is properly reduced to lower values.
66   for (auto& X2_ch : X2) {
67     X2_ch.fill(500 * 1000.f * 1000.f);
68   }
69   Y2[converged_idx].fill(10 * X2[0][0]);
70   for (size_t k = 0; k < 200; ++k) {
71     estimator.Update(converged_filters, X2, Y2);
72   }
73   VerifyErl(estimator.Erl(), estimator.ErlTimeDomain(), 10.f);
74 
75   // Verifies that the ERL is not immediately increased when the ERL in the
76   // data increases.
77   Y2[converged_idx].fill(10000 * X2[0][0]);
78   for (size_t k = 0; k < 998; ++k) {
79     estimator.Update(converged_filters, X2, Y2);
80   }
81   VerifyErl(estimator.Erl(), estimator.ErlTimeDomain(), 10.f);
82 
83   // Verifies that the rate of increase is 3 dB.
84   estimator.Update(converged_filters, X2, Y2);
85   VerifyErl(estimator.Erl(), estimator.ErlTimeDomain(), 20.f);
86 
87   // Verifies that the maximum ERL is achieved when there are no low RLE
88   // estimates.
89   for (size_t k = 0; k < 1000; ++k) {
90     estimator.Update(converged_filters, X2, Y2);
91   }
92   VerifyErl(estimator.Erl(), estimator.ErlTimeDomain(), 1000.f);
93 
94   // Verifies that the ERL estimate is is not updated for low-level signals
95   for (auto& X2_ch : X2) {
96     X2_ch.fill(1000.f * 1000.f);
97   }
98   Y2[converged_idx].fill(10 * X2[0][0]);
99   for (size_t k = 0; k < 200; ++k) {
100     estimator.Update(converged_filters, X2, Y2);
101   }
102   VerifyErl(estimator.Erl(), estimator.ErlTimeDomain(), 1000.f);
103 }
104 }  // namespace webrtc
105