ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 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 | |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 11 | #include <cmath> |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 12 | #include <complex> |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 13 | #include <vector> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "modules/audio_processing/intelligibility/intelligibility_utils.h" |
| 16 | #include "rtc_base/arraysize.h" |
| 17 | #include "test/gtest.h" |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 18 | |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 19 | namespace webrtc { |
| 20 | |
| 21 | namespace intelligibility { |
| 22 | |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 23 | std::vector<std::vector<std::complex<float>>> GenerateTestData(size_t freqs, |
| 24 | size_t samples) { |
| 25 | std::vector<std::vector<std::complex<float>>> data(samples); |
| 26 | for (size_t i = 0; i < samples; ++i) { |
| 27 | for (size_t j = 0; j < freqs; ++j) { |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 28 | const float val = 0.99f / ((i + 1) * (j + 1)); |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 29 | data[i].push_back(std::complex<float>(val, val)); |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 30 | } |
| 31 | } |
| 32 | return data; |
| 33 | } |
| 34 | |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 35 | // Tests PowerEstimator, for all power step types. |
| 36 | TEST(IntelligibilityUtilsTest, TestPowerEstimator) { |
| 37 | const size_t kFreqs = 10; |
| 38 | const size_t kSamples = 100; |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 39 | const float kDecay = 0.5f; |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 40 | const std::vector<std::vector<std::complex<float>>> test_data( |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 41 | GenerateTestData(kFreqs, kSamples)); |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 42 | PowerEstimator<std::complex<float>> power_estimator(kFreqs, kDecay); |
| 43 | EXPECT_EQ(0, power_estimator.power()[0]); |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 44 | |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 45 | // Makes sure Step is doing something. |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 46 | power_estimator.Step(test_data[0].data()); |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 47 | for (size_t i = 1; i < kSamples; ++i) { |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 48 | power_estimator.Step(test_data[i].data()); |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 49 | for (size_t j = 0; j < kFreqs; ++j) { |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 50 | EXPECT_GE(power_estimator.power()[j], 0.f); |
| 51 | EXPECT_LE(power_estimator.power()[j], 1.f); |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // Tests gain applier. |
| 57 | TEST(IntelligibilityUtilsTest, TestGainApplier) { |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 58 | const size_t kFreqs = 10; |
| 59 | const size_t kSamples = 100; |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 60 | const float kChangeLimit = 0.1f; |
| 61 | GainApplier gain_applier(kFreqs, kChangeLimit); |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 62 | const std::vector<std::vector<std::complex<float>>> in_data( |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 63 | GenerateTestData(kFreqs, kSamples)); |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 64 | std::vector<std::vector<std::complex<float>>> out_data( |
| 65 | GenerateTestData(kFreqs, kSamples)); |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 66 | for (size_t i = 0; i < kSamples; ++i) { |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 67 | gain_applier.Apply(in_data[i].data(), out_data[i].data()); |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 68 | for (size_t j = 0; j < kFreqs; ++j) { |
| 69 | EXPECT_GT(out_data[i][j].real(), 0.f); |
| 70 | EXPECT_LT(out_data[i][j].real(), 1.f); |
| 71 | EXPECT_GT(out_data[i][j].imag(), 0.f); |
| 72 | EXPECT_LT(out_data[i][j].imag(), 1.f); |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | } // namespace intelligibility |
| 78 | |
| 79 | } // namespace webrtc |