Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: webrtc/modules/audio_processing/intelligibility/intelligibility_enhancer.cc

Issue 1685703004: Fix and simplify the power estimation in the IntelligibilityEnhancer (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@ie
Patch Set: Naming Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 //
12 // Implements core class for intelligibility enhancer.
13 //
14 // Details of the model and algorithm can be found in the original paper:
15 // http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=6882788
16 //
17
18 #include "webrtc/modules/audio_processing/intelligibility/intelligibility_enhanc er.h" 11 #include "webrtc/modules/audio_processing/intelligibility/intelligibility_enhanc er.h"
19 12
20 #include <math.h> 13 #include <math.h>
21 #include <stdlib.h> 14 #include <stdlib.h>
22 #include <algorithm> 15 #include <algorithm>
23 #include <numeric> 16 #include <numeric>
24 17
25 #include "webrtc/base/checks.h" 18 #include "webrtc/base/checks.h"
26 #include "webrtc/common_audio/include/audio_util.h" 19 #include "webrtc/common_audio/include/audio_util.h"
27 #include "webrtc/common_audio/window_generator.h" 20 #include "webrtc/common_audio/window_generator.h"
28 21
29 namespace webrtc { 22 namespace webrtc {
30 23
31 namespace { 24 namespace {
32 25
33 const size_t kErbResolution = 2; 26 const size_t kErbResolution = 2;
34 const int kWindowSizeMs = 2; 27 const int kWindowSizeMs = 16;
35 const int kChunkSizeMs = 10; // Size provided by APM. 28 const int kChunkSizeMs = 10; // Size provided by APM.
36 const float kClipFreq = 200.0f; 29 const float kClipFreq = 200.0f;
37 const float kConfigRho = 0.02f; // Default production and interpretation SNR. 30 const float kConfigRho = 0.02f; // Default production and interpretation SNR.
38 const float kKbdAlpha = 1.5f; 31 const float kKbdAlpha = 1.5f;
39 const float kLambdaBot = -1.0f; // Extreme values in bisection 32 const float kLambdaBot = -1.0f; // Extreme values in bisection
40 const float kLambdaTop = -10e-18f; // search for lamda. 33 const float kLambdaTop = -10e-18f; // search for lamda.
41 34
42 // Returns dot product of vectors |a| and |b| with size |length|. 35 // Returns dot product of vectors |a| and |b| with size |length|.
43 float DotProduct(const float* a, const float* b, size_t length) { 36 float DotProduct(const float* a, const float* b, size_t length) {
44 float ret = 0.f; 37 float ret = 0.f;
45 for (size_t i = 0; i < length; ++i) { 38 for (size_t i = 0; i < length; ++i) {
46 ret = fmaf(a[i], b[i], ret); 39 ret = fmaf(a[i], b[i], ret);
47 } 40 }
48 return ret; 41 return ret;
49 } 42 }
50 43
51 // Computes the power across ERB filters from the power spectral density |var|. 44 // Computes the power across ERB bands from the power spectral density |pow|.
52 // Stores it in |result|. 45 // Stores it in |result|.
53 void FilterVariance(const float* var, 46 void MapToErbBands(const float* pow,
54 const std::vector<std::vector<float>>& filter_bank, 47 const std::vector<std::vector<float>>& filter_bank,
55 float* result) { 48 float* result) {
56 for (size_t i = 0; i < filter_bank.size(); ++i) { 49 for (size_t i = 0; i < filter_bank.size(); ++i) {
57 RTC_DCHECK_GT(filter_bank[i].size(), 0u); 50 RTC_DCHECK_GT(filter_bank[i].size(), 0u);
58 result[i] = DotProduct(&filter_bank[i][0], var, filter_bank[i].size()); 51 result[i] = DotProduct(&filter_bank[i][0], pow, filter_bank[i].size());
59 } 52 }
60 } 53 }
61 54
62 } // namespace 55 } // namespace
63 56
64 using std::complex;
65 using std::max;
66 using std::min;
67 using VarianceType = intelligibility::VarianceArray::StepType;
68
69 IntelligibilityEnhancer::TransformCallback::TransformCallback( 57 IntelligibilityEnhancer::TransformCallback::TransformCallback(
70 IntelligibilityEnhancer* parent) 58 IntelligibilityEnhancer* parent)
71 : parent_(parent) { 59 : parent_(parent) {
72 } 60 }
73 61
74 void IntelligibilityEnhancer::TransformCallback::ProcessAudioBlock( 62 void IntelligibilityEnhancer::TransformCallback::ProcessAudioBlock(
75 const complex<float>* const* in_block, 63 const std::complex<float>* const* in_block,
76 size_t in_channels, 64 size_t in_channels,
77 size_t frames, 65 size_t frames,
78 size_t /* out_channels */, 66 size_t /* out_channels */,
79 complex<float>* const* out_block) { 67 std::complex<float>* const* out_block) {
80 RTC_DCHECK_EQ(parent_->freqs_, frames); 68 RTC_DCHECK_EQ(parent_->freqs_, frames);
81 for (size_t i = 0; i < in_channels; ++i) { 69 for (size_t i = 0; i < in_channels; ++i) {
82 parent_->ProcessClearBlock(in_block[i], out_block[i]); 70 parent_->ProcessClearBlock(in_block[i], out_block[i]);
83 } 71 }
84 } 72 }
85 73
86 IntelligibilityEnhancer::IntelligibilityEnhancer() 74 IntelligibilityEnhancer::IntelligibilityEnhancer()
87 : IntelligibilityEnhancer(IntelligibilityEnhancer::Config()) { 75 : IntelligibilityEnhancer(IntelligibilityEnhancer::Config()) {
88 } 76 }
89 77
90 IntelligibilityEnhancer::IntelligibilityEnhancer(const Config& config) 78 IntelligibilityEnhancer::IntelligibilityEnhancer(const Config& config)
91 : freqs_(RealFourier::ComplexLength( 79 : freqs_(RealFourier::ComplexLength(
92 RealFourier::FftOrder(config.sample_rate_hz * kWindowSizeMs / 1000))), 80 RealFourier::FftOrder(config.sample_rate_hz * kWindowSizeMs / 1000))),
93 window_size_(static_cast<size_t>(1 << RealFourier::FftOrder(freqs_))), 81 window_size_(static_cast<size_t>(1 << RealFourier::FftOrder(freqs_))),
94 chunk_length_( 82 chunk_length_(
95 static_cast<size_t>(config.sample_rate_hz * kChunkSizeMs / 1000)), 83 static_cast<size_t>(config.sample_rate_hz * kChunkSizeMs / 1000)),
96 bank_size_(GetBankSize(config.sample_rate_hz, kErbResolution)), 84 bank_size_(GetBankSize(config.sample_rate_hz, kErbResolution)),
97 sample_rate_hz_(config.sample_rate_hz), 85 sample_rate_hz_(config.sample_rate_hz),
98 erb_resolution_(kErbResolution), 86 erb_resolution_(kErbResolution),
99 num_capture_channels_(config.num_capture_channels), 87 num_capture_channels_(config.num_capture_channels),
100 num_render_channels_(config.num_render_channels), 88 num_render_channels_(config.num_render_channels),
101 analysis_rate_(config.analysis_rate), 89 analysis_rate_(config.analysis_rate),
102 active_(true), 90 active_(true),
103 clear_variance_(freqs_, 91 clear_power_(freqs_, config.decay_rate),
104 config.var_type,
105 config.var_window_size,
106 config.var_decay_rate),
107 noise_power_(freqs_, 0.f), 92 noise_power_(freqs_, 0.f),
108 filtered_clear_var_(new float[bank_size_]), 93 filtered_clear_pow_(new float[bank_size_]),
109 filtered_noise_var_(new float[bank_size_]), 94 filtered_noise_pow_(new float[bank_size_]),
110 center_freqs_(new float[bank_size_]), 95 center_freqs_(new float[bank_size_]),
111 render_filter_bank_(CreateErbBank(freqs_)), 96 render_filter_bank_(CreateErbBank(freqs_)),
112 rho_(new float[bank_size_]), 97 rho_(new float[bank_size_]),
113 gains_eq_(new float[bank_size_]), 98 gains_eq_(new float[bank_size_]),
114 gain_applier_(freqs_, config.gain_change_limit), 99 gain_applier_(freqs_, config.gain_change_limit),
115 temp_render_out_buffer_(chunk_length_, num_render_channels_), 100 temp_render_out_buffer_(chunk_length_, num_render_channels_),
116 kbd_window_(new float[window_size_]), 101 kbd_window_(new float[window_size_]),
117 render_callback_(this), 102 render_callback_(this),
118 block_count_(0), 103 block_count_(0),
119 analysis_step_(0) { 104 analysis_step_(0) {
120 RTC_DCHECK_LE(config.rho, 1.0f); 105 RTC_DCHECK_LE(config.rho, 1.0f);
121 106
122 memset(filtered_clear_var_.get(), 107 memset(filtered_clear_pow_.get(),
123 0, 108 0,
124 bank_size_ * sizeof(filtered_clear_var_[0])); 109 bank_size_ * sizeof(filtered_clear_pow_[0]));
125 memset(filtered_noise_var_.get(), 110 memset(filtered_noise_pow_.get(),
126 0, 111 0,
127 bank_size_ * sizeof(filtered_noise_var_[0])); 112 bank_size_ * sizeof(filtered_noise_pow_[0]));
128 113
129 // Assumes all rho equal. 114 // Assumes all rho equal.
130 for (size_t i = 0; i < bank_size_; ++i) { 115 for (size_t i = 0; i < bank_size_; ++i) {
131 rho_[i] = config.rho * config.rho; 116 rho_[i] = config.rho * config.rho;
132 } 117 }
133 118
134 float freqs_khz = kClipFreq / 1000.0f; 119 float freqs_khz = kClipFreq / 1000.0f;
135 size_t erb_index = static_cast<size_t>(ceilf( 120 size_t erb_index = static_cast<size_t>(ceilf(
136 11.17f * logf((freqs_khz + 0.312f) / (freqs_khz + 14.6575f)) + 43.0f)); 121 11.17f * logf((freqs_khz + 0.312f) / (freqs_khz + 14.6575f)) + 43.0f));
137 start_freq_ = std::max(static_cast<size_t>(1), erb_index * erb_resolution_); 122 start_freq_ = std::max(static_cast<size_t>(1), erb_index * erb_resolution_);
(...skipping 30 matching lines...) Expand all
168 } 153 }
169 154
170 if (active_) { 155 if (active_) {
171 for (size_t i = 0; i < num_render_channels_; ++i) { 156 for (size_t i = 0; i < num_render_channels_; ++i) {
172 memcpy(audio[i], temp_render_out_buffer_.channels()[i], 157 memcpy(audio[i], temp_render_out_buffer_.channels()[i],
173 chunk_length_ * sizeof(**audio)); 158 chunk_length_ * sizeof(**audio));
174 } 159 }
175 } 160 }
176 } 161 }
177 162
178 void IntelligibilityEnhancer::ProcessClearBlock(const complex<float>* in_block, 163 void IntelligibilityEnhancer::ProcessClearBlock(
179 complex<float>* out_block) { 164 const std::complex<float>* in_block,
165 std::complex<float>* out_block) {
180 if (block_count_ < 2) { 166 if (block_count_ < 2) {
181 memset(out_block, 0, freqs_ * sizeof(*out_block)); 167 memset(out_block, 0, freqs_ * sizeof(*out_block));
182 ++block_count_; 168 ++block_count_;
183 return; 169 return;
184 } 170 }
185 171
186 // TODO(ekm): Use VAD to |Step| and |AnalyzeClearBlock| only if necessary. 172 // TODO(ekm): Use VAD to |Step| and |AnalyzeClearBlock| only if necessary.
187 if (true) { 173 if (true) {
turaj 2016/02/12 23:12:35 Can we get rid off this "if(true)"?
aluebs-webrtc 2016/02/13 01:47:49 I did that in the next CL: https://codereview.webr
188 clear_variance_.Step(in_block, false); 174 clear_power_.Step(in_block);
189 if (block_count_ % analysis_rate_ == analysis_rate_ - 1) { 175 if (block_count_ % analysis_rate_ == analysis_rate_ - 1) {
190 const float power_target = std::accumulate( 176 AnalyzeClearBlock();
191 clear_variance_.variance(), clear_variance_.variance() + freqs_, 0.f);
192 AnalyzeClearBlock(power_target);
193 ++analysis_step_; 177 ++analysis_step_;
194 } 178 }
195 ++block_count_; 179 ++block_count_;
196 } 180 }
197 181
198 if (active_) { 182 if (active_) {
199 gain_applier_.Apply(in_block, out_block); 183 gain_applier_.Apply(in_block, out_block);
200 } 184 }
201 } 185 }
202 186
203 void IntelligibilityEnhancer::AnalyzeClearBlock(float power_target) { 187 void IntelligibilityEnhancer::AnalyzeClearBlock() {
204 FilterVariance(clear_variance_.variance(), 188 const float* clear_power = clear_power_.Power();
205 render_filter_bank_, 189 MapToErbBands(clear_power,
206 filtered_clear_var_.get()); 190 render_filter_bank_,
207 FilterVariance(&noise_power_[0], 191 filtered_clear_pow_.get());
208 capture_filter_bank_, 192 MapToErbBands(&noise_power_[0],
209 filtered_noise_var_.get()); 193 capture_filter_bank_,
194 filtered_noise_pow_.get());
210 SolveForGainsGivenLambda(kLambdaTop, start_freq_, gains_eq_.get()); 195 SolveForGainsGivenLambda(kLambdaTop, start_freq_, gains_eq_.get());
196 const float power_target = std::accumulate(
197 clear_power, clear_power + freqs_, 0.f);
211 const float power_top = 198 const float power_top =
212 DotProduct(gains_eq_.get(), filtered_clear_var_.get(), bank_size_); 199 DotProduct(gains_eq_.get(), filtered_clear_pow_.get(), bank_size_);
213 SolveForGainsGivenLambda(kLambdaBot, start_freq_, gains_eq_.get()); 200 SolveForGainsGivenLambda(kLambdaBot, start_freq_, gains_eq_.get());
214 const float power_bot = 201 const float power_bot =
215 DotProduct(gains_eq_.get(), filtered_clear_var_.get(), bank_size_); 202 DotProduct(gains_eq_.get(), filtered_clear_pow_.get(), bank_size_);
216 if (power_target >= power_bot && power_target <= power_top) { 203 if (power_target >= power_bot && power_target <= power_top) {
217 SolveForLambda(power_target, power_bot, power_top); 204 SolveForLambda(power_target, power_bot, power_top);
218 UpdateErbGains(); 205 UpdateErbGains();
219 } // Else experiencing variance underflow, so do nothing. 206 } // Else experiencing power underflow, so do nothing.
220 } 207 }
221 208
222 void IntelligibilityEnhancer::SolveForLambda(float power_target, 209 void IntelligibilityEnhancer::SolveForLambda(float power_target,
223 float power_bot, 210 float power_bot,
224 float power_top) { 211 float power_top) {
225 const float kConvergeThresh = 0.001f; // TODO(ekmeyerson): Find best values 212 const float kConvergeThresh = 0.001f; // TODO(ekmeyerson): Find best values
226 const int kMaxIters = 100; // for these, based on experiments. 213 const int kMaxIters = 100; // for these, based on experiments.
227 214
228 const float reciprocal_power_target = 1.f / power_target; 215 const float reciprocal_power_target = 1.f / power_target;
229 float lambda_bot = kLambdaBot; 216 float lambda_bot = kLambdaBot;
230 float lambda_top = kLambdaTop; 217 float lambda_top = kLambdaTop;
231 float power_ratio = 2.0f; // Ratio of achieved power to target power. 218 float power_ratio = 2.0f; // Ratio of achieved power to target power.
232 int iters = 0; 219 int iters = 0;
233 while (std::fabs(power_ratio - 1.0f) > kConvergeThresh && 220 while (std::fabs(power_ratio - 1.0f) > kConvergeThresh &&
234 iters <= kMaxIters) { 221 iters <= kMaxIters) {
235 const float lambda = lambda_bot + (lambda_top - lambda_bot) / 2.0f; 222 const float lambda = lambda_bot + (lambda_top - lambda_bot) / 2.0f;
236 SolveForGainsGivenLambda(lambda, start_freq_, gains_eq_.get()); 223 SolveForGainsGivenLambda(lambda, start_freq_, gains_eq_.get());
237 const float power = 224 const float power =
238 DotProduct(gains_eq_.get(), filtered_clear_var_.get(), bank_size_); 225 DotProduct(gains_eq_.get(), filtered_clear_pow_.get(), bank_size_);
239 if (power < power_target) { 226 if (power < power_target) {
240 lambda_bot = lambda; 227 lambda_bot = lambda;
241 } else { 228 } else {
242 lambda_top = lambda; 229 lambda_top = lambda;
243 } 230 }
244 power_ratio = std::fabs(power * reciprocal_power_target); 231 power_ratio = std::fabs(power * reciprocal_power_target);
245 ++iters; 232 ++iters;
246 } 233 }
247 } 234 }
248 235
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 } 268 }
282 269
283 for (size_t i = 0; i < bank_size_; ++i) { 270 for (size_t i = 0; i < bank_size_; ++i) {
284 filter_bank[i].resize(num_freqs); 271 filter_bank[i].resize(num_freqs);
285 } 272 }
286 273
287 for (size_t i = 1; i <= bank_size_; ++i) { 274 for (size_t i = 1; i <= bank_size_; ++i) {
288 size_t lll, ll, rr, rrr; 275 size_t lll, ll, rr, rrr;
289 static const size_t kOne = 1; // Avoids repeated static_cast<>s below. 276 static const size_t kOne = 1; // Avoids repeated static_cast<>s below.
290 lll = static_cast<size_t>(round( 277 lll = static_cast<size_t>(round(
291 center_freqs_[max(kOne, i - lf) - 1] * num_freqs / 278 center_freqs_[std::max(kOne, i - lf) - 1] * num_freqs /
292 (0.5f * sample_rate_hz_))); 279 (0.5f * sample_rate_hz_)));
293 ll = static_cast<size_t>(round( 280 ll = static_cast<size_t>(round(
294 center_freqs_[max(kOne, i) - 1] * num_freqs / 281 center_freqs_[std::max(kOne, i) - 1] * num_freqs /
295 (0.5f * sample_rate_hz_))); 282 (0.5f * sample_rate_hz_)));
296 lll = min(num_freqs, max(lll, kOne)) - 1; 283 lll = std::min(num_freqs, std::max(lll, kOne)) - 1;
297 ll = min(num_freqs, max(ll, kOne)) - 1; 284 ll = std::min(num_freqs, std::max(ll, kOne)) - 1;
298 285
299 rrr = static_cast<size_t>(round( 286 rrr = static_cast<size_t>(round(
300 center_freqs_[min(bank_size_, i + rf) - 1] * num_freqs / 287 center_freqs_[std::min(bank_size_, i + rf) - 1] * num_freqs /
301 (0.5f * sample_rate_hz_))); 288 (0.5f * sample_rate_hz_)));
302 rr = static_cast<size_t>(round( 289 rr = static_cast<size_t>(round(
303 center_freqs_[min(bank_size_, i + 1) - 1] * num_freqs / 290 center_freqs_[std::min(bank_size_, i + 1) - 1] * num_freqs /
304 (0.5f * sample_rate_hz_))); 291 (0.5f * sample_rate_hz_)));
305 rrr = min(num_freqs, max(rrr, kOne)) - 1; 292 rrr = std::min(num_freqs, std::max(rrr, kOne)) - 1;
306 rr = min(num_freqs, max(rr, kOne)) - 1; 293 rr = std::min(num_freqs, std::max(rr, kOne)) - 1;
307 294
308 float step, element; 295 float step, element;
309 296
310 step = 1.0f / (ll - lll); 297 step = 1.0f / (ll - lll);
311 element = 0.0f; 298 element = 0.0f;
312 for (size_t j = lll; j <= ll; ++j) { 299 for (size_t j = lll; j <= ll; ++j) {
313 filter_bank[i - 1][j] = element; 300 filter_bank[i - 1][j] = element;
314 element += step; 301 element += step;
315 } 302 }
316 step = 1.0f / (rrr - rr); 303 step = 1.0f / (rrr - rr);
(...skipping 17 matching lines...) Expand all
334 filter_bank[j][i] /= sum; 321 filter_bank[j][i] /= sum;
335 } 322 }
336 } 323 }
337 return filter_bank; 324 return filter_bank;
338 } 325 }
339 326
340 void IntelligibilityEnhancer::SolveForGainsGivenLambda(float lambda, 327 void IntelligibilityEnhancer::SolveForGainsGivenLambda(float lambda,
341 size_t start_freq, 328 size_t start_freq,
342 float* sols) { 329 float* sols) {
343 bool quadratic = (kConfigRho < 1.0f); 330 bool quadratic = (kConfigRho < 1.0f);
344 const float* var_x0 = filtered_clear_var_.get(); 331 const float* pow_x0 = filtered_clear_pow_.get();
345 const float* var_n0 = filtered_noise_var_.get(); 332 const float* pow_n0 = filtered_noise_pow_.get();
346 333
347 for (size_t n = 0; n < start_freq; ++n) { 334 for (size_t n = 0; n < start_freq; ++n) {
348 sols[n] = 1.0f; 335 sols[n] = 1.0f;
349 } 336 }
350 337
351 // Analytic solution for optimal gains. See paper for derivation. 338 // Analytic solution for optimal gains. See paper for derivation.
352 for (size_t n = start_freq - 1; n < bank_size_; ++n) { 339 for (size_t n = start_freq - 1; n < bank_size_; ++n) {
353 float alpha0, beta0, gamma0; 340 float alpha0, beta0, gamma0;
354 gamma0 = 0.5f * rho_[n] * var_x0[n] * var_n0[n] + 341 gamma0 = 0.5f * rho_[n] * pow_x0[n] * pow_n0[n] +
355 lambda * var_x0[n] * var_n0[n] * var_n0[n]; 342 lambda * pow_x0[n] * pow_n0[n] * pow_n0[n];
356 beta0 = lambda * var_x0[n] * (2 - rho_[n]) * var_x0[n] * var_n0[n]; 343 beta0 = lambda * pow_x0[n] * (2 - rho_[n]) * pow_x0[n] * pow_n0[n];
357 if (quadratic) { 344 if (quadratic) {
358 alpha0 = lambda * var_x0[n] * (1 - rho_[n]) * var_x0[n] * var_x0[n]; 345 alpha0 = lambda * pow_x0[n] * (1 - rho_[n]) * pow_x0[n] * pow_x0[n];
359 sols[n] = 346 sols[n] =
360 (-beta0 - sqrtf(beta0 * beta0 - 4 * alpha0 * gamma0)) / (2 * alpha0); 347 (-beta0 - sqrtf(beta0 * beta0 - 4 * alpha0 * gamma0)) / (2 * alpha0);
361 } else { 348 } else {
362 sols[n] = -gamma0 / beta0; 349 sols[n] = -gamma0 / beta0;
363 } 350 }
364 sols[n] = fmax(0, sols[n]); 351 sols[n] = fmax(0, sols[n]);
365 } 352 }
366 } 353 }
367 354
368 bool IntelligibilityEnhancer::active() const { 355 bool IntelligibilityEnhancer::active() const {
369 return active_; 356 return active_;
370 } 357 }
371 358
372 } // namespace webrtc 359 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698