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