• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_time_domain_(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_time_domain_.begin(),
56             hold_counters_time_domain_.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_time_domain_[ch] = kBlocksToHoldErle;
75           erle_time_domain_log2_[ch] +=
76               0.1f * ((instantaneous_erle_[ch].GetInstErleLog2().value()) -
77                       erle_time_domain_log2_[ch]);
78           erle_time_domain_log2_[ch] = rtc::SafeClamp(
79               erle_time_domain_log2_[ch], min_erle_log2_, max_erle_lf_log2);
80         }
81       }
82     }
83     --hold_counters_time_domain_[ch];
84     if (hold_counters_time_domain_[ch] <= 0) {
85       erle_time_domain_log2_[ch] =
86           std::max(min_erle_log2_, erle_time_domain_log2_[ch] - 0.044f);
87     }
88     if (hold_counters_time_domain_[ch] == 0) {
89       instantaneous_erle_[ch].ResetAccumulators();
90     }
91   }
92 
93   UpdateQualityEstimates();
94 }
95 
Dump(const std::unique_ptr<ApmDataDumper> & data_dumper) const96 void FullBandErleEstimator::Dump(
97     const std::unique_ptr<ApmDataDumper>& data_dumper) const {
98   data_dumper->DumpRaw("aec3_fullband_erle_log2", FullbandErleLog2());
99   instantaneous_erle_[0].Dump(data_dumper);
100 }
101 
UpdateQualityEstimates()102 void FullBandErleEstimator::UpdateQualityEstimates() {
103   for (size_t ch = 0; ch < instantaneous_erle_.size(); ++ch) {
104     linear_filters_qualities_[ch] =
105         instantaneous_erle_[ch].GetQualityEstimate();
106   }
107 }
108 
ErleInstantaneous(const EchoCanceller3Config::Erle & config)109 FullBandErleEstimator::ErleInstantaneous::ErleInstantaneous(
110     const EchoCanceller3Config::Erle& config)
111     : clamp_inst_quality_to_zero_(config.clamp_quality_estimate_to_zero),
112       clamp_inst_quality_to_one_(config.clamp_quality_estimate_to_one) {
113   Reset();
114 }
115 
116 FullBandErleEstimator::ErleInstantaneous::~ErleInstantaneous() = default;
117 
Update(const float Y2_sum,const float E2_sum)118 bool FullBandErleEstimator::ErleInstantaneous::Update(const float Y2_sum,
119                                                       const float E2_sum) {
120   bool update_estimates = false;
121   E2_acum_ += E2_sum;
122   Y2_acum_ += Y2_sum;
123   num_points_++;
124   if (num_points_ == kPointsToAccumulate) {
125     if (E2_acum_ > 0.f) {
126       update_estimates = true;
127       erle_log2_ = FastApproxLog2f(Y2_acum_ / E2_acum_ + kEpsilon);
128     }
129     num_points_ = 0;
130     E2_acum_ = 0.f;
131     Y2_acum_ = 0.f;
132   }
133 
134   if (update_estimates) {
135     UpdateMaxMin();
136     UpdateQualityEstimate();
137   }
138   return update_estimates;
139 }
140 
Reset()141 void FullBandErleEstimator::ErleInstantaneous::Reset() {
142   ResetAccumulators();
143   max_erle_log2_ = -10.f;  // -30 dB.
144   min_erle_log2_ = 33.f;   // 100 dB.
145   inst_quality_estimate_ = 0.f;
146 }
147 
ResetAccumulators()148 void FullBandErleEstimator::ErleInstantaneous::ResetAccumulators() {
149   erle_log2_ = absl::nullopt;
150   inst_quality_estimate_ = 0.f;
151   num_points_ = 0;
152   E2_acum_ = 0.f;
153   Y2_acum_ = 0.f;
154 }
155 
Dump(const std::unique_ptr<ApmDataDumper> & data_dumper) const156 void FullBandErleEstimator::ErleInstantaneous::Dump(
157     const std::unique_ptr<ApmDataDumper>& data_dumper) const {
158   data_dumper->DumpRaw("aec3_fullband_erle_inst_log2",
159                        erle_log2_ ? *erle_log2_ : -10.f);
160   data_dumper->DumpRaw(
161       "aec3_erle_instantaneous_quality",
162       GetQualityEstimate() ? GetQualityEstimate().value() : 0.f);
163   data_dumper->DumpRaw("aec3_fullband_erle_max_log2", max_erle_log2_);
164   data_dumper->DumpRaw("aec3_fullband_erle_min_log2", min_erle_log2_);
165 }
166 
UpdateMaxMin()167 void FullBandErleEstimator::ErleInstantaneous::UpdateMaxMin() {
168   RTC_DCHECK(erle_log2_);
169   if (erle_log2_.value() > max_erle_log2_) {
170     max_erle_log2_ = erle_log2_.value();
171   } else {
172     max_erle_log2_ -= 0.0004;  // Forget factor, approx 1dB every 3 sec.
173   }
174 
175   if (erle_log2_.value() < min_erle_log2_) {
176     min_erle_log2_ = erle_log2_.value();
177   } else {
178     min_erle_log2_ += 0.0004;  // Forget factor, approx 1dB every 3 sec.
179   }
180 }
181 
UpdateQualityEstimate()182 void FullBandErleEstimator::ErleInstantaneous::UpdateQualityEstimate() {
183   const float alpha = 0.07f;
184   float quality_estimate = 0.f;
185   RTC_DCHECK(erle_log2_);
186   // TODO(peah): Currently, the estimate can become be less than 0; this should
187   // be corrected.
188   if (max_erle_log2_ > min_erle_log2_) {
189     quality_estimate = (erle_log2_.value() - min_erle_log2_) /
190                        (max_erle_log2_ - min_erle_log2_);
191   }
192   if (quality_estimate > inst_quality_estimate_) {
193     inst_quality_estimate_ = quality_estimate;
194   } else {
195     inst_quality_estimate_ +=
196         alpha * (quality_estimate - inst_quality_estimate_);
197   }
198 }
199 
200 }  // namespace webrtc
201