blob: 385e6dd7e634940ed2dc38d19f74d6798c38a91d [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/erle_estimator.h"
peah522d71b2017-02-23 05:16:26 -080012
13#include <algorithm>
Gustaf Ullbergc9b89aa2017-11-16 11:20:59 +010014#include <numeric>
peah522d71b2017-02-23 05:16:26 -080015
Karl Wiberge40468b2017-11-22 10:42:26 +010016#include "rtc_base/numerics/safe_minmax.h"
kwiberg07038562017-06-12 11:40:47 -070017
peah522d71b2017-02-23 05:16:26 -080018namespace webrtc {
19
peah8cee56f2017-08-24 22:36:53 -070020ErleEstimator::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_);
peah522d71b2017-02-23 05:16:26 -080027 hold_counters_.fill(0);
Gustaf Ullbergc9b89aa2017-11-16 11:20:59 +010028 erle_time_domain_ = min_erle_;
29 hold_counter_time_domain_ = 0;
peah522d71b2017-02-23 05:16:26 -080030}
31
32ErleEstimator::~ErleEstimator() = default;
33
34void 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.
peah1b927222017-05-23 23:44:47 -070046 auto erle_update = [&](size_t start, size_t stop, float max_erle) {
47 for (size_t k = start; k < stop; ++k) {
peah1d680892017-05-23 04:07:10 -070048 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]);
peah8cee56f2017-08-24 22:36:53 -070053 erle_[k] = rtc::SafeClamp(erle_[k], min_erle_, max_erle);
peah1d680892017-05-23 04:07:10 -070054 }
peah522d71b2017-02-23 05:16:26 -080055 }
56 }
peah1b927222017-05-23 23:44:47 -070057 };
peah8cee56f2017-08-24 22:36:53 -070058 erle_update(1, kFftLengthBy2 / 2, max_erle_lf_);
59 erle_update(kFftLengthBy2 / 2, kFftLengthBy2, max_erle_hf_);
peah522d71b2017-02-23 05:16:26 -080060
61 std::for_each(hold_counters_.begin(), hold_counters_.end(),
62 [](int& a) { --a; });
63 std::transform(hold_counters_.begin(), hold_counters_.end(),
peah8cee56f2017-08-24 22:36:53 -070064 erle_.begin() + 1, erle_.begin() + 1, [&](int a, float b) {
65 return a > 0 ? b : std::max(min_erle_, 0.97f * b);
peah522d71b2017-02-23 05:16:26 -080066 });
67
68 erle_[0] = erle_[1];
69 erle_[kFftLengthBy2] = erle_[kFftLengthBy2 - 1];
Gustaf Ullbergc9b89aa2017-11-16 11:20:59 +010070
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_);
peah522d71b2017-02-23 05:16:26 -080088}
89
90} // namespace webrtc