blob: 75aa33134f093e49ef9361eefe854be435be7ce8 [file] [log] [blame]
Per Åhgren38e2d952017-11-17 14:54:28 +01001/*
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
14namespace webrtc {
15namespace {
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).
19const CascadedBiQuadFilter::BiQuadCoefficients kLowPassFilterCoefficients2 = {
20 {0.22711796f, 0.45423593f, 0.22711796f},
21 {-0.27666461f, 0.18513647f}};
22constexpr 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).
26const CascadedBiQuadFilter::BiQuadCoefficients kLowPassFilterCoefficients4 = {
27 {0.0179f, 0.0357f, 0.0179f},
28 {-1.5879f, 0.6594f}};
29constexpr 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).
33const CascadedBiQuadFilter::BiQuadCoefficients kLowPassFilterCoefficients8 = {
34 {0.02008337f, 0.04016673f, 0.02008337f},
35 {-1.56101808f, 0.64135154f}};
36constexpr int kNumFilters8 = 4;
37
Gustaf Ullberg6bf5a0d2018-05-18 15:45:09 +020038// b, a = signal.butter(2, 1000/8000.0, 'highpass', analog=False)
39const CascadedBiQuadFilter::BiQuadCoefficients kHighPassFilterCoefficients4 = {
40 {0.75707638f, -1.51415275f, 0.75707638f},
41 {-1.45424359f, 0.57406192f}};
42constexpr int kNumFiltersHP4 = 1;
43
Per Åhgren38e2d952017-11-17 14:54:28 +010044} // namespace
45
46Decimator::Decimator(size_t down_sampling_factor)
47 : down_sampling_factor_(down_sampling_factor),
48 low_pass_filter_(
49 down_sampling_factor_ == 4
50 ? kLowPassFilterCoefficients4
51 : (down_sampling_factor_ == 8 ? kLowPassFilterCoefficients8
52 : kLowPassFilterCoefficients2),
53 down_sampling_factor_ == 4
54 ? kNumFilters4
Gustaf Ullberg6bf5a0d2018-05-18 15:45:09 +020055 : (down_sampling_factor_ == 8 ? kNumFilters8 : kNumFilters2)),
56 high_pass_filter_(kHighPassFilterCoefficients4, kNumFiltersHP4) {
Per Åhgren38e2d952017-11-17 14:54:28 +010057 RTC_DCHECK(down_sampling_factor_ == 2 || down_sampling_factor_ == 4 ||
58 down_sampling_factor_ == 8);
59}
60
61void Decimator::Decimate(rtc::ArrayView<const float> in,
62 rtc::ArrayView<float> out) {
63 RTC_DCHECK_EQ(kBlockSize, in.size());
64 RTC_DCHECK_EQ(kBlockSize / down_sampling_factor_, out.size());
65 std::array<float, kBlockSize> x;
66
67 // Limit the frequency content of the signal to avoid aliasing.
68 low_pass_filter_.Process(in, x);
69
Gustaf Ullberg6bf5a0d2018-05-18 15:45:09 +020070 // High-pass filter to reduce the impact of near-end noise.
71 if (down_sampling_factor_ == 4)
72 high_pass_filter_.Process(x, x);
73
Per Åhgren38e2d952017-11-17 14:54:28 +010074 // Downsample the signal.
75 for (size_t j = 0, k = 0; j < out.size(); ++j, k += down_sampling_factor_) {
76 RTC_DCHECK_GT(kBlockSize, k);
77 out[j] = x[k];
78 }
79}
80
81} // namespace webrtc