ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2014 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 | #include "webrtc/modules/audio_processing/intelligibility/intelligibility_utils.h" |
| 12 | |
| 13 | #include <algorithm> |
| 14 | #include <cmath> |
| 15 | #include <cstring> |
| 16 | |
| 17 | using std::complex; |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | // Return |current| changed towards |target|, with the change being at most |
| 22 | // |limit|. |
| 23 | inline float UpdateFactor(float target, float current, float limit) { |
| 24 | float delta = fabsf(target - current); |
| 25 | float sign = copysign(1.0f, target - current); |
| 26 | return current + sign * fminf(delta, limit); |
| 27 | } |
| 28 | |
| 29 | // std::isfinite for complex numbers. |
| 30 | inline bool cplxfinite(complex<float> c) { |
| 31 | return std::isfinite(c.real()) && std::isfinite(c.imag()); |
| 32 | } |
| 33 | |
| 34 | // std::isnormal for complex numbers. |
| 35 | inline bool cplxnormal(complex<float> c) { |
| 36 | return std::isnormal(c.real()) && std::isnormal(c.imag()); |
| 37 | } |
| 38 | |
| 39 | // Apply a small fudge to degenerate complex values. The numbers in the array |
| 40 | // were chosen randomly, so that even a series of all zeroes has some small |
| 41 | // variability. |
| 42 | inline complex<float> zerofudge(complex<float> c) { |
| 43 | const static complex<float> fudge[7] = { |
| 44 | {0.001f, 0.002f}, {0.008f, 0.001f}, {0.003f, 0.008f}, {0.0006f, 0.0009f}, |
| 45 | {0.001f, 0.004f}, {0.003f, 0.004f}, {0.002f, 0.009f} |
| 46 | }; |
| 47 | static int fudge_index = 0; |
| 48 | if (cplxfinite(c) && !cplxnormal(c)) { |
| 49 | fudge_index = (fudge_index + 1) % 7; |
| 50 | return c + fudge[fudge_index]; |
| 51 | } |
| 52 | return c; |
| 53 | } |
| 54 | |
| 55 | // Incremental mean computation. Return the mean of the series with the |
| 56 | // mean |mean| with added |data|. |
| 57 | inline complex<float> NewMean(complex<float> mean, complex<float> data, |
| 58 | int count) { |
| 59 | return mean + (data - mean) / static_cast<float>(count); |
| 60 | } |
| 61 | |
| 62 | inline void AddToMean(complex<float> data, int count, complex<float>* mean) { |
| 63 | (*mean) = NewMean(*mean, data, count); |
| 64 | } |
| 65 | |
| 66 | } // namespace |
| 67 | |
| 68 | using std::min; |
| 69 | |
| 70 | namespace webrtc { |
| 71 | |
| 72 | namespace intelligibility { |
| 73 | |
| 74 | static const int kWindowBlockSize = 10; |
| 75 | |
| 76 | VarianceArray::VarianceArray(int freqs, StepType type, int window_size, |
| 77 | float decay) |
| 78 | : running_mean_(new complex<float>[freqs]()), |
| 79 | running_mean_sq_(new complex<float>[freqs]()), |
| 80 | sub_running_mean_(new complex<float>[freqs]()), |
| 81 | sub_running_mean_sq_(new complex<float>[freqs]()), |
| 82 | variance_(new float[freqs]()), |
| 83 | conj_sum_(new float[freqs]()), |
| 84 | freqs_(freqs), |
| 85 | window_size_(window_size), |
| 86 | decay_(decay), |
| 87 | history_cursor_(0), |
| 88 | count_(0), |
| 89 | array_mean_(0.0f) { |
| 90 | history_.reset(new scoped_ptr<complex<float>[]>[freqs_]()); |
| 91 | for (int i = 0; i < freqs_; ++i) { |
| 92 | history_[i].reset(new complex<float>[window_size_]()); |
| 93 | } |
| 94 | subhistory_.reset(new scoped_ptr<complex<float>[]>[freqs_]()); |
| 95 | for (int i = 0; i < freqs_; ++i) { |
| 96 | subhistory_[i].reset(new complex<float>[window_size_]()); |
| 97 | } |
| 98 | subhistory_sq_.reset(new scoped_ptr<complex<float>[]>[freqs_]()); |
| 99 | for (int i = 0; i < freqs_; ++i) { |
| 100 | subhistory_sq_[i].reset(new complex<float>[window_size_]()); |
| 101 | } |
| 102 | switch (type) { |
| 103 | case kStepInfinite: |
| 104 | step_func_ = &VarianceArray::InfiniteStep; |
| 105 | break; |
| 106 | case kStepDecaying: |
| 107 | step_func_ = &VarianceArray::DecayStep; |
| 108 | break; |
| 109 | case kStepWindowed: |
| 110 | step_func_ = &VarianceArray::WindowedStep; |
| 111 | break; |
| 112 | case kStepBlocked: |
| 113 | step_func_ = &VarianceArray::BlockedStep; |
| 114 | break; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // Compute the variance with Welford's algorithm, adding some fudge to |
| 119 | // the input in case of all-zeroes. |
| 120 | void VarianceArray::InfiniteStep(const complex<float>* data, bool skip_fudge) { |
| 121 | array_mean_ = 0.0f; |
| 122 | ++count_; |
| 123 | for (int i = 0; i < freqs_; ++i) { |
| 124 | complex<float> sample = data[i]; |
| 125 | if (!skip_fudge) { |
| 126 | sample = zerofudge(sample); |
| 127 | } |
| 128 | if (count_ == 1) { |
| 129 | running_mean_[i] = sample; |
| 130 | variance_[i] = 0.0f; |
| 131 | } else { |
| 132 | float old_sum = conj_sum_[i]; |
| 133 | complex<float> old_mean = running_mean_[i]; |
| 134 | running_mean_[i] = old_mean + (sample - old_mean) / |
| 135 | static_cast<float>(count_); |
| 136 | conj_sum_[i] = (old_sum + std::conj(sample - old_mean) * |
| 137 | (sample - running_mean_[i])).real(); |
| 138 | variance_[i] = conj_sum_[i] / (count_ - 1); // + fudge[fudge_index].real(); |
| 139 | if (skip_fudge && false) { |
| 140 | //variance_[i] -= fudge[fudge_index].real(); |
| 141 | } |
| 142 | } |
| 143 | array_mean_ += (variance_[i] - array_mean_) / (i + 1); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // Compute the variance from the beginning, with exponential decaying of the |
| 148 | // series data. |
| 149 | void VarianceArray::DecayStep(const complex<float>* data, bool /*dummy*/) { |
| 150 | array_mean_ = 0.0f; |
| 151 | ++count_; |
| 152 | for (int i = 0; i < freqs_; ++i) { |
| 153 | complex<float> sample = data[i]; |
| 154 | sample = zerofudge(sample); |
| 155 | |
| 156 | if (count_ == 1) { |
| 157 | running_mean_[i] = sample; |
| 158 | running_mean_sq_[i] = sample * std::conj(sample); |
| 159 | variance_[i] = 0.0f; |
| 160 | } else { |
| 161 | complex<float> prev = running_mean_[i]; |
| 162 | complex<float> prev2 = running_mean_sq_[i]; |
| 163 | running_mean_[i] = decay_ * prev + (1.0f - decay_) * sample; |
| 164 | running_mean_sq_[i] = decay_ * prev2 + |
| 165 | (1.0f - decay_) * sample * std::conj(sample); |
| 166 | //variance_[i] = decay_ * variance_[i] + (1.0f - decay_) * ( |
| 167 | // (sample - running_mean_[i]) * std::conj(sample - running_mean_[i])).real(); |
| 168 | variance_[i] = (running_mean_sq_[i] - running_mean_[i] * std::conj(running_mean_[i])).real(); |
| 169 | } |
| 170 | |
| 171 | array_mean_ += (variance_[i] - array_mean_) / (i + 1); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // Windowed variance computation. On each step, the variances for the |
| 176 | // window are recomputed from scratch, using Welford's algorithm. |
| 177 | void VarianceArray::WindowedStep(const complex<float>* data, bool /*dummy*/) { |
| 178 | int num = min(count_ + 1, window_size_); |
| 179 | array_mean_ = 0.0f; |
| 180 | for (int i = 0; i < freqs_; ++i) { |
| 181 | complex<float> mean; |
| 182 | float conj_sum = 0.0f; |
| 183 | |
| 184 | history_[i][history_cursor_] = data[i]; |
| 185 | |
| 186 | mean = history_[i][history_cursor_]; |
| 187 | variance_[i] = 0.0f; |
| 188 | for (int j = 1; j < num; ++j) { |
| 189 | complex<float> sample = zerofudge( |
| 190 | history_[i][(history_cursor_ + j) % window_size_]); |
| 191 | sample = history_[i][(history_cursor_ + j) % window_size_]; |
| 192 | float old_sum = conj_sum; |
| 193 | complex<float> old_mean = mean; |
| 194 | |
| 195 | mean = old_mean + (sample - old_mean) / static_cast<float>(j + 1); |
| 196 | conj_sum = (old_sum + std::conj(sample - old_mean) * |
| 197 | (sample - mean)).real(); |
| 198 | variance_[i] = conj_sum / (j); |
| 199 | } |
| 200 | array_mean_ += (variance_[i] - array_mean_) / (i + 1); |
| 201 | } |
| 202 | history_cursor_ = (history_cursor_ + 1) % window_size_; |
| 203 | ++count_; |
| 204 | } |
| 205 | |
| 206 | // Variance with a window of blocks. Within each block, the variances are |
| 207 | // recomputed from scratch at every stp, using |Var(X) = E(X^2) - E^2(X)|. |
| 208 | // Once a block is filled with kWindowBlockSize samples, it is added to the |
| 209 | // history window and a new block is started. The variances for the window |
| 210 | // are recomputed from scratch at each of these transitions. |
| 211 | void VarianceArray::BlockedStep(const complex<float>* data, bool /*dummy*/) { |
| 212 | int blocks = min(window_size_, history_cursor_); |
| 213 | for (int i = 0; i < freqs_; ++i) { |
| 214 | AddToMean(data[i], count_ + 1, &sub_running_mean_[i]); |
| 215 | AddToMean(data[i] * std::conj(data[i]), count_ + 1, |
| 216 | &sub_running_mean_sq_[i]); |
| 217 | subhistory_[i][history_cursor_ % window_size_] = sub_running_mean_[i]; |
| 218 | subhistory_sq_[i][history_cursor_ % window_size_] = sub_running_mean_sq_[i]; |
| 219 | |
| 220 | variance_[i] = (NewMean(running_mean_sq_[i], sub_running_mean_sq_[i], |
| 221 | blocks) - |
| 222 | NewMean(running_mean_[i], sub_running_mean_[i], blocks) * |
| 223 | std::conj(NewMean(running_mean_[i], sub_running_mean_[i], |
| 224 | blocks))).real(); |
| 225 | if (count_ == kWindowBlockSize - 1) { |
| 226 | sub_running_mean_[i] = complex<float>(0.0f, 0.0f); |
| 227 | sub_running_mean_sq_[i] = complex<float>(0.0f, 0.0f); |
| 228 | running_mean_[i] = complex<float>(0.0f, 0.0f); |
| 229 | running_mean_sq_[i] = complex<float>(0.0f, 0.0f); |
| 230 | for (int j = 0; j < min(window_size_, history_cursor_); ++j) { |
| 231 | AddToMean(subhistory_[i][j], j, &running_mean_[i]); |
| 232 | AddToMean(subhistory_sq_[i][j], j, &running_mean_sq_[i]); |
| 233 | } |
| 234 | ++history_cursor_; |
| 235 | } |
| 236 | } |
| 237 | ++count_; |
| 238 | if (count_ == kWindowBlockSize) { |
| 239 | count_ = 0; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | void VarianceArray::Clear() { |
| 244 | memset(running_mean_.get(), 0, sizeof(*running_mean_.get()) * freqs_); |
| 245 | memset(running_mean_sq_.get(), 0, sizeof(*running_mean_sq_.get()) * freqs_); |
| 246 | memset(variance_.get(), 0, sizeof(*variance_.get()) * freqs_); |
| 247 | memset(conj_sum_.get(), 0, sizeof(*conj_sum_.get()) * freqs_); |
| 248 | history_cursor_ = 0; |
| 249 | count_ = 0; |
| 250 | array_mean_ = 0.0f; |
| 251 | } |
| 252 | |
| 253 | void VarianceArray::ApplyScale(float scale) { |
| 254 | array_mean_ = 0.0f; |
| 255 | for (int i = 0; i < freqs_; ++i) { |
| 256 | variance_[i] *= scale * scale; |
| 257 | array_mean_ += (variance_[i] - array_mean_) / (i + 1); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | GainApplier::GainApplier(int freqs, float change_limit) |
| 262 | : freqs_(freqs), |
| 263 | change_limit_(change_limit), |
| 264 | target_(new float[freqs]()), |
| 265 | current_(new float[freqs]()) { |
| 266 | for (int i = 0; i < freqs; ++i) { |
| 267 | target_[i] = 1.0f; |
| 268 | current_[i] = 1.0f; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | void GainApplier::Apply(const complex<float>* in_block, |
| 273 | complex<float>* out_block) { |
| 274 | for (int i = 0; i < freqs_; ++i) { |
| 275 | float factor = sqrtf(fabsf(current_[i])); |
| 276 | if (!std::isnormal(factor)) { |
| 277 | factor = 1.0f; |
| 278 | } |
| 279 | out_block[i] = factor * in_block[i]; |
| 280 | current_[i] = UpdateFactor(target_[i], current_[i], change_limit_); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | } // namespace intelligibility |
| 285 | |
| 286 | } // namespace webrtc |
| 287 | |