peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_processing/aec3/erle_estimator.h" |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 12 | |
| 13 | #include <algorithm> |
Gustaf Ullberg | c9b89aa | 2017-11-16 11:20:59 +0100 | [diff] [blame] | 14 | #include <numeric> |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 15 | |
Karl Wiberg | e40468b | 2017-11-22 10:42:26 +0100 | [diff] [blame] | 16 | #include "rtc_base/numerics/safe_minmax.h" |
kwiberg | 0703856 | 2017-06-12 11:40:47 -0700 | [diff] [blame] | 17 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 18 | namespace webrtc { |
| 19 | |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 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_); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 27 | hold_counters_.fill(0); |
Gustaf Ullberg | c9b89aa | 2017-11-16 11:20:59 +0100 | [diff] [blame] | 28 | erle_time_domain_ = min_erle_; |
| 29 | hold_counter_time_domain_ = 0; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | ErleEstimator::~ErleEstimator() = default; |
| 33 | |
| 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. |
peah | 1b92722 | 2017-05-23 23:44:47 -0700 | [diff] [blame] | 46 | auto erle_update = [&](size_t start, size_t stop, float max_erle) { |
| 47 | for (size_t k = start; k < stop; ++k) { |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 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]); |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 53 | erle_[k] = rtc::SafeClamp(erle_[k], min_erle_, max_erle); |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 54 | } |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 55 | } |
| 56 | } |
peah | 1b92722 | 2017-05-23 23:44:47 -0700 | [diff] [blame] | 57 | }; |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 58 | erle_update(1, kFftLengthBy2 / 2, max_erle_lf_); |
| 59 | erle_update(kFftLengthBy2 / 2, kFftLengthBy2, max_erle_hf_); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 60 | |
| 61 | std::for_each(hold_counters_.begin(), hold_counters_.end(), |
| 62 | [](int& a) { --a; }); |
| 63 | std::transform(hold_counters_.begin(), hold_counters_.end(), |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 64 | erle_.begin() + 1, erle_.begin() + 1, [&](int a, float b) { |
| 65 | return a > 0 ? b : std::max(min_erle_, 0.97f * b); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 66 | }); |
| 67 | |
| 68 | erle_[0] = erle_[1]; |
| 69 | erle_[kFftLengthBy2] = erle_[kFftLengthBy2 - 1]; |
Gustaf Ullberg | c9b89aa | 2017-11-16 11:20:59 +0100 | [diff] [blame] | 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_); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | } // namespace webrtc |