Chromium Code Reviews| 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(); |
|
hlundin-webrtc
2016/12/13 12:02:51
nit: const
minyue-webrtc
2016/12/13 12:25:25
Done.
| |
| 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) { |
|
hlundin-webrtc
2016/12/13 12:02:51
I think this method should return a bool, indicati
minyue-webrtc
2016/12/13 12:25:25
Done.
| |
| 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 as shown in the Appendix, we can find that the | |
| 82 // state can be updated in a similar manner as if alpha is a constant, | |
| 83 // except for a different 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 |
| 103 | |
| 104 // Appendix: derivation of extrapolation during initialization phase. | |
| 105 // (LaTeX syntax) | |
| 106 // Assuming | |
| 107 // \begin{align} | |
| 108 // y(n) &= \alpha_{n-1} y(n-1) + \left(1 - \alpha_{n-1}\right) x(m) \\ | |
| 109 // &= \left(\prod_{i=m}^{n-1} \alpha_i\right) y(m) + | |
| 110 // \left(1 - \prod_{i=m}^{n-1} \alpha_i \right) x(m) | |
| 111 // \end{align} | |
| 112 // Taking $\alpha_{n} = \exp{\gamma^n}$, $\gamma$ denotes |init_factor_|, the | |
|
hlundin-webrtc
2016/12/13 12:02:51
Won't you have to escape the underscore characters
minyue-webrtc
2016/12/13 12:25:25
I did not try to compile the text part. But yes, i
| |
| 113 // multiplier becomes | |
| 114 // \begin{align} | |
| 115 // \prod_{i=m}^{n-1} \alpha_i | |
| 116 // &= \exp\left(\prod_{i=m}^{n-1} \gamma^i \right) \\ | |
| 117 // &= \exp\left(\frac{\gamma^m - \gamma^n}{1 - \gamma} \right) | |
| 118 // \end{align} | |
| 119 // We know $\gamma = T^\frac{1}{T}$, where $T$ denotes |init_time_ms_|. Then | |
| 120 // $1 - \gamma$ approaches zero when $T$ increases. This can cause numerical | |
| 121 // difficulties. We multiply $T$ to both numerator and denominator in the | |
| 122 // fraction. See. | |
| 123 // \begin{align} | |
| 124 // \frac{\gamma^m - \gamma^n}{1 - \gamma} | |
| 125 // &= \frac{T^\frac{T-m}{T} - T^\frac{T-n}{T}}{T - T^{1-\frac{1}{T}}} | |
| 126 // \end{align} | |
| OLD | NEW |