blob: 6508df89a46f02ccc4988f3d9aa428711dfb2f08 [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
Gustaf Ullbergee84d392019-09-10 09:36:43 +020072void Decimator::Decimate(const std::vector<std::vector<float>>& in,
73 bool downmix,
Per Åhgren38e2d952017-11-17 14:54:28 +010074 rtc::ArrayView<float> out) {
Gustaf Ullbergee84d392019-09-10 09:36:43 +020075 RTC_DCHECK_EQ(kBlockSize, in[0].size());
Per Åhgren38e2d952017-11-17 14:54:28 +010076 RTC_DCHECK_EQ(kBlockSize / down_sampling_factor_, out.size());
Gustaf Ullbergee84d392019-09-10 09:36:43 +020077 std::array<float, kBlockSize> in_downmixed;
Per Åhgren38e2d952017-11-17 14:54:28 +010078 std::array<float, kBlockSize> x;
79
Gustaf Ullbergee84d392019-09-10 09:36:43 +020080 // Mix channels before decimation.
81 std::copy(in[0].begin(), in[0].end(), in_downmixed.begin());
82 if (downmix && in.size() > 1) {
83 for (size_t channel = 1; channel < in.size(); channel++) {
84 const auto& data = in[channel];
85 for (size_t i = 0; i < kBlockSize; i++) {
86 in_downmixed[i] += data[i];
87 }
88 }
89
90 const float one_by_num_channels = 1.f / in.size();
91 for (size_t i = 0; i < kBlockSize; i++) {
92 in_downmixed[i] *= one_by_num_channels;
93 }
94 }
95
Per Åhgren38e2d952017-11-17 14:54:28 +010096 // Limit the frequency content of the signal to avoid aliasing.
Gustaf Ullbergee84d392019-09-10 09:36:43 +020097 anti_aliasing_filter_.Process(in_downmixed, x);
Per Åhgren38e2d952017-11-17 14:54:28 +010098
Gustaf Ullberg78b1c4a2018-05-25 10:12:58 +020099 // Reduce the impact of near-end noise.
100 noise_reduction_filter_.Process(x);
Gustaf Ullberg6bf5a0d2018-05-18 15:45:09 +0200101
Per Åhgren38e2d952017-11-17 14:54:28 +0100102 // Downsample the signal.
103 for (size_t j = 0, k = 0; j < out.size(); ++j, k += down_sampling_factor_) {
104 RTC_DCHECK_GT(kBlockSize, k);
105 out[j] = x[k];
106 }
107}
108
109} // namespace webrtc