peah | 522d71b | 2017-02-23 13:16:26 | [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 04:47:31 | [diff] [blame] | 11 | #include "modules/audio_processing/aec3/erl_estimator.h" |
peah | 522d71b | 2017-02-23 13:16:26 | [diff] [blame] | 12 | |
Sam Zackrisson | 6e5433c | 2019-10-18 14:49:13 | [diff] [blame] | 13 | #include "rtc_base/strings/string_builder.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 14 | #include "test/gtest.h" |
peah | 522d71b | 2017-02-23 13:16:26 | [diff] [blame] | 15 | |
| 16 | namespace webrtc { |
| 17 | |
| 18 | namespace { |
Sam Zackrisson | 6e5433c | 2019-10-18 14:49:13 | [diff] [blame] | 19 | std::string ProduceDebugText(size_t num_render_channels, |
| 20 | size_t num_capture_channels) { |
| 21 | rtc::StringBuilder ss; |
| 22 | ss << "Render channels: " << num_render_channels; |
| 23 | ss << ", Capture channels: " << num_capture_channels; |
| 24 | return ss.Release(); |
| 25 | } |
peah | 522d71b | 2017-02-23 13:16:26 | [diff] [blame] | 26 | |
| 27 | void VerifyErl(const std::array<float, kFftLengthBy2Plus1>& erl, |
Gustaf Ullberg | fe4d673 | 2017-11-16 08:31:27 | [diff] [blame] | 28 | float erl_time_domain, |
peah | 522d71b | 2017-02-23 13:16:26 | [diff] [blame] | 29 | float reference) { |
| 30 | std::for_each(erl.begin(), erl.end(), |
| 31 | [reference](float a) { EXPECT_NEAR(reference, a, 0.001); }); |
Gustaf Ullberg | fe4d673 | 2017-11-16 08:31:27 | [diff] [blame] | 32 | EXPECT_NEAR(reference, erl_time_domain, 0.001); |
peah | 522d71b | 2017-02-23 13:16:26 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | } // namespace |
| 36 | |
Sam Zackrisson | b18c4eb | 2020-01-24 11:55:17 | [diff] [blame] | 37 | class ErlEstimatorMultiChannel |
| 38 | : public ::testing::Test, |
| 39 | public ::testing::WithParamInterface<std::tuple<size_t, size_t>> {}; |
| 40 | |
| 41 | INSTANTIATE_TEST_SUITE_P(MultiChannel, |
| 42 | ErlEstimatorMultiChannel, |
| 43 | ::testing::Combine(::testing::Values(1, 2, 8), |
| 44 | ::testing::Values(1, 2, 8))); |
| 45 | |
peah | 522d71b | 2017-02-23 13:16:26 | [diff] [blame] | 46 | // Verifies that the correct ERL estimates are achieved. |
Sam Zackrisson | b18c4eb | 2020-01-24 11:55:17 | [diff] [blame] | 47 | TEST_P(ErlEstimatorMultiChannel, Estimates) { |
| 48 | const size_t num_render_channels = std::get<0>(GetParam()); |
| 49 | const size_t num_capture_channels = std::get<1>(GetParam()); |
| 50 | SCOPED_TRACE(ProduceDebugText(num_render_channels, num_capture_channels)); |
| 51 | std::vector<std::array<float, kFftLengthBy2Plus1>> X2(num_render_channels); |
| 52 | for (auto& X2_ch : X2) { |
| 53 | X2_ch.fill(0.f); |
peah | 522d71b | 2017-02-23 13:16:26 | [diff] [blame] | 54 | } |
Sam Zackrisson | b18c4eb | 2020-01-24 11:55:17 | [diff] [blame] | 55 | std::vector<std::array<float, kFftLengthBy2Plus1>> Y2(num_capture_channels); |
| 56 | for (auto& Y2_ch : Y2) { |
| 57 | Y2_ch.fill(0.f); |
| 58 | } |
| 59 | std::vector<bool> converged_filters(num_capture_channels, false); |
| 60 | const size_t converged_idx = num_capture_channels - 1; |
| 61 | converged_filters[converged_idx] = true; |
peah | 522d71b | 2017-02-23 13:16:26 | [diff] [blame] | 62 | |
Sam Zackrisson | b18c4eb | 2020-01-24 11:55:17 | [diff] [blame] | 63 | ErlEstimator estimator(0); |
| 64 | |
| 65 | // Verifies that the ERL estimate is properly reduced to lower values. |
| 66 | for (auto& X2_ch : X2) { |
| 67 | X2_ch.fill(500 * 1000.f * 1000.f); |
| 68 | } |
| 69 | Y2[converged_idx].fill(10 * X2[0][0]); |
| 70 | for (size_t k = 0; k < 200; ++k) { |
| 71 | estimator.Update(converged_filters, X2, Y2); |
| 72 | } |
| 73 | VerifyErl(estimator.Erl(), estimator.ErlTimeDomain(), 10.f); |
| 74 | |
| 75 | // Verifies that the ERL is not immediately increased when the ERL in the |
| 76 | // data increases. |
| 77 | Y2[converged_idx].fill(10000 * X2[0][0]); |
| 78 | for (size_t k = 0; k < 998; ++k) { |
| 79 | estimator.Update(converged_filters, X2, Y2); |
| 80 | } |
| 81 | VerifyErl(estimator.Erl(), estimator.ErlTimeDomain(), 10.f); |
| 82 | |
| 83 | // Verifies that the rate of increase is 3 dB. |
| 84 | estimator.Update(converged_filters, X2, Y2); |
| 85 | VerifyErl(estimator.Erl(), estimator.ErlTimeDomain(), 20.f); |
| 86 | |
| 87 | // Verifies that the maximum ERL is achieved when there are no low RLE |
| 88 | // estimates. |
| 89 | for (size_t k = 0; k < 1000; ++k) { |
| 90 | estimator.Update(converged_filters, X2, Y2); |
| 91 | } |
| 92 | VerifyErl(estimator.Erl(), estimator.ErlTimeDomain(), 1000.f); |
| 93 | |
| 94 | // Verifies that the ERL estimate is is not updated for low-level signals |
| 95 | for (auto& X2_ch : X2) { |
| 96 | X2_ch.fill(1000.f * 1000.f); |
| 97 | } |
| 98 | Y2[converged_idx].fill(10 * X2[0][0]); |
| 99 | for (size_t k = 0; k < 200; ++k) { |
| 100 | estimator.Update(converged_filters, X2, Y2); |
| 101 | } |
| 102 | VerifyErl(estimator.Erl(), estimator.ErlTimeDomain(), 1000.f); |
| 103 | } |
peah | 522d71b | 2017-02-23 13:16:26 | [diff] [blame] | 104 | } // namespace webrtc |