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/erl_estimator.h" |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 12 | |
| 13 | #include <algorithm> |
Gustaf Ullberg | fe4d673 | 2017-11-16 09:31:27 +0100 | [diff] [blame] | 14 | #include <numeric> |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 15 | |
| 16 | namespace webrtc { |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | constexpr float kMinErl = 0.01f; |
| 21 | constexpr float kMaxErl = 1000.f; |
| 22 | |
| 23 | } // namespace |
| 24 | |
| 25 | ErlEstimator::ErlEstimator() { |
| 26 | erl_.fill(kMaxErl); |
| 27 | hold_counters_.fill(0); |
Gustaf Ullberg | fe4d673 | 2017-11-16 09:31:27 +0100 | [diff] [blame] | 28 | erl_time_domain_ = kMaxErl; |
| 29 | hold_counter_time_domain_ = 0; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | ErlEstimator::~ErlEstimator() = default; |
| 33 | |
Per Ã…hgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 34 | void 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()); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 38 | 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 Ullberg | fe4d673 | 2017-11-16 09:31:27 +0100 | [diff] [blame] | 50 | erl_[k] += 0.1f * (new_erl - erl_[k]); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 51 | 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 Ullberg | fe4d673 | 2017-11-16 09:31:27 +0100 | [diff] [blame] | 65 | |
| 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_); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | } // namespace webrtc |