blob: b2849db18af6894699f246a3e5cb51cbef46fc52 [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
16namespace webrtc {
17
18namespace {
19
20constexpr float kMinErl = 0.01f;
21constexpr float kMaxErl = 1000.f;
22
23} // namespace
24
25ErlEstimator::ErlEstimator() {
26 erl_.fill(kMaxErl);
27 hold_counters_.fill(0);
Gustaf Ullbergfe4d6732017-11-16 09:31:27 +010028 erl_time_domain_ = kMaxErl;
29 hold_counter_time_domain_ = 0;
peah522d71b2017-02-23 05:16:26 -080030}
31
32ErlEstimator::~ErlEstimator() = default;
33
Per Ã…hgren8ba58612017-12-01 23:01:44 +010034void ErlEstimator::Update(rtc::ArrayView<const float> render_spectrum,
35 rtc::ArrayView<const float> capture_spectrum) {
36 RTC_DCHECK_EQ(kFftLengthBy2Plus1, render_spectrum.size());
37 RTC_DCHECK_EQ(kFftLengthBy2Plus1, capture_spectrum.size());
peah522d71b2017-02-23 05:16:26 -080038 const auto& X2 = render_spectrum;
39 const auto& Y2 = capture_spectrum;
40
41 // Corresponds to WGN of power -46 dBFS.
42 constexpr float kX2Min = 44015068.0f;
43
44 // Update the estimates in a maximum statistics manner.
45 for (size_t k = 1; k < kFftLengthBy2; ++k) {
46 if (X2[k] > kX2Min) {
47 const float new_erl = Y2[k] / X2[k];
48 if (new_erl < erl_[k]) {
49 hold_counters_[k - 1] = 1000;
Gustaf Ullbergfe4d6732017-11-16 09:31:27 +010050 erl_[k] += 0.1f * (new_erl - erl_[k]);
peah522d71b2017-02-23 05:16:26 -080051 erl_[k] = std::max(erl_[k], kMinErl);
52 }
53 }
54 }
55
56 std::for_each(hold_counters_.begin(), hold_counters_.end(),
57 [](int& a) { --a; });
58 std::transform(hold_counters_.begin(), hold_counters_.end(), erl_.begin() + 1,
59 erl_.begin() + 1, [](int a, float b) {
60 return a > 0 ? b : std::min(kMaxErl, 2.f * b);
61 });
62
63 erl_[0] = erl_[1];
64 erl_[kFftLengthBy2] = erl_[kFftLengthBy2 - 1];
Gustaf Ullbergfe4d6732017-11-16 09:31:27 +010065
66 // Compute ERL over all frequency bins.
67 const float X2_sum = std::accumulate(X2.begin(), X2.end(), 0.0f);
68
69 if (X2_sum > kX2Min * X2.size()) {
70 const float Y2_sum = std::accumulate(Y2.begin(), Y2.end(), 0.0f);
71 const float new_erl = Y2_sum / X2_sum;
72 if (new_erl < erl_time_domain_) {
73 hold_counter_time_domain_ = 1000;
74 erl_time_domain_ += 0.1f * (new_erl - erl_time_domain_);
75 erl_time_domain_ = std::max(erl_time_domain_, kMinErl);
76 }
77 }
78
79 --hold_counter_time_domain_;
80 erl_time_domain_ = (hold_counter_time_domain_ > 0)
81 ? erl_time_domain_
82 : std::min(kMaxErl, 2.f * erl_time_domain_);
peah522d71b2017-02-23 05:16:26 -080083}
84
85} // namespace webrtc