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