1 /*
2  *  Copyright (c) 2012 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 "modules/audio_processing/vad/pole_zero_filter.h"
12 
13 #include <math.h>
14 #include <stdio.h>
15 
16 #include <memory>
17 
18 #include "modules/audio_processing/vad/vad_audio_proc_internal.h"
19 #include "test/gtest.h"
20 #include "test/testsupport/file_utils.h"
21 
22 namespace webrtc {
23 
24 static const int kInputSamples = 50;
25 
26 static const int16_t kInput[kInputSamples] = {
27     -2136,  -7116, 10715,  2464,   3164,   8139,   11393, 24013, -32117, -5544,
28     -27740, 10181, 14190,  -24055, -15912, 17393,  6359,  -9950, -13894, 32432,
29     -23944, 3437,  -8381,  19768,  3087,   -19795, -5920, 13310, 1407,   3876,
30     4059,   3524,  -23130, 19121,  -27900, -24840, 4089,  21422, -3625,  3015,
31     -11236, 28856, 13424,  6571,   -19761, -6361,  15821, -9469, 29727,  32229};
32 
33 static const float kReferenceOutput[kInputSamples] = {
34     -2082.230472f,  -6878.572941f,  10697.090871f,  2358.373952f,
35     2973.936512f,   7738.580650f,   10690.803213f,  22687.091576f,
36     -32676.684717f, -5879.621684f,  -27359.297432f, 10368.735888f,
37     13994.584604f,  -23676.126249f, -15078.250390f, 17818.253338f,
38     6577.743123f,   -9498.369315f,  -13073.651079f, 32460.026588f,
39     -23391.849347f, 3953.805667f,   -7667.761363f,  19995.153447f,
40     3185.575477f,   -19207.365160f, -5143.103201f,  13756.317237f,
41     1779.654794f,   4142.269755f,   4209.475034f,   3572.991789f,
42     -22509.089546f, 19307.878964f,  -27060.439759f, -23319.042810f,
43     5547.685267f,   22312.718676f,  -2707.309027f,  3852.358490f,
44     -10135.510093f, 29241.509970f,  13394.397233f,  6340.721417f,
45     -19510.207905f, -5908.442086f,  15882.301634f,  -9211.335255f,
46     29253.056735f,  30874.443046f};
47 
48 class PoleZeroFilterTest : public ::testing::Test {
49  protected:
PoleZeroFilterTest()50   PoleZeroFilterTest()
51       : my_filter_(PoleZeroFilter::Create(kCoeffNumerator,
52                                           kFilterOrder,
53                                           kCoeffDenominator,
54                                           kFilterOrder)) {}
55 
~PoleZeroFilterTest()56   ~PoleZeroFilterTest() override {}
57 
58   void FilterSubframes(int num_subframes);
59 
60  private:
61   void TestClean();
62   std::unique_ptr<PoleZeroFilter> my_filter_;
63 };
64 
FilterSubframes(int num_subframes)65 void PoleZeroFilterTest::FilterSubframes(int num_subframes) {
66   float output[kInputSamples];
67   const int num_subframe_samples = kInputSamples / num_subframes;
68   EXPECT_EQ(num_subframe_samples * num_subframes, kInputSamples);
69 
70   for (int n = 0; n < num_subframes; n++) {
71     my_filter_->Filter(&kInput[n * num_subframe_samples], num_subframe_samples,
72                        &output[n * num_subframe_samples]);
73   }
74   for (int n = 0; n < kInputSamples; n++) {
75     EXPECT_NEAR(output[n], kReferenceOutput[n], 1);
76   }
77 }
78 
TEST_F(PoleZeroFilterTest,OneSubframe)79 TEST_F(PoleZeroFilterTest, OneSubframe) {
80   FilterSubframes(1);
81 }
82 
TEST_F(PoleZeroFilterTest,TwoSubframes)83 TEST_F(PoleZeroFilterTest, TwoSubframes) {
84   FilterSubframes(2);
85 }
86 
TEST_F(PoleZeroFilterTest,FiveSubframes)87 TEST_F(PoleZeroFilterTest, FiveSubframes) {
88   FilterSubframes(5);
89 }
90 
TEST_F(PoleZeroFilterTest,TenSubframes)91 TEST_F(PoleZeroFilterTest, TenSubframes) {
92   FilterSubframes(10);
93 }
94 
TEST_F(PoleZeroFilterTest,TwentyFiveSubframes)95 TEST_F(PoleZeroFilterTest, TwentyFiveSubframes) {
96   FilterSubframes(25);
97 }
98 
TEST_F(PoleZeroFilterTest,FiftySubframes)99 TEST_F(PoleZeroFilterTest, FiftySubframes) {
100   FilterSubframes(50);
101 }
102 
103 }  // namespace webrtc
104