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

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

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