| OLD | NEW |
| 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 // | 11 // |
| 12 // Implements core class for intelligibility enhancer. | 12 // Implements core class for intelligibility enhancer. |
| 13 // | 13 // |
| 14 // Details of the model and algorithm can be found in the original paper: | 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 | 15 // http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=6882788 |
| 16 // | 16 // |
| 17 | 17 |
| 18 #include "webrtc/modules/audio_processing/intelligibility/intelligibility_enhanc
er.h" | 18 #include "webrtc/modules/audio_processing/intelligibility/intelligibility_enhanc
er.h" |
| 19 | 19 |
| 20 #include <math.h> | 20 #include <math.h> |
| 21 #include <stdlib.h> | 21 #include <stdlib.h> |
| 22 #include <algorithm> | 22 #include <algorithm> |
| 23 #include <limits> |
| 23 #include <numeric> | 24 #include <numeric> |
| 24 | 25 |
| 25 #include "webrtc/base/checks.h" | 26 #include "webrtc/base/checks.h" |
| 26 #include "webrtc/common_audio/include/audio_util.h" | 27 #include "webrtc/common_audio/include/audio_util.h" |
| 27 #include "webrtc/common_audio/window_generator.h" | 28 #include "webrtc/common_audio/window_generator.h" |
| 28 | 29 |
| 29 namespace webrtc { | 30 namespace webrtc { |
| 30 | 31 |
| 31 namespace { | 32 namespace { |
| 32 | 33 |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 UpdateErbGains(); | 218 UpdateErbGains(); |
| 218 } // Else experiencing variance underflow, so do nothing. | 219 } // Else experiencing variance underflow, so do nothing. |
| 219 } | 220 } |
| 220 | 221 |
| 221 void IntelligibilityEnhancer::SolveForLambda(float power_target, | 222 void IntelligibilityEnhancer::SolveForLambda(float power_target, |
| 222 float power_bot, | 223 float power_bot, |
| 223 float power_top) { | 224 float power_top) { |
| 224 const float kConvergeThresh = 0.001f; // TODO(ekmeyerson): Find best values | 225 const float kConvergeThresh = 0.001f; // TODO(ekmeyerson): Find best values |
| 225 const int kMaxIters = 100; // for these, based on experiments. | 226 const int kMaxIters = 100; // for these, based on experiments. |
| 226 | 227 |
| 227 const float reciprocal_power_target = 1.f / power_target; | 228 const float reciprocal_power_target = |
| 229 1.f / (power_target + std::numeric_limits<float>::epsilon()); |
| 228 float lambda_bot = kLambdaBot; | 230 float lambda_bot = kLambdaBot; |
| 229 float lambda_top = kLambdaTop; | 231 float lambda_top = kLambdaTop; |
| 230 float power_ratio = 2.0f; // Ratio of achieved power to target power. | 232 float power_ratio = 2.0f; // Ratio of achieved power to target power. |
| 231 int iters = 0; | 233 int iters = 0; |
| 232 while (std::fabs(power_ratio - 1.0f) > kConvergeThresh && | 234 while (std::fabs(power_ratio - 1.0f) > kConvergeThresh && |
| 233 iters <= kMaxIters) { | 235 iters <= kMaxIters) { |
| 234 const float lambda = lambda_bot + (lambda_top - lambda_bot) / 2.0f; | 236 const float lambda = lambda_bot + (lambda_top - lambda_bot) / 2.0f; |
| 235 SolveForGainsGivenLambda(lambda, start_freq_, gains_eq_.get()); | 237 SolveForGainsGivenLambda(lambda, start_freq_, gains_eq_.get()); |
| 236 const float power = | 238 const float power = |
| 237 DotProduct(gains_eq_.get(), filtered_clear_var_.get(), bank_size_); | 239 DotProduct(gains_eq_.get(), filtered_clear_var_.get(), bank_size_); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 center_freqs_[min(bank_size_, i + rf) - 1] * num_freqs / | 301 center_freqs_[min(bank_size_, i + rf) - 1] * num_freqs / |
| 300 (0.5f * sample_rate_hz_))); | 302 (0.5f * sample_rate_hz_))); |
| 301 rr = static_cast<size_t>(round( | 303 rr = static_cast<size_t>(round( |
| 302 center_freqs_[min(bank_size_, i + 1) - 1] * num_freqs / | 304 center_freqs_[min(bank_size_, i + 1) - 1] * num_freqs / |
| 303 (0.5f * sample_rate_hz_))); | 305 (0.5f * sample_rate_hz_))); |
| 304 rrr = min(num_freqs, max(rrr, kOne)) - 1; | 306 rrr = min(num_freqs, max(rrr, kOne)) - 1; |
| 305 rr = min(num_freqs, max(rr, kOne)) - 1; | 307 rr = min(num_freqs, max(rr, kOne)) - 1; |
| 306 | 308 |
| 307 float step, element; | 309 float step, element; |
| 308 | 310 |
| 309 step = 1.0f / (ll - lll); | 311 step = ll == lll ? 0.f : 1.f / (ll - lll); |
| 310 element = 0.0f; | 312 element = 0.0f; |
| 311 for (size_t j = lll; j <= ll; ++j) { | 313 for (size_t j = lll; j <= ll; ++j) { |
| 312 filter_bank[i - 1][j] = element; | 314 filter_bank[i - 1][j] = element; |
| 313 element += step; | 315 element += step; |
| 314 } | 316 } |
| 315 step = 1.0f / (rrr - rr); | 317 step = rr == rrr ? 0.f : 1.f / (rrr - rr); |
| 316 element = 1.0f; | 318 element = 1.0f; |
| 317 for (size_t j = rr; j <= rrr; ++j) { | 319 for (size_t j = rr; j <= rrr; ++j) { |
| 318 filter_bank[i - 1][j] = element; | 320 filter_bank[i - 1][j] = element; |
| 319 element -= step; | 321 element -= step; |
| 320 } | 322 } |
| 321 for (size_t j = ll; j <= rr; ++j) { | 323 for (size_t j = ll; j <= rr; ++j) { |
| 322 filter_bank[i - 1][j] = 1.0f; | 324 filter_bank[i - 1][j] = 1.0f; |
| 323 } | 325 } |
| 324 } | 326 } |
| 325 | 327 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 349 | 351 |
| 350 // Analytic solution for optimal gains. See paper for derivation. | 352 // Analytic solution for optimal gains. See paper for derivation. |
| 351 for (size_t n = start_freq - 1; n < bank_size_; ++n) { | 353 for (size_t n = start_freq - 1; n < bank_size_; ++n) { |
| 352 float alpha0, beta0, gamma0; | 354 float alpha0, beta0, gamma0; |
| 353 gamma0 = 0.5f * rho_[n] * var_x0[n] * var_n0[n] + | 355 gamma0 = 0.5f * rho_[n] * var_x0[n] * var_n0[n] + |
| 354 lambda * var_x0[n] * var_n0[n] * var_n0[n]; | 356 lambda * var_x0[n] * var_n0[n] * var_n0[n]; |
| 355 beta0 = lambda * var_x0[n] * (2 - rho_[n]) * var_x0[n] * var_n0[n]; | 357 beta0 = lambda * var_x0[n] * (2 - rho_[n]) * var_x0[n] * var_n0[n]; |
| 356 if (quadratic) { | 358 if (quadratic) { |
| 357 alpha0 = lambda * var_x0[n] * (1 - rho_[n]) * var_x0[n] * var_x0[n]; | 359 alpha0 = lambda * var_x0[n] * (1 - rho_[n]) * var_x0[n] * var_x0[n]; |
| 358 sols[n] = | 360 sols[n] = |
| 359 (-beta0 - sqrtf(beta0 * beta0 - 4 * alpha0 * gamma0)) / (2 * alpha0); | 361 (-beta0 - sqrtf(beta0 * beta0 - 4 * alpha0 * gamma0)) / |
| 362 (2 * alpha0 + std::numeric_limits<float>::epsilon()); |
| 360 } else { | 363 } else { |
| 361 sols[n] = -gamma0 / beta0; | 364 sols[n] = -gamma0 / beta0; |
| 362 } | 365 } |
| 363 sols[n] = fmax(0, sols[n]); | 366 sols[n] = fmax(0, sols[n]); |
| 364 } | 367 } |
| 365 } | 368 } |
| 366 | 369 |
| 367 bool IntelligibilityEnhancer::active() const { | 370 bool IntelligibilityEnhancer::active() const { |
| 368 return active_; | 371 return active_; |
| 369 } | 372 } |
| 370 | 373 |
| 371 } // namespace webrtc | 374 } // namespace webrtc |
| OLD | NEW |