blob: 6135db5bc281b28e62e9aeb31e2ee8615a77bad4 [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
Gustaf Ullberg78b1c4a2018-05-25 10:12:58 +020031// b, a = signal.cheby1(1, 6, [1000/8000, 2000/8000], btype='bandpass',
32// analog=False)
33const CascadedBiQuadFilter::BiQuadCoefficients kBandPassFilterCoefficients8 = {
34 {0.10330478f, 0.f, -0.10330478f},
35 {-1.520363f, 0.79339043f}};
36constexpr int kNumFilters8 = 5;
Per Åhgren38e2d952017-11-17 14:54:28 +010037
Gustaf Ullberg6bf5a0d2018-05-18 15:45:09 +020038// b, a = signal.butter(2, 1000/8000.0, 'highpass', analog=False)
Gustaf Ullberg78b1c4a2018-05-25 10:12:58 +020039const CascadedBiQuadFilter::BiQuadCoefficients kHighPassFilterCoefficients = {
Gustaf Ullberg6bf5a0d2018-05-18 15:45:09 +020040 {0.75707638f, -1.51415275f, 0.75707638f},
41 {-1.45424359f, 0.57406192f}};
Gustaf Ullberg78b1c4a2018-05-25 10:12:58 +020042constexpr int kNumFiltersHP2 = 1;
Gustaf Ullberg6bf5a0d2018-05-18 15:45:09 +020043constexpr int kNumFiltersHP4 = 1;
Gustaf Ullberg78b1c4a2018-05-25 10:12:58 +020044constexpr int kNumFiltersHP8 = 0;
Gustaf Ullberg6bf5a0d2018-05-18 15:45:09 +020045
Per Åhgren38e2d952017-11-17 14:54:28 +010046} // namespace
47
48Decimator::Decimator(size_t down_sampling_factor)
49 : down_sampling_factor_(down_sampling_factor),
Gustaf Ullberg78b1c4a2018-05-25 10:12:58 +020050 anti_aliasing_filter_(
Per Åhgren38e2d952017-11-17 14:54:28 +010051 down_sampling_factor_ == 4
52 ? kLowPassFilterCoefficients4
Gustaf Ullberg78b1c4a2018-05-25 10:12:58 +020053 : (down_sampling_factor_ == 8 ? kBandPassFilterCoefficients8
Per Åhgren38e2d952017-11-17 14:54:28 +010054 : kLowPassFilterCoefficients2),
55 down_sampling_factor_ == 4
56 ? kNumFilters4
Gustaf Ullberg6bf5a0d2018-05-18 15:45:09 +020057 : (down_sampling_factor_ == 8 ? kNumFilters8 : kNumFilters2)),
Gustaf Ullberg78b1c4a2018-05-25 10:12:58 +020058 noise_reduction_filter_(
59 kHighPassFilterCoefficients,
60 down_sampling_factor_ == 4
61 ? kNumFiltersHP4
62 : (down_sampling_factor_ == 8 ? kNumFiltersHP8
63 : kNumFiltersHP2)) {
Per Åhgren38e2d952017-11-17 14:54:28 +010064 RTC_DCHECK(down_sampling_factor_ == 2 || down_sampling_factor_ == 4 ||
65 down_sampling_factor_ == 8);
66}
67
68void Decimator::Decimate(rtc::ArrayView<const float> in,
69 rtc::ArrayView<float> out) {
70 RTC_DCHECK_EQ(kBlockSize, in.size());
71 RTC_DCHECK_EQ(kBlockSize / down_sampling_factor_, out.size());
72 std::array<float, kBlockSize> x;
73
74 // Limit the frequency content of the signal to avoid aliasing.
Gustaf Ullberg78b1c4a2018-05-25 10:12:58 +020075 anti_aliasing_filter_.Process(in, x);
Per Åhgren38e2d952017-11-17 14:54:28 +010076
Gustaf Ullberg78b1c4a2018-05-25 10:12:58 +020077 // Reduce the impact of near-end noise.
78 noise_reduction_filter_.Process(x);
Gustaf Ullberg6bf5a0d2018-05-18 15:45:09 +020079
Per Åhgren38e2d952017-11-17 14:54:28 +010080 // Downsample the signal.
81 for (size_t j = 0, k = 0; j < out.size(); ++j, k += down_sampling_factor_) {
82 RTC_DCHECK_GT(kBlockSize, k);
83 out[j] = x[k];
84 }
85}
86
87} // namespace webrtc