| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/modules/audio_processing/aec3/erle_estimator.h" | 11 #include "webrtc/modules/audio_processing/aec3/erle_estimator.h" |
| 12 | 12 |
| 13 #include <algorithm> | 13 #include <algorithm> |
| 14 | 14 |
| 15 #include "webrtc/rtc_base/safe_minmax.h" | 15 #include "webrtc/rtc_base/safe_minmax.h" |
| 16 | 16 |
| 17 namespace webrtc { | 17 namespace webrtc { |
| 18 | 18 |
| 19 namespace { | 19 ErleEstimator::ErleEstimator(float min_erle, |
| 20 | 20 float max_erle_lf, |
| 21 constexpr float kMinErle = 1.f; | 21 float max_erle_hf) |
| 22 constexpr float kMaxLfErle = 8.f; | 22 : min_erle_(min_erle), |
| 23 constexpr float kMaxHfErle = 1.5f; | 23 max_erle_lf_(max_erle_lf), |
| 24 | 24 max_erle_hf_(max_erle_hf) { |
| 25 } // namespace | 25 erle_.fill(min_erle_); |
| 26 | |
| 27 ErleEstimator::ErleEstimator() { | |
| 28 erle_.fill(kMinErle); | |
| 29 hold_counters_.fill(0); | 26 hold_counters_.fill(0); |
| 30 } | 27 } |
| 31 | 28 |
| 32 ErleEstimator::~ErleEstimator() = default; | 29 ErleEstimator::~ErleEstimator() = default; |
| 33 | 30 |
| 34 void ErleEstimator::Update( | 31 void ErleEstimator::Update( |
| 35 const std::array<float, kFftLengthBy2Plus1>& render_spectrum, | 32 const std::array<float, kFftLengthBy2Plus1>& render_spectrum, |
| 36 const std::array<float, kFftLengthBy2Plus1>& capture_spectrum, | 33 const std::array<float, kFftLengthBy2Plus1>& capture_spectrum, |
| 37 const std::array<float, kFftLengthBy2Plus1>& subtractor_spectrum) { | 34 const std::array<float, kFftLengthBy2Plus1>& subtractor_spectrum) { |
| 38 const auto& X2 = render_spectrum; | 35 const auto& X2 = render_spectrum; |
| 39 const auto& Y2 = capture_spectrum; | 36 const auto& Y2 = capture_spectrum; |
| 40 const auto& E2 = subtractor_spectrum; | 37 const auto& E2 = subtractor_spectrum; |
| 41 | 38 |
| 42 // Corresponds of WGN of power -46 dBFS. | 39 // Corresponds of WGN of power -46 dBFS. |
| 43 constexpr float kX2Min = 44015068.0f; | 40 constexpr float kX2Min = 44015068.0f; |
| 44 | 41 |
| 45 // Update the estimates in a clamped minimum statistics manner. | 42 // Update the estimates in a clamped minimum statistics manner. |
| 46 auto erle_update = [&](size_t start, size_t stop, float max_erle) { | 43 auto erle_update = [&](size_t start, size_t stop, float max_erle) { |
| 47 for (size_t k = start; k < stop; ++k) { | 44 for (size_t k = start; k < stop; ++k) { |
| 48 if (X2[k] > kX2Min && E2[k] > 0.f) { | 45 if (X2[k] > kX2Min && E2[k] > 0.f) { |
| 49 const float new_erle = Y2[k] / E2[k]; | 46 const float new_erle = Y2[k] / E2[k]; |
| 50 if (new_erle > erle_[k]) { | 47 if (new_erle > erle_[k]) { |
| 51 hold_counters_[k - 1] = 100; | 48 hold_counters_[k - 1] = 100; |
| 52 erle_[k] += 0.1f * (new_erle - erle_[k]); | 49 erle_[k] += 0.1f * (new_erle - erle_[k]); |
| 53 erle_[k] = rtc::SafeClamp(erle_[k], kMinErle, max_erle); | 50 erle_[k] = rtc::SafeClamp(erle_[k], min_erle_, max_erle); |
| 54 } | 51 } |
| 55 } | 52 } |
| 56 } | 53 } |
| 57 }; | 54 }; |
| 58 erle_update(1, kFftLengthBy2 / 2, kMaxLfErle); | 55 erle_update(1, kFftLengthBy2 / 2, max_erle_lf_); |
| 59 erle_update(kFftLengthBy2 / 2, kFftLengthBy2, kMaxHfErle); | 56 erle_update(kFftLengthBy2 / 2, kFftLengthBy2, max_erle_hf_); |
| 60 | 57 |
| 61 std::for_each(hold_counters_.begin(), hold_counters_.end(), | 58 std::for_each(hold_counters_.begin(), hold_counters_.end(), |
| 62 [](int& a) { --a; }); | 59 [](int& a) { --a; }); |
| 63 std::transform(hold_counters_.begin(), hold_counters_.end(), | 60 std::transform(hold_counters_.begin(), hold_counters_.end(), |
| 64 erle_.begin() + 1, erle_.begin() + 1, [](int a, float b) { | 61 erle_.begin() + 1, erle_.begin() + 1, [&](int a, float b) { |
| 65 return a > 0 ? b : std::max(kMinErle, 0.97f * b); | 62 return a > 0 ? b : std::max(min_erle_, 0.97f * b); |
| 66 }); | 63 }); |
| 67 | 64 |
| 68 erle_[0] = erle_[1]; | 65 erle_[0] = erle_[1]; |
| 69 erle_[kFftLengthBy2] = erle_[kFftLengthBy2 - 1]; | 66 erle_[kFftLengthBy2] = erle_[kFftLengthBy2 - 1]; |
| 70 } | 67 } |
| 71 | 68 |
| 72 } // namespace webrtc | 69 } // namespace webrtc |
| OLD | NEW |