peah | 2192089 | 2017-02-08 05:08:56 -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 | |
Per Åhgren | 38e2d95 | 2017-11-17 14:54:28 +0100 | [diff] [blame] | 11 | #include "modules/audio_processing/aec3/decimator.h" |
peah | 2192089 | 2017-02-08 05:08:56 -0800 | [diff] [blame] | 12 | |
| 13 | #include <math.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 14 | |
peah | 2192089 | 2017-02-08 05:08:56 -0800 | [diff] [blame] | 15 | #include <algorithm> |
| 16 | #include <array> |
Mirko Bonadei | dbce090 | 2019-03-15 07:39:02 +0100 | [diff] [blame] | 17 | #include <cmath> |
Gustaf Ullberg | ee84d39 | 2019-09-10 09:36:43 +0200 | [diff] [blame] | 18 | #include <cstring> |
peah | 2192089 | 2017-02-08 05:08:56 -0800 | [diff] [blame] | 19 | #include <numeric> |
peah | 2192089 | 2017-02-08 05:08:56 -0800 | [diff] [blame] | 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "modules/audio_processing/aec3/aec3_common.h" |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 24 | #include "rtc_base/strings/string_builder.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "test/gtest.h" |
peah | 2192089 | 2017-02-08 05:08:56 -0800 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
| 28 | |
| 29 | namespace { |
| 30 | |
| 31 | std::string ProduceDebugText(int sample_rate_hz) { |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 32 | rtc::StringBuilder ss; |
peah | 2192089 | 2017-02-08 05:08:56 -0800 | [diff] [blame] | 33 | ss << "Sample rate: " << sample_rate_hz; |
Jonas Olsson | 84df1c7 | 2018-09-14 16:59:32 +0200 | [diff] [blame] | 34 | return ss.Release(); |
peah | 2192089 | 2017-02-08 05:08:56 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Per Åhgren | 38e2d95 | 2017-11-17 14:54:28 +0100 | [diff] [blame] | 37 | constexpr size_t kDownSamplingFactors[] = {2, 4, 8}; |
peah | 2192089 | 2017-02-08 05:08:56 -0800 | [diff] [blame] | 38 | constexpr float kPi = 3.141592f; |
| 39 | constexpr size_t kNumStartupBlocks = 50; |
| 40 | constexpr size_t kNumBlocks = 1000; |
| 41 | |
| 42 | void ProduceDecimatedSinusoidalOutputPower(int sample_rate_hz, |
Per Åhgren | 38e2d95 | 2017-11-17 14:54:28 +0100 | [diff] [blame] | 43 | size_t down_sampling_factor, |
peah | 2192089 | 2017-02-08 05:08:56 -0800 | [diff] [blame] | 44 | float sinusoidal_frequency_hz, |
| 45 | float* input_power, |
| 46 | float* output_power) { |
| 47 | float input[kBlockSize * kNumBlocks]; |
Per Åhgren | 38e2d95 | 2017-11-17 14:54:28 +0100 | [diff] [blame] | 48 | const size_t sub_block_size = kBlockSize / down_sampling_factor; |
peah | 2192089 | 2017-02-08 05:08:56 -0800 | [diff] [blame] | 49 | |
| 50 | // Produce a sinusoid of the specified frequency. |
| 51 | for (size_t k = 0; k < kBlockSize * kNumBlocks; ++k) { |
Mirko Bonadei | dbce090 | 2019-03-15 07:39:02 +0100 | [diff] [blame] | 52 | input[k] = 32767.f * std::sin(2.f * kPi * sinusoidal_frequency_hz * k / |
| 53 | sample_rate_hz); |
peah | 2192089 | 2017-02-08 05:08:56 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Per Åhgren | 38e2d95 | 2017-11-17 14:54:28 +0100 | [diff] [blame] | 56 | Decimator decimator(down_sampling_factor); |
| 57 | std::vector<float> output(sub_block_size * kNumBlocks); |
peah | 2192089 | 2017-02-08 05:08:56 -0800 | [diff] [blame] | 58 | |
| 59 | for (size_t k = 0; k < kNumBlocks; ++k) { |
Per Åhgren | 38e2d95 | 2017-11-17 14:54:28 +0100 | [diff] [blame] | 60 | std::vector<float> sub_block(sub_block_size); |
Per Åhgren | 6a05bb1 | 2019-12-03 11:24:59 +0100 | [diff] [blame] | 61 | decimator.Decimate( |
| 62 | rtc::ArrayView<const float>(&input[k * kBlockSize], kBlockSize), |
| 63 | sub_block); |
peah | 2192089 | 2017-02-08 05:08:56 -0800 | [diff] [blame] | 64 | |
| 65 | std::copy(sub_block.begin(), sub_block.end(), |
Per Åhgren | 38e2d95 | 2017-11-17 14:54:28 +0100 | [diff] [blame] | 66 | output.begin() + k * sub_block_size); |
peah | 2192089 | 2017-02-08 05:08:56 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | ASSERT_GT(kNumBlocks, kNumStartupBlocks); |
| 70 | rtc::ArrayView<const float> input_to_evaluate( |
| 71 | &input[kNumStartupBlocks * kBlockSize], |
| 72 | (kNumBlocks - kNumStartupBlocks) * kBlockSize); |
| 73 | rtc::ArrayView<const float> output_to_evaluate( |
Per Åhgren | 38e2d95 | 2017-11-17 14:54:28 +0100 | [diff] [blame] | 74 | &output[kNumStartupBlocks * sub_block_size], |
| 75 | (kNumBlocks - kNumStartupBlocks) * sub_block_size); |
peah | 2192089 | 2017-02-08 05:08:56 -0800 | [diff] [blame] | 76 | *input_power = |
| 77 | std::inner_product(input_to_evaluate.begin(), input_to_evaluate.end(), |
| 78 | input_to_evaluate.begin(), 0.f) / |
| 79 | input_to_evaluate.size(); |
| 80 | *output_power = |
| 81 | std::inner_product(output_to_evaluate.begin(), output_to_evaluate.end(), |
| 82 | output_to_evaluate.begin(), 0.f) / |
| 83 | output_to_evaluate.size(); |
| 84 | } |
| 85 | |
| 86 | } // namespace |
| 87 | |
| 88 | // Verifies that there is little aliasing from upper frequencies in the |
| 89 | // downsampling. |
Per Åhgren | 38e2d95 | 2017-11-17 14:54:28 +0100 | [diff] [blame] | 90 | TEST(Decimator, NoLeakageFromUpperFrequencies) { |
peah | 2192089 | 2017-02-08 05:08:56 -0800 | [diff] [blame] | 91 | float input_power; |
| 92 | float output_power; |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 93 | for (auto rate : {16000, 32000, 48000}) { |
Per Åhgren | 38e2d95 | 2017-11-17 14:54:28 +0100 | [diff] [blame] | 94 | for (auto down_sampling_factor : kDownSamplingFactors) { |
| 95 | ProduceDebugText(rate); |
| 96 | ProduceDecimatedSinusoidalOutputPower(rate, down_sampling_factor, |
| 97 | 3.f / 8.f * rate, &input_power, |
| 98 | &output_power); |
| 99 | EXPECT_GT(0.0001f * input_power, output_power); |
| 100 | } |
peah | 2192089 | 2017-02-08 05:08:56 -0800 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
peah | 2192089 | 2017-02-08 05:08:56 -0800 | [diff] [blame] | 104 | #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
| 105 | // Verifies the check for the input size. |
Tommi | a5e07cc | 2020-05-26 21:40:37 +0200 | [diff] [blame] | 106 | TEST(DecimatorDeathTest, WrongInputSize) { |
Per Åhgren | 38e2d95 | 2017-11-17 14:54:28 +0100 | [diff] [blame] | 107 | Decimator decimator(4); |
Per Åhgren | 6a05bb1 | 2019-12-03 11:24:59 +0100 | [diff] [blame] | 108 | std::vector<float> x(kBlockSize - 1, 0.f); |
Per Åhgren | 38e2d95 | 2017-11-17 14:54:28 +0100 | [diff] [blame] | 109 | std::array<float, kBlockSize / 4> x_downsampled; |
Per Åhgren | 6a05bb1 | 2019-12-03 11:24:59 +0100 | [diff] [blame] | 110 | EXPECT_DEATH(decimator.Decimate(x, x_downsampled), ""); |
peah | 2192089 | 2017-02-08 05:08:56 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | // Verifies the check for non-null output parameter. |
Tommi | a5e07cc | 2020-05-26 21:40:37 +0200 | [diff] [blame] | 114 | TEST(DecimatorDeathTest, NullOutput) { |
Per Åhgren | 38e2d95 | 2017-11-17 14:54:28 +0100 | [diff] [blame] | 115 | Decimator decimator(4); |
Per Åhgren | 6a05bb1 | 2019-12-03 11:24:59 +0100 | [diff] [blame] | 116 | std::vector<float> x(kBlockSize, 0.f); |
| 117 | EXPECT_DEATH(decimator.Decimate(x, nullptr), ""); |
peah | 2192089 | 2017-02-08 05:08:56 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Per Åhgren | 38e2d95 | 2017-11-17 14:54:28 +0100 | [diff] [blame] | 120 | // Verifies the check for the output size. |
Tommi | a5e07cc | 2020-05-26 21:40:37 +0200 | [diff] [blame] | 121 | TEST(DecimatorDeathTest, WrongOutputSize) { |
Per Åhgren | 38e2d95 | 2017-11-17 14:54:28 +0100 | [diff] [blame] | 122 | Decimator decimator(4); |
Per Åhgren | 6a05bb1 | 2019-12-03 11:24:59 +0100 | [diff] [blame] | 123 | std::vector<float> x(kBlockSize, 0.f); |
Per Åhgren | 38e2d95 | 2017-11-17 14:54:28 +0100 | [diff] [blame] | 124 | std::array<float, kBlockSize / 4 - 1> x_downsampled; |
Per Åhgren | 6a05bb1 | 2019-12-03 11:24:59 +0100 | [diff] [blame] | 125 | EXPECT_DEATH(decimator.Decimate(x, x_downsampled), ""); |
Per Åhgren | 38e2d95 | 2017-11-17 14:54:28 +0100 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | // Verifies the check for the correct downsampling factor. |
Tommi | a5e07cc | 2020-05-26 21:40:37 +0200 | [diff] [blame] | 129 | TEST(DecimatorDeathTest, CorrectDownSamplingFactor) { |
Per Åhgren | 38e2d95 | 2017-11-17 14:54:28 +0100 | [diff] [blame] | 130 | EXPECT_DEATH(Decimator(3), ""); |
| 131 | } |
| 132 | |
peah | 2192089 | 2017-02-08 05:08:56 -0800 | [diff] [blame] | 133 | #endif |
| 134 | |
| 135 | } // namespace webrtc |