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 11 #ifndef MODULES_AUDIO_PROCESSING_AEC3_MATCHED_FILTER_H_ 12 #define MODULES_AUDIO_PROCESSING_AEC3_MATCHED_FILTER_H_ 13 14 #include <stddef.h> 15 16 #include <vector> 17 18 #include "api/array_view.h" 19 #include "modules/audio_processing/aec3/aec3_common.h" 20 #include "rtc_base/constructor_magic.h" 21 #include "rtc_base/system/arch.h" 22 23 namespace webrtc { 24 25 class ApmDataDumper; 26 struct DownsampledRenderBuffer; 27 28 namespace aec3 { 29 30 #if defined(WEBRTC_HAS_NEON) 31 32 // Filter core for the matched filter that is optimized for NEON. 33 void MatchedFilterCore_NEON(size_t x_start_index, 34 float x2_sum_threshold, 35 float smoothing, 36 rtc::ArrayView<const float> x, 37 rtc::ArrayView<const float> y, 38 rtc::ArrayView<float> h, 39 bool* filters_updated, 40 float* error_sum); 41 42 #endif 43 44 #if defined(WEBRTC_ARCH_X86_FAMILY) 45 46 // Filter core for the matched filter that is optimized for SSE2. 47 void MatchedFilterCore_SSE2(size_t x_start_index, 48 float x2_sum_threshold, 49 float smoothing, 50 rtc::ArrayView<const float> x, 51 rtc::ArrayView<const float> y, 52 rtc::ArrayView<float> h, 53 bool* filters_updated, 54 float* error_sum); 55 56 #endif 57 58 // Filter core for the matched filter. 59 void MatchedFilterCore(size_t x_start_index, 60 float x2_sum_threshold, 61 float smoothing, 62 rtc::ArrayView<const float> x, 63 rtc::ArrayView<const float> y, 64 rtc::ArrayView<float> h, 65 bool* filters_updated, 66 float* error_sum); 67 68 } // namespace aec3 69 70 // Produces recursively updated cross-correlation estimates for several signal 71 // shifts where the intra-shift spacing is uniform. 72 class MatchedFilter { 73 public: 74 // Stores properties for the lag estimate corresponding to a particular signal 75 // shift. 76 struct LagEstimate { 77 LagEstimate() = default; LagEstimateLagEstimate78 LagEstimate(float accuracy, bool reliable, size_t lag, bool updated) 79 : accuracy(accuracy), reliable(reliable), lag(lag), updated(updated) {} 80 81 float accuracy = 0.f; 82 bool reliable = false; 83 size_t lag = 0; 84 bool updated = false; 85 }; 86 87 MatchedFilter(ApmDataDumper* data_dumper, 88 Aec3Optimization optimization, 89 size_t sub_block_size, 90 size_t window_size_sub_blocks, 91 int num_matched_filters, 92 size_t alignment_shift_sub_blocks, 93 float excitation_limit, 94 float smoothing, 95 float matching_filter_threshold); 96 97 ~MatchedFilter(); 98 99 // Updates the correlation with the values in the capture buffer. 100 void Update(const DownsampledRenderBuffer& render_buffer, 101 rtc::ArrayView<const float> capture); 102 103 // Resets the matched filter. 104 void Reset(); 105 106 // Returns the current lag estimates. GetLagEstimates()107 rtc::ArrayView<const MatchedFilter::LagEstimate> GetLagEstimates() const { 108 return lag_estimates_; 109 } 110 111 // Returns the maximum filter lag. GetMaxFilterLag()112 size_t GetMaxFilterLag() const { 113 return filters_.size() * filter_intra_lag_shift_ + filters_[0].size(); 114 } 115 116 // Log matched filter properties. 117 void LogFilterProperties(int sample_rate_hz, 118 size_t shift, 119 size_t downsampling_factor) const; 120 121 private: 122 ApmDataDumper* const data_dumper_; 123 const Aec3Optimization optimization_; 124 const size_t sub_block_size_; 125 const size_t filter_intra_lag_shift_; 126 std::vector<std::vector<float>> filters_; 127 std::vector<LagEstimate> lag_estimates_; 128 std::vector<size_t> filters_offsets_; 129 const float excitation_limit_; 130 const float smoothing_; 131 const float matching_filter_threshold_; 132 133 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(MatchedFilter); 134 }; 135 136 } // namespace webrtc 137 138 #endif // MODULES_AUDIO_PROCESSING_AEC3_MATCHED_FILTER_H_ 139