blob: bd03237ca080fb5341452a1faa39e160edeeee07 [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
Yves Gerey988cc082018-10-23 12:03:01 +020012#include <array>
13#include <vector>
14
15#include "modules/audio_processing/aec3/aec3_common.h"
Per Åhgren38e2d952017-11-17 14:54:28 +010016#include "rtc_base/checks.h"
17
18namespace webrtc {
19namespace {
20
Gustaf Ullberg34c9f122018-06-07 07:55:01 +020021// signal.butter(2, 3400/8000.0, 'lowpass', analog=False)
Gustaf Ullberged51a6e2018-06-07 19:18:35 +020022const 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}
Per Åhgren38e2d952017-11-17 14:54:28 +010028
Gustaf Ullbergf469b632018-06-07 11:25:21 +020029// signal.ellip(6, 1, 40, 1800/8000, btype='lowpass', analog=False)
Gustaf Ullberged51a6e2018-06-07 19:18:35 +020030const 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}
Per Åhgren38e2d952017-11-17 14:54:28 +010036
Gustaf Ullberg34c9f122018-06-07 07:55:01 +020037// signal.cheby1(1, 6, [1000/8000, 2000/8000], btype='bandpass', analog=False)
Gustaf Ullberged51a6e2018-06-07 19:18:35 +020038const 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}
Per Åhgren38e2d952017-11-17 14:54:28 +010046
Gustaf Ullberg34c9f122018-06-07 07:55:01 +020047// signal.butter(2, 1000/8000.0, 'highpass', analog=False)
Gustaf Ullberged51a6e2018-06-07 19:18:35 +020048const std::vector<CascadedBiQuadFilter::BiQuadParam> GetHighPassFilter() {
49 return std::vector<CascadedBiQuadFilter::BiQuadParam>{
50 {{1.f, 0.f}, {0.72712179f, 0.21296904f}, 0.7570763753338849f}};
51}
Gustaf Ullberg6bf5a0d2018-05-18 15:45:09 +020052
Gustaf Ullberged51a6e2018-06-07 19:18:35 +020053const std::vector<CascadedBiQuadFilter::BiQuadParam> GetPassThroughFilter() {
54 return std::vector<CascadedBiQuadFilter::BiQuadParam>{};
55}
Per Åhgren38e2d952017-11-17 14:54:28 +010056} // namespace
57
58Decimator::Decimator(size_t down_sampling_factor)
59 : down_sampling_factor_(down_sampling_factor),
Gustaf Ullberg34c9f122018-06-07 07:55:01 +020060 anti_aliasing_filter_(down_sampling_factor_ == 4
Gustaf Ullberged51a6e2018-06-07 19:18:35 +020061 ? GetLowPassFilterDS4()
Gustaf Ullberg34c9f122018-06-07 07:55:01 +020062 : (down_sampling_factor_ == 8
Gustaf Ullberged51a6e2018-06-07 19:18:35 +020063 ? GetBandPassFilterDS8()
64 : GetLowPassFilterDS2())),
65 noise_reduction_filter_(down_sampling_factor_ == 8
66 ? GetPassThroughFilter()
67 : GetHighPassFilter()) {
Per Åhgren38e2d952017-11-17 14:54:28 +010068 RTC_DCHECK(down_sampling_factor_ == 2 || down_sampling_factor_ == 4 ||
69 down_sampling_factor_ == 8);
70}
71
72void 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.
Gustaf Ullberg78b1c4a2018-05-25 10:12:58 +020079 anti_aliasing_filter_.Process(in, x);
Per Åhgren38e2d952017-11-17 14:54:28 +010080
Gustaf Ullberg78b1c4a2018-05-25 10:12:58 +020081 // Reduce the impact of near-end noise.
82 noise_reduction_filter_.Process(x);
Gustaf Ullberg6bf5a0d2018-05-18 15:45:09 +020083
Per Åhgren38e2d952017-11-17 14:54:28 +010084 // 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