1 /*
2 * Copyright (c) 2017 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 #include "modules/audio_processing/aec3/decimator.h"
11
12 #include "rtc_base/checks.h"
13
14 namespace webrtc {
15 namespace {
16
17 // b, a = signal.butter(2, 3400/8000.0, 'lowpass', analog=False) which are the
18 // same as b, a = signal.butter(2, 1700/4000.0, 'lowpass', analog=False).
19 const CascadedBiQuadFilter::BiQuadCoefficients kLowPassFilterCoefficients2 = {
20 {0.22711796f, 0.45423593f, 0.22711796f},
21 {-0.27666461f, 0.18513647f}};
22 constexpr int kNumFilters2 = 3;
23
24 // b, a = signal.butter(2, 1500/8000.0, 'lowpass', analog=False) which are the
25 // same as b, a = signal.butter(2, 75/4000.0, 'lowpass', analog=False).
26 const CascadedBiQuadFilter::BiQuadCoefficients kLowPassFilterCoefficients4 = {
27 {0.0179f, 0.0357f, 0.0179f},
28 {-1.5879f, 0.6594f}};
29 constexpr int kNumFilters4 = 3;
30
31 // b, a = signal.butter(2, 800/8000.0, 'lowpass', analog=False) which are the
32 // same as b, a = signal.butter(2, 400/4000.0, 'lowpass', analog=False).
33 const CascadedBiQuadFilter::BiQuadCoefficients kLowPassFilterCoefficients8 = {
34 {0.02008337f, 0.04016673f, 0.02008337f},
35 {-1.56101808f, 0.64135154f}};
36 constexpr int kNumFilters8 = 4;
37
38 } // namespace
39
Decimator(size_t down_sampling_factor)40 Decimator::Decimator(size_t down_sampling_factor)
41 : down_sampling_factor_(down_sampling_factor),
42 low_pass_filter_(
43 down_sampling_factor_ == 4
44 ? kLowPassFilterCoefficients4
45 : (down_sampling_factor_ == 8 ? kLowPassFilterCoefficients8
46 : kLowPassFilterCoefficients2),
47 down_sampling_factor_ == 4
48 ? kNumFilters4
49 : (down_sampling_factor_ == 8 ? kNumFilters8 : kNumFilters2)) {
50 RTC_DCHECK(down_sampling_factor_ == 2 || down_sampling_factor_ == 4 ||
51 down_sampling_factor_ == 8);
52 }
53
Decimate(rtc::ArrayView<const float> in,rtc::ArrayView<float> out)54 void Decimator::Decimate(rtc::ArrayView<const float> in,
55 rtc::ArrayView<float> out) {
56 RTC_DCHECK_EQ(kBlockSize, in.size());
57 RTC_DCHECK_EQ(kBlockSize / down_sampling_factor_, out.size());
58 std::array<float, kBlockSize> x;
59
60 // Limit the frequency content of the signal to avoid aliasing.
61 low_pass_filter_.Process(in, x);
62
63 // Downsample the signal.
64 for (size_t j = 0, k = 0; j < out.size(); ++j, k += down_sampling_factor_) {
65 RTC_DCHECK_GT(kBlockSize, k);
66 out[j] = x[k];
67 }
68 }
69
70 } // namespace webrtc
71