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 "test/gtest.h"
14 
15 namespace webrtc {
16 
17 namespace {
18 
VerifyErl(const std::array<float,kFftLengthBy2Plus1> & erl,float erl_time_domain,float reference)19 void VerifyErl(const std::array<float, kFftLengthBy2Plus1>& erl,
20                float erl_time_domain,
21                float reference) {
22   std::for_each(erl.begin(), erl.end(),
23                 [reference](float a) { EXPECT_NEAR(reference, a, 0.001); });
24   EXPECT_NEAR(reference, erl_time_domain, 0.001);
25 }
26 
27 }  // namespace
28 
29 // Verifies that the correct ERL estimates are achieved.
TEST(ErlEstimator,Estimates)30 TEST(ErlEstimator, Estimates) {
31   std::array<float, kFftLengthBy2Plus1> X2;
32   std::array<float, kFftLengthBy2Plus1> Y2;
33 
34   ErlEstimator estimator;
35 
36   // Verifies that the ERL estimate is properly reduced to lower values.
37   X2.fill(500 * 1000.f * 1000.f);
38   Y2.fill(10 * X2[0]);
39   for (size_t k = 0; k < 200; ++k) {
40     estimator.Update(X2, Y2);
41   }
42   VerifyErl(estimator.Erl(), estimator.ErlTimeDomain(), 10.f);
43 
44   // Verifies that the ERL is not immediately increased when the ERL in the data
45   // increases.
46   Y2.fill(10000 * X2[0]);
47   for (size_t k = 0; k < 998; ++k) {
48     estimator.Update(X2, Y2);
49   }
50   VerifyErl(estimator.Erl(), estimator.ErlTimeDomain(), 10.f);
51 
52   // Verifies that the rate of increase is 3 dB.
53   estimator.Update(X2, Y2);
54   VerifyErl(estimator.Erl(), estimator.ErlTimeDomain(), 20.f);
55 
56   // Verifies that the maximum ERL is achieved when there are no low RLE
57   // estimates.
58   for (size_t k = 0; k < 1000; ++k) {
59     estimator.Update(X2, Y2);
60   }
61   VerifyErl(estimator.Erl(), estimator.ErlTimeDomain(), 1000.f);
62 
63   // Verifies that the ERL estimate is is not updated for low-level signals
64   X2.fill(1000.f * 1000.f);
65   Y2.fill(10 * X2[0]);
66   for (size_t k = 0; k < 200; ++k) {
67     estimator.Update(X2, Y2);
68   }
69   VerifyErl(estimator.Erl(), estimator.ErlTimeDomain(), 1000.f);
70 }
71 
72 }  // namespace webrtc
73