pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_processing/transient/moving_moments.h" |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 12 | |
kwiberg | 85d8bb0 | 2016-02-16 20:39:36 -0800 | [diff] [blame] | 13 | #include <memory> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "test/gtest.h" |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 16 | |
| 17 | namespace webrtc { |
| 18 | |
| 19 | static const float kTolerance = 0.0001f; |
| 20 | |
| 21 | class MovingMomentsTest : public ::testing::Test { |
| 22 | protected: |
| 23 | static const size_t kMovingMomentsBufferLength = 5; |
| 24 | static const size_t kMaxOutputLength = 20; // Valid for this tests only. |
| 25 | |
| 26 | virtual void SetUp(); |
| 27 | // Calls CalculateMoments and verifies that it produces the expected |
| 28 | // outputs. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 29 | void CalculateMomentsAndVerify(const float* input, |
| 30 | size_t input_length, |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 31 | const float* expected_mean, |
| 32 | const float* expected_mean_squares); |
| 33 | |
kwiberg | 85d8bb0 | 2016-02-16 20:39:36 -0800 | [diff] [blame] | 34 | std::unique_ptr<MovingMoments> moving_moments_; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 35 | float output_mean_[kMaxOutputLength]; |
| 36 | float output_mean_squares_[kMaxOutputLength]; |
| 37 | }; |
| 38 | |
| 39 | const size_t MovingMomentsTest::kMaxOutputLength; |
| 40 | |
| 41 | void MovingMomentsTest::SetUp() { |
| 42 | moving_moments_.reset(new MovingMoments(kMovingMomentsBufferLength)); |
| 43 | } |
| 44 | |
| 45 | void MovingMomentsTest::CalculateMomentsAndVerify( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 46 | const float* input, |
| 47 | size_t input_length, |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 48 | const float* expected_mean, |
| 49 | const float* expected_mean_squares) { |
| 50 | ASSERT_LE(input_length, kMaxOutputLength); |
| 51 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 52 | moving_moments_->CalculateMoments(input, input_length, output_mean_, |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 53 | output_mean_squares_); |
| 54 | |
| 55 | for (size_t i = 1; i < input_length; ++i) { |
| 56 | EXPECT_NEAR(expected_mean[i], output_mean_[i], kTolerance); |
| 57 | EXPECT_NEAR(expected_mean_squares[i], output_mean_squares_[i], kTolerance); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | TEST_F(MovingMomentsTest, CorrectMomentsOfAnAllZerosBuffer) { |
| 62 | const float kInput[] = {0.f, 0.f, 0.f, 0.f, 0.f}; |
| 63 | const size_t kInputLength = sizeof(kInput) / sizeof(kInput[0]); |
| 64 | |
| 65 | const float expected_mean[kInputLength] = {0.f, 0.f, 0.f, 0.f, 0.f}; |
| 66 | const float expected_mean_squares[kInputLength] = {0.f, 0.f, 0.f, 0.f, 0.f}; |
| 67 | |
| 68 | CalculateMomentsAndVerify(kInput, kInputLength, expected_mean, |
| 69 | expected_mean_squares); |
| 70 | } |
| 71 | |
| 72 | TEST_F(MovingMomentsTest, CorrectMomentsOfAConstantBuffer) { |
| 73 | const float kInput[] = {5.f, 5.f, 5.f, 5.f, 5.f, 5.f, 5.f, 5.f, 5.f, 5.f}; |
| 74 | const size_t kInputLength = sizeof(kInput) / sizeof(kInput[0]); |
| 75 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 76 | const float expected_mean[kInputLength] = {1.f, 2.f, 3.f, 4.f, 5.f, |
| 77 | 5.f, 5.f, 5.f, 5.f, 5.f}; |
| 78 | const float expected_mean_squares[kInputLength] = { |
| 79 | 5.f, 10.f, 15.f, 20.f, 25.f, 25.f, 25.f, 25.f, 25.f, 25.f}; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 80 | |
| 81 | CalculateMomentsAndVerify(kInput, kInputLength, expected_mean, |
| 82 | expected_mean_squares); |
| 83 | } |
| 84 | |
| 85 | TEST_F(MovingMomentsTest, CorrectMomentsOfAnIncreasingBuffer) { |
| 86 | const float kInput[] = {1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f, 9.f}; |
| 87 | const size_t kInputLength = sizeof(kInput) / sizeof(kInput[0]); |
| 88 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 89 | const float expected_mean[kInputLength] = {0.2f, 0.6f, 1.2f, 2.f, 3.f, |
| 90 | 4.f, 5.f, 6.f, 7.f}; |
| 91 | const float expected_mean_squares[kInputLength] = { |
| 92 | 0.2f, 1.f, 2.8f, 6.f, 11.f, 18.f, 27.f, 38.f, 51.f}; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 93 | |
| 94 | CalculateMomentsAndVerify(kInput, kInputLength, expected_mean, |
| 95 | expected_mean_squares); |
| 96 | } |
| 97 | |
| 98 | TEST_F(MovingMomentsTest, CorrectMomentsOfADecreasingBuffer) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 99 | const float kInput[] = {-1.f, -2.f, -3.f, -4.f, -5.f, -6.f, -7.f, -8.f, -9.f}; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 100 | const size_t kInputLength = sizeof(kInput) / sizeof(kInput[0]); |
| 101 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 102 | const float expected_mean[kInputLength] = {-0.2f, -0.6f, -1.2f, -2.f, -3.f, |
| 103 | -4.f, -5.f, -6.f, -7.f}; |
| 104 | const float expected_mean_squares[kInputLength] = { |
| 105 | 0.2f, 1.f, 2.8f, 6.f, 11.f, 18.f, 27.f, 38.f, 51.f}; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 106 | |
| 107 | CalculateMomentsAndVerify(kInput, kInputLength, expected_mean, |
| 108 | expected_mean_squares); |
| 109 | } |
| 110 | |
| 111 | TEST_F(MovingMomentsTest, CorrectMomentsOfAZeroMeanSequence) { |
| 112 | const size_t kMovingMomentsBufferLength = 4; |
| 113 | moving_moments_.reset(new MovingMoments(kMovingMomentsBufferLength)); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 114 | const float kInput[] = {1.f, -1.f, 1.f, -1.f, 1.f, |
| 115 | -1.f, 1.f, -1.f, 1.f, -1.f}; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 116 | const size_t kInputLength = sizeof(kInput) / sizeof(kInput[0]); |
| 117 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 118 | const float expected_mean[kInputLength] = {0.25f, 0.f, 0.25f, 0.f, 0.f, |
| 119 | 0.f, 0.f, 0.f, 0.f, 0.f}; |
| 120 | const float expected_mean_squares[kInputLength] = { |
| 121 | 0.25f, 0.5f, 0.75f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f}; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 122 | |
| 123 | CalculateMomentsAndVerify(kInput, kInputLength, expected_mean, |
| 124 | expected_mean_squares); |
| 125 | } |
| 126 | |
| 127 | TEST_F(MovingMomentsTest, CorrectMomentsOfAnArbitraryBuffer) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 128 | const float kInput[] = {0.2f, 0.3f, 0.5f, 0.7f, 0.11f, |
| 129 | 0.13f, 0.17f, 0.19f, 0.23f}; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 130 | const size_t kInputLength = sizeof(kInput) / sizeof(kInput[0]); |
| 131 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 132 | const float expected_mean[kInputLength] = { |
| 133 | 0.04f, 0.1f, 0.2f, 0.34f, 0.362f, 0.348f, 0.322f, 0.26f, 0.166f}; |
| 134 | const float expected_mean_squares[kInputLength] = {0.008f, 0.026f, 0.076f, |
| 135 | 0.174f, 0.1764f, 0.1718f, |
| 136 | 0.1596f, 0.1168f, 0.0294f}; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 137 | |
| 138 | CalculateMomentsAndVerify(kInput, kInputLength, expected_mean, |
| 139 | expected_mean_squares); |
| 140 | } |
| 141 | |
| 142 | TEST_F(MovingMomentsTest, MutipleCalculateMomentsCalls) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 143 | const float kInputFirstCall[] = {0.2f, 0.3f, 0.5f, 0.7f, 0.11f, |
| 144 | 0.13f, 0.17f, 0.19f, 0.23f}; |
| 145 | const size_t kInputFirstCallLength = |
| 146 | sizeof(kInputFirstCall) / sizeof(kInputFirstCall[0]); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 147 | const float kInputSecondCall[] = {0.29f, 0.31f}; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 148 | const size_t kInputSecondCallLength = |
| 149 | sizeof(kInputSecondCall) / sizeof(kInputSecondCall[0]); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 150 | const float kInputThirdCall[] = {0.37f, 0.41f, 0.43f, 0.47f}; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 151 | const size_t kInputThirdCallLength = |
| 152 | sizeof(kInputThirdCall) / sizeof(kInputThirdCall[0]); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 153 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 154 | const float expected_mean_first_call[kInputFirstCallLength] = { |
| 155 | 0.04f, 0.1f, 0.2f, 0.34f, 0.362f, 0.348f, 0.322f, 0.26f, 0.166f}; |
| 156 | const float expected_mean_squares_first_call[kInputFirstCallLength] = { |
| 157 | 0.008f, 0.026f, 0.076f, 0.174f, 0.1764f, |
| 158 | 0.1718f, 0.1596f, 0.1168f, 0.0294f}; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 159 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 160 | const float expected_mean_second_call[kInputSecondCallLength] = {0.202f, |
| 161 | 0.238f}; |
| 162 | const float expected_mean_squares_second_call[kInputSecondCallLength] = { |
| 163 | 0.0438f, 0.0596f}; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 164 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 165 | const float expected_mean_third_call[kInputThirdCallLength] = { |
| 166 | 0.278f, 0.322f, 0.362f, 0.398f}; |
| 167 | const float expected_mean_squares_third_call[kInputThirdCallLength] = { |
| 168 | 0.0812f, 0.1076f, 0.134f, 0.1614f}; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 169 | |
| 170 | CalculateMomentsAndVerify(kInputFirstCall, kInputFirstCallLength, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 171 | expected_mean_first_call, |
| 172 | expected_mean_squares_first_call); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 173 | |
| 174 | CalculateMomentsAndVerify(kInputSecondCall, kInputSecondCallLength, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 175 | expected_mean_second_call, |
| 176 | expected_mean_squares_second_call); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 177 | |
| 178 | CalculateMomentsAndVerify(kInputThirdCall, kInputThirdCallLength, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 179 | expected_mean_third_call, |
| 180 | expected_mean_squares_third_call); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 183 | TEST_F(MovingMomentsTest, VerifySampleBasedVsBlockBasedCalculation) { |
| 184 | const float kInput[] = {0.2f, 0.3f, 0.5f, 0.7f, 0.11f, |
| 185 | 0.13f, 0.17f, 0.19f, 0.23f}; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 186 | const size_t kInputLength = sizeof(kInput) / sizeof(kInput[0]); |
| 187 | |
| 188 | float output_mean_block_based[kInputLength]; |
| 189 | float output_mean_squares_block_based[kInputLength]; |
| 190 | |
| 191 | float output_mean_sample_based; |
| 192 | float output_mean_squares_sample_based; |
| 193 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 194 | moving_moments_->CalculateMoments(kInput, kInputLength, |
| 195 | output_mean_block_based, |
| 196 | output_mean_squares_block_based); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 197 | moving_moments_.reset(new MovingMoments(kMovingMomentsBufferLength)); |
| 198 | for (size_t i = 0; i < kInputLength; ++i) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 199 | moving_moments_->CalculateMoments(&kInput[i], 1, &output_mean_sample_based, |
| 200 | &output_mean_squares_sample_based); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 201 | EXPECT_FLOAT_EQ(output_mean_block_based[i], output_mean_sample_based); |
| 202 | EXPECT_FLOAT_EQ(output_mean_squares_block_based[i], |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 203 | output_mean_squares_sample_based); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
| 207 | } // namespace webrtc |