blob: d9faa62dc446976a13d3ea06f0b3294ddf3acd67 [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
Gustaf Ullberg34c9f122018-06-07 07:55:01 +020017// signal.butter(2, 3400/8000.0, 'lowpass', analog=False)
Gustaf Ullberged51a6e2018-06-07 19:18:35 +020018const std::vector<CascadedBiQuadFilter::BiQuadParam> GetLowPassFilterDS2() {
19 return std::vector<CascadedBiQuadFilter::BiQuadParam>{
20 {{-1.f, 0.f}, {0.13833231f, 0.40743176f}, 0.22711796393486466f},
21 {{-1.f, 0.f}, {0.13833231f, 0.40743176f}, 0.22711796393486466f},
22 {{-1.f, 0.f}, {0.13833231f, 0.40743176f}, 0.22711796393486466f}};
23}
Per Åhgren38e2d952017-11-17 14:54:28 +010024
Gustaf Ullbergf469b632018-06-07 11:25:21 +020025// signal.ellip(6, 1, 40, 1800/8000, btype='lowpass', analog=False)
Gustaf Ullberged51a6e2018-06-07 19:18:35 +020026const std::vector<CascadedBiQuadFilter::BiQuadParam> GetLowPassFilterDS4() {
27 return std::vector<CascadedBiQuadFilter::BiQuadParam>{
28 {{-0.08873842f, 0.99605496f}, {0.75916227f, 0.23841065f}, 0.26250696827f},
29 {{0.62273832f, 0.78243018f}, {0.74892112f, 0.5410152f}, 0.26250696827f},
30 {{0.71107693f, 0.70311421f}, {0.74895534f, 0.63924616f}, 0.26250696827f}};
31}
Per Åhgren38e2d952017-11-17 14:54:28 +010032
Gustaf Ullberg34c9f122018-06-07 07:55:01 +020033// signal.cheby1(1, 6, [1000/8000, 2000/8000], btype='bandpass', analog=False)
Gustaf Ullberged51a6e2018-06-07 19:18:35 +020034const std::vector<CascadedBiQuadFilter::BiQuadParam> GetBandPassFilterDS8() {
35 return std::vector<CascadedBiQuadFilter::BiQuadParam>{
36 {{1.f, 0.f}, {0.7601815f, 0.46423542f}, 0.10330478266505948f, true},
37 {{1.f, 0.f}, {0.7601815f, 0.46423542f}, 0.10330478266505948f, true},
38 {{1.f, 0.f}, {0.7601815f, 0.46423542f}, 0.10330478266505948f, true},
39 {{1.f, 0.f}, {0.7601815f, 0.46423542f}, 0.10330478266505948f, true},
40 {{1.f, 0.f}, {0.7601815f, 0.46423542f}, 0.10330478266505948f, true}};
41}
Per Åhgren38e2d952017-11-17 14:54:28 +010042
Gustaf Ullberg34c9f122018-06-07 07:55:01 +020043// signal.butter(2, 1000/8000.0, 'highpass', analog=False)
Gustaf Ullberged51a6e2018-06-07 19:18:35 +020044const std::vector<CascadedBiQuadFilter::BiQuadParam> GetHighPassFilter() {
45 return std::vector<CascadedBiQuadFilter::BiQuadParam>{
46 {{1.f, 0.f}, {0.72712179f, 0.21296904f}, 0.7570763753338849f}};
47}
Gustaf Ullberg6bf5a0d2018-05-18 15:45:09 +020048
Gustaf Ullberged51a6e2018-06-07 19:18:35 +020049const std::vector<CascadedBiQuadFilter::BiQuadParam> GetPassThroughFilter() {
50 return std::vector<CascadedBiQuadFilter::BiQuadParam>{};
51}
Per Åhgren38e2d952017-11-17 14:54:28 +010052} // namespace
53
54Decimator::Decimator(size_t down_sampling_factor)
55 : down_sampling_factor_(down_sampling_factor),
Gustaf Ullberg34c9f122018-06-07 07:55:01 +020056 anti_aliasing_filter_(down_sampling_factor_ == 4
Gustaf Ullberged51a6e2018-06-07 19:18:35 +020057 ? GetLowPassFilterDS4()
Gustaf Ullberg34c9f122018-06-07 07:55:01 +020058 : (down_sampling_factor_ == 8
Gustaf Ullberged51a6e2018-06-07 19:18:35 +020059 ? GetBandPassFilterDS8()
60 : GetLowPassFilterDS2())),
61 noise_reduction_filter_(down_sampling_factor_ == 8
62 ? GetPassThroughFilter()
63 : GetHighPassFilter()) {
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