minyue | 3548357 | 2016-09-21 06:13:08 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #include "common_audio/smoothing_filter.h" |
minyue | 3548357 | 2016-09-21 06:13:08 | [diff] [blame] | 12 | |
minyue | 301fc4a | 2016-12-13 14:52:56 | [diff] [blame] | 13 | #include <cmath> |
| 14 | |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 15 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 16 | #include "rtc_base/time_utils.h" |
michaelt | 92aef17 | 2017-04-18 07:11:48 | [diff] [blame] | 17 | |
minyue | 3548357 | 2016-09-21 06:13:08 | [diff] [blame] | 18 | namespace webrtc { |
| 19 | |
michaelt | 92aef17 | 2017-04-18 07:11:48 | [diff] [blame] | 20 | SmoothingFilterImpl::SmoothingFilterImpl(int init_time_ms) |
minyue | 7667db4 | 2016-12-28 10:57:50 | [diff] [blame] | 21 | : init_time_ms_(init_time_ms), |
minyue | 301fc4a | 2016-12-13 14:52:56 | [diff] [blame] | 22 | // Duing the initalization time, we use an increasing alpha. Specifically, |
minyue | 7667db4 | 2016-12-28 10:57:50 | [diff] [blame] | 23 | // alpha(n) = exp(-powf(init_factor_, n)), |
minyue | 301fc4a | 2016-12-13 14:52:56 | [diff] [blame] | 24 | // where |init_factor_| is chosen such that |
| 25 | // alpha(init_time_ms_) = exp(-1.0f / init_time_ms_), |
michaelt | 92aef17 | 2017-04-18 07:11:48 | [diff] [blame] | 26 | init_factor_(init_time_ms_ == 0 |
| 27 | ? 0.0f |
| 28 | : powf(init_time_ms_, -1.0f / init_time_ms_)), |
minyue | 301fc4a | 2016-12-13 14:52:56 | [diff] [blame] | 29 | // |init_const_| is to a factor to help the calculation during |
| 30 | // initialization phase. |
minyue | 7667db4 | 2016-12-28 10:57:50 | [diff] [blame] | 31 | init_const_(init_time_ms_ == 0 |
| 32 | ? 0.0f |
| 33 | : init_time_ms_ - |
michaelt | 92aef17 | 2017-04-18 07:11:48 | [diff] [blame] | 34 | powf(init_time_ms_, 1.0f - 1.0f / init_time_ms_)) { |
minyue | 301fc4a | 2016-12-13 14:52:56 | [diff] [blame] | 35 | UpdateAlpha(init_time_ms_); |
| 36 | } |
| 37 | |
| 38 | SmoothingFilterImpl::~SmoothingFilterImpl() = default; |
minyue | 3548357 | 2016-09-21 06:13:08 | [diff] [blame] | 39 | |
| 40 | void SmoothingFilterImpl::AddSample(float sample) { |
michaelt | 92aef17 | 2017-04-18 07:11:48 | [diff] [blame] | 41 | const int64_t now_ms = rtc::TimeMillis(); |
minyue | 3548357 | 2016-09-21 06:13:08 | [diff] [blame] | 42 | |
minyue | 7667db4 | 2016-12-28 10:57:50 | [diff] [blame] | 43 | if (!init_end_time_ms_) { |
minyue | 301fc4a | 2016-12-13 14:52:56 | [diff] [blame] | 44 | // This is equivalent to assuming the filter has been receiving the same |
| 45 | // value as the first sample since time -infinity. |
| 46 | state_ = last_sample_ = sample; |
Oskar Sundbom | 5e1a749 | 2017-11-16 09:57:19 | [diff] [blame] | 47 | init_end_time_ms_ = now_ms + init_time_ms_; |
minyue | 301fc4a | 2016-12-13 14:52:56 | [diff] [blame] | 48 | last_state_time_ms_ = now_ms; |
minyue | 3548357 | 2016-09-21 06:13:08 | [diff] [blame] | 49 | return; |
| 50 | } |
| 51 | |
minyue | 301fc4a | 2016-12-13 14:52:56 | [diff] [blame] | 52 | ExtrapolateLastSample(now_ms); |
| 53 | last_sample_ = sample; |
| 54 | } |
| 55 | |
Danil Chapovalov | 196100e | 2018-06-21 08:17:24 | [diff] [blame] | 56 | absl::optional<float> SmoothingFilterImpl::GetAverage() { |
minyue | 7667db4 | 2016-12-28 10:57:50 | [diff] [blame] | 57 | if (!init_end_time_ms_) { |
| 58 | // |init_end_time_ms_| undefined since we have not received any sample. |
Danil Chapovalov | 196100e | 2018-06-21 08:17:24 | [diff] [blame] | 59 | return absl::nullopt; |
minyue | 7667db4 | 2016-12-28 10:57:50 | [diff] [blame] | 60 | } |
michaelt | 92aef17 | 2017-04-18 07:11:48 | [diff] [blame] | 61 | ExtrapolateLastSample(rtc::TimeMillis()); |
Oskar Sundbom | 5e1a749 | 2017-11-16 09:57:19 | [diff] [blame] | 62 | return state_; |
minyue | 301fc4a | 2016-12-13 14:52:56 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | bool SmoothingFilterImpl::SetTimeConstantMs(int time_constant_ms) { |
minyue | 7667db4 | 2016-12-28 10:57:50 | [diff] [blame] | 66 | if (!init_end_time_ms_ || last_state_time_ms_ < *init_end_time_ms_) { |
minyue | 301fc4a | 2016-12-13 14:52:56 | [diff] [blame] | 67 | return false; |
| 68 | } |
| 69 | UpdateAlpha(time_constant_ms); |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | void SmoothingFilterImpl::UpdateAlpha(int time_constant_ms) { |
minyue | 7667db4 | 2016-12-28 10:57:50 | [diff] [blame] | 74 | alpha_ = time_constant_ms == 0 ? 0.0f : exp(-1.0f / time_constant_ms); |
minyue | 301fc4a | 2016-12-13 14:52:56 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void SmoothingFilterImpl::ExtrapolateLastSample(int64_t time_ms) { |
| 78 | RTC_DCHECK_GE(time_ms, last_state_time_ms_); |
minyue | 7667db4 | 2016-12-28 10:57:50 | [diff] [blame] | 79 | RTC_DCHECK(init_end_time_ms_); |
minyue | 301fc4a | 2016-12-13 14:52:56 | [diff] [blame] | 80 | |
| 81 | float multiplier = 0.0f; |
minyue | 7667db4 | 2016-12-28 10:57:50 | [diff] [blame] | 82 | |
| 83 | if (time_ms <= *init_end_time_ms_) { |
minyue | 301fc4a | 2016-12-13 14:52:56 | [diff] [blame] | 84 | // Current update is to be made during initialization phase. |
| 85 | // We update the state as if the |alpha| has been increased according |
minyue | 7667db4 | 2016-12-28 10:57:50 | [diff] [blame] | 86 | // alpha(n) = exp(-powf(init_factor_, n)), |
minyue | 301fc4a | 2016-12-13 14:52:56 | [diff] [blame] | 87 | // where n is the time (in millisecond) since the first sample received. |
| 88 | // With algebraic derivation as shown in the Appendix, we can find that the |
| 89 | // state can be updated in a similar manner as if alpha is a constant, |
| 90 | // except for a different multiplier. |
minyue | 7667db4 | 2016-12-28 10:57:50 | [diff] [blame] | 91 | if (init_time_ms_ == 0) { |
| 92 | // This means |init_factor_| = 0. |
| 93 | multiplier = 0.0f; |
| 94 | } else if (init_time_ms_ == 1) { |
| 95 | // This means |init_factor_| = 1. |
| 96 | multiplier = exp(last_state_time_ms_ - time_ms); |
| 97 | } else { |
| 98 | multiplier = |
| 99 | exp(-(powf(init_factor_, last_state_time_ms_ - *init_end_time_ms_) - |
| 100 | powf(init_factor_, time_ms - *init_end_time_ms_)) / |
| 101 | init_const_); |
| 102 | } |
minyue | 301fc4a | 2016-12-13 14:52:56 | [diff] [blame] | 103 | } else { |
minyue | 7667db4 | 2016-12-28 10:57:50 | [diff] [blame] | 104 | if (last_state_time_ms_ < *init_end_time_ms_) { |
minyue | 301fc4a | 2016-12-13 14:52:56 | [diff] [blame] | 105 | // The latest state update was made during initialization phase. |
| 106 | // We first extrapolate to the initialization time. |
minyue | 7667db4 | 2016-12-28 10:57:50 | [diff] [blame] | 107 | ExtrapolateLastSample(*init_end_time_ms_); |
minyue | 301fc4a | 2016-12-13 14:52:56 | [diff] [blame] | 108 | // Then extrapolate the rest by the following. |
minyue | 3548357 | 2016-09-21 06:13:08 | [diff] [blame] | 109 | } |
minyue | 7667db4 | 2016-12-28 10:57:50 | [diff] [blame] | 110 | multiplier = powf(alpha_, time_ms - last_state_time_ms_); |
minyue | 3548357 | 2016-09-21 06:13:08 | [diff] [blame] | 111 | } |
| 112 | |
minyue | 301fc4a | 2016-12-13 14:52:56 | [diff] [blame] | 113 | state_ = multiplier * state_ + (1.0f - multiplier) * last_sample_; |
| 114 | last_state_time_ms_ = time_ms; |
michaelt | 2fedf9c | 2016-11-28 10:34:18 | [diff] [blame] | 115 | } |
| 116 | |
minyue | 3548357 | 2016-09-21 06:13:08 | [diff] [blame] | 117 | } // namespace webrtc |
minyue | 301fc4a | 2016-12-13 14:52:56 | [diff] [blame] | 118 | |
| 119 | // Appendix: derivation of extrapolation during initialization phase. |
| 120 | // (LaTeX syntax) |
| 121 | // Assuming |
| 122 | // \begin{align} |
| 123 | // y(n) &= \alpha_{n-1} y(n-1) + \left(1 - \alpha_{n-1}\right) x(m) \\* |
| 124 | // &= \left(\prod_{i=m}^{n-1} \alpha_i\right) y(m) + |
| 125 | // \left(1 - \prod_{i=m}^{n-1} \alpha_i \right) x(m) |
| 126 | // \end{align} |
minyue | 7667db4 | 2016-12-28 10:57:50 | [diff] [blame] | 127 | // Taking $\alpha_{n} = \exp(-\gamma^n)$, $\gamma$ denotes init\_factor\_, the |
minyue | 301fc4a | 2016-12-13 14:52:56 | [diff] [blame] | 128 | // multiplier becomes |
| 129 | // \begin{align} |
| 130 | // \prod_{i=m}^{n-1} \alpha_i |
minyue | 7667db4 | 2016-12-28 10:57:50 | [diff] [blame] | 131 | // &= \exp\left(-\sum_{i=m}^{n-1} \gamma^i \right) \\* |
| 132 | // &= \begin{cases} |
| 133 | // \exp\left(-\frac{\gamma^m - \gamma^n}{1 - \gamma} \right) |
| 134 | // & \gamma \neq 1 \\* |
| 135 | // m-n & \gamma = 1 |
| 136 | // \end{cases} |
minyue | 301fc4a | 2016-12-13 14:52:56 | [diff] [blame] | 137 | // \end{align} |
minyue | 7667db4 | 2016-12-28 10:57:50 | [diff] [blame] | 138 | // We know $\gamma = T^{-\frac{1}{T}}$, where $T$ denotes init\_time\_ms\_. Then |
minyue | 301fc4a | 2016-12-13 14:52:56 | [diff] [blame] | 139 | // $1 - \gamma$ approaches zero when $T$ increases. This can cause numerical |
minyue | 7667db4 | 2016-12-28 10:57:50 | [diff] [blame] | 140 | // difficulties. We multiply $T$ (if $T > 0$) to both numerator and denominator |
| 141 | // in the fraction. See. |
minyue | 301fc4a | 2016-12-13 14:52:56 | [diff] [blame] | 142 | // \begin{align} |
| 143 | // \frac{\gamma^m - \gamma^n}{1 - \gamma} |
| 144 | // &= \frac{T^\frac{T-m}{T} - T^\frac{T-n}{T}}{T - T^{1-\frac{1}{T}}} |
| 145 | // \end{align} |