Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(30)

Side by Side Diff: webrtc/common_audio/smoothing_filter.h

Issue 2551363002: Update common_audio/smoothing_filter. (Closed)
Patch Set: adding derivation as comment. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #ifndef WEBRTC_COMMON_AUDIO_SMOOTHING_FILTER_H_ 11 #ifndef WEBRTC_COMMON_AUDIO_SMOOTHING_FILTER_H_
12 #define WEBRTC_COMMON_AUDIO_SMOOTHING_FILTER_H_ 12 #define WEBRTC_COMMON_AUDIO_SMOOTHING_FILTER_H_
13 13
14 #include "webrtc/base/analytics/exp_filter.h"
15 #include "webrtc/base/constructormagic.h" 14 #include "webrtc/base/constructormagic.h"
16 #include "webrtc/base/optional.h" 15 #include "webrtc/base/optional.h"
17 #include "webrtc/system_wrappers/include/clock.h" 16 #include "webrtc/system_wrappers/include/clock.h"
18 17
19 namespace webrtc { 18 namespace webrtc {
20 19
21 class SmoothingFilter { 20 class SmoothingFilter {
22 public: 21 public:
23 virtual ~SmoothingFilter() = default; 22 virtual ~SmoothingFilter() = default;
24 virtual void AddSample(float sample) = 0; 23 virtual void AddSample(float sample) = 0;
25 virtual rtc::Optional<float> GetAverage() const = 0; 24 virtual rtc::Optional<float> GetAverage() = 0;
26 virtual void SetTimeConstantMs(int time_constant_ms) = 0; 25 virtual void SetTimeConstantMs(int time_constant_ms) = 0;
27 }; 26 };
28 27
29 // SmoothingFilterImpl applies an exponential filter 28 // SmoothingFilterImpl applies an exponential filter
30 // alpha = exp(-sample_interval / time_constant); 29 // alpha = exp(-1.0 / time_constant_ms);
31 // y[t] = alpha * y[t-1] + (1 - alpha) * sample; 30 // y[t] = alpha * y[t-1] + (1 - alpha) * sample;
31 // This implies a sample rate of 1000 Hz, i.e., 1 sample / ms.
32 // But SmoothingFilterImpl allows sparse samples. All missing samples will be
33 // assumed to equal the last received sample.
32 class SmoothingFilterImpl final : public SmoothingFilter { 34 class SmoothingFilterImpl final : public SmoothingFilter {
33 public: 35 public:
34 // |time_constant_ms| is the time constant for the exponential filter. 36 // |init_time_ms| is initialization time. It defines a period starting from
35 SmoothingFilterImpl(int time_constant_ms, const Clock* clock); 37 // the arriving time of the first sample. During this period, the exponential
38 // filter uses a varying time constant so that a smaller time constant will be
39 // applied to the earlier samples. This is to allow the the filter to adapt to
40 // earlier samples quickly. After the initialization period, the time constant
41 // will be set to |init_time_ms| first and can be changed through
42 // |SetTimeConstantMs|.
43 SmoothingFilterImpl(int init_time_ms, const Clock* clock);
44 ~SmoothingFilterImpl() override;
36 45
37 void AddSample(float sample) override; 46 void AddSample(float sample) override;
38 rtc::Optional<float> GetAverage() const override; 47 rtc::Optional<float> GetAverage() override;
39 void SetTimeConstantMs(int time_constant_ms) override; 48 void SetTimeConstantMs(int time_constant_ms) override;
40 49
50 // Methods used for unittests.
51 float alpha() const { return alpha_; }
52
41 private: 53 private:
42 int time_constant_ms_; 54 void UpdateAlpha(int time_constant_ms);
55 void ExtrapolateLastSample(int64_t time_ms);
56
57 const int init_time_ms_;
58 const float init_factor_;
59 const float init_const_;
43 const Clock* const clock_; 60 const Clock* const clock_;
44 61
45 bool first_sample_received_; 62 rtc::Optional<int64_t> first_sample_time_ms_;
46 bool initialized_; 63 float last_sample_;
47 int64_t first_sample_time_ms_; 64 float alpha_;
48 int64_t last_sample_time_ms_; 65 float state_;
49 rtc::ExpFilter filter_; 66 int64_t last_state_time_ms_;
50 67
51 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(SmoothingFilterImpl); 68 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(SmoothingFilterImpl);
52 }; 69 };
53 70
54 } // namespace webrtc 71 } // namespace webrtc
55 72
56 #endif // WEBRTC_COMMON_AUDIO_SMOOTHING_FILTER_H_ 73 #endif // WEBRTC_COMMON_AUDIO_SMOOTHING_FILTER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698