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 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 fudge_index = (fudge_index + 1) % 7; | 56 fudge_index = (fudge_index + 1) % 7; |
57 return c + fudge[fudge_index]; | 57 return c + fudge[fudge_index]; |
58 } | 58 } |
59 return c; | 59 return c; |
60 } | 60 } |
61 | 61 |
62 // Incremental mean computation. Return the mean of the series with the | 62 // Incremental mean computation. Return the mean of the series with the |
63 // mean |mean| with added |data|. | 63 // mean |mean| with added |data|. |
64 inline complex<float> NewMean(complex<float> mean, | 64 inline complex<float> NewMean(complex<float> mean, |
65 complex<float> data, | 65 complex<float> data, |
66 int count) { | 66 size_t count) { |
67 return mean + (data - mean) / static_cast<float>(count); | 67 return mean + (data - mean) / static_cast<float>(count); |
68 } | 68 } |
69 | 69 |
70 inline void AddToMean(complex<float> data, int count, complex<float>* mean) { | 70 inline void AddToMean(complex<float> data, size_t count, complex<float>* mean) { |
71 (*mean) = NewMean(*mean, data, count); | 71 (*mean) = NewMean(*mean, data, count); |
72 } | 72 } |
73 | 73 |
74 } // namespace | 74 } // namespace |
75 | 75 |
76 using std::min; | 76 using std::min; |
77 | 77 |
78 namespace webrtc { | 78 namespace webrtc { |
79 | 79 |
80 namespace intelligibility { | 80 namespace intelligibility { |
81 | 81 |
82 static const int kWindowBlockSize = 10; | 82 static const size_t kWindowBlockSize = 10; |
83 | 83 |
84 VarianceArray::VarianceArray(int freqs, | 84 VarianceArray::VarianceArray(size_t freqs, |
85 StepType type, | 85 StepType type, |
86 int window_size, | 86 size_t window_size, |
87 float decay) | 87 float decay) |
88 : running_mean_(new complex<float>[freqs]()), | 88 : running_mean_(new complex<float>[freqs]()), |
89 running_mean_sq_(new complex<float>[freqs]()), | 89 running_mean_sq_(new complex<float>[freqs]()), |
90 sub_running_mean_(new complex<float>[freqs]()), | 90 sub_running_mean_(new complex<float>[freqs]()), |
91 sub_running_mean_sq_(new complex<float>[freqs]()), | 91 sub_running_mean_sq_(new complex<float>[freqs]()), |
92 variance_(new float[freqs]()), | 92 variance_(new float[freqs]()), |
93 conj_sum_(new float[freqs]()), | 93 conj_sum_(new float[freqs]()), |
94 freqs_(freqs), | 94 freqs_(freqs), |
95 window_size_(window_size), | 95 window_size_(window_size), |
96 decay_(decay), | 96 decay_(decay), |
97 history_cursor_(0), | 97 history_cursor_(0), |
98 count_(0), | 98 count_(0), |
99 array_mean_(0.0f) { | 99 array_mean_(0.0f) { |
100 history_.reset(new rtc::scoped_ptr<complex<float>[]>[freqs_]()); | 100 history_.reset(new rtc::scoped_ptr<complex<float>[]>[freqs_]()); |
101 for (int i = 0; i < freqs_; ++i) { | 101 for (size_t i = 0; i < freqs_; ++i) { |
102 history_[i].reset(new complex<float>[window_size_]()); | 102 history_[i].reset(new complex<float>[window_size_]()); |
103 } | 103 } |
104 subhistory_.reset(new rtc::scoped_ptr<complex<float>[]>[freqs_]()); | 104 subhistory_.reset(new rtc::scoped_ptr<complex<float>[]>[freqs_]()); |
105 for (int i = 0; i < freqs_; ++i) { | 105 for (size_t i = 0; i < freqs_; ++i) { |
106 subhistory_[i].reset(new complex<float>[window_size_]()); | 106 subhistory_[i].reset(new complex<float>[window_size_]()); |
107 } | 107 } |
108 subhistory_sq_.reset(new rtc::scoped_ptr<complex<float>[]>[freqs_]()); | 108 subhistory_sq_.reset(new rtc::scoped_ptr<complex<float>[]>[freqs_]()); |
109 for (int i = 0; i < freqs_; ++i) { | 109 for (size_t i = 0; i < freqs_; ++i) { |
110 subhistory_sq_[i].reset(new complex<float>[window_size_]()); | 110 subhistory_sq_[i].reset(new complex<float>[window_size_]()); |
111 } | 111 } |
112 switch (type) { | 112 switch (type) { |
113 case kStepInfinite: | 113 case kStepInfinite: |
114 step_func_ = &VarianceArray::InfiniteStep; | 114 step_func_ = &VarianceArray::InfiniteStep; |
115 break; | 115 break; |
116 case kStepDecaying: | 116 case kStepDecaying: |
117 step_func_ = &VarianceArray::DecayStep; | 117 step_func_ = &VarianceArray::DecayStep; |
118 break; | 118 break; |
119 case kStepWindowed: | 119 case kStepWindowed: |
120 step_func_ = &VarianceArray::WindowedStep; | 120 step_func_ = &VarianceArray::WindowedStep; |
121 break; | 121 break; |
122 case kStepBlocked: | 122 case kStepBlocked: |
123 step_func_ = &VarianceArray::BlockedStep; | 123 step_func_ = &VarianceArray::BlockedStep; |
124 break; | 124 break; |
125 } | 125 } |
126 } | 126 } |
127 | 127 |
128 // Compute the variance with Welford's algorithm, adding some fudge to | 128 // Compute the variance with Welford's algorithm, adding some fudge to |
129 // the input in case of all-zeroes. | 129 // the input in case of all-zeroes. |
130 void VarianceArray::InfiniteStep(const complex<float>* data, bool skip_fudge) { | 130 void VarianceArray::InfiniteStep(const complex<float>* data, bool skip_fudge) { |
131 array_mean_ = 0.0f; | 131 array_mean_ = 0.0f; |
132 ++count_; | 132 ++count_; |
133 for (int i = 0; i < freqs_; ++i) { | 133 for (size_t i = 0; i < freqs_; ++i) { |
134 complex<float> sample = data[i]; | 134 complex<float> sample = data[i]; |
135 if (!skip_fudge) { | 135 if (!skip_fudge) { |
136 sample = zerofudge(sample); | 136 sample = zerofudge(sample); |
137 } | 137 } |
138 if (count_ == 1) { | 138 if (count_ == 1) { |
139 running_mean_[i] = sample; | 139 running_mean_[i] = sample; |
140 variance_[i] = 0.0f; | 140 variance_[i] = 0.0f; |
141 } else { | 141 } else { |
142 float old_sum = conj_sum_[i]; | 142 float old_sum = conj_sum_[i]; |
143 complex<float> old_mean = running_mean_[i]; | 143 complex<float> old_mean = running_mean_[i]; |
144 running_mean_[i] = | 144 running_mean_[i] = |
145 old_mean + (sample - old_mean) / static_cast<float>(count_); | 145 old_mean + (sample - old_mean) / static_cast<float>(count_); |
146 conj_sum_[i] = | 146 conj_sum_[i] = |
147 (old_sum + std::conj(sample - old_mean) * (sample - running_mean_[i])) | 147 (old_sum + std::conj(sample - old_mean) * (sample - running_mean_[i])) |
148 .real(); | 148 .real(); |
149 variance_[i] = | 149 variance_[i] = |
150 conj_sum_[i] / (count_ - 1); // + fudge[fudge_index].real(); | 150 conj_sum_[i] / (count_ - 1); // + fudge[fudge_index].real(); |
151 if (skip_fudge && false) { | 151 // if (skip_fudge) { |
152 // variance_[i] -= fudge[fudge_index].real(); | 152 // variance_[i] -= fudge[fudge_index].real(); |
153 } | 153 // } |
154 } | 154 } |
155 array_mean_ += (variance_[i] - array_mean_) / (i + 1); | 155 array_mean_ += (variance_[i] - array_mean_) / (i + 1); |
156 } | 156 } |
157 } | 157 } |
158 | 158 |
159 // Compute the variance from the beginning, with exponential decaying of the | 159 // Compute the variance from the beginning, with exponential decaying of the |
160 // series data. | 160 // series data. |
161 void VarianceArray::DecayStep(const complex<float>* data, bool /*dummy*/) { | 161 void VarianceArray::DecayStep(const complex<float>* data, bool /*dummy*/) { |
162 array_mean_ = 0.0f; | 162 array_mean_ = 0.0f; |
163 ++count_; | 163 ++count_; |
164 for (int i = 0; i < freqs_; ++i) { | 164 for (size_t i = 0; i < freqs_; ++i) { |
165 complex<float> sample = data[i]; | 165 complex<float> sample = data[i]; |
166 sample = zerofudge(sample); | 166 sample = zerofudge(sample); |
167 | 167 |
168 if (count_ == 1) { | 168 if (count_ == 1) { |
169 running_mean_[i] = sample; | 169 running_mean_[i] = sample; |
170 running_mean_sq_[i] = sample * std::conj(sample); | 170 running_mean_sq_[i] = sample * std::conj(sample); |
171 variance_[i] = 0.0f; | 171 variance_[i] = 0.0f; |
172 } else { | 172 } else { |
173 complex<float> prev = running_mean_[i]; | 173 complex<float> prev = running_mean_[i]; |
174 complex<float> prev2 = running_mean_sq_[i]; | 174 complex<float> prev2 = running_mean_sq_[i]; |
175 running_mean_[i] = decay_ * prev + (1.0f - decay_) * sample; | 175 running_mean_[i] = decay_ * prev + (1.0f - decay_) * sample; |
176 running_mean_sq_[i] = | 176 running_mean_sq_[i] = |
177 decay_ * prev2 + (1.0f - decay_) * sample * std::conj(sample); | 177 decay_ * prev2 + (1.0f - decay_) * sample * std::conj(sample); |
178 // variance_[i] = decay_ * variance_[i] + (1.0f - decay_) * ( | 178 // variance_[i] = decay_ * variance_[i] + (1.0f - decay_) * ( |
179 // (sample - running_mean_[i]) * std::conj(sample - | 179 // (sample - running_mean_[i]) * std::conj(sample - |
180 // running_mean_[i])).real(); | 180 // running_mean_[i])).real(); |
181 variance_[i] = (running_mean_sq_[i] - | 181 variance_[i] = (running_mean_sq_[i] - |
182 running_mean_[i] * std::conj(running_mean_[i])).real(); | 182 running_mean_[i] * std::conj(running_mean_[i])).real(); |
183 } | 183 } |
184 | 184 |
185 array_mean_ += (variance_[i] - array_mean_) / (i + 1); | 185 array_mean_ += (variance_[i] - array_mean_) / (i + 1); |
186 } | 186 } |
187 } | 187 } |
188 | 188 |
189 // Windowed variance computation. On each step, the variances for the | 189 // Windowed variance computation. On each step, the variances for the |
190 // window are recomputed from scratch, using Welford's algorithm. | 190 // window are recomputed from scratch, using Welford's algorithm. |
191 void VarianceArray::WindowedStep(const complex<float>* data, bool /*dummy*/) { | 191 void VarianceArray::WindowedStep(const complex<float>* data, bool /*dummy*/) { |
192 int num = min(count_ + 1, window_size_); | 192 size_t num = min(count_ + 1, window_size_); |
193 array_mean_ = 0.0f; | 193 array_mean_ = 0.0f; |
194 for (int i = 0; i < freqs_; ++i) { | 194 for (size_t i = 0; i < freqs_; ++i) { |
195 complex<float> mean; | 195 complex<float> mean; |
196 float conj_sum = 0.0f; | 196 float conj_sum = 0.0f; |
197 | 197 |
198 history_[i][history_cursor_] = data[i]; | 198 history_[i][history_cursor_] = data[i]; |
199 | 199 |
200 mean = history_[i][history_cursor_]; | 200 mean = history_[i][history_cursor_]; |
201 variance_[i] = 0.0f; | 201 variance_[i] = 0.0f; |
202 for (int j = 1; j < num; ++j) { | 202 for (size_t j = 1; j < num; ++j) { |
203 complex<float> sample = | 203 complex<float> sample = |
204 zerofudge(history_[i][(history_cursor_ + j) % window_size_]); | 204 zerofudge(history_[i][(history_cursor_ + j) % window_size_]); |
205 sample = history_[i][(history_cursor_ + j) % window_size_]; | 205 sample = history_[i][(history_cursor_ + j) % window_size_]; |
206 float old_sum = conj_sum; | 206 float old_sum = conj_sum; |
207 complex<float> old_mean = mean; | 207 complex<float> old_mean = mean; |
208 | 208 |
209 mean = old_mean + (sample - old_mean) / static_cast<float>(j + 1); | 209 mean = old_mean + (sample - old_mean) / static_cast<float>(j + 1); |
210 conj_sum = | 210 conj_sum = |
211 (old_sum + std::conj(sample - old_mean) * (sample - mean)).real(); | 211 (old_sum + std::conj(sample - old_mean) * (sample - mean)).real(); |
212 variance_[i] = conj_sum / (j); | 212 variance_[i] = conj_sum / (j); |
213 } | 213 } |
214 array_mean_ += (variance_[i] - array_mean_) / (i + 1); | 214 array_mean_ += (variance_[i] - array_mean_) / (i + 1); |
215 } | 215 } |
216 history_cursor_ = (history_cursor_ + 1) % window_size_; | 216 history_cursor_ = (history_cursor_ + 1) % window_size_; |
217 ++count_; | 217 ++count_; |
218 } | 218 } |
219 | 219 |
220 // Variance with a window of blocks. Within each block, the variances are | 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)|. | 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 | 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 | 223 // history window and a new block is started. The variances for the window |
224 // are recomputed from scratch at each of these transitions. | 224 // are recomputed from scratch at each of these transitions. |
225 void VarianceArray::BlockedStep(const complex<float>* data, bool /*dummy*/) { | 225 void VarianceArray::BlockedStep(const complex<float>* data, bool /*dummy*/) { |
226 int blocks = min(window_size_, history_cursor_); | 226 size_t blocks = min(window_size_, history_cursor_); |
227 for (int i = 0; i < freqs_; ++i) { | 227 for (size_t i = 0; i < freqs_; ++i) { |
228 AddToMean(data[i], count_ + 1, &sub_running_mean_[i]); | 228 AddToMean(data[i], count_ + 1, &sub_running_mean_[i]); |
229 AddToMean(data[i] * std::conj(data[i]), count_ + 1, | 229 AddToMean(data[i] * std::conj(data[i]), count_ + 1, |
230 &sub_running_mean_sq_[i]); | 230 &sub_running_mean_sq_[i]); |
231 subhistory_[i][history_cursor_ % window_size_] = sub_running_mean_[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]; | 232 subhistory_sq_[i][history_cursor_ % window_size_] = sub_running_mean_sq_[i]; |
233 | 233 |
234 variance_[i] = | 234 variance_[i] = |
235 (NewMean(running_mean_sq_[i], sub_running_mean_sq_[i], blocks) - | 235 (NewMean(running_mean_sq_[i], sub_running_mean_sq_[i], blocks) - |
236 NewMean(running_mean_[i], sub_running_mean_[i], blocks) * | 236 NewMean(running_mean_[i], sub_running_mean_[i], blocks) * |
237 std::conj(NewMean(running_mean_[i], sub_running_mean_[i], blocks))) | 237 std::conj(NewMean(running_mean_[i], sub_running_mean_[i], blocks))) |
238 .real(); | 238 .real(); |
239 if (count_ == kWindowBlockSize - 1) { | 239 if (count_ == kWindowBlockSize - 1) { |
240 sub_running_mean_[i] = complex<float>(0.0f, 0.0f); | 240 sub_running_mean_[i] = complex<float>(0.0f, 0.0f); |
241 sub_running_mean_sq_[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); | 242 running_mean_[i] = complex<float>(0.0f, 0.0f); |
243 running_mean_sq_[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) { | 244 for (size_t j = 0; j < min(window_size_, history_cursor_); ++j) { |
245 AddToMean(subhistory_[i][j], j, &running_mean_[i]); | 245 AddToMean(subhistory_[i][j], j, &running_mean_[i]); |
246 AddToMean(subhistory_sq_[i][j], j, &running_mean_sq_[i]); | 246 AddToMean(subhistory_sq_[i][j], j, &running_mean_sq_[i]); |
247 } | 247 } |
248 ++history_cursor_; | 248 ++history_cursor_; |
249 } | 249 } |
250 } | 250 } |
251 ++count_; | 251 ++count_; |
252 if (count_ == kWindowBlockSize) { | 252 if (count_ == kWindowBlockSize) { |
253 count_ = 0; | 253 count_ = 0; |
254 } | 254 } |
255 } | 255 } |
256 | 256 |
257 void VarianceArray::Clear() { | 257 void VarianceArray::Clear() { |
258 memset(running_mean_.get(), 0, sizeof(*running_mean_.get()) * freqs_); | 258 memset(running_mean_.get(), 0, sizeof(*running_mean_.get()) * freqs_); |
259 memset(running_mean_sq_.get(), 0, sizeof(*running_mean_sq_.get()) * freqs_); | 259 memset(running_mean_sq_.get(), 0, sizeof(*running_mean_sq_.get()) * freqs_); |
260 memset(variance_.get(), 0, sizeof(*variance_.get()) * freqs_); | 260 memset(variance_.get(), 0, sizeof(*variance_.get()) * freqs_); |
261 memset(conj_sum_.get(), 0, sizeof(*conj_sum_.get()) * freqs_); | 261 memset(conj_sum_.get(), 0, sizeof(*conj_sum_.get()) * freqs_); |
262 history_cursor_ = 0; | 262 history_cursor_ = 0; |
263 count_ = 0; | 263 count_ = 0; |
264 array_mean_ = 0.0f; | 264 array_mean_ = 0.0f; |
265 } | 265 } |
266 | 266 |
267 void VarianceArray::ApplyScale(float scale) { | 267 void VarianceArray::ApplyScale(float scale) { |
268 array_mean_ = 0.0f; | 268 array_mean_ = 0.0f; |
269 for (int i = 0; i < freqs_; ++i) { | 269 for (size_t i = 0; i < freqs_; ++i) { |
270 variance_[i] *= scale * scale; | 270 variance_[i] *= scale * scale; |
271 array_mean_ += (variance_[i] - array_mean_) / (i + 1); | 271 array_mean_ += (variance_[i] - array_mean_) / (i + 1); |
272 } | 272 } |
273 } | 273 } |
274 | 274 |
275 GainApplier::GainApplier(int freqs, float change_limit) | 275 GainApplier::GainApplier(size_t freqs, float change_limit) |
276 : freqs_(freqs), | 276 : freqs_(freqs), |
277 change_limit_(change_limit), | 277 change_limit_(change_limit), |
278 target_(new float[freqs]()), | 278 target_(new float[freqs]()), |
279 current_(new float[freqs]()) { | 279 current_(new float[freqs]()) { |
280 for (int i = 0; i < freqs; ++i) { | 280 for (size_t i = 0; i < freqs; ++i) { |
281 target_[i] = 1.0f; | 281 target_[i] = 1.0f; |
282 current_[i] = 1.0f; | 282 current_[i] = 1.0f; |
283 } | 283 } |
284 } | 284 } |
285 | 285 |
286 void GainApplier::Apply(const complex<float>* in_block, | 286 void GainApplier::Apply(const complex<float>* in_block, |
287 complex<float>* out_block) { | 287 complex<float>* out_block) { |
288 for (int i = 0; i < freqs_; ++i) { | 288 for (size_t i = 0; i < freqs_; ++i) { |
289 float factor = sqrtf(fabsf(current_[i])); | 289 float factor = sqrtf(fabsf(current_[i])); |
290 if (!std::isnormal(factor)) { | 290 if (!std::isnormal(factor)) { |
291 factor = 1.0f; | 291 factor = 1.0f; |
292 } | 292 } |
293 out_block[i] = factor * in_block[i]; | 293 out_block[i] = factor * in_block[i]; |
294 current_[i] = UpdateFactor(target_[i], current_[i], change_limit_); | 294 current_[i] = UpdateFactor(target_[i], current_[i], change_limit_); |
295 } | 295 } |
296 } | 296 } |
297 | 297 |
298 } // namespace intelligibility | 298 } // namespace intelligibility |
299 | 299 |
300 } // namespace webrtc | 300 } // namespace webrtc |
OLD | NEW |