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 <array>
13 #include <vector>
14 
15 #include "modules/audio_processing/aec3/aec3_common.h"
16 #include "rtc_base/checks.h"
17 
18 namespace webrtc {
19 namespace {
20 
21 // signal.butter(2, 3400/8000.0, 'lowpass', analog=False)
GetLowPassFilterDS2()22 const std::vector<CascadedBiQuadFilter::BiQuadParam> GetLowPassFilterDS2() {
23   return std::vector<CascadedBiQuadFilter::BiQuadParam>{
24       {{-1.f, 0.f}, {0.13833231f, 0.40743176f}, 0.22711796393486466f},
25       {{-1.f, 0.f}, {0.13833231f, 0.40743176f}, 0.22711796393486466f},
26       {{-1.f, 0.f}, {0.13833231f, 0.40743176f}, 0.22711796393486466f}};
27 }
28 
29 // signal.ellip(6, 1, 40, 1800/8000, btype='lowpass', analog=False)
GetLowPassFilterDS4()30 const std::vector<CascadedBiQuadFilter::BiQuadParam> GetLowPassFilterDS4() {
31   return std::vector<CascadedBiQuadFilter::BiQuadParam>{
32       {{-0.08873842f, 0.99605496f}, {0.75916227f, 0.23841065f}, 0.26250696827f},
33       {{0.62273832f, 0.78243018f}, {0.74892112f, 0.5410152f}, 0.26250696827f},
34       {{0.71107693f, 0.70311421f}, {0.74895534f, 0.63924616f}, 0.26250696827f}};
35 }
36 
37 // signal.cheby1(1, 6, [1000/8000, 2000/8000], btype='bandpass', analog=False)
GetBandPassFilterDS8()38 const std::vector<CascadedBiQuadFilter::BiQuadParam> GetBandPassFilterDS8() {
39   return std::vector<CascadedBiQuadFilter::BiQuadParam>{
40       {{1.f, 0.f}, {0.7601815f, 0.46423542f}, 0.10330478266505948f, true},
41       {{1.f, 0.f}, {0.7601815f, 0.46423542f}, 0.10330478266505948f, true},
42       {{1.f, 0.f}, {0.7601815f, 0.46423542f}, 0.10330478266505948f, true},
43       {{1.f, 0.f}, {0.7601815f, 0.46423542f}, 0.10330478266505948f, true},
44       {{1.f, 0.f}, {0.7601815f, 0.46423542f}, 0.10330478266505948f, true}};
45 }
46 
47 // signal.butter(2, 1000/8000.0, 'highpass', analog=False)
GetHighPassFilter()48 const std::vector<CascadedBiQuadFilter::BiQuadParam> GetHighPassFilter() {
49   return std::vector<CascadedBiQuadFilter::BiQuadParam>{
50       {{1.f, 0.f}, {0.72712179f, 0.21296904f}, 0.7570763753338849f}};
51 }
52 
GetPassThroughFilter()53 const std::vector<CascadedBiQuadFilter::BiQuadParam> GetPassThroughFilter() {
54   return std::vector<CascadedBiQuadFilter::BiQuadParam>{};
55 }
56 }  // namespace
57 
Decimator(size_t down_sampling_factor)58 Decimator::Decimator(size_t down_sampling_factor)
59     : down_sampling_factor_(down_sampling_factor),
60       anti_aliasing_filter_(down_sampling_factor_ == 4
61                                 ? GetLowPassFilterDS4()
62                                 : (down_sampling_factor_ == 8
63                                        ? GetBandPassFilterDS8()
64                                        : GetLowPassFilterDS2())),
65       noise_reduction_filter_(down_sampling_factor_ == 8
66                                   ? GetPassThroughFilter()
67                                   : GetHighPassFilter()) {
68   RTC_DCHECK(down_sampling_factor_ == 2 || down_sampling_factor_ == 4 ||
69              down_sampling_factor_ == 8);
70 }
71 
Decimate(rtc::ArrayView<const float> in,rtc::ArrayView<float> out)72 void Decimator::Decimate(rtc::ArrayView<const float> in,
73                          rtc::ArrayView<float> out) {
74   RTC_DCHECK_EQ(kBlockSize, in.size());
75   RTC_DCHECK_EQ(kBlockSize / down_sampling_factor_, out.size());
76   std::array<float, kBlockSize> x;
77 
78   // Limit the frequency content of the signal to avoid aliasing.
79   anti_aliasing_filter_.Process(in, x);
80 
81   // Reduce the impact of near-end noise.
82   noise_reduction_filter_.Process(x);
83 
84   // Downsample the signal.
85   for (size_t j = 0, k = 0; j < out.size(); ++j, k += down_sampling_factor_) {
86     RTC_DCHECK_GT(kBlockSize, k);
87     out[j] = x[k];
88   }
89 }
90 
91 }  // namespace webrtc
92