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/down_sampler_4khz.h" | |
12 | |
13 #include <math.h> | |
14 #include <sstream> | |
15 #include <string> | |
16 #include <vector> | |
17 | |
18 #include "webrtc/base/array_view.h" | |
aleloi
2017/01/27 15:37:47
Included in related header.
peah-webrtc
2017/02/02 14:04:47
Done.
| |
19 #include "webrtc/modules/audio_processing/aec3/aec3_constants.h" | |
20 #include "webrtc/test/gtest.h" | |
21 | |
22 namespace webrtc { | |
23 | |
24 namespace { | |
25 | |
26 std::string ProduceDebugText(int sample_rate_hz) { | |
27 std::ostringstream ss; | |
28 ss << "Sample rate: " << sample_rate_hz; | |
29 return ss.str(); | |
30 } | |
31 | |
32 constexpr float kPi = 3.141592f; | |
33 | |
34 } // namespace | |
35 | |
36 // Verifies that there is little aliasing from upper frequencies in the | |
37 // downsampling. | |
38 TEST(DownSampler4kHz, NoLeakageFromUpperFrequencies) { | |
39 constexpr size_t kNumBlocks = 1000; | |
40 | |
41 for (auto rate : {8000, 16000, 32000, 48000}) { | |
42 ProduceDebugText(rate); | |
43 float input[kBlockSize * kNumBlocks]; | |
44 | |
45 for (size_t k = 0; k < kBlockSize * kNumBlocks; ++k) { | |
46 input[k] = 32767.f * sin(2.f * kPi * 3.f / 8.f * k); | |
hlundin-webrtc
2017/02/01 08:30:01
Produces a sinusoid at frequency 3/8*fs. Comment a
peah-webrtc
2017/02/02 14:04:47
Done.
| |
47 } | |
48 | |
49 DownSampler4kHz down_sampler(rate); | |
50 float output[kSubBlockSize * kNumBlocks]; | |
51 | |
52 for (size_t frame = 0; frame < kNumBlocks; ++frame) { | |
53 down_sampler.DownSample( | |
54 rtc::ArrayView<const float>(&input[frame * kBlockSize], kBlockSize), | |
55 rtc::ArrayView<float>(&output[frame * kSubBlockSize], kSubBlockSize)); | |
56 } | |
57 | |
58 constexpr size_t kNumStartupBlocks = 50; | |
59 RTC_DCHECK_GT(kNumBlocks, kNumStartupBlocks); | |
60 rtc::ArrayView<const float> input_to_evaluate( | |
61 &input[kNumStartupBlocks * kBlockSize], | |
62 (kNumBlocks - kNumStartupBlocks) * kBlockSize); | |
63 rtc::ArrayView<const float> output_to_evaluate( | |
64 &output[kNumStartupBlocks * kSubBlockSize], | |
65 (kNumBlocks - kNumStartupBlocks) * kSubBlockSize); | |
66 const double input_power = | |
67 std::accumulate(input_to_evaluate.begin(), input_to_evaluate.end(), 0., | |
hlundin-webrtc
2017/02/01 08:30:01
std::inner_product
peah-webrtc
2017/02/02 14:04:47
Done.
| |
68 [](float a, float b) -> double { return a + b * b; }) / | |
69 input_to_evaluate.size(); | |
70 const double output_power = | |
71 std::accumulate(output_to_evaluate.begin(), output_to_evaluate.end(), | |
hlundin-webrtc
2017/02/01 08:30:01
std::inner_product
peah-webrtc
2017/02/02 14:04:47
Done.
| |
72 0., | |
73 [](float a, float b) -> double { return a + b * b; }) / | |
74 output_to_evaluate.size(); | |
75 // EXPECT_LT(0.25f, downsampled_power); | |
aleloi
2017/01/27 15:37:47
Remove outcommented code
peah-webrtc
2017/02/02 14:04:47
Done.
| |
76 EXPECT_GT(0.0001f * input_power, output_power); | |
77 } | |
78 } | |
79 | |
80 // Verifies that the impact of low-frequency content is small during the | |
81 // downsampling. | |
82 TEST(DownSampler4kHz, NoImpactOnLowerFrequencies) { | |
hlundin-webrtc
2017/02/01 08:30:02
Soooo much code dupe with the above. Iiuc, the onl
peah-webrtc
2017/02/02 14:04:47
Done.
| |
83 constexpr size_t kNumBlocks = 100; | |
84 | |
85 for (auto rate : {8000, 16000, 32000, 48000}) { | |
86 ProduceDebugText(rate); | |
87 float input[kBlockSize * kNumBlocks]; | |
88 | |
89 for (size_t k = 0; k < kBlockSize * kNumBlocks; ++k) { | |
90 input[k] = 32767.f * sin(2.f * kPi * 200.f * k / rate); | |
91 } | |
92 | |
93 DownSampler4kHz down_sampler(rate); | |
94 float output[kSubBlockSize * kNumBlocks]; | |
95 | |
96 for (size_t frame = 0; frame < kNumBlocks; ++frame) { | |
97 down_sampler.DownSample( | |
98 rtc::ArrayView<const float>(&input[frame * kBlockSize], kBlockSize), | |
99 rtc::ArrayView<float>(&output[frame * kSubBlockSize], kSubBlockSize)); | |
100 } | |
101 | |
102 constexpr size_t kNumStartupBlocks = 50; | |
103 RTC_DCHECK_GT(kNumBlocks, kNumStartupBlocks); | |
104 rtc::ArrayView<const float> input_to_evaluate( | |
105 &input[kNumStartupBlocks * kBlockSize], | |
106 (kNumBlocks - kNumStartupBlocks) * kBlockSize); | |
107 rtc::ArrayView<const float> output_to_evaluate( | |
108 &output[kNumStartupBlocks * kSubBlockSize], | |
109 (kNumBlocks - kNumStartupBlocks) * kSubBlockSize); | |
110 const double input_power = | |
111 std::accumulate(input_to_evaluate.begin(), input_to_evaluate.end(), 0., | |
112 [](float a, float b) -> double { return a + b * b; }) / | |
113 input_to_evaluate.size(); | |
114 const double output_power = | |
115 std::accumulate(output_to_evaluate.begin(), output_to_evaluate.end(), | |
116 0., | |
117 [](float a, float b) -> double { return a + b * b; }) / | |
118 output_to_evaluate.size(); | |
119 EXPECT_LT(0.5f * input_power, output_power); | |
aleloi
2017/01/27 15:37:47
It seems wrong if as much as half the energy/time
peah-webrtc
2017/02/02 14:04:47
True. I increased it a bit, but the impact on 8 kH
| |
120 } | |
121 } | |
122 | |
123 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) | |
124 // Verifies the check for correct sample rate. | |
125 TEST(DownSampler4kHz, WrongSampleRate) { | |
126 EXPECT_DEATH(DownSampler4kHz down_sampler(8001), ""); | |
127 } | |
128 | |
129 // Verifies the check for the input size. | |
130 TEST(DownSampler4kHz, WrongInputSize) { | |
131 for (auto rate : {8000, 16000, 32000, 48000}) { | |
132 ProduceDebugText(rate); | |
133 DownSampler4kHz down_sampler(rate); | |
134 std::vector<float> x(std::vector<float>(kBlockSize - 1, 0.f)); | |
135 std::vector<float> x_downsampled(std::vector<float>(kSubBlockSize, 0.f)); | |
136 EXPECT_DEATH(down_sampler.DownSample(x, x_downsampled), ""); | |
137 } | |
138 } | |
139 | |
140 // Verifies the check for the output size. | |
141 TEST(DownSampler4kHz, WrongOutputSize) { | |
142 for (auto rate : {8000, 16000, 32000, 48000}) { | |
143 ProduceDebugText(rate); | |
144 DownSampler4kHz down_sampler(rate); | |
145 std::vector<float> x(std::vector<float>(kBlockSize, 0.f)); | |
146 std::vector<float> x_downsampled( | |
147 std::vector<float>(kSubBlockSize - 1, 0.f)); | |
148 EXPECT_DEATH(down_sampler.DownSample(x, x_downsampled), ""); | |
149 } | |
150 } | |
151 | |
152 #endif | |
153 | |
154 } // namespace webrtc | |
OLD | NEW |