OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/common_audio/smoothing_filter.h" | |
12 | |
11 #include <cmath> | 13 #include <cmath> |
12 | 14 |
13 #include "webrtc/common_audio/smoothing_filter.h" | 15 #include "webrtc/base/logging.h" |
14 | 16 |
15 namespace webrtc { | 17 namespace webrtc { |
16 | 18 |
17 SmoothingFilterImpl::SmoothingFilterImpl(int time_constant_ms, | 19 SmoothingFilterImpl::SmoothingFilterImpl(int init_time_ms_, const Clock* clock) |
18 const Clock* clock) | 20 : init_time_ms_(init_time_ms_), |
19 : time_constant_ms_(time_constant_ms), | 21 // Duing the initalization time, we use an increasing alpha. Specifically, |
20 clock_(clock), | 22 // alpha(n) = exp(pow(init_factor_, n)), |
21 first_sample_received_(false), | 23 // where |init_factor_| is chosen such that |
22 initialized_(false), | 24 // alpha(init_time_ms_) = exp(-1.0f / init_time_ms_), |
23 first_sample_time_ms_(0), | 25 init_factor_(pow(init_time_ms_, 1.0f / init_time_ms_)), |
24 last_sample_time_ms_(0), | 26 init_const_(1.0f / (init_time_ms_ - |
25 filter_(0.0) {} | 27 pow(init_time_ms_, 1.0f - 1.0f / init_time_ms_))), |
28 clock_(clock) { | |
29 UpdateAlpha(init_time_ms_); | |
30 } | |
31 | |
32 SmoothingFilterImpl::~SmoothingFilterImpl() = default; | |
26 | 33 |
27 void SmoothingFilterImpl::AddSample(float sample) { | 34 void SmoothingFilterImpl::AddSample(float sample) { |
28 if (!first_sample_received_) { | 35 int64_t now_ms = clock_->TimeInMilliseconds(); |
29 last_sample_time_ms_ = first_sample_time_ms_ = clock_->TimeInMilliseconds(); | |
30 first_sample_received_ = true; | |
31 RTC_DCHECK_EQ(rtc::ExpFilter::kValueUndefined, filter_.filtered()); | |
32 | 36 |
33 // Since this is first sample, any value for argument 1 should work. | 37 if (!first_sample_time_ms_) { |
34 filter_.Apply(0.0f, sample); | 38 // This is equivalent to assuming the filter has been receiving the same |
39 // value as the first sample since time -infinity. | |
40 state_ = last_sample_ = sample; | |
41 first_sample_time_ms_ = rtc::Optional<int64_t>(now_ms); | |
42 last_state_time_ms_ = now_ms; | |
35 return; | 43 return; |
36 } | 44 } |
37 | 45 |
38 int64_t now_ms = clock_->TimeInMilliseconds(); | 46 ExtrapolateLastSample(now_ms); |
39 if (!initialized_) { | 47 last_sample_ = sample; |
40 float duration = now_ms - first_sample_time_ms_; | |
41 if (duration < static_cast<int64_t>(time_constant_ms_)) { | |
42 filter_.UpdateBase(exp(1.0f / duration)); | |
43 } else { | |
44 initialized_ = true; | |
45 filter_.UpdateBase(exp(1.0f / time_constant_ms_)); | |
46 } | |
47 } | |
48 | |
49 // The filter will do the following: | |
50 // float alpha = pow(base, last_update_time_ms_ - now_ms); | |
51 // filtered_ = alpha * filtered_ + (1 - alpha) * sample; | |
52 filter_.Apply(static_cast<float>(last_sample_time_ms_ - now_ms), sample); | |
53 last_sample_time_ms_ = now_ms; | |
54 } | 48 } |
55 | 49 |
56 rtc::Optional<float> SmoothingFilterImpl::GetAverage() const { | 50 rtc::Optional<float> SmoothingFilterImpl::GetAverage() { |
57 float value = filter_.filtered(); | 51 if (!first_sample_time_ms_) |
58 return value == rtc::ExpFilter::kValueUndefined ? rtc::Optional<float>() | 52 return rtc::Optional<float>(); |
59 : rtc::Optional<float>(value); | 53 ExtrapolateLastSample(clock_->TimeInMilliseconds()); |
54 return rtc::Optional<float>(state_); | |
60 } | 55 } |
61 | 56 |
62 void SmoothingFilterImpl::SetTimeConstantMs(int time_constant_ms) { | 57 void SmoothingFilterImpl::SetTimeConstantMs(int time_constant_ms) { |
63 time_constant_ms_ = time_constant_ms; | 58 if (!first_sample_time_ms_ || |
64 filter_.UpdateBase(exp(1.0f / time_constant_ms_)); | 59 last_state_time_ms_ < *first_sample_time_ms_ + init_time_ms_) { |
60 LOG(LS_INFO) << "SmoothingFilterImpl: Cannot set time constant " | |
61 << time_constant_ms << " ms during the initialization time."; | |
62 return; | |
63 } | |
64 UpdateAlpha(time_constant_ms); | |
65 } | |
66 | |
67 void SmoothingFilterImpl::UpdateAlpha(int time_constant_ms) { | |
68 alpha_ = exp(-1.0f / time_constant_ms); | |
69 } | |
70 | |
71 void SmoothingFilterImpl::ExtrapolateLastSample(int64_t time_ms) { | |
72 RTC_DCHECK_GE(time_ms, last_state_time_ms_); | |
73 RTC_DCHECK(first_sample_time_ms_); | |
74 | |
75 float multiplier = 0.0f; | |
76 if (time_ms <= *first_sample_time_ms_ + init_time_ms_) { | |
77 // Current update is to be made during initialization phase. | |
78 // We update the state as if the |alpha| has been increased according | |
79 // alpha(n) = exp(pow(init_factor_, n)), | |
80 // where n is the time (in millisecond) since the first sample received. | |
81 // With algebraic derivation, we can find that the state can be updated | |
michaelt
2016/12/12 09:05:30
Could you add the algebraic derivation ?
minyue-webrtc
2016/12/12 09:12:14
I will try. But pure text will be a bit cumbersome
| |
82 // in a similar manner as if alpha is a constant, except for a different | |
83 // multiplier. | |
84 multiplier = exp(-init_const_ * | |
85 (pow(init_factor_, | |
86 *first_sample_time_ms_ + init_time_ms_ - last_state_time_ms_) - | |
87 pow(init_factor_, *first_sample_time_ms_ + init_time_ms_ - time_ms))); | |
88 } else { | |
89 if (last_state_time_ms_ < *first_sample_time_ms_ + init_time_ms_) { | |
90 // The latest state update was made during initialization phase. | |
91 // We first extrapolate to the initialization time. | |
92 ExtrapolateLastSample(*first_sample_time_ms_ + init_time_ms_); | |
93 // Then extrapolate the rest by the following. | |
94 } | |
95 multiplier = pow(alpha_, time_ms - last_state_time_ms_); | |
96 } | |
97 | |
98 state_ = multiplier * state_ + (1.0f - multiplier) * last_sample_; | |
99 last_state_time_ms_ = time_ms; | |
65 } | 100 } |
66 | 101 |
67 } // namespace webrtc | 102 } // namespace webrtc |
OLD | NEW |