/* * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. An additional intellectual property rights grant can be found * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree. */ #include "common_audio/smoothing_filter.h" #include <math.h> #include <cmath> #include "rtc_base/checks.h" #include "rtc_base/time_utils.h" namespace webrtc { SmoothingFilterImpl::SmoothingFilterImpl(int init_time_ms) : … { … } SmoothingFilterImpl::~SmoothingFilterImpl() = default; void SmoothingFilterImpl::AddSample(float sample) { … } absl::optional<float> SmoothingFilterImpl::GetAverage() { … } bool SmoothingFilterImpl::SetTimeConstantMs(int time_constant_ms) { … } void SmoothingFilterImpl::UpdateAlpha(int time_constant_ms) { … } void SmoothingFilterImpl::ExtrapolateLastSample(int64_t time_ms) { … } } // namespace webrtc // Appendix: derivation of extrapolation during initialization phase. // (LaTeX syntax) // Assuming // \begin{align} // y(n) &= \alpha_{n-1} y(n-1) + \left(1 - \alpha_{n-1}\right) x(m) \\* // &= \left(\prod_{i=m}^{n-1} \alpha_i\right) y(m) + // \left(1 - \prod_{i=m}^{n-1} \alpha_i \right) x(m) // \end{align} // Taking $\alpha_{n} = \exp(-\gamma^n)$, $\gamma$ denotes init\_factor\_, the // multiplier becomes // \begin{align} // \prod_{i=m}^{n-1} \alpha_i // &= \exp\left(-\sum_{i=m}^{n-1} \gamma^i \right) \\* // &= \begin{cases} // \exp\left(-\frac{\gamma^m - \gamma^n}{1 - \gamma} \right) // & \gamma \neq 1 \\* // m-n & \gamma = 1 // \end{cases} // \end{align} // We know $\gamma = T^{-\frac{1}{T}}$, where $T$ denotes init\_time\_ms\_. Then // $1 - \gamma$ approaches zero when $T$ increases. This can cause numerical // difficulties. We multiply $T$ (if $T > 0$) to both numerator and denominator // in the fraction. See. // \begin{align} // \frac{\gamma^m - \gamma^n}{1 - \gamma} // &= \frac{T^\frac{T-m}{T} - T^\frac{T-n}{T}}{T - T^{1-\frac{1}{T}}} // \end{align}