Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Side by Side Diff: webrtc/modules/audio_processing/intelligibility/intelligibility_utils.cc

Issue 1187033005: Revert of Allow intelligibility to compile in apm (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/modules/audio_processing/intelligibility/intelligibility_utils.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 //
12 // Implements helper functions and classes for intelligibility enhancement.
13 //
14
15 #include "webrtc/modules/audio_processing/intelligibility/intelligibility_utils. h" 11 #include "webrtc/modules/audio_processing/intelligibility/intelligibility_utils. h"
16 12
17 #include <algorithm> 13 #include <algorithm>
18 #include <cmath> 14 #include <cmath>
19 #include <cstring> 15 #include <cstring>
20 16
21 using std::complex; 17 using std::complex;
22 18
23 namespace { 19 namespace {
24 20
(...skipping 12 matching lines...) Expand all
37 33
38 // std::isnormal for complex numbers. 34 // std::isnormal for complex numbers.
39 inline bool cplxnormal(complex<float> c) { 35 inline bool cplxnormal(complex<float> c) {
40 return std::isnormal(c.real()) && std::isnormal(c.imag()); 36 return std::isnormal(c.real()) && std::isnormal(c.imag());
41 } 37 }
42 38
43 // Apply a small fudge to degenerate complex values. The numbers in the array 39 // 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 40 // were chosen randomly, so that even a series of all zeroes has some small
45 // variability. 41 // variability.
46 inline complex<float> zerofudge(complex<float> c) { 42 inline complex<float> zerofudge(complex<float> c) {
47 const static complex<float> fudge[7] = {{0.001f, 0.002f}, 43 const static complex<float> fudge[7] = {
48 {0.008f, 0.001f}, 44 {0.001f, 0.002f}, {0.008f, 0.001f}, {0.003f, 0.008f}, {0.0006f, 0.0009f},
49 {0.003f, 0.008f}, 45 {0.001f, 0.004f}, {0.003f, 0.004f}, {0.002f, 0.009f}
50 {0.0006f, 0.0009f}, 46 };
51 {0.001f, 0.004f},
52 {0.003f, 0.004f},
53 {0.002f, 0.009f}};
54 static int fudge_index = 0; 47 static int fudge_index = 0;
55 if (cplxfinite(c) && !cplxnormal(c)) { 48 if (cplxfinite(c) && !cplxnormal(c)) {
56 fudge_index = (fudge_index + 1) % 7; 49 fudge_index = (fudge_index + 1) % 7;
57 return c + fudge[fudge_index]; 50 return c + fudge[fudge_index];
58 } 51 }
59 return c; 52 return c;
60 } 53 }
61 54
62 // Incremental mean computation. Return the mean of the series with the 55 // Incremental mean computation. Return the mean of the series with the
63 // mean |mean| with added |data|. 56 // mean |mean| with added |data|.
64 inline complex<float> NewMean(complex<float> mean, 57 inline complex<float> NewMean(complex<float> mean, complex<float> data,
65 complex<float> data, 58 int count) {
66 int count) {
67 return mean + (data - mean) / static_cast<float>(count); 59 return mean + (data - mean) / static_cast<float>(count);
68 } 60 }
69 61
70 inline void AddToMean(complex<float> data, int count, complex<float>* mean) { 62 inline void AddToMean(complex<float> data, int count, complex<float>* mean) {
71 (*mean) = NewMean(*mean, data, count); 63 (*mean) = NewMean(*mean, data, count);
72 } 64 }
73 65
74 } // namespace 66 } // namespace
75 67
76 using std::min; 68 using std::min;
77 69
78 namespace webrtc { 70 namespace webrtc {
79 71
80 namespace intelligibility { 72 namespace intelligibility {
81 73
82 static const int kWindowBlockSize = 10; 74 static const int kWindowBlockSize = 10;
83 75
84 VarianceArray::VarianceArray(int freqs, 76 VarianceArray::VarianceArray(int freqs, StepType type, int window_size,
85 StepType type,
86 int window_size,
87 float decay) 77 float decay)
88 : running_mean_(new complex<float>[freqs]()), 78 : running_mean_(new complex<float>[freqs]()),
89 running_mean_sq_(new complex<float>[freqs]()), 79 running_mean_sq_(new complex<float>[freqs]()),
90 sub_running_mean_(new complex<float>[freqs]()), 80 sub_running_mean_(new complex<float>[freqs]()),
91 sub_running_mean_sq_(new complex<float>[freqs]()), 81 sub_running_mean_sq_(new complex<float>[freqs]()),
92 variance_(new float[freqs]()), 82 variance_(new float[freqs]()),
93 conj_sum_(new float[freqs]()), 83 conj_sum_(new float[freqs]()),
94 freqs_(freqs), 84 freqs_(freqs),
95 window_size_(window_size), 85 window_size_(window_size),
96 decay_(decay), 86 decay_(decay),
97 history_cursor_(0), 87 history_cursor_(0),
98 count_(0), 88 count_(0),
99 array_mean_(0.0f) { 89 array_mean_(0.0f) {
100 history_.reset(new rtc::scoped_ptr<complex<float>[]>[freqs_]()); 90 history_.reset(new scoped_ptr<complex<float>[]>[freqs_]());
101 for (int i = 0; i < freqs_; ++i) { 91 for (int i = 0; i < freqs_; ++i) {
102 history_[i].reset(new complex<float>[window_size_]()); 92 history_[i].reset(new complex<float>[window_size_]());
103 } 93 }
104 subhistory_.reset(new rtc::scoped_ptr<complex<float>[]>[freqs_]()); 94 subhistory_.reset(new scoped_ptr<complex<float>[]>[freqs_]());
105 for (int i = 0; i < freqs_; ++i) { 95 for (int i = 0; i < freqs_; ++i) {
106 subhistory_[i].reset(new complex<float>[window_size_]()); 96 subhistory_[i].reset(new complex<float>[window_size_]());
107 } 97 }
108 subhistory_sq_.reset(new rtc::scoped_ptr<complex<float>[]>[freqs_]()); 98 subhistory_sq_.reset(new scoped_ptr<complex<float>[]>[freqs_]());
109 for (int i = 0; i < freqs_; ++i) { 99 for (int i = 0; i < freqs_; ++i) {
110 subhistory_sq_[i].reset(new complex<float>[window_size_]()); 100 subhistory_sq_[i].reset(new complex<float>[window_size_]());
111 } 101 }
112 switch (type) { 102 switch (type) {
113 case kStepInfinite: 103 case kStepInfinite:
114 step_func_ = &VarianceArray::InfiniteStep; 104 step_func_ = &VarianceArray::InfiniteStep;
115 break; 105 break;
116 case kStepDecaying: 106 case kStepDecaying:
117 step_func_ = &VarianceArray::DecayStep; 107 step_func_ = &VarianceArray::DecayStep;
118 break; 108 break;
(...skipping 15 matching lines...) Expand all
134 complex<float> sample = data[i]; 124 complex<float> sample = data[i];
135 if (!skip_fudge) { 125 if (!skip_fudge) {
136 sample = zerofudge(sample); 126 sample = zerofudge(sample);
137 } 127 }
138 if (count_ == 1) { 128 if (count_ == 1) {
139 running_mean_[i] = sample; 129 running_mean_[i] = sample;
140 variance_[i] = 0.0f; 130 variance_[i] = 0.0f;
141 } else { 131 } else {
142 float old_sum = conj_sum_[i]; 132 float old_sum = conj_sum_[i];
143 complex<float> old_mean = running_mean_[i]; 133 complex<float> old_mean = running_mean_[i];
144 running_mean_[i] = 134 running_mean_[i] = old_mean + (sample - old_mean) /
145 old_mean + (sample - old_mean) / static_cast<float>(count_); 135 static_cast<float>(count_);
146 conj_sum_[i] = 136 conj_sum_[i] = (old_sum + std::conj(sample - old_mean) *
147 (old_sum + std::conj(sample - old_mean) * (sample - running_mean_[i])) 137 (sample - running_mean_[i])).real();
148 .real(); 138 variance_[i] = conj_sum_[i] / (count_ - 1); // + fudge[fudge_index].real() ;
149 variance_[i] =
150 conj_sum_[i] / (count_ - 1); // + fudge[fudge_index].real();
151 if (skip_fudge && false) { 139 if (skip_fudge && false) {
152 // variance_[i] -= fudge[fudge_index].real(); 140 //variance_[i] -= fudge[fudge_index].real();
153 } 141 }
154 } 142 }
155 array_mean_ += (variance_[i] - array_mean_) / (i + 1); 143 array_mean_ += (variance_[i] - array_mean_) / (i + 1);
156 } 144 }
157 } 145 }
158 146
159 // Compute the variance from the beginning, with exponential decaying of the 147 // Compute the variance from the beginning, with exponential decaying of the
160 // series data. 148 // series data.
161 void VarianceArray::DecayStep(const complex<float>* data, bool /*dummy*/) { 149 void VarianceArray::DecayStep(const complex<float>* data, bool /*dummy*/) {
162 array_mean_ = 0.0f; 150 array_mean_ = 0.0f;
163 ++count_; 151 ++count_;
164 for (int i = 0; i < freqs_; ++i) { 152 for (int i = 0; i < freqs_; ++i) {
165 complex<float> sample = data[i]; 153 complex<float> sample = data[i];
166 sample = zerofudge(sample); 154 sample = zerofudge(sample);
167 155
168 if (count_ == 1) { 156 if (count_ == 1) {
169 running_mean_[i] = sample; 157 running_mean_[i] = sample;
170 running_mean_sq_[i] = sample * std::conj(sample); 158 running_mean_sq_[i] = sample * std::conj(sample);
171 variance_[i] = 0.0f; 159 variance_[i] = 0.0f;
172 } else { 160 } else {
173 complex<float> prev = running_mean_[i]; 161 complex<float> prev = running_mean_[i];
174 complex<float> prev2 = running_mean_sq_[i]; 162 complex<float> prev2 = running_mean_sq_[i];
175 running_mean_[i] = decay_ * prev + (1.0f - decay_) * sample; 163 running_mean_[i] = decay_ * prev + (1.0f - decay_) * sample;
176 running_mean_sq_[i] = 164 running_mean_sq_[i] = decay_ * prev2 +
177 decay_ * prev2 + (1.0f - decay_) * sample * std::conj(sample); 165 (1.0f - decay_) * sample * std::conj(sample);
178 // variance_[i] = decay_ * variance_[i] + (1.0f - decay_) * ( 166 //variance_[i] = decay_ * variance_[i] + (1.0f - decay_) * (
179 // (sample - running_mean_[i]) * std::conj(sample - 167 // (sample - running_mean_[i]) * std::conj(sample - running_mean_[i])).re al();
180 // running_mean_[i])).real(); 168 variance_[i] = (running_mean_sq_[i] - running_mean_[i] * std::conj(running _mean_[i])).real();
181 variance_[i] = (running_mean_sq_[i] -
182 running_mean_[i] * std::conj(running_mean_[i])).real();
183 } 169 }
184 170
185 array_mean_ += (variance_[i] - array_mean_) / (i + 1); 171 array_mean_ += (variance_[i] - array_mean_) / (i + 1);
186 } 172 }
187 } 173 }
188 174
189 // Windowed variance computation. On each step, the variances for the 175 // Windowed variance computation. On each step, the variances for the
190 // window are recomputed from scratch, using Welford's algorithm. 176 // window are recomputed from scratch, using Welford's algorithm.
191 void VarianceArray::WindowedStep(const complex<float>* data, bool /*dummy*/) { 177 void VarianceArray::WindowedStep(const complex<float>* data, bool /*dummy*/) {
192 int num = min(count_ + 1, window_size_); 178 int num = min(count_ + 1, window_size_);
193 array_mean_ = 0.0f; 179 array_mean_ = 0.0f;
194 for (int i = 0; i < freqs_; ++i) { 180 for (int i = 0; i < freqs_; ++i) {
195 complex<float> mean; 181 complex<float> mean;
196 float conj_sum = 0.0f; 182 float conj_sum = 0.0f;
197 183
198 history_[i][history_cursor_] = data[i]; 184 history_[i][history_cursor_] = data[i];
199 185
200 mean = history_[i][history_cursor_]; 186 mean = history_[i][history_cursor_];
201 variance_[i] = 0.0f; 187 variance_[i] = 0.0f;
202 for (int j = 1; j < num; ++j) { 188 for (int j = 1; j < num; ++j) {
203 complex<float> sample = 189 complex<float> sample = zerofudge(
204 zerofudge(history_[i][(history_cursor_ + j) % window_size_]); 190 history_[i][(history_cursor_ + j) % window_size_]);
205 sample = history_[i][(history_cursor_ + j) % window_size_]; 191 sample = history_[i][(history_cursor_ + j) % window_size_];
206 float old_sum = conj_sum; 192 float old_sum = conj_sum;
207 complex<float> old_mean = mean; 193 complex<float> old_mean = mean;
208 194
209 mean = old_mean + (sample - old_mean) / static_cast<float>(j + 1); 195 mean = old_mean + (sample - old_mean) / static_cast<float>(j + 1);
210 conj_sum = 196 conj_sum = (old_sum + std::conj(sample - old_mean) *
211 (old_sum + std::conj(sample - old_mean) * (sample - mean)).real(); 197 (sample - mean)).real();
212 variance_[i] = conj_sum / (j); 198 variance_[i] = conj_sum / (j);
213 } 199 }
214 array_mean_ += (variance_[i] - array_mean_) / (i + 1); 200 array_mean_ += (variance_[i] - array_mean_) / (i + 1);
215 } 201 }
216 history_cursor_ = (history_cursor_ + 1) % window_size_; 202 history_cursor_ = (history_cursor_ + 1) % window_size_;
217 ++count_; 203 ++count_;
218 } 204 }
219 205
220 // Variance with a window of blocks. Within each block, the variances are 206 // 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)|. 207 // 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 208 // 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 209 // history window and a new block is started. The variances for the window
224 // are recomputed from scratch at each of these transitions. 210 // are recomputed from scratch at each of these transitions.
225 void VarianceArray::BlockedStep(const complex<float>* data, bool /*dummy*/) { 211 void VarianceArray::BlockedStep(const complex<float>* data, bool /*dummy*/) {
226 int blocks = min(window_size_, history_cursor_); 212 int blocks = min(window_size_, history_cursor_);
227 for (int i = 0; i < freqs_; ++i) { 213 for (int i = 0; i < freqs_; ++i) {
228 AddToMean(data[i], count_ + 1, &sub_running_mean_[i]); 214 AddToMean(data[i], count_ + 1, &sub_running_mean_[i]);
229 AddToMean(data[i] * std::conj(data[i]), count_ + 1, 215 AddToMean(data[i] * std::conj(data[i]), count_ + 1,
230 &sub_running_mean_sq_[i]); 216 &sub_running_mean_sq_[i]);
231 subhistory_[i][history_cursor_ % window_size_] = sub_running_mean_[i]; 217 subhistory_[i][history_cursor_ % window_size_] = sub_running_mean_[i];
232 subhistory_sq_[i][history_cursor_ % window_size_] = sub_running_mean_sq_[i]; 218 subhistory_sq_[i][history_cursor_ % window_size_] = sub_running_mean_sq_[i];
233 219
234 variance_[i] = 220 variance_[i] = (NewMean(running_mean_sq_[i], sub_running_mean_sq_[i],
235 (NewMean(running_mean_sq_[i], sub_running_mean_sq_[i], blocks) - 221 blocks) -
236 NewMean(running_mean_[i], sub_running_mean_[i], blocks) * 222 NewMean(running_mean_[i], sub_running_mean_[i], blocks) *
237 std::conj(NewMean(running_mean_[i], sub_running_mean_[i], blocks))) 223 std::conj(NewMean(running_mean_[i], sub_running_mean_[i],
238 .real(); 224 blocks))).real();
239 if (count_ == kWindowBlockSize - 1) { 225 if (count_ == kWindowBlockSize - 1) {
240 sub_running_mean_[i] = complex<float>(0.0f, 0.0f); 226 sub_running_mean_[i] = complex<float>(0.0f, 0.0f);
241 sub_running_mean_sq_[i] = complex<float>(0.0f, 0.0f); 227 sub_running_mean_sq_[i] = complex<float>(0.0f, 0.0f);
242 running_mean_[i] = complex<float>(0.0f, 0.0f); 228 running_mean_[i] = complex<float>(0.0f, 0.0f);
243 running_mean_sq_[i] = complex<float>(0.0f, 0.0f); 229 running_mean_sq_[i] = complex<float>(0.0f, 0.0f);
244 for (int j = 0; j < min(window_size_, history_cursor_); ++j) { 230 for (int j = 0; j < min(window_size_, history_cursor_); ++j) {
245 AddToMean(subhistory_[i][j], j, &running_mean_[i]); 231 AddToMean(subhistory_[i][j], j, &running_mean_[i]);
246 AddToMean(subhistory_sq_[i][j], j, &running_mean_sq_[i]); 232 AddToMean(subhistory_sq_[i][j], j, &running_mean_sq_[i]);
247 } 233 }
248 ++history_cursor_; 234 ++history_cursor_;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 factor = 1.0f; 277 factor = 1.0f;
292 } 278 }
293 out_block[i] = factor * in_block[i]; 279 out_block[i] = factor * in_block[i];
294 current_[i] = UpdateFactor(target_[i], current_[i], change_limit_); 280 current_[i] = UpdateFactor(target_[i], current_[i], change_limit_);
295 } 281 }
296 } 282 }
297 283
298 } // namespace intelligibility 284 } // namespace intelligibility
299 285
300 } // namespace webrtc 286 } // namespace webrtc
287
OLDNEW
« no previous file with comments | « webrtc/modules/audio_processing/intelligibility/intelligibility_utils.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698