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 
13 #include <algorithm>
14 #include <numeric>
15 
16 #include "rtc_base/numerics/safe_minmax.h"
17 
18 namespace webrtc {
19 
ErleEstimator(float min_erle,float max_erle_lf,float max_erle_hf)20 ErleEstimator::ErleEstimator(float min_erle,
21                              float max_erle_lf,
22                              float max_erle_hf)
23     : min_erle_(min_erle),
24       max_erle_lf_(max_erle_lf),
25       max_erle_hf_(max_erle_hf) {
26   erle_.fill(min_erle_);
27   hold_counters_.fill(0);
28   erle_time_domain_ = min_erle_;
29   hold_counter_time_domain_ = 0;
30 }
31 
32 ErleEstimator::~ErleEstimator() = default;
33 
Update(const std::array<float,kFftLengthBy2Plus1> & render_spectrum,const std::array<float,kFftLengthBy2Plus1> & capture_spectrum,const std::array<float,kFftLengthBy2Plus1> & subtractor_spectrum)34 void ErleEstimator::Update(
35     const std::array<float, kFftLengthBy2Plus1>& render_spectrum,
36     const std::array<float, kFftLengthBy2Plus1>& capture_spectrum,
37     const std::array<float, kFftLengthBy2Plus1>& subtractor_spectrum) {
38   const auto& X2 = render_spectrum;
39   const auto& Y2 = capture_spectrum;
40   const auto& E2 = subtractor_spectrum;
41 
42   // Corresponds of WGN of power -46 dBFS.
43   constexpr float kX2Min = 44015068.0f;
44 
45   // Update the estimates in a clamped minimum statistics manner.
46   auto erle_update = [&](size_t start, size_t stop, float max_erle) {
47     for (size_t k = start; k < stop; ++k) {
48       if (X2[k] > kX2Min && E2[k] > 0.f) {
49         const float new_erle = Y2[k] / E2[k];
50         if (new_erle > erle_[k]) {
51           hold_counters_[k - 1] = 100;
52           erle_[k] += 0.1f * (new_erle - erle_[k]);
53           erle_[k] = rtc::SafeClamp(erle_[k], min_erle_, max_erle);
54         }
55       }
56     }
57   };
58   erle_update(1, kFftLengthBy2 / 2, max_erle_lf_);
59   erle_update(kFftLengthBy2 / 2, kFftLengthBy2, max_erle_hf_);
60 
61   std::for_each(hold_counters_.begin(), hold_counters_.end(),
62                 [](int& a) { --a; });
63   std::transform(hold_counters_.begin(), hold_counters_.end(),
64                  erle_.begin() + 1, erle_.begin() + 1, [&](int a, float b) {
65                    return a > 0 ? b : std::max(min_erle_, 0.97f * b);
66                  });
67 
68   erle_[0] = erle_[1];
69   erle_[kFftLengthBy2] = erle_[kFftLengthBy2 - 1];
70 
71   // Compute ERLE over all frequency bins.
72   const float X2_sum = std::accumulate(X2.begin(), X2.end(), 0.0f);
73   const float E2_sum = std::accumulate(E2.begin(), E2.end(), 0.0f);
74   if (X2_sum > kX2Min * X2.size() && E2_sum > 0.f) {
75     const float Y2_sum = std::accumulate(Y2.begin(), Y2.end(), 0.0f);
76     const float new_erle = Y2_sum / E2_sum;
77     if (new_erle > erle_time_domain_) {
78       hold_counter_time_domain_ = 100;
79       erle_time_domain_ += 0.1f * (new_erle - erle_time_domain_);
80       erle_time_domain_ =
81           rtc::SafeClamp(erle_time_domain_, min_erle_, max_erle_lf_);
82     }
83   }
84   --hold_counter_time_domain_;
85   erle_time_domain_ = (hold_counter_time_domain_ > 0)
86                         ? erle_time_domain_
87                         : std::max(min_erle_, 0.97f * erle_time_domain_);
88 }
89 
90 }  // namespace webrtc
91