OLD | NEW |
---|---|
(Empty) | |
1 /* | |
hlundin-webrtc
2017/02/01 08:30:00
This file needs:
1. Reference to or comments conta
peah-webrtc
2017/02/02 14:04:45
Done.
| |
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 "webrtc/modules/audio_processing/aec3/correlator.h" | |
11 | |
12 #include <algorithm> | |
13 | |
14 #include "webrtc/modules/audio_processing/aec3/aec3_constants.h" | |
15 #include "webrtc/modules/audio_processing/include/audio_processing.h" | |
16 | |
17 namespace webrtc { | |
18 | |
19 Correlator::IndexedBuffer::IndexedBuffer(size_t size) : data(size, 0.f) {} | |
20 | |
21 Correlator::IndexedBuffer::~IndexedBuffer() = default; | |
22 | |
23 Correlator::Correlator(ApmDataDumper* data_dumper, | |
24 size_t window_size_sub_blocks, | |
25 size_t num_alignment_shifts, | |
26 size_t alignment_shift_sub_blocks) | |
27 : data_dumper_(data_dumper), | |
28 correlator_intra_lag_shift_(alignment_shift_sub_blocks * kSubBlockSize), | |
29 correlations_( | |
30 num_alignment_shifts + 1, | |
31 std::vector<float>(window_size_sub_blocks * kSubBlockSize, 0.f)), | |
32 lag_estimates_(num_alignment_shifts + 1), | |
33 render_buffer_(kSubBlockSize * | |
34 (alignment_shift_sub_blocks * num_alignment_shifts + 1 + | |
35 window_size_sub_blocks + | |
36 1)) { | |
37 RTC_DCHECK(data_dumper); | |
38 RTC_DCHECK_EQ(0, render_buffer_.data.size() % kSubBlockSize); | |
39 RTC_DCHECK_LT(0, window_size_sub_blocks); | |
40 } | |
41 | |
42 Correlator::~Correlator() = default; | |
43 | |
44 void Correlator::Update(rtc::ArrayView<const float> render, | |
45 rtc::ArrayView<const float> capture) { | |
46 RTC_DCHECK_EQ(kSubBlockSize, capture.size()); | |
47 RTC_DCHECK_EQ(render.size(), capture.size()); | |
48 const float signal_strength_threshold = | |
49 correlations_[0].size() * 150.f * 150.f; | |
50 | |
51 // Insert the new x sub block into x_buffer. | |
aleloi
2017/01/27 15:37:47
Is x_sub and x_buffer references to other variable
peah-webrtc
2017/02/02 14:04:45
x sub block refers to x subblock.
Changed the nam
| |
52 render_buffer_.index = | |
53 (render_buffer_.index + kSubBlockSize) % render_buffer_.data.size(); | |
aleloi
2017/01/27 15:37:47
Will the buffer always have room for kSubBlockSize
peah-webrtc
2017/02/02 14:04:45
Done.
| |
54 std::copy(render.begin(), render.end(), | |
hlundin-webrtc
2017/02/01 08:30:00
RTC_DCHECK_LE(render_buffer_.index + render.size()
peah-webrtc
2017/02/02 14:04:45
Done.
| |
55 render_buffer_.data.begin() + render_buffer_.index); | |
56 | |
57 // Update the correlations for all sub-correlators. | |
58 size_t alignment_shift = 0; | |
59 for (size_t sub_correlator = 0; sub_correlator < correlations_.size(); | |
60 ++sub_correlator) { | |
61 float error = 0.f; | |
62 bool correlations_updated = false; | |
63 size_t render_0_index = | |
hlundin-webrtc
2017/02/01 08:30:00
What is this? Can the name be shorter?
peah-webrtc
2017/02/02 14:04:45
This is the index of the first value in the render
| |
64 (render_buffer_.index - alignment_shift + render_buffer_.data.size()) % | |
65 render_buffer_.data.size(); | |
66 | |
67 // Update the correlation estimates. | |
68 RTC_DCHECK_GT(render_buffer_.data.size(), | |
69 render_0_index + kSubBlockSize - 1); | |
70 for (size_t i = 0; i < kSubBlockSize; ++i, ++render_0_index) { | |
71 const size_t render_buffer_part1_size = | |
hlundin-webrtc
2017/02/01 08:30:00
Please, comment on the fact that this is due to pr
peah-webrtc
2017/02/02 14:04:45
Done.
| |
72 std::min(correlations_[sub_correlator].size(), render_0_index + 1); | |
73 | |
74 // Compute the correlation estimate update factor. | |
75 float s = 0.f; | |
hlundin-webrtc
2017/02/01 08:30:00
For the sake of my own understanding, I'm going to
peah-webrtc
2017/02/02 14:04:45
Done.
| |
76 float signal_strength = 0.f; | |
77 for (size_t lag = 0; lag < render_buffer_part1_size; ++lag) { | |
78 const float render_k = render_buffer_.data[render_0_index - lag]; | |
79 signal_strength += render_k * render_k; | |
80 s += correlations_[sub_correlator][lag] * render_k; | |
hlundin-webrtc
2017/02/01 08:30:00
Consider calculating s only when you know you'll n
peah-webrtc
2017/02/02 14:04:45
I don't think that's possible. In order to compute
| |
81 } | |
82 | |
83 for (size_t lag = render_buffer_part1_size, | |
84 render_buffer_index = render_buffer_.data.size() - 1; | |
85 lag < correlations_[sub_correlator].size(); | |
86 ++lag, --render_buffer_index) { | |
87 const float render_k = render_buffer_.data[render_buffer_index]; | |
88 signal_strength += render_k * render_k; | |
89 s += correlations_[sub_correlator][lag] * render_k; | |
90 } | |
91 | |
92 const float update_factor = | |
93 std::min(32767.f, std::max(-32768.f, capture[i] - s)); | |
94 error += update_factor * update_factor; | |
95 | |
96 // Update the correlation. | |
97 if (signal_strength > signal_strength_threshold) { | |
98 correlations_updated = true; | |
99 const float normalized_update_factor = | |
100 0.7f * update_factor / signal_strength; | |
101 | |
102 for (size_t lag = 0; lag < render_buffer_part1_size; ++lag) { | |
103 correlations_[sub_correlator][lag] += | |
104 normalized_update_factor * | |
105 render_buffer_.data[render_0_index - lag]; | |
106 } | |
107 | |
108 for (size_t lag = render_buffer_part1_size, | |
109 render_buffer_index = render_buffer_.data.size() - 1; | |
110 lag < correlations_[sub_correlator].size(); | |
111 ++lag, --render_buffer_index) { | |
112 correlations_[sub_correlator][lag] += | |
113 normalized_update_factor * | |
114 render_buffer_.data[render_buffer_index]; | |
115 } | |
116 } | |
117 } | |
118 | |
119 const float error_anchor = | |
hlundin-webrtc
2017/02/01 08:30:00
// Energy of capture.
peah-webrtc
2017/02/02 14:04:45
Yes, but I prefer to name it differently as it is
| |
120 std::accumulate(capture.begin(), capture.end(), 0.f, | |
hlundin-webrtc
2017/02/01 08:30:00
This should be the same as:
std::inner_product(ca
peah-webrtc
2017/02/02 14:04:45
Nice!
Done.
| |
121 [](float a, float b) -> float { return a + b * b; }); | |
122 const size_t lag_estimate = std::distance( | |
hlundin-webrtc
2017/02/01 08:30:00
// Distance from start of sub_correlator to strong
peah-webrtc
2017/02/02 14:04:45
Done.
| |
123 correlations_[sub_correlator].begin(), | |
124 std::max_element( | |
125 correlations_[sub_correlator].begin(), | |
126 correlations_[sub_correlator].end(), | |
127 [](float a, float b) -> bool { return a * a < b * b; })); | |
128 | |
hlundin-webrtc
2017/02/01 08:30:00
What can you say about lag_estimate. DCHECK any lo
peah-webrtc
2017/02/02 14:04:45
There are no bounds, except from 0 and filters_[n]
| |
129 lag_estimates_[sub_correlator].Set( | |
130 error_anchor - error, error < 0.3f * error_anchor, | |
hlundin-webrtc
2017/02/01 08:30:00
Explain the thresholding with 0.3.
peah-webrtc
2017/02/02 14:04:45
It is a tuning parameter. I made a constant and ad
| |
131 lag_estimate + alignment_shift, correlations_updated); | |
132 | |
133 // TODO(peah): Remove once development of EchoCanceller3 is fully done. | |
134 RTC_DCHECK_EQ(4, correlations_.size()); | |
135 switch (sub_correlator) { | |
136 case 0: | |
137 data_dumper_->DumpRaw("aec3_correlator_0_h", correlations_[0]); | |
138 break; | |
139 case 1: | |
140 data_dumper_->DumpRaw("aec3_correlator_1_h", correlations_[1]); | |
141 break; | |
142 case 2: | |
143 data_dumper_->DumpRaw("aec3_correlator_2_h", correlations_[2]); | |
144 break; | |
145 case 3: | |
146 data_dumper_->DumpRaw("aec3_correlator_3_h", correlations_[3]); | |
147 break; | |
148 default: | |
149 RTC_DCHECK(false); | |
150 } | |
151 | |
152 alignment_shift += correlator_intra_lag_shift_; | |
153 } | |
154 } | |
155 | |
156 } // namespace webrtc | |
OLD | NEW |