summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/modules/audio_processing/transient/moving_moments_unittest.cc
blob: b0e613e7ab1eb0558dec98986d50483c6ae15952 (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 *  Copyright (c) 2013 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/transient/moving_moments.h"

#include <memory>

#include "test/gtest.h"

namespace webrtc {

static const float kTolerance = 0.0001f;

class MovingMomentsTest : public ::testing::Test {
 protected:
  static const size_t kMovingMomentsBufferLength = 5;
  static const size_t kMaxOutputLength = 20;  // Valid for this tests only.

  virtual void SetUp();
  // Calls CalculateMoments and verifies that it produces the expected
  // outputs.
  void CalculateMomentsAndVerify(const float* input,
                                 size_t input_length,
                                 const float* expected_mean,
                                 const float* expected_mean_squares);

  std::unique_ptr<MovingMoments> moving_moments_;
  float output_mean_[kMaxOutputLength];
  float output_mean_squares_[kMaxOutputLength];
};

const size_t MovingMomentsTest::kMaxOutputLength;

void MovingMomentsTest::SetUp() {
  moving_moments_.reset(new MovingMoments(kMovingMomentsBufferLength));
}

void MovingMomentsTest::CalculateMomentsAndVerify(
    const float* input,
    size_t input_length,
    const float* expected_mean,
    const float* expected_mean_squares) {
  ASSERT_LE(input_length, kMaxOutputLength);

  moving_moments_->CalculateMoments(input, input_length, output_mean_,
                                    output_mean_squares_);

  for (size_t i = 1; i < input_length; ++i) {
    EXPECT_NEAR(expected_mean[i], output_mean_[i], kTolerance);
    EXPECT_NEAR(expected_mean_squares[i], output_mean_squares_[i], kTolerance);
  }
}

TEST_F(MovingMomentsTest, CorrectMomentsOfAnAllZerosBuffer) {
  const float kInput[] = {0.f, 0.f, 0.f, 0.f, 0.f};
  const size_t kInputLength = sizeof(kInput) / sizeof(kInput[0]);

  const float expected_mean[kInputLength] = {0.f, 0.f, 0.f, 0.f, 0.f};
  const float expected_mean_squares[kInputLength] = {0.f, 0.f, 0.f, 0.f, 0.f};

  CalculateMomentsAndVerify(kInput, kInputLength, expected_mean,
                            expected_mean_squares);
}

TEST_F(MovingMomentsTest, CorrectMomentsOfAConstantBuffer) {
  const float kInput[] = {5.f, 5.f, 5.f, 5.f, 5.f, 5.f, 5.f, 5.f, 5.f, 5.f};
  const size_t kInputLength = sizeof(kInput) / sizeof(kInput[0]);

  const float expected_mean[kInputLength] = {1.f, 2.f, 3.f, 4.f, 5.f,
                                             5.f, 5.f, 5.f, 5.f, 5.f};
  const float expected_mean_squares[kInputLength] = {
      5.f, 10.f, 15.f, 20.f, 25.f, 25.f, 25.f, 25.f, 25.f, 25.f};

  CalculateMomentsAndVerify(kInput, kInputLength, expected_mean,
                            expected_mean_squares);
}

TEST_F(MovingMomentsTest, CorrectMomentsOfAnIncreasingBuffer) {
  const float kInput[] = {1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f, 9.f};
  const size_t kInputLength = sizeof(kInput) / sizeof(kInput[0]);

  const float expected_mean[kInputLength] = {0.2f, 0.6f, 1.2f, 2.f, 3.f,
                                             4.f,  5.f,  6.f,  7.f};
  const float expected_mean_squares[kInputLength] = {
      0.2f, 1.f, 2.8f, 6.f, 11.f, 18.f, 27.f, 38.f, 51.f};

  CalculateMomentsAndVerify(kInput, kInputLength, expected_mean,
                            expected_mean_squares);
}

TEST_F(MovingMomentsTest, CorrectMomentsOfADecreasingBuffer) {
  const float kInput[] = {-1.f, -2.f, -3.f, -4.f, -5.f, -6.f, -7.f, -8.f, -9.f};
  const size_t kInputLength = sizeof(kInput) / sizeof(kInput[0]);

  const float expected_mean[kInputLength] = {-0.2f, -0.6f, -1.2f, -2.f, -3.f,
                                             -4.f,  -5.f,  -6.f,  -7.f};
  const float expected_mean_squares[kInputLength] = {
      0.2f, 1.f, 2.8f, 6.f, 11.f, 18.f, 27.f, 38.f, 51.f};

  CalculateMomentsAndVerify(kInput, kInputLength, expected_mean,
                            expected_mean_squares);
}

TEST_F(MovingMomentsTest, CorrectMomentsOfAZeroMeanSequence) {
  const size_t kMovingMomentsBufferLength = 4;
  moving_moments_.reset(new MovingMoments(kMovingMomentsBufferLength));
  const float kInput[] = {1.f,  -1.f, 1.f,  -1.f, 1.f,
                          -1.f, 1.f,  -1.f, 1.f,  -1.f};
  const size_t kInputLength = sizeof(kInput) / sizeof(kInput[0]);

  const float expected_mean[kInputLength] = {0.25f, 0.f, 0.25f, 0.f, 0.f,
                                             0.f,   0.f, 0.f,   0.f, 0.f};
  const float expected_mean_squares[kInputLength] = {
      0.25f, 0.5f, 0.75f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f};

  CalculateMomentsAndVerify(kInput, kInputLength, expected_mean,
                            expected_mean_squares);
}

TEST_F(MovingMomentsTest, CorrectMomentsOfAnArbitraryBuffer) {
  const float kInput[] = {0.2f,  0.3f,  0.5f,  0.7f, 0.11f,
                          0.13f, 0.17f, 0.19f, 0.23f};
  const size_t kInputLength = sizeof(kInput) / sizeof(kInput[0]);

  const float expected_mean[kInputLength] = {
      0.04f, 0.1f, 0.2f, 0.34f, 0.362f, 0.348f, 0.322f, 0.26f, 0.166f};
  const float expected_mean_squares[kInputLength] = {0.008f,  0.026f,  0.076f,
                                                     0.174f,  0.1764f, 0.1718f,
                                                     0.1596f, 0.1168f, 0.0294f};

  CalculateMomentsAndVerify(kInput, kInputLength, expected_mean,
                            expected_mean_squares);
}

TEST_F(MovingMomentsTest, MutipleCalculateMomentsCalls) {
  const float kInputFirstCall[] = {0.2f,  0.3f,  0.5f,  0.7f, 0.11f,
                                   0.13f, 0.17f, 0.19f, 0.23f};
  const size_t kInputFirstCallLength =
      sizeof(kInputFirstCall) / sizeof(kInputFirstCall[0]);
  const float kInputSecondCall[] = {0.29f, 0.31f};
  const size_t kInputSecondCallLength =
      sizeof(kInputSecondCall) / sizeof(kInputSecondCall[0]);
  const float kInputThirdCall[] = {0.37f, 0.41f, 0.43f, 0.47f};
  const size_t kInputThirdCallLength =
      sizeof(kInputThirdCall) / sizeof(kInputThirdCall[0]);

  const float expected_mean_first_call[kInputFirstCallLength] = {
      0.04f, 0.1f, 0.2f, 0.34f, 0.362f, 0.348f, 0.322f, 0.26f, 0.166f};
  const float expected_mean_squares_first_call[kInputFirstCallLength] = {
      0.008f,  0.026f,  0.076f,  0.174f, 0.1764f,
      0.1718f, 0.1596f, 0.1168f, 0.0294f};

  const float expected_mean_second_call[kInputSecondCallLength] = {0.202f,
                                                                   0.238f};
  const float expected_mean_squares_second_call[kInputSecondCallLength] = {
      0.0438f, 0.0596f};

  const float expected_mean_third_call[kInputThirdCallLength] = {
      0.278f, 0.322f, 0.362f, 0.398f};
  const float expected_mean_squares_third_call[kInputThirdCallLength] = {
      0.0812f, 0.1076f, 0.134f, 0.1614f};

  CalculateMomentsAndVerify(kInputFirstCall, kInputFirstCallLength,
                            expected_mean_first_call,
                            expected_mean_squares_first_call);

  CalculateMomentsAndVerify(kInputSecondCall, kInputSecondCallLength,
                            expected_mean_second_call,
                            expected_mean_squares_second_call);

  CalculateMomentsAndVerify(kInputThirdCall, kInputThirdCallLength,
                            expected_mean_third_call,
                            expected_mean_squares_third_call);
}

TEST_F(MovingMomentsTest, VerifySampleBasedVsBlockBasedCalculation) {
  const float kInput[] = {0.2f,  0.3f,  0.5f,  0.7f, 0.11f,
                          0.13f, 0.17f, 0.19f, 0.23f};
  const size_t kInputLength = sizeof(kInput) / sizeof(kInput[0]);

  float output_mean_block_based[kInputLength];
  float output_mean_squares_block_based[kInputLength];

  float output_mean_sample_based;
  float output_mean_squares_sample_based;

  moving_moments_->CalculateMoments(kInput, kInputLength,
                                    output_mean_block_based,
                                    output_mean_squares_block_based);
  moving_moments_.reset(new MovingMoments(kMovingMomentsBufferLength));
  for (size_t i = 0; i < kInputLength; ++i) {
    moving_moments_->CalculateMoments(&kInput[i], 1, &output_mean_sample_based,
                                      &output_mean_squares_sample_based);
    EXPECT_FLOAT_EQ(output_mean_block_based[i], output_mean_sample_based);
    EXPECT_FLOAT_EQ(output_mean_squares_block_based[i],
                    output_mean_squares_sample_based);
  }
}

}  // namespace webrtc