OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2016 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 <memory> |
| 12 |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "webrtc/modules/audio_coding/audio_network_adaptor/smoothing_filter.h" |
| 15 |
| 16 namespace webrtc { |
| 17 |
| 18 namespace { |
| 19 |
| 20 constexpr int kTimeConstantMs = 1000; |
| 21 constexpr float kMaxAbsError = 0.0001f; |
| 22 constexpr int64_t kClockInitialTime = 123456; |
| 23 |
| 24 struct SmoothingFilterStates { |
| 25 std::unique_ptr<SimulatedClock> simulated_clock; |
| 26 std::unique_ptr<SmoothingFilter> smoothing_filter; |
| 27 }; |
| 28 |
| 29 SmoothingFilterStates CreateSmoothingFilter() { |
| 30 SmoothingFilterStates states; |
| 31 states.simulated_clock.reset(new SimulatedClock(kClockInitialTime)); |
| 32 states.smoothing_filter.reset( |
| 33 new SmoothingFilterImpl(kTimeConstantMs, states.simulated_clock.get())); |
| 34 return states; |
| 35 } |
| 36 |
| 37 void CheckOutput(SmoothingFilterStates* states, |
| 38 int advance_time_ms, |
| 39 float sample, |
| 40 float expected_ouput) { |
| 41 states->simulated_clock->AdvanceTimeMilliseconds(advance_time_ms); |
| 42 states->smoothing_filter->AddSample(sample); |
| 43 auto output = states->smoothing_filter->GetAverage(); |
| 44 EXPECT_TRUE(output); |
| 45 EXPECT_NEAR(expected_ouput, *output, kMaxAbsError); |
| 46 } |
| 47 |
| 48 } // namespace |
| 49 |
| 50 TEST(SmoothingFilterTest, NoOutputWhenNoSampleAdded) { |
| 51 auto states = CreateSmoothingFilter(); |
| 52 EXPECT_FALSE(states.smoothing_filter->GetAverage()); |
| 53 } |
| 54 |
| 55 // Python script to calculate the reference values used in this test. |
| 56 // import math |
| 57 // |
| 58 // class ExpFilter: |
| 59 // alpha = 0.0 |
| 60 // old_value = 0.0 |
| 61 // def calc(self, new_value): |
| 62 // self.old_value = self.old_value * self.alpha |
| 63 // + (1.0 - self.alpha) * new_value |
| 64 // return self.old_value |
| 65 // |
| 66 // delta_t = 100.0 |
| 67 // filter = ExpFilter() |
| 68 // total_t = 100.0 |
| 69 // filter.alpha = math.exp(-delta_t/ total_t) |
| 70 // print filter.calc(1.0) |
| 71 // total_t = 200.0 |
| 72 // filter.alpha = math.exp(-delta_t/ total_t) |
| 73 // print filter.calc(0.0) |
| 74 // total_t = 300.0 |
| 75 // filter.alpha = math.exp(-delta_t/ total_t) |
| 76 // print filter.calc(1.0) |
| 77 TEST(SmoothingFilterTest, CheckBehaviorBeforeInitialized) { |
| 78 // Adding three samples, all added before |kTimeConstantMs| is reached. |
| 79 constexpr int kTimeIntervalMs = 100; |
| 80 auto states = CreateSmoothingFilter(); |
| 81 states.smoothing_filter->AddSample(0.0); |
| 82 CheckOutput(&states, kTimeIntervalMs, 1.0, 0.63212f); |
| 83 CheckOutput(&states, kTimeIntervalMs, 0.0, 0.38340f); |
| 84 CheckOutput(&states, kTimeIntervalMs, 1.0, 0.55818f); |
| 85 } |
| 86 |
| 87 // Python script to calculate the reference value used in this test. |
| 88 // (after defining ExpFilter as for CheckBehaviorBeforeInitialized) |
| 89 // time_constant_ms = 1000.0 |
| 90 // filter = ExpFilter() |
| 91 // delta_t = 1100.0 |
| 92 // filter.alpha = math.exp(-delta_t/ time_constant_ms) |
| 93 // print filter.calc(1.0) |
| 94 // delta_t = 100.0 |
| 95 // filter.alpha = math.exp(-delta_t/ time_constant_ms) |
| 96 // print filter.calc(0.0) |
| 97 // print filter.calc(1.0) |
| 98 TEST(SmoothingFilterTest, CheckBehaviorAfterInitialized) { |
| 99 constexpr int kTimeIntervalMs = 100; |
| 100 auto states = CreateSmoothingFilter(); |
| 101 states.smoothing_filter->AddSample(0.0); |
| 102 states.simulated_clock->AdvanceTimeMilliseconds(kTimeConstantMs); |
| 103 CheckOutput(&states, kTimeIntervalMs, 1.0, 0.66713f); |
| 104 CheckOutput(&states, kTimeIntervalMs, 0.0, 0.60364f); |
| 105 CheckOutput(&states, kTimeIntervalMs, 1.0, 0.64136f); |
| 106 } |
| 107 |
| 108 } // namespace webrtc |
OLD | NEW |