xref: /aosp_15_r20/external/webrtc/modules/audio_processing/aec3/fullband_erle_estimator.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2018 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/fullband_erle_estimator.h"
12 
13 #include <algorithm>
14 #include <memory>
15 #include <numeric>
16 
17 #include "absl/types/optional.h"
18 #include "api/array_view.h"
19 #include "modules/audio_processing/aec3/aec3_common.h"
20 #include "modules/audio_processing/logging/apm_data_dumper.h"
21 #include "rtc_base/checks.h"
22 #include "rtc_base/numerics/safe_minmax.h"
23 
24 namespace webrtc {
25 
26 namespace {
27 constexpr float kEpsilon = 1e-3f;
28 constexpr float kX2BandEnergyThreshold = 44015068.0f;
29 constexpr int kBlocksToHoldErle = 100;
30 constexpr int kPointsToAccumulate = 6;
31 }  // namespace
32 
FullBandErleEstimator(const EchoCanceller3Config::Erle & config,size_t num_capture_channels)33 FullBandErleEstimator::FullBandErleEstimator(
34     const EchoCanceller3Config::Erle& config,
35     size_t num_capture_channels)
36     : min_erle_log2_(FastApproxLog2f(config.min + kEpsilon)),
37       max_erle_lf_log2_(FastApproxLog2f(config.max_l + kEpsilon)),
38       hold_counters_instantaneous_erle_(num_capture_channels, 0),
39       erle_time_domain_log2_(num_capture_channels, min_erle_log2_),
40       instantaneous_erle_(num_capture_channels, ErleInstantaneous(config)),
41       linear_filters_qualities_(num_capture_channels) {
42   Reset();
43 }
44 
45 FullBandErleEstimator::~FullBandErleEstimator() = default;
46 
Reset()47 void FullBandErleEstimator::Reset() {
48   for (auto& instantaneous_erle_ch : instantaneous_erle_) {
49     instantaneous_erle_ch.Reset();
50   }
51 
52   UpdateQualityEstimates();
53   std::fill(erle_time_domain_log2_.begin(), erle_time_domain_log2_.end(),
54             min_erle_log2_);
55   std::fill(hold_counters_instantaneous_erle_.begin(),
56             hold_counters_instantaneous_erle_.end(), 0);
57 }
58 
Update(rtc::ArrayView<const float> X2,rtc::ArrayView<const std::array<float,kFftLengthBy2Plus1>> Y2,rtc::ArrayView<const std::array<float,kFftLengthBy2Plus1>> E2,const std::vector<bool> & converged_filters)59 void FullBandErleEstimator::Update(
60     rtc::ArrayView<const float> X2,
61     rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> Y2,
62     rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> E2,
63     const std::vector<bool>& converged_filters) {
64   for (size_t ch = 0; ch < Y2.size(); ++ch) {
65     if (converged_filters[ch]) {
66       // Computes the fullband ERLE.
67       const float X2_sum = std::accumulate(X2.begin(), X2.end(), 0.0f);
68       if (X2_sum > kX2BandEnergyThreshold * X2.size()) {
69         const float Y2_sum =
70             std::accumulate(Y2[ch].begin(), Y2[ch].end(), 0.0f);
71         const float E2_sum =
72             std::accumulate(E2[ch].begin(), E2[ch].end(), 0.0f);
73         if (instantaneous_erle_[ch].Update(Y2_sum, E2_sum)) {
74           hold_counters_instantaneous_erle_[ch] = kBlocksToHoldErle;
75           erle_time_domain_log2_[ch] +=
76               0.05f * ((instantaneous_erle_[ch].GetInstErleLog2().value()) -
77                        erle_time_domain_log2_[ch]);
78           erle_time_domain_log2_[ch] =
79               std::max(erle_time_domain_log2_[ch], min_erle_log2_);
80         }
81       }
82     }
83     --hold_counters_instantaneous_erle_[ch];
84     if (hold_counters_instantaneous_erle_[ch] == 0) {
85       instantaneous_erle_[ch].ResetAccumulators();
86     }
87   }
88 
89   UpdateQualityEstimates();
90 }
91 
Dump(const std::unique_ptr<ApmDataDumper> & data_dumper) const92 void FullBandErleEstimator::Dump(
93     const std::unique_ptr<ApmDataDumper>& data_dumper) const {
94   data_dumper->DumpRaw("aec3_fullband_erle_log2", FullbandErleLog2());
95   instantaneous_erle_[0].Dump(data_dumper);
96 }
97 
UpdateQualityEstimates()98 void FullBandErleEstimator::UpdateQualityEstimates() {
99   for (size_t ch = 0; ch < instantaneous_erle_.size(); ++ch) {
100     linear_filters_qualities_[ch] =
101         instantaneous_erle_[ch].GetQualityEstimate();
102   }
103 }
104 
ErleInstantaneous(const EchoCanceller3Config::Erle & config)105 FullBandErleEstimator::ErleInstantaneous::ErleInstantaneous(
106     const EchoCanceller3Config::Erle& config)
107     : clamp_inst_quality_to_zero_(config.clamp_quality_estimate_to_zero),
108       clamp_inst_quality_to_one_(config.clamp_quality_estimate_to_one) {
109   Reset();
110 }
111 
112 FullBandErleEstimator::ErleInstantaneous::~ErleInstantaneous() = default;
113 
Update(const float Y2_sum,const float E2_sum)114 bool FullBandErleEstimator::ErleInstantaneous::Update(const float Y2_sum,
115                                                       const float E2_sum) {
116   bool update_estimates = false;
117   E2_acum_ += E2_sum;
118   Y2_acum_ += Y2_sum;
119   num_points_++;
120   if (num_points_ == kPointsToAccumulate) {
121     if (E2_acum_ > 0.f) {
122       update_estimates = true;
123       erle_log2_ = FastApproxLog2f(Y2_acum_ / E2_acum_ + kEpsilon);
124     }
125     num_points_ = 0;
126     E2_acum_ = 0.f;
127     Y2_acum_ = 0.f;
128   }
129 
130   if (update_estimates) {
131     UpdateMaxMin();
132     UpdateQualityEstimate();
133   }
134   return update_estimates;
135 }
136 
Reset()137 void FullBandErleEstimator::ErleInstantaneous::Reset() {
138   ResetAccumulators();
139   max_erle_log2_ = -10.f;  // -30 dB.
140   min_erle_log2_ = 33.f;   // 100 dB.
141   inst_quality_estimate_ = 0.f;
142 }
143 
ResetAccumulators()144 void FullBandErleEstimator::ErleInstantaneous::ResetAccumulators() {
145   erle_log2_ = absl::nullopt;
146   inst_quality_estimate_ = 0.f;
147   num_points_ = 0;
148   E2_acum_ = 0.f;
149   Y2_acum_ = 0.f;
150 }
151 
Dump(const std::unique_ptr<ApmDataDumper> & data_dumper) const152 void FullBandErleEstimator::ErleInstantaneous::Dump(
153     const std::unique_ptr<ApmDataDumper>& data_dumper) const {
154   data_dumper->DumpRaw("aec3_fullband_erle_inst_log2",
155                        erle_log2_ ? *erle_log2_ : -10.f);
156   data_dumper->DumpRaw(
157       "aec3_erle_instantaneous_quality",
158       GetQualityEstimate() ? GetQualityEstimate().value() : 0.f);
159   data_dumper->DumpRaw("aec3_fullband_erle_max_log2", max_erle_log2_);
160   data_dumper->DumpRaw("aec3_fullband_erle_min_log2", min_erle_log2_);
161 }
162 
UpdateMaxMin()163 void FullBandErleEstimator::ErleInstantaneous::UpdateMaxMin() {
164   RTC_DCHECK(erle_log2_);
165   // Adding the forgetting factors for the maximum and minimum and capping the
166   // result to the incoming value.
167   max_erle_log2_ -= 0.0004f;  // Forget factor, approx 1dB every 3 sec.
168   max_erle_log2_ = std::max(max_erle_log2_, erle_log2_.value());
169   min_erle_log2_ += 0.0004f;  // Forget factor, approx 1dB every 3 sec.
170   min_erle_log2_ = std::min(min_erle_log2_, erle_log2_.value());
171 }
172 
UpdateQualityEstimate()173 void FullBandErleEstimator::ErleInstantaneous::UpdateQualityEstimate() {
174   const float alpha = 0.07f;
175   float quality_estimate = 0.f;
176   RTC_DCHECK(erle_log2_);
177   // TODO(peah): Currently, the estimate can become be less than 0; this should
178   // be corrected.
179   if (max_erle_log2_ > min_erle_log2_) {
180     quality_estimate = (erle_log2_.value() - min_erle_log2_) /
181                        (max_erle_log2_ - min_erle_log2_);
182   }
183   if (quality_estimate > inst_quality_estimate_) {
184     inst_quality_estimate_ = quality_estimate;
185   } else {
186     inst_quality_estimate_ +=
187         alpha * (quality_estimate - inst_quality_estimate_);
188   }
189 }
190 
191 }  // namespace webrtc
192