xref: /aosp_15_r20/external/webrtc/modules/audio_processing/aec3/echo_remover_metrics_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/echo_remover_metrics.h"
12 
13 #include <math.h>
14 
15 #include <cmath>
16 
17 #include "modules/audio_processing/aec3/aec3_fft.h"
18 #include "modules/audio_processing/aec3/aec_state.h"
19 #include "test/gtest.h"
20 
21 namespace webrtc {
22 
23 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
24 
25 // Verifies the check for non-null input.
TEST(UpdateDbMetricDeathTest,NullValue)26 TEST(UpdateDbMetricDeathTest, NullValue) {
27   std::array<float, kFftLengthBy2Plus1> value;
28   value.fill(0.f);
29   EXPECT_DEATH(aec3::UpdateDbMetric(value, nullptr), "");
30 }
31 
32 #endif
33 
34 // Verifies the updating functionality of UpdateDbMetric.
TEST(UpdateDbMetric,Updating)35 TEST(UpdateDbMetric, Updating) {
36   std::array<float, kFftLengthBy2Plus1> value;
37   std::array<EchoRemoverMetrics::DbMetric, 2> statistic;
38   statistic.fill(EchoRemoverMetrics::DbMetric(0.f, 100.f, -100.f));
39   constexpr float kValue0 = 10.f;
40   constexpr float kValue1 = 20.f;
41   std::fill(value.begin(), value.begin() + 32, kValue0);
42   std::fill(value.begin() + 32, value.begin() + 64, kValue1);
43 
44   aec3::UpdateDbMetric(value, &statistic);
45   EXPECT_FLOAT_EQ(kValue0, statistic[0].sum_value);
46   EXPECT_FLOAT_EQ(kValue0, statistic[0].ceil_value);
47   EXPECT_FLOAT_EQ(kValue0, statistic[0].floor_value);
48   EXPECT_FLOAT_EQ(kValue1, statistic[1].sum_value);
49   EXPECT_FLOAT_EQ(kValue1, statistic[1].ceil_value);
50   EXPECT_FLOAT_EQ(kValue1, statistic[1].floor_value);
51 
52   aec3::UpdateDbMetric(value, &statistic);
53   EXPECT_FLOAT_EQ(2.f * kValue0, statistic[0].sum_value);
54   EXPECT_FLOAT_EQ(kValue0, statistic[0].ceil_value);
55   EXPECT_FLOAT_EQ(kValue0, statistic[0].floor_value);
56   EXPECT_FLOAT_EQ(2.f * kValue1, statistic[1].sum_value);
57   EXPECT_FLOAT_EQ(kValue1, statistic[1].ceil_value);
58   EXPECT_FLOAT_EQ(kValue1, statistic[1].floor_value);
59 }
60 
61 // Verifies that the TransformDbMetricForReporting method produces the desired
62 // output for values for dBFS.
TEST(TransformDbMetricForReporting,DbFsScaling)63 TEST(TransformDbMetricForReporting, DbFsScaling) {
64   std::array<float, kBlockSize> x;
65   FftData X;
66   std::array<float, kFftLengthBy2Plus1> X2;
67   Aec3Fft fft;
68   x.fill(1000.f);
69   fft.ZeroPaddedFft(x, Aec3Fft::Window::kRectangular, &X);
70   X.Spectrum(Aec3Optimization::kNone, X2);
71 
72   float offset = -10.f * std::log10(32768.f * 32768.f);
73   EXPECT_NEAR(offset, -90.3f, 0.1f);
74   EXPECT_EQ(
75       static_cast<int>(30.3f),
76       aec3::TransformDbMetricForReporting(
77           true, 0.f, 90.f, offset, 1.f / (kBlockSize * kBlockSize), X2[0]));
78 }
79 
80 // Verifies that the TransformDbMetricForReporting method is able to properly
81 // limit the output.
TEST(TransformDbMetricForReporting,Limits)82 TEST(TransformDbMetricForReporting, Limits) {
83   EXPECT_EQ(0, aec3::TransformDbMetricForReporting(false, 0.f, 10.f, 0.f, 1.f,
84                                                    0.001f));
85   EXPECT_EQ(10, aec3::TransformDbMetricForReporting(false, 0.f, 10.f, 0.f, 1.f,
86                                                     100.f));
87 }
88 
89 // Verifies that the TransformDbMetricForReporting method is able to properly
90 // negate output.
TEST(TransformDbMetricForReporting,Negate)91 TEST(TransformDbMetricForReporting, Negate) {
92   EXPECT_EQ(10, aec3::TransformDbMetricForReporting(true, -20.f, 20.f, 0.f, 1.f,
93                                                     0.1f));
94   EXPECT_EQ(-10, aec3::TransformDbMetricForReporting(true, -20.f, 20.f, 0.f,
95                                                      1.f, 10.f));
96 }
97 
98 // Verify the Update functionality of DbMetric.
TEST(DbMetric,Update)99 TEST(DbMetric, Update) {
100   EchoRemoverMetrics::DbMetric metric(0.f, 20.f, -20.f);
101   constexpr int kNumValues = 100;
102   constexpr float kValue = 10.f;
103   for (int k = 0; k < kNumValues; ++k) {
104     metric.Update(kValue);
105   }
106   EXPECT_FLOAT_EQ(kValue * kNumValues, metric.sum_value);
107   EXPECT_FLOAT_EQ(kValue, metric.ceil_value);
108   EXPECT_FLOAT_EQ(kValue, metric.floor_value);
109 }
110 
111 // Verify the Update functionality of DbMetric.
TEST(DbMetric,UpdateInstant)112 TEST(DbMetric, UpdateInstant) {
113   EchoRemoverMetrics::DbMetric metric(0.f, 20.f, -20.f);
114   constexpr float kMinValue = -77.f;
115   constexpr float kMaxValue = 33.f;
116   constexpr float kLastValue = (kMinValue + kMaxValue) / 2.0f;
117   for (float value = kMinValue; value <= kMaxValue; value++)
118     metric.UpdateInstant(value);
119   metric.UpdateInstant(kLastValue);
120   EXPECT_FLOAT_EQ(kLastValue, metric.sum_value);
121   EXPECT_FLOAT_EQ(kMaxValue, metric.ceil_value);
122   EXPECT_FLOAT_EQ(kMinValue, metric.floor_value);
123 }
124 
125 // Verify the constructor functionality of DbMetric.
TEST(DbMetric,Constructor)126 TEST(DbMetric, Constructor) {
127   EchoRemoverMetrics::DbMetric metric;
128   EXPECT_FLOAT_EQ(0.f, metric.sum_value);
129   EXPECT_FLOAT_EQ(0.f, metric.ceil_value);
130   EXPECT_FLOAT_EQ(0.f, metric.floor_value);
131 
132   metric = EchoRemoverMetrics::DbMetric(1.f, 2.f, 3.f);
133   EXPECT_FLOAT_EQ(1.f, metric.sum_value);
134   EXPECT_FLOAT_EQ(2.f, metric.floor_value);
135   EXPECT_FLOAT_EQ(3.f, metric.ceil_value);
136 }
137 
138 // Verify the general functionality of EchoRemoverMetrics.
TEST(EchoRemoverMetrics,NormalUsage)139 TEST(EchoRemoverMetrics, NormalUsage) {
140   EchoRemoverMetrics metrics;
141   AecState aec_state(EchoCanceller3Config{}, 1);
142   std::array<float, kFftLengthBy2Plus1> comfort_noise_spectrum;
143   std::array<float, kFftLengthBy2Plus1> suppressor_gain;
144   comfort_noise_spectrum.fill(10.f);
145   suppressor_gain.fill(1.f);
146   for (int j = 0; j < 3; ++j) {
147     for (int k = 0; k < kMetricsReportingIntervalBlocks - 1; ++k) {
148       metrics.Update(aec_state, comfort_noise_spectrum, suppressor_gain);
149       EXPECT_FALSE(metrics.MetricsReported());
150     }
151     metrics.Update(aec_state, comfort_noise_spectrum, suppressor_gain);
152     EXPECT_TRUE(metrics.MetricsReported());
153   }
154 }
155 
156 }  // namespace webrtc
157