| 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 helper functions and classes for intelligibility enhancement. | 12 // Implements helper functions and classes for intelligibility enhancement. |
| 13 // | 13 // |
| 14 | 14 |
| 15 #include "webrtc/modules/audio_processing/intelligibility/intelligibility_utils.
h" | 15 #include "webrtc/modules/audio_processing/intelligibility/intelligibility_utils.
h" |
| 16 | 16 |
| 17 #include <math.h> | 17 #include <math.h> |
| 18 #include <stdlib.h> |
| 18 #include <string.h> | 19 #include <string.h> |
| 19 #include <algorithm> | 20 #include <algorithm> |
| 20 | 21 |
| 21 using std::complex; | 22 using std::complex; |
| 22 using std::min; | 23 using std::min; |
| 23 | 24 |
| 24 namespace webrtc { | 25 namespace webrtc { |
| 25 | 26 |
| 26 namespace intelligibility { | 27 namespace intelligibility { |
| 27 | 28 |
| 28 float UpdateFactor(float target, float current, float limit) { | 29 float UpdateFactor(float target, float current, float limit) { |
| 29 float delta = fabsf(target - current); | 30 float delta = fabsf(target - current); |
| 30 float sign = copysign(1.0f, target - current); | 31 float sign = copysign(1.0f, target - current); |
| 31 return current + sign * fminf(delta, limit); | 32 return current + sign * fminf(delta, limit); |
| 32 } | 33 } |
| 33 | 34 |
| 34 bool cplxfinite(complex<float> c) { | 35 float AddDitherIfZero(float value) { |
| 35 return std::isfinite(c.real()) && std::isfinite(c.imag()); | 36 return value == 0.f ? std::rand() * 0.01f / RAND_MAX : value; |
| 36 } | |
| 37 | |
| 38 bool cplxnormal(complex<float> c) { | |
| 39 return std::isnormal(c.real()) && std::isnormal(c.imag()); | |
| 40 } | 37 } |
| 41 | 38 |
| 42 complex<float> zerofudge(complex<float> c) { | 39 complex<float> zerofudge(complex<float> c) { |
| 43 const static complex<float> fudge[7] = {{0.001f, 0.002f}, | 40 return complex<float>(AddDitherIfZero(c.real()), AddDitherIfZero(c.imag())); |
| 44 {0.008f, 0.001f}, | |
| 45 {0.003f, 0.008f}, | |
| 46 {0.0006f, 0.0009f}, | |
| 47 {0.001f, 0.004f}, | |
| 48 {0.003f, 0.004f}, | |
| 49 {0.002f, 0.009f}}; | |
| 50 static int fudge_index = 0; | |
| 51 if (cplxfinite(c) && !cplxnormal(c)) { | |
| 52 fudge_index = (fudge_index + 1) % 7; | |
| 53 return c + fudge[fudge_index]; | |
| 54 } | |
| 55 return c; | |
| 56 } | 41 } |
| 57 | 42 |
| 58 complex<float> NewMean(complex<float> mean, complex<float> data, int count) { | 43 complex<float> NewMean(complex<float> mean, complex<float> data, int count) { |
| 59 return mean + (data - mean) / static_cast<float>(count); | 44 return mean + (data - mean) / static_cast<float>(count); |
| 60 } | 45 } |
| 61 | 46 |
| 62 void AddToMean(complex<float> data, int count, complex<float>* mean) { | 47 void AddToMean(complex<float> data, int count, complex<float>* mean) { |
| 63 (*mean) = NewMean(*mean, data, count); | 48 (*mean) = NewMean(*mean, data, count); |
| 64 } | 49 } |
| 65 | 50 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 variance_[i] = 0.0f; | 114 variance_[i] = 0.0f; |
| 130 } else { | 115 } else { |
| 131 float old_sum = conj_sum_[i]; | 116 float old_sum = conj_sum_[i]; |
| 132 complex<float> old_mean = running_mean_[i]; | 117 complex<float> old_mean = running_mean_[i]; |
| 133 running_mean_[i] = | 118 running_mean_[i] = |
| 134 old_mean + (sample - old_mean) / static_cast<float>(count_); | 119 old_mean + (sample - old_mean) / static_cast<float>(count_); |
| 135 conj_sum_[i] = | 120 conj_sum_[i] = |
| 136 (old_sum + std::conj(sample - old_mean) * (sample - running_mean_[i])) | 121 (old_sum + std::conj(sample - old_mean) * (sample - running_mean_[i])) |
| 137 .real(); | 122 .real(); |
| 138 variance_[i] = | 123 variance_[i] = |
| 139 conj_sum_[i] / (count_ - 1); // + fudge[fudge_index].real(); | 124 conj_sum_[i] / (count_ - 1); |
| 140 // if (skip_fudge) { | |
| 141 // variance_[i] -= fudge[fudge_index].real(); | |
| 142 // } | |
| 143 } | 125 } |
| 144 array_mean_ += (variance_[i] - array_mean_) / (i + 1); | 126 array_mean_ += (variance_[i] - array_mean_) / (i + 1); |
| 145 } | 127 } |
| 146 } | 128 } |
| 147 | 129 |
| 148 // Compute the variance from the beginning, with exponential decaying of the | 130 // Compute the variance from the beginning, with exponential decaying of the |
| 149 // series data. | 131 // series data. |
| 150 void VarianceArray::DecayStep(const complex<float>* data, bool /*dummy*/) { | 132 void VarianceArray::DecayStep(const complex<float>* data, bool /*dummy*/) { |
| 151 array_mean_ = 0.0f; | 133 array_mean_ = 0.0f; |
| 152 ++count_; | 134 ++count_; |
| 153 for (int i = 0; i < freqs_; ++i) { | 135 for (int i = 0; i < freqs_; ++i) { |
| 154 complex<float> sample = data[i]; | 136 complex<float> sample = data[i]; |
| 155 sample = zerofudge(sample); | 137 sample = zerofudge(sample); |
| 156 | 138 |
| 157 if (count_ == 1) { | 139 if (count_ == 1) { |
| 158 running_mean_[i] = sample; | 140 running_mean_[i] = sample; |
| 159 running_mean_sq_[i] = sample * std::conj(sample); | 141 running_mean_sq_[i] = sample * std::conj(sample); |
| 160 variance_[i] = 0.0f; | 142 variance_[i] = 0.0f; |
| 161 } else { | 143 } else { |
| 162 complex<float> prev = running_mean_[i]; | 144 complex<float> prev = running_mean_[i]; |
| 163 complex<float> prev2 = running_mean_sq_[i]; | 145 complex<float> prev2 = running_mean_sq_[i]; |
| 164 running_mean_[i] = decay_ * prev + (1.0f - decay_) * sample; | 146 running_mean_[i] = decay_ * prev + (1.0f - decay_) * sample; |
| 165 running_mean_sq_[i] = | 147 running_mean_sq_[i] = |
| 166 decay_ * prev2 + (1.0f - decay_) * sample * std::conj(sample); | 148 decay_ * prev2 + (1.0f - decay_) * sample * std::conj(sample); |
| 167 // variance_[i] = decay_ * variance_[i] + (1.0f - decay_) * ( | |
| 168 // (sample - running_mean_[i]) * std::conj(sample - | |
| 169 // running_mean_[i])).real(); | |
| 170 variance_[i] = (running_mean_sq_[i] - | 149 variance_[i] = (running_mean_sq_[i] - |
| 171 running_mean_[i] * std::conj(running_mean_[i])).real(); | 150 running_mean_[i] * std::conj(running_mean_[i])).real(); |
| 172 } | 151 } |
| 173 | 152 |
| 174 array_mean_ += (variance_[i] - array_mean_) / (i + 1); | 153 array_mean_ += (variance_[i] - array_mean_) / (i + 1); |
| 175 } | 154 } |
| 176 } | 155 } |
| 177 | 156 |
| 178 // Windowed variance computation. On each step, the variances for the | 157 // Windowed variance computation. On each step, the variances for the |
| 179 // window are recomputed from scratch, using Welford's algorithm. | 158 // window are recomputed from scratch, using Welford's algorithm. |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 factor = 1.0f; | 304 factor = 1.0f; |
| 326 } | 305 } |
| 327 out_block[i] = factor * in_block[i]; | 306 out_block[i] = factor * in_block[i]; |
| 328 current_[i] = UpdateFactor(target_[i], current_[i], change_limit_); | 307 current_[i] = UpdateFactor(target_[i], current_[i], change_limit_); |
| 329 } | 308 } |
| 330 } | 309 } |
| 331 | 310 |
| 332 } // namespace intelligibility | 311 } // namespace intelligibility |
| 333 | 312 |
| 334 } // namespace webrtc | 313 } // namespace webrtc |
| OLD | NEW |