blob: ecfb5c252b09b0554ed676a438aeedd2508f09b4 [file] [log] [blame]
minyue35483572016-09-20 23:13:08 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "common_audio/smoothing_filter.h"
minyue35483572016-09-20 23:13:08 -070012
minyue301fc4a2016-12-13 06:52:56 -080013#include <cmath>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "rtc_base/timeutils.h"
michaelt92aef172017-04-18 00:11:48 -070016
minyue35483572016-09-20 23:13:08 -070017namespace webrtc {
18
michaelt92aef172017-04-18 00:11:48 -070019SmoothingFilterImpl::SmoothingFilterImpl(int init_time_ms)
minyue7667db42016-12-28 02:57:50 -080020 : init_time_ms_(init_time_ms),
minyue301fc4a2016-12-13 06:52:56 -080021 // Duing the initalization time, we use an increasing alpha. Specifically,
minyue7667db42016-12-28 02:57:50 -080022 // alpha(n) = exp(-powf(init_factor_, n)),
minyue301fc4a2016-12-13 06:52:56 -080023 // where |init_factor_| is chosen such that
24 // alpha(init_time_ms_) = exp(-1.0f / init_time_ms_),
michaelt92aef172017-04-18 00:11:48 -070025 init_factor_(init_time_ms_ == 0
26 ? 0.0f
27 : powf(init_time_ms_, -1.0f / init_time_ms_)),
minyue301fc4a2016-12-13 06:52:56 -080028 // |init_const_| is to a factor to help the calculation during
29 // initialization phase.
minyue7667db42016-12-28 02:57:50 -080030 init_const_(init_time_ms_ == 0
31 ? 0.0f
32 : init_time_ms_ -
michaelt92aef172017-04-18 00:11:48 -070033 powf(init_time_ms_, 1.0f - 1.0f / init_time_ms_)) {
minyue301fc4a2016-12-13 06:52:56 -080034 UpdateAlpha(init_time_ms_);
35}
36
37SmoothingFilterImpl::~SmoothingFilterImpl() = default;
minyue35483572016-09-20 23:13:08 -070038
39void SmoothingFilterImpl::AddSample(float sample) {
michaelt92aef172017-04-18 00:11:48 -070040 const int64_t now_ms = rtc::TimeMillis();
minyue35483572016-09-20 23:13:08 -070041
minyue7667db42016-12-28 02:57:50 -080042 if (!init_end_time_ms_) {
minyue301fc4a2016-12-13 06:52:56 -080043 // This is equivalent to assuming the filter has been receiving the same
44 // value as the first sample since time -infinity.
45 state_ = last_sample_ = sample;
Oskar Sundbom5e1a7492017-11-16 10:57:19 +010046 init_end_time_ms_ = now_ms + init_time_ms_;
minyue301fc4a2016-12-13 06:52:56 -080047 last_state_time_ms_ = now_ms;
minyue35483572016-09-20 23:13:08 -070048 return;
49 }
50
minyue301fc4a2016-12-13 06:52:56 -080051 ExtrapolateLastSample(now_ms);
52 last_sample_ = sample;
53}
54
55rtc::Optional<float> SmoothingFilterImpl::GetAverage() {
minyue7667db42016-12-28 02:57:50 -080056 if (!init_end_time_ms_) {
57 // |init_end_time_ms_| undefined since we have not received any sample.
Oskar Sundbom5e1a7492017-11-16 10:57:19 +010058 return rtc::nullopt;
minyue7667db42016-12-28 02:57:50 -080059 }
michaelt92aef172017-04-18 00:11:48 -070060 ExtrapolateLastSample(rtc::TimeMillis());
Oskar Sundbom5e1a7492017-11-16 10:57:19 +010061 return state_;
minyue301fc4a2016-12-13 06:52:56 -080062}
63
64bool SmoothingFilterImpl::SetTimeConstantMs(int time_constant_ms) {
minyue7667db42016-12-28 02:57:50 -080065 if (!init_end_time_ms_ || last_state_time_ms_ < *init_end_time_ms_) {
minyue301fc4a2016-12-13 06:52:56 -080066 return false;
67 }
68 UpdateAlpha(time_constant_ms);
69 return true;
70}
71
72void SmoothingFilterImpl::UpdateAlpha(int time_constant_ms) {
minyue7667db42016-12-28 02:57:50 -080073 alpha_ = time_constant_ms == 0 ? 0.0f : exp(-1.0f / time_constant_ms);
minyue301fc4a2016-12-13 06:52:56 -080074}
75
76void SmoothingFilterImpl::ExtrapolateLastSample(int64_t time_ms) {
77 RTC_DCHECK_GE(time_ms, last_state_time_ms_);
minyue7667db42016-12-28 02:57:50 -080078 RTC_DCHECK(init_end_time_ms_);
minyue301fc4a2016-12-13 06:52:56 -080079
80 float multiplier = 0.0f;
minyue7667db42016-12-28 02:57:50 -080081
82 if (time_ms <= *init_end_time_ms_) {
minyue301fc4a2016-12-13 06:52:56 -080083 // Current update is to be made during initialization phase.
84 // We update the state as if the |alpha| has been increased according
minyue7667db42016-12-28 02:57:50 -080085 // alpha(n) = exp(-powf(init_factor_, n)),
minyue301fc4a2016-12-13 06:52:56 -080086 // where n is the time (in millisecond) since the first sample received.
87 // With algebraic derivation as shown in the Appendix, we can find that the
88 // state can be updated in a similar manner as if alpha is a constant,
89 // except for a different multiplier.
minyue7667db42016-12-28 02:57:50 -080090 if (init_time_ms_ == 0) {
91 // This means |init_factor_| = 0.
92 multiplier = 0.0f;
93 } else if (init_time_ms_ == 1) {
94 // This means |init_factor_| = 1.
95 multiplier = exp(last_state_time_ms_ - time_ms);
96 } else {
97 multiplier =
98 exp(-(powf(init_factor_, last_state_time_ms_ - *init_end_time_ms_) -
99 powf(init_factor_, time_ms - *init_end_time_ms_)) /
100 init_const_);
101 }
minyue301fc4a2016-12-13 06:52:56 -0800102 } else {
minyue7667db42016-12-28 02:57:50 -0800103 if (last_state_time_ms_ < *init_end_time_ms_) {
minyue301fc4a2016-12-13 06:52:56 -0800104 // The latest state update was made during initialization phase.
105 // We first extrapolate to the initialization time.
minyue7667db42016-12-28 02:57:50 -0800106 ExtrapolateLastSample(*init_end_time_ms_);
minyue301fc4a2016-12-13 06:52:56 -0800107 // Then extrapolate the rest by the following.
minyue35483572016-09-20 23:13:08 -0700108 }
minyue7667db42016-12-28 02:57:50 -0800109 multiplier = powf(alpha_, time_ms - last_state_time_ms_);
minyue35483572016-09-20 23:13:08 -0700110 }
111
minyue301fc4a2016-12-13 06:52:56 -0800112 state_ = multiplier * state_ + (1.0f - multiplier) * last_sample_;
113 last_state_time_ms_ = time_ms;
michaelt2fedf9c2016-11-28 02:34:18 -0800114}
115
minyue35483572016-09-20 23:13:08 -0700116} // namespace webrtc
minyue301fc4a2016-12-13 06:52:56 -0800117
118// Appendix: derivation of extrapolation during initialization phase.
119// (LaTeX syntax)
120// Assuming
121// \begin{align}
122// y(n) &= \alpha_{n-1} y(n-1) + \left(1 - \alpha_{n-1}\right) x(m) \\*
123// &= \left(\prod_{i=m}^{n-1} \alpha_i\right) y(m) +
124// \left(1 - \prod_{i=m}^{n-1} \alpha_i \right) x(m)
125// \end{align}
minyue7667db42016-12-28 02:57:50 -0800126// Taking $\alpha_{n} = \exp(-\gamma^n)$, $\gamma$ denotes init\_factor\_, the
minyue301fc4a2016-12-13 06:52:56 -0800127// multiplier becomes
128// \begin{align}
129// \prod_{i=m}^{n-1} \alpha_i
minyue7667db42016-12-28 02:57:50 -0800130// &= \exp\left(-\sum_{i=m}^{n-1} \gamma^i \right) \\*
131// &= \begin{cases}
132// \exp\left(-\frac{\gamma^m - \gamma^n}{1 - \gamma} \right)
133// & \gamma \neq 1 \\*
134// m-n & \gamma = 1
135// \end{cases}
minyue301fc4a2016-12-13 06:52:56 -0800136// \end{align}
minyue7667db42016-12-28 02:57:50 -0800137// We know $\gamma = T^{-\frac{1}{T}}$, where $T$ denotes init\_time\_ms\_. Then
minyue301fc4a2016-12-13 06:52:56 -0800138// $1 - \gamma$ approaches zero when $T$ increases. This can cause numerical
minyue7667db42016-12-28 02:57:50 -0800139// difficulties. We multiply $T$ (if $T > 0$) to both numerator and denominator
140// in the fraction. See.
minyue301fc4a2016-12-13 06:52:56 -0800141// \begin{align}
142// \frac{\gamma^m - \gamma^n}{1 - \gamma}
143// &= \frac{T^\frac{T-m}{T} - T^\frac{T-n}{T}}{T - T^{1-\frac{1}{T}}}
144// \end{align}