OLD | NEW |
(Empty) | |
| 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 |
| 11 #include "webrtc/modules/audio_processing/aec3/decimator_by_4.h" |
| 12 |
| 13 #include <math.h> |
| 14 #include <algorithm> |
| 15 #include <array> |
| 16 #include <sstream> |
| 17 #include <string> |
| 18 #include <vector> |
| 19 |
| 20 #include "webrtc/modules/audio_processing/aec3/aec3_constants.h" |
| 21 #include "webrtc/test/gtest.h" |
| 22 |
| 23 namespace webrtc { |
| 24 |
| 25 namespace { |
| 26 |
| 27 std::string ProduceDebugText(int sample_rate_hz) { |
| 28 std::ostringstream ss; |
| 29 ss << "Sample rate: " << sample_rate_hz; |
| 30 return ss.str(); |
| 31 } |
| 32 |
| 33 constexpr float kPi = 3.141592f; |
| 34 constexpr size_t kNumStartupBlocks = 50; |
| 35 constexpr size_t kNumBlocks = 1000; |
| 36 |
| 37 void ProduceDecimatedSinusoidalOutputPower(int sample_rate_hz, |
| 38 float sinusoidal_frequency_hz, |
| 39 float* input_power, |
| 40 float* output_power) { |
| 41 float input[kBlockSize * kNumBlocks]; |
| 42 |
| 43 // Produce a sinusoid of the specified frequency. |
| 44 for (size_t k = 0; k < kBlockSize * kNumBlocks; ++k) { |
| 45 input[k] = |
| 46 32767.f * sin(2.f * kPi * sinusoidal_frequency_hz * k / sample_rate_hz); |
| 47 } |
| 48 |
| 49 DecimatorBy4 decimator; |
| 50 std::array<float, kSubBlockSize * kNumBlocks> output; |
| 51 |
| 52 for (size_t k = 0; k < kNumBlocks; ++k) { |
| 53 std::array<float, kSubBlockSize> sub_block; |
| 54 |
| 55 decimator.Decimate( |
| 56 rtc::ArrayView<const float>(&input[k * kBlockSize], kBlockSize), |
| 57 &sub_block); |
| 58 |
| 59 std::copy(sub_block.begin(), sub_block.end(), |
| 60 output.begin() + k * kSubBlockSize); |
| 61 } |
| 62 |
| 63 ASSERT_GT(kNumBlocks, kNumStartupBlocks); |
| 64 rtc::ArrayView<const float> input_to_evaluate( |
| 65 &input[kNumStartupBlocks * kBlockSize], |
| 66 (kNumBlocks - kNumStartupBlocks) * kBlockSize); |
| 67 rtc::ArrayView<const float> output_to_evaluate( |
| 68 &output[kNumStartupBlocks * kSubBlockSize], |
| 69 (kNumBlocks - kNumStartupBlocks) * kSubBlockSize); |
| 70 *input_power = |
| 71 std::inner_product(input_to_evaluate.begin(), input_to_evaluate.end(), |
| 72 input_to_evaluate.begin(), 0.f) / |
| 73 input_to_evaluate.size(); |
| 74 *output_power = |
| 75 std::inner_product(output_to_evaluate.begin(), output_to_evaluate.end(), |
| 76 output_to_evaluate.begin(), 0.f) / |
| 77 output_to_evaluate.size(); |
| 78 } |
| 79 |
| 80 } // namespace |
| 81 |
| 82 // Verifies that there is little aliasing from upper frequencies in the |
| 83 // downsampling. |
| 84 TEST(DecimatorBy4, NoLeakageFromUpperFrequencies) { |
| 85 float input_power; |
| 86 float output_power; |
| 87 for (auto rate : {8000, 16000, 32000, 48000}) { |
| 88 ProduceDebugText(rate); |
| 89 ProduceDecimatedSinusoidalOutputPower(rate, 3.f / 8.f * rate, &input_power, |
| 90 &output_power); |
| 91 EXPECT_GT(0.0001f * input_power, output_power); |
| 92 } |
| 93 } |
| 94 |
| 95 // Verifies that the impact of low-frequency content is small during the |
| 96 // downsampling. |
| 97 TEST(DecimatorBy4, NoImpactOnLowerFrequencies) { |
| 98 float input_power; |
| 99 float output_power; |
| 100 for (auto rate : {8000, 16000, 32000, 48000}) { |
| 101 ProduceDebugText(rate); |
| 102 ProduceDecimatedSinusoidalOutputPower(rate, 200.f, &input_power, |
| 103 &output_power); |
| 104 EXPECT_LT(0.7f * input_power, output_power); |
| 105 } |
| 106 } |
| 107 |
| 108 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
| 109 // Verifies the check for the input size. |
| 110 TEST(DecimatorBy4, WrongInputSize) { |
| 111 DecimatorBy4 decimator; |
| 112 std::vector<float> x(std::vector<float>(kBlockSize - 1, 0.f)); |
| 113 std::array<float, kSubBlockSize> x_downsampled; |
| 114 EXPECT_DEATH(decimator.Decimate(x, &x_downsampled), ""); |
| 115 } |
| 116 |
| 117 // Verifies the check for non-null output parameter. |
| 118 TEST(DecimatorBy4, WrongOutputSize) { |
| 119 DecimatorBy4 decimator; |
| 120 std::vector<float> x(std::vector<float>(kBlockSize, 0.f)); |
| 121 EXPECT_DEATH(decimator.Decimate(x, nullptr), ""); |
| 122 } |
| 123 |
| 124 #endif |
| 125 |
| 126 } // namespace webrtc |
OLD | NEW |