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/erle_estimator.h"
12 #include "test/gtest.h"
13 
14 namespace webrtc {
15 
16 namespace {
17 
18 constexpr int kLowFrequencyLimit = kFftLengthBy2 / 2;
19 
VerifyErle(const std::array<float,kFftLengthBy2Plus1> & erle,float erle_time_domain,float reference_lf,float reference_hf)20 void VerifyErle(const std::array<float, kFftLengthBy2Plus1>& erle,
21                 float erle_time_domain,
22                 float reference_lf,
23                 float reference_hf) {
24   std::for_each(
25       erle.begin(), erle.begin() + kLowFrequencyLimit,
26       [reference_lf](float a) { EXPECT_NEAR(reference_lf, a, 0.001); });
27   std::for_each(
28       erle.begin() + kLowFrequencyLimit, erle.end(),
29       [reference_hf](float a) { EXPECT_NEAR(reference_hf, a, 0.001); });
30   EXPECT_NEAR(reference_lf, erle_time_domain, 0.001);
31 }
32 
33 }  // namespace
34 
35 // Verifies that the correct ERLE estimates are achieved.
TEST(ErleEstimator,Estimates)36 TEST(ErleEstimator, Estimates) {
37   std::array<float, kFftLengthBy2Plus1> X2;
38   std::array<float, kFftLengthBy2Plus1> E2;
39   std::array<float, kFftLengthBy2Plus1> Y2;
40 
41   ErleEstimator estimator(1.f, 8.f, 1.5f);
42 
43   // Verifies that the ERLE estimate is properley increased to higher values.
44   X2.fill(500 * 1000.f * 1000.f);
45   E2.fill(1000.f * 1000.f);
46   Y2.fill(10 * E2[0]);
47   for (size_t k = 0; k < 200; ++k) {
48     estimator.Update(X2, Y2, E2);
49   }
50   VerifyErle(estimator.Erle(), estimator.ErleTimeDomain(), 8.f, 1.5f);
51 
52   // Verifies that the ERLE is not immediately decreased when the ERLE in the
53   // data decreases.
54   Y2.fill(0.1f * E2[0]);
55   for (size_t k = 0; k < 98; ++k) {
56     estimator.Update(X2, Y2, E2);
57   }
58   VerifyErle(estimator.Erle(), estimator.ErleTimeDomain(), 8.f, 1.5f);
59 
60   // Verifies that the minimum ERLE is eventually achieved.
61   for (size_t k = 0; k < 1000; ++k) {
62     estimator.Update(X2, Y2, E2);
63   }
64   VerifyErle(estimator.Erle(), estimator.ErleTimeDomain(), 1.f, 1.f);
65 
66   // Verifies that the ERLE estimate is is not updated for low-level render
67   // signals.
68   X2.fill(1000.f * 1000.f);
69   Y2.fill(10 * E2[0]);
70   for (size_t k = 0; k < 200; ++k) {
71     estimator.Update(X2, Y2, E2);
72   }
73   VerifyErle(estimator.Erle(), estimator.ErleTimeDomain(), 1.f, 1.f);
74 }
75 }  // namespace webrtc
76