1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
/*
* Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "modules/audio_processing/aec3/decimator.h"
#include <math.h>
#include <algorithm>
#include <array>
#include <cmath>
#include <cstring>
#include <numbers>
#include <numeric>
#include <string>
#include <vector>
#include "api/array_view.h"
#include "modules/audio_processing/aec3/aec3_common.h"
#include "rtc_base/checks.h"
#include "rtc_base/strings/string_builder.h"
#include "test/gtest.h"
namespace webrtc {
namespace {
std::string ProduceDebugText(int sample_rate_hz) {
StringBuilder ss;
ss << "Sample rate: " << sample_rate_hz;
return ss.Release();
}
constexpr size_t kDownSamplingFactors[] = {4, 8};
constexpr float kPi = std::numbers::pi_v<float>;
constexpr size_t kNumStartupBlocks = 50;
constexpr size_t kNumBlocks = 1000;
void ProduceDecimatedSinusoidalOutputPower(int sample_rate_hz,
size_t down_sampling_factor,
float sinusoidal_frequency_hz,
float* input_power,
float* output_power) {
float input[kBlockSize * kNumBlocks];
const size_t sub_block_size = kBlockSize / down_sampling_factor;
// Produce a sinusoid of the specified frequency.
for (size_t k = 0; k < kBlockSize * kNumBlocks; ++k) {
input[k] = 32767.f * std::sin(2.f * kPi * sinusoidal_frequency_hz * k /
sample_rate_hz);
}
Decimator decimator(down_sampling_factor);
std::vector<float> output(sub_block_size * kNumBlocks);
for (size_t k = 0; k < kNumBlocks; ++k) {
std::vector<float> sub_block(sub_block_size);
decimator.Decimate(
ArrayView<const float>(&input[k * kBlockSize], kBlockSize), sub_block);
std::copy(sub_block.begin(), sub_block.end(),
output.begin() + k * sub_block_size);
}
ASSERT_GT(kNumBlocks, kNumStartupBlocks);
ArrayView<const float> input_to_evaluate(
&input[kNumStartupBlocks * kBlockSize],
(kNumBlocks - kNumStartupBlocks) * kBlockSize);
ArrayView<const float> output_to_evaluate(
&output[kNumStartupBlocks * sub_block_size],
(kNumBlocks - kNumStartupBlocks) * sub_block_size);
*input_power =
std::inner_product(input_to_evaluate.begin(), input_to_evaluate.end(),
input_to_evaluate.begin(), 0.f) /
input_to_evaluate.size();
*output_power =
std::inner_product(output_to_evaluate.begin(), output_to_evaluate.end(),
output_to_evaluate.begin(), 0.f) /
output_to_evaluate.size();
}
} // namespace
// Verifies that there is little aliasing from upper frequencies in the
// downsampling.
TEST(Decimator, NoLeakageFromUpperFrequencies) {
float input_power;
float output_power;
for (auto rate : {16000, 32000, 48000}) {
for (auto down_sampling_factor : kDownSamplingFactors) {
ProduceDebugText(rate);
ProduceDecimatedSinusoidalOutputPower(rate, down_sampling_factor,
3.f / 8.f * rate, &input_power,
&output_power);
EXPECT_GT(0.0001f * input_power, output_power);
}
}
}
#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
// Verifies the check for the input size.
TEST(DecimatorDeathTest, WrongInputSize) {
Decimator decimator(4);
std::vector<float> x(kBlockSize - 1, 0.f);
std::array<float, kBlockSize / 4> x_downsampled;
EXPECT_DEATH(decimator.Decimate(x, x_downsampled), "");
}
// Verifies the check for non-null output parameter.
TEST(DecimatorDeathTest, NullOutput) {
Decimator decimator(4);
std::vector<float> x(kBlockSize, 0.f);
EXPECT_DEATH(decimator.Decimate(x, nullptr), "");
}
// Verifies the check for the output size.
TEST(DecimatorDeathTest, WrongOutputSize) {
Decimator decimator(4);
std::vector<float> x(kBlockSize, 0.f);
std::array<float, kBlockSize / 4 - 1> x_downsampled;
EXPECT_DEATH(decimator.Decimate(x, x_downsampled), "");
}
// Verifies the check for the correct downsampling factor.
TEST(DecimatorDeathTest, CorrectDownSamplingFactor) {
EXPECT_DEATH(Decimator(3), "");
}
#endif
} // namespace webrtc
|