blob: 85b1e022da2f87266e0e8572a741cc84b11c86c1 [file] [log] [blame]
peah522d71b2017-02-23 05:16:26 -08001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/aec3/erl_estimator.h"
peah522d71b2017-02-23 05:16:26 -080012
13#include <algorithm>
Gustaf Ullbergfe4d6732017-11-16 09:31:27 +010014#include <numeric>
peah522d71b2017-02-23 05:16:26 -080015
Yves Gerey988cc082018-10-23 12:03:01 +020016#include "rtc_base/checks.h"
17
peah522d71b2017-02-23 05:16:26 -080018namespace webrtc {
19
20namespace {
21
22constexpr float kMinErl = 0.01f;
23constexpr float kMaxErl = 1000.f;
24
25} // namespace
26
Per Åhgrenc5a38ad2018-10-04 15:37:54 +020027ErlEstimator::ErlEstimator(size_t startup_phase_length_blocks_)
28 : startup_phase_length_blocks__(startup_phase_length_blocks_) {
peah522d71b2017-02-23 05:16:26 -080029 erl_.fill(kMaxErl);
30 hold_counters_.fill(0);
Gustaf Ullbergfe4d6732017-11-16 09:31:27 +010031 erl_time_domain_ = kMaxErl;
32 hold_counter_time_domain_ = 0;
peah522d71b2017-02-23 05:16:26 -080033}
34
35ErlEstimator::~ErlEstimator() = default;
36
Per Åhgrenc5a38ad2018-10-04 15:37:54 +020037void ErlEstimator::Reset() {
38 blocks_since_reset_ = 0;
39}
40
41void ErlEstimator::Update(bool converged_filter,
42 rtc::ArrayView<const float> render_spectrum,
Per Åhgren8ba58612017-12-01 23:01:44 +010043 rtc::ArrayView<const float> capture_spectrum) {
44 RTC_DCHECK_EQ(kFftLengthBy2Plus1, render_spectrum.size());
45 RTC_DCHECK_EQ(kFftLengthBy2Plus1, capture_spectrum.size());
peah522d71b2017-02-23 05:16:26 -080046 const auto& X2 = render_spectrum;
47 const auto& Y2 = capture_spectrum;
48
49 // Corresponds to WGN of power -46 dBFS.
50 constexpr float kX2Min = 44015068.0f;
51
Per Åhgrenc5a38ad2018-10-04 15:37:54 +020052 if (++blocks_since_reset_ < startup_phase_length_blocks__ ||
53 !converged_filter) {
54 return;
55 }
56
peah522d71b2017-02-23 05:16:26 -080057 // Update the estimates in a maximum statistics manner.
58 for (size_t k = 1; k < kFftLengthBy2; ++k) {
59 if (X2[k] > kX2Min) {
60 const float new_erl = Y2[k] / X2[k];
61 if (new_erl < erl_[k]) {
62 hold_counters_[k - 1] = 1000;
Gustaf Ullbergfe4d6732017-11-16 09:31:27 +010063 erl_[k] += 0.1f * (new_erl - erl_[k]);
peah522d71b2017-02-23 05:16:26 -080064 erl_[k] = std::max(erl_[k], kMinErl);
65 }
66 }
67 }
68
69 std::for_each(hold_counters_.begin(), hold_counters_.end(),
70 [](int& a) { --a; });
71 std::transform(hold_counters_.begin(), hold_counters_.end(), erl_.begin() + 1,
72 erl_.begin() + 1, [](int a, float b) {
73 return a > 0 ? b : std::min(kMaxErl, 2.f * b);
74 });
75
76 erl_[0] = erl_[1];
77 erl_[kFftLengthBy2] = erl_[kFftLengthBy2 - 1];
Gustaf Ullbergfe4d6732017-11-16 09:31:27 +010078
79 // Compute ERL over all frequency bins.
80 const float X2_sum = std::accumulate(X2.begin(), X2.end(), 0.0f);
81
82 if (X2_sum > kX2Min * X2.size()) {
83 const float Y2_sum = std::accumulate(Y2.begin(), Y2.end(), 0.0f);
84 const float new_erl = Y2_sum / X2_sum;
85 if (new_erl < erl_time_domain_) {
86 hold_counter_time_domain_ = 1000;
87 erl_time_domain_ += 0.1f * (new_erl - erl_time_domain_);
88 erl_time_domain_ = std::max(erl_time_domain_, kMinErl);
89 }
90 }
91
92 --hold_counter_time_domain_;
93 erl_time_domain_ = (hold_counter_time_domain_ > 0)
94 ? erl_time_domain_
95 : std::min(kMaxErl, 2.f * erl_time_domain_);
peah522d71b2017-02-23 05:16:26 -080096}
97
98} // namespace webrtc