Per Åhgren | 38e2d95 | 2017-11-17 14:54:28 +0100 | [diff] [blame] | 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 "rtc_base/checks.h" |
| 13 | |
| 14 | namespace webrtc { |
| 15 | namespace { |
| 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). |
| 19 | const CascadedBiQuadFilter::BiQuadCoefficients kLowPassFilterCoefficients2 = { |
| 20 | {0.22711796f, 0.45423593f, 0.22711796f}, |
| 21 | {-0.27666461f, 0.18513647f}}; |
| 22 | constexpr 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). |
| 26 | const CascadedBiQuadFilter::BiQuadCoefficients kLowPassFilterCoefficients4 = { |
| 27 | {0.0179f, 0.0357f, 0.0179f}, |
| 28 | {-1.5879f, 0.6594f}}; |
| 29 | constexpr int kNumFilters4 = 3; |
| 30 | |
| 31 | // b, a = signal.butter(2, 800/8000.0, 'lowpass', analog=False) which are the |
| 32 | // same as b, a = signal.butter(2, 400/4000.0, 'lowpass', analog=False). |
| 33 | const CascadedBiQuadFilter::BiQuadCoefficients kLowPassFilterCoefficients8 = { |
| 34 | {0.02008337f, 0.04016673f, 0.02008337f}, |
| 35 | {-1.56101808f, 0.64135154f}}; |
| 36 | constexpr int kNumFilters8 = 4; |
| 37 | |
Gustaf Ullberg | 6bf5a0d | 2018-05-18 15:45:09 +0200 | [diff] [blame] | 38 | // b, a = signal.butter(2, 1000/8000.0, 'highpass', analog=False) |
| 39 | const CascadedBiQuadFilter::BiQuadCoefficients kHighPassFilterCoefficients4 = { |
| 40 | {0.75707638f, -1.51415275f, 0.75707638f}, |
| 41 | {-1.45424359f, 0.57406192f}}; |
| 42 | constexpr int kNumFiltersHP4 = 1; |
| 43 | |
Per Åhgren | 38e2d95 | 2017-11-17 14:54:28 +0100 | [diff] [blame] | 44 | } // namespace |
| 45 | |
| 46 | Decimator::Decimator(size_t down_sampling_factor) |
| 47 | : down_sampling_factor_(down_sampling_factor), |
| 48 | low_pass_filter_( |
| 49 | down_sampling_factor_ == 4 |
| 50 | ? kLowPassFilterCoefficients4 |
| 51 | : (down_sampling_factor_ == 8 ? kLowPassFilterCoefficients8 |
| 52 | : kLowPassFilterCoefficients2), |
| 53 | down_sampling_factor_ == 4 |
| 54 | ? kNumFilters4 |
Gustaf Ullberg | 6bf5a0d | 2018-05-18 15:45:09 +0200 | [diff] [blame] | 55 | : (down_sampling_factor_ == 8 ? kNumFilters8 : kNumFilters2)), |
| 56 | high_pass_filter_(kHighPassFilterCoefficients4, kNumFiltersHP4) { |
Per Åhgren | 38e2d95 | 2017-11-17 14:54:28 +0100 | [diff] [blame] | 57 | RTC_DCHECK(down_sampling_factor_ == 2 || down_sampling_factor_ == 4 || |
| 58 | down_sampling_factor_ == 8); |
| 59 | } |
| 60 | |
| 61 | void Decimator::Decimate(rtc::ArrayView<const float> in, |
| 62 | rtc::ArrayView<float> out) { |
| 63 | RTC_DCHECK_EQ(kBlockSize, in.size()); |
| 64 | RTC_DCHECK_EQ(kBlockSize / down_sampling_factor_, out.size()); |
| 65 | std::array<float, kBlockSize> x; |
| 66 | |
| 67 | // Limit the frequency content of the signal to avoid aliasing. |
| 68 | low_pass_filter_.Process(in, x); |
| 69 | |
Gustaf Ullberg | 6bf5a0d | 2018-05-18 15:45:09 +0200 | [diff] [blame] | 70 | // High-pass filter to reduce the impact of near-end noise. |
| 71 | if (down_sampling_factor_ == 4) |
| 72 | high_pass_filter_.Process(x, x); |
| 73 | |
Per Åhgren | 38e2d95 | 2017-11-17 14:54:28 +0100 | [diff] [blame] | 74 | // Downsample the signal. |
| 75 | for (size_t j = 0, k = 0; j < out.size(); ++j, k += down_sampling_factor_) { |
| 76 | RTC_DCHECK_GT(kBlockSize, k); |
| 77 | out[j] = x[k]; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | } // namespace webrtc |