xref: /aosp_15_r20/external/webrtc/modules/audio_processing/aec3/echo_remover_metrics.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 #include <stddef.h>
15 
16 #include <algorithm>
17 #include <cmath>
18 #include <numeric>
19 
20 #include "rtc_base/checks.h"
21 #include "rtc_base/numerics/safe_minmax.h"
22 #include "system_wrappers/include/metrics.h"
23 
24 namespace webrtc {
25 
DbMetric()26 EchoRemoverMetrics::DbMetric::DbMetric() : DbMetric(0.f, 0.f, 0.f) {}
DbMetric(float sum_value,float floor_value,float ceil_value)27 EchoRemoverMetrics::DbMetric::DbMetric(float sum_value,
28                                        float floor_value,
29                                        float ceil_value)
30     : sum_value(sum_value), floor_value(floor_value), ceil_value(ceil_value) {}
31 
Update(float value)32 void EchoRemoverMetrics::DbMetric::Update(float value) {
33   sum_value += value;
34   floor_value = std::min(floor_value, value);
35   ceil_value = std::max(ceil_value, value);
36 }
37 
UpdateInstant(float value)38 void EchoRemoverMetrics::DbMetric::UpdateInstant(float value) {
39   sum_value = value;
40   floor_value = std::min(floor_value, value);
41   ceil_value = std::max(ceil_value, value);
42 }
43 
EchoRemoverMetrics()44 EchoRemoverMetrics::EchoRemoverMetrics() {
45   ResetMetrics();
46 }
47 
ResetMetrics()48 void EchoRemoverMetrics::ResetMetrics() {
49   erl_time_domain_ = DbMetric(0.f, 10000.f, 0.000f);
50   erle_time_domain_ = DbMetric(0.f, 0.f, 1000.f);
51   saturated_capture_ = false;
52 }
53 
Update(const AecState & aec_state,const std::array<float,kFftLengthBy2Plus1> & comfort_noise_spectrum,const std::array<float,kFftLengthBy2Plus1> & suppressor_gain)54 void EchoRemoverMetrics::Update(
55     const AecState& aec_state,
56     const std::array<float, kFftLengthBy2Plus1>& comfort_noise_spectrum,
57     const std::array<float, kFftLengthBy2Plus1>& suppressor_gain) {
58   metrics_reported_ = false;
59   if (++block_counter_ <= kMetricsCollectionBlocks) {
60     erl_time_domain_.UpdateInstant(aec_state.ErlTimeDomain());
61     erle_time_domain_.UpdateInstant(aec_state.FullBandErleLog2());
62     saturated_capture_ = saturated_capture_ || aec_state.SaturatedCapture();
63   } else {
64     // Report the metrics over several frames in order to lower the impact of
65     // the logarithms involved on the computational complexity.
66     switch (block_counter_) {
67       case kMetricsCollectionBlocks + 1:
68         RTC_HISTOGRAM_BOOLEAN(
69             "WebRTC.Audio.EchoCanceller.UsableLinearEstimate",
70             static_cast<int>(aec_state.UsableLinearEstimate() ? 1 : 0));
71         RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.EchoCanceller.FilterDelay",
72                                     aec_state.MinDirectPathFilterDelay(), 0, 30,
73                                     31);
74         RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.EchoCanceller.CaptureSaturation",
75                               static_cast<int>(saturated_capture_ ? 1 : 0));
76         break;
77       case kMetricsCollectionBlocks + 2:
78         RTC_HISTOGRAM_COUNTS_LINEAR(
79             "WebRTC.Audio.EchoCanceller.Erl.Value",
80             aec3::TransformDbMetricForReporting(true, 0.f, 59.f, 30.f, 1.f,
81                                                 erl_time_domain_.sum_value),
82             0, 59, 30);
83         RTC_HISTOGRAM_COUNTS_LINEAR(
84             "WebRTC.Audio.EchoCanceller.Erl.Max",
85             aec3::TransformDbMetricForReporting(true, 0.f, 59.f, 30.f, 1.f,
86                                                 erl_time_domain_.ceil_value),
87             0, 59, 30);
88         RTC_HISTOGRAM_COUNTS_LINEAR(
89             "WebRTC.Audio.EchoCanceller.Erl.Min",
90             aec3::TransformDbMetricForReporting(true, 0.f, 59.f, 30.f, 1.f,
91                                                 erl_time_domain_.floor_value),
92             0, 59, 30);
93         break;
94       case kMetricsCollectionBlocks + 3:
95         RTC_HISTOGRAM_COUNTS_LINEAR(
96             "WebRTC.Audio.EchoCanceller.Erle.Value",
97             aec3::TransformDbMetricForReporting(false, 0.f, 19.f, 0.f, 1.f,
98                                                 erle_time_domain_.sum_value),
99             0, 19, 20);
100         RTC_HISTOGRAM_COUNTS_LINEAR(
101             "WebRTC.Audio.EchoCanceller.Erle.Max",
102             aec3::TransformDbMetricForReporting(false, 0.f, 19.f, 0.f, 1.f,
103                                                 erle_time_domain_.ceil_value),
104             0, 19, 20);
105         RTC_HISTOGRAM_COUNTS_LINEAR(
106             "WebRTC.Audio.EchoCanceller.Erle.Min",
107             aec3::TransformDbMetricForReporting(false, 0.f, 19.f, 0.f, 1.f,
108                                                 erle_time_domain_.floor_value),
109             0, 19, 20);
110         metrics_reported_ = true;
111         RTC_DCHECK_EQ(kMetricsReportingIntervalBlocks, block_counter_);
112         block_counter_ = 0;
113         ResetMetrics();
114         break;
115       default:
116         RTC_DCHECK_NOTREACHED();
117         break;
118     }
119   }
120 }
121 
122 namespace aec3 {
123 
UpdateDbMetric(const std::array<float,kFftLengthBy2Plus1> & value,std::array<EchoRemoverMetrics::DbMetric,2> * statistic)124 void UpdateDbMetric(const std::array<float, kFftLengthBy2Plus1>& value,
125                     std::array<EchoRemoverMetrics::DbMetric, 2>* statistic) {
126   RTC_DCHECK(statistic);
127   // Truncation is intended in the band width computation.
128   constexpr int kNumBands = 2;
129   constexpr int kBandWidth = 65 / kNumBands;
130   constexpr float kOneByBandWidth = 1.f / kBandWidth;
131   RTC_DCHECK_EQ(kNumBands, statistic->size());
132   RTC_DCHECK_EQ(65, value.size());
133   for (size_t k = 0; k < statistic->size(); ++k) {
134     float average_band =
135         std::accumulate(value.begin() + kBandWidth * k,
136                         value.begin() + kBandWidth * (k + 1), 0.f) *
137         kOneByBandWidth;
138     (*statistic)[k].Update(average_band);
139   }
140 }
141 
TransformDbMetricForReporting(bool negate,float min_value,float max_value,float offset,float scaling,float value)142 int TransformDbMetricForReporting(bool negate,
143                                   float min_value,
144                                   float max_value,
145                                   float offset,
146                                   float scaling,
147                                   float value) {
148   float new_value = 10.f * std::log10(value * scaling + 1e-10f) + offset;
149   if (negate) {
150     new_value = -new_value;
151   }
152   return static_cast<int>(rtc::SafeClamp(new_value, min_value, max_value));
153 }
154 
155 }  // namespace aec3
156 
157 }  // namespace webrtc
158