| 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 namespace webrtc { | 15 namespace webrtc { |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 constexpr float kMinErle = 1.f; | 19 constexpr float kMinErle = 1.f; |
| 20 constexpr float kMaxErle = 8.f; | 20 constexpr float kMaxLfErle = 8.f; |
| 21 constexpr float kMaxHfErle = 1.5f; |
| 21 | 22 |
| 22 } // namespace | 23 } // namespace |
| 23 | 24 |
| 24 ErleEstimator::ErleEstimator() { | 25 ErleEstimator::ErleEstimator() { |
| 25 erle_.fill(kMinErle); | 26 erle_.fill(kMinErle); |
| 26 hold_counters_.fill(0); | 27 hold_counters_.fill(0); |
| 27 } | 28 } |
| 28 | 29 |
| 29 ErleEstimator::~ErleEstimator() = default; | 30 ErleEstimator::~ErleEstimator() = default; |
| 30 | 31 |
| 31 void ErleEstimator::Update( | 32 void ErleEstimator::Update( |
| 32 const std::array<float, kFftLengthBy2Plus1>& render_spectrum, | 33 const std::array<float, kFftLengthBy2Plus1>& render_spectrum, |
| 33 const std::array<float, kFftLengthBy2Plus1>& capture_spectrum, | 34 const std::array<float, kFftLengthBy2Plus1>& capture_spectrum, |
| 34 const std::array<float, kFftLengthBy2Plus1>& subtractor_spectrum) { | 35 const std::array<float, kFftLengthBy2Plus1>& subtractor_spectrum) { |
| 35 const auto& X2 = render_spectrum; | 36 const auto& X2 = render_spectrum; |
| 36 const auto& Y2 = capture_spectrum; | 37 const auto& Y2 = capture_spectrum; |
| 37 const auto& E2 = subtractor_spectrum; | 38 const auto& E2 = subtractor_spectrum; |
| 38 | 39 |
| 39 // Corresponds of WGN of power -46 dBFS. | 40 // Corresponds of WGN of power -46 dBFS. |
| 40 constexpr float kX2Min = 44015068.0f; | 41 constexpr float kX2Min = 44015068.0f; |
| 41 | 42 |
| 42 // Update the estimates in a clamped minimum statistics manner. | 43 // Update the estimates in a clamped minimum statistics manner. |
| 43 for (size_t k = 1; k < kFftLengthBy2; ++k) { | 44 size_t k = 1; |
| 44 if (X2[k] > kX2Min && E2[k] > 0.f) { | 45 size_t band_limit = kFftLengthBy2 / 2; |
| 45 const float new_erle = Y2[k] / E2[k]; | 46 float max_erle = kMaxLfErle; |
| 46 if (new_erle > erle_[k]) { | 47 for (int j = 0; j < 2; ++j) { |
| 47 hold_counters_[k - 1] = 100; | 48 for (; k < band_limit; ++k) { |
| 48 erle_[k] += 0.1f * (new_erle - erle_[k]); | 49 if (X2[k] > kX2Min && E2[k] > 0.f) { |
| 49 erle_[k] = std::max(kMinErle, std::min(erle_[k], kMaxErle)); | 50 const float new_erle = Y2[k] / E2[k]; |
| 51 if (new_erle > erle_[k]) { |
| 52 hold_counters_[k - 1] = 100; |
| 53 erle_[k] += 0.1f * (new_erle - erle_[k]); |
| 54 erle_[k] = std::max(kMinErle, std::min(erle_[k], max_erle)); |
| 55 } |
| 50 } | 56 } |
| 51 } | 57 } |
| 58 band_limit = kFftLengthBy2; |
| 59 max_erle = kMaxHfErle; |
| 52 } | 60 } |
| 53 | 61 |
| 54 std::for_each(hold_counters_.begin(), hold_counters_.end(), | 62 std::for_each(hold_counters_.begin(), hold_counters_.end(), |
| 55 [](int& a) { --a; }); | 63 [](int& a) { --a; }); |
| 56 std::transform(hold_counters_.begin(), hold_counters_.end(), | 64 std::transform(hold_counters_.begin(), hold_counters_.end(), |
| 57 erle_.begin() + 1, erle_.begin() + 1, [](int a, float b) { | 65 erle_.begin() + 1, erle_.begin() + 1, [](int a, float b) { |
| 58 return a > 0 ? b : std::max(kMinErle, 0.97f * b); | 66 return a > 0 ? b : std::max(kMinErle, 0.97f * b); |
| 59 }); | 67 }); |
| 60 | 68 |
| 61 erle_[0] = erle_[1]; | 69 erle_[0] = erle_[1]; |
| 62 erle_[kFftLengthBy2] = erle_[kFftLengthBy2 - 1]; | 70 erle_[kFftLengthBy2] = erle_[kFftLengthBy2 - 1]; |
| 63 } | 71 } |
| 64 | 72 |
| 65 } // namespace webrtc | 73 } // namespace webrtc |
| OLD | NEW |