blob: e5ce987786f72c8512e2ce00bb7f9650859b60f0 [file] [log] [blame]
minyue35483572016-09-21 06:13:081/*
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 04:47:3111#include "common_audio/smoothing_filter.h"
minyue35483572016-09-21 06:13:0812
minyue301fc4a2016-12-13 14:52:5613#include <cmath>
14
Yves Gerey988cc082018-10-23 10:03:0115#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 17:11:0016#include "rtc_base/time_utils.h"
michaelt92aef172017-04-18 07:11:4817
minyue35483572016-09-21 06:13:0818namespace webrtc {
19
michaelt92aef172017-04-18 07:11:4820SmoothingFilterImpl::SmoothingFilterImpl(int init_time_ms)
minyue7667db42016-12-28 10:57:5021 : init_time_ms_(init_time_ms),
minyue301fc4a2016-12-13 14:52:5622 // Duing the initalization time, we use an increasing alpha. Specifically,
minyue7667db42016-12-28 10:57:5023 // alpha(n) = exp(-powf(init_factor_, n)),
minyue301fc4a2016-12-13 14:52:5624 // where |init_factor_| is chosen such that
25 // alpha(init_time_ms_) = exp(-1.0f / init_time_ms_),
michaelt92aef172017-04-18 07:11:4826 init_factor_(init_time_ms_ == 0
27 ? 0.0f
28 : powf(init_time_ms_, -1.0f / init_time_ms_)),
minyue301fc4a2016-12-13 14:52:5629 // |init_const_| is to a factor to help the calculation during
30 // initialization phase.
minyue7667db42016-12-28 10:57:5031 init_const_(init_time_ms_ == 0
32 ? 0.0f
33 : init_time_ms_ -
michaelt92aef172017-04-18 07:11:4834 powf(init_time_ms_, 1.0f - 1.0f / init_time_ms_)) {
minyue301fc4a2016-12-13 14:52:5635 UpdateAlpha(init_time_ms_);
36}
37
38SmoothingFilterImpl::~SmoothingFilterImpl() = default;
minyue35483572016-09-21 06:13:0839
40void SmoothingFilterImpl::AddSample(float sample) {
michaelt92aef172017-04-18 07:11:4841 const int64_t now_ms = rtc::TimeMillis();
minyue35483572016-09-21 06:13:0842
minyue7667db42016-12-28 10:57:5043 if (!init_end_time_ms_) {
minyue301fc4a2016-12-13 14:52:5644 // 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 Sundbom5e1a7492017-11-16 09:57:1947 init_end_time_ms_ = now_ms + init_time_ms_;
minyue301fc4a2016-12-13 14:52:5648 last_state_time_ms_ = now_ms;
minyue35483572016-09-21 06:13:0849 return;
50 }
51
minyue301fc4a2016-12-13 14:52:5652 ExtrapolateLastSample(now_ms);
53 last_sample_ = sample;
54}
55
Danil Chapovalov196100e2018-06-21 08:17:2456absl::optional<float> SmoothingFilterImpl::GetAverage() {
minyue7667db42016-12-28 10:57:5057 if (!init_end_time_ms_) {
58 // |init_end_time_ms_| undefined since we have not received any sample.
Danil Chapovalov196100e2018-06-21 08:17:2459 return absl::nullopt;
minyue7667db42016-12-28 10:57:5060 }
michaelt92aef172017-04-18 07:11:4861 ExtrapolateLastSample(rtc::TimeMillis());
Oskar Sundbom5e1a7492017-11-16 09:57:1962 return state_;
minyue301fc4a2016-12-13 14:52:5663}
64
65bool SmoothingFilterImpl::SetTimeConstantMs(int time_constant_ms) {
minyue7667db42016-12-28 10:57:5066 if (!init_end_time_ms_ || last_state_time_ms_ < *init_end_time_ms_) {
minyue301fc4a2016-12-13 14:52:5667 return false;
68 }
69 UpdateAlpha(time_constant_ms);
70 return true;
71}
72
73void SmoothingFilterImpl::UpdateAlpha(int time_constant_ms) {
minyue7667db42016-12-28 10:57:5074 alpha_ = time_constant_ms == 0 ? 0.0f : exp(-1.0f / time_constant_ms);
minyue301fc4a2016-12-13 14:52:5675}
76
77void SmoothingFilterImpl::ExtrapolateLastSample(int64_t time_ms) {
78 RTC_DCHECK_GE(time_ms, last_state_time_ms_);
minyue7667db42016-12-28 10:57:5079 RTC_DCHECK(init_end_time_ms_);
minyue301fc4a2016-12-13 14:52:5680
81 float multiplier = 0.0f;
minyue7667db42016-12-28 10:57:5082
83 if (time_ms <= *init_end_time_ms_) {
minyue301fc4a2016-12-13 14:52:5684 // Current update is to be made during initialization phase.
85 // We update the state as if the |alpha| has been increased according
minyue7667db42016-12-28 10:57:5086 // alpha(n) = exp(-powf(init_factor_, n)),
minyue301fc4a2016-12-13 14:52:5687 // 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.
minyue7667db42016-12-28 10:57:5091 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 }
minyue301fc4a2016-12-13 14:52:56103 } else {
minyue7667db42016-12-28 10:57:50104 if (last_state_time_ms_ < *init_end_time_ms_) {
minyue301fc4a2016-12-13 14:52:56105 // The latest state update was made during initialization phase.
106 // We first extrapolate to the initialization time.
minyue7667db42016-12-28 10:57:50107 ExtrapolateLastSample(*init_end_time_ms_);
minyue301fc4a2016-12-13 14:52:56108 // Then extrapolate the rest by the following.
minyue35483572016-09-21 06:13:08109 }
minyue7667db42016-12-28 10:57:50110 multiplier = powf(alpha_, time_ms - last_state_time_ms_);
minyue35483572016-09-21 06:13:08111 }
112
minyue301fc4a2016-12-13 14:52:56113 state_ = multiplier * state_ + (1.0f - multiplier) * last_sample_;
114 last_state_time_ms_ = time_ms;
michaelt2fedf9c2016-11-28 10:34:18115}
116
minyue35483572016-09-21 06:13:08117} // namespace webrtc
minyue301fc4a2016-12-13 14:52:56118
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}
minyue7667db42016-12-28 10:57:50127// Taking $\alpha_{n} = \exp(-\gamma^n)$, $\gamma$ denotes init\_factor\_, the
minyue301fc4a2016-12-13 14:52:56128// multiplier becomes
129// \begin{align}
130// \prod_{i=m}^{n-1} \alpha_i
minyue7667db42016-12-28 10:57:50131// &= \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}
minyue301fc4a2016-12-13 14:52:56137// \end{align}
minyue7667db42016-12-28 10:57:50138// We know $\gamma = T^{-\frac{1}{T}}$, where $T$ denotes init\_time\_ms\_. Then
minyue301fc4a2016-12-13 14:52:56139// $1 - \gamma$ approaches zero when $T$ increases. This can cause numerical
minyue7667db42016-12-28 10:57:50140// difficulties. We multiply $T$ (if $T > 0$) to both numerator and denominator
141// in the fraction. See.
minyue301fc4a2016-12-13 14:52:56142// \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}