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

Side by Side Diff: webrtc/modules/audio_processing/intelligibility/intelligibility_enhancer.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
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 core class for intelligibility enhancer.
13 //
14 // Details of the model and algorithm can be found in the original paper:
15 // http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=6882788
16 //
17
18 #include "webrtc/modules/audio_processing/intelligibility/intelligibility_enhanc er.h" 11 #include "webrtc/modules/audio_processing/intelligibility/intelligibility_enhanc er.h"
19 12
20 #include <cmath> 13 #include <cmath>
21 #include <cstdlib> 14 #include <cstdlib>
22 15
23 #include <algorithm> 16 #include <algorithm>
24 17
25 #include "webrtc/base/checks.h" 18 #include "webrtc/base/checks.h"
26 #include "webrtc/common_audio/vad/include/webrtc_vad.h" 19 #include "webrtc/common_audio/vad/include/webrtc_vad.h"
27 #include "webrtc/common_audio/window_generator.h" 20 #include "webrtc/common_audio/window_generator.h"
28 21
29 using std::complex; 22 using std::complex;
30 using std::max; 23 using std::max;
31 using std::min; 24 using std::min;
32 25
33 namespace webrtc { 26 namespace webrtc {
34 27
35 const int IntelligibilityEnhancer::kErbResolution = 2; 28 const int IntelligibilityEnhancer::kErbResolution = 2;
36 const int IntelligibilityEnhancer::kWindowSizeMs = 2; 29 const int IntelligibilityEnhancer::kWindowSizeMs = 2;
37 const int IntelligibilityEnhancer::kChunkSizeMs = 10; // Size provided by APM. 30 // The size of the chunk provided by APM, in milliseconds.
31 const int IntelligibilityEnhancer::kChunkSizeMs = 10;
38 const int IntelligibilityEnhancer::kAnalyzeRate = 800; 32 const int IntelligibilityEnhancer::kAnalyzeRate = 800;
39 const int IntelligibilityEnhancer::kVarianceRate = 2; 33 const int IntelligibilityEnhancer::kVarianceRate = 2;
40 const float IntelligibilityEnhancer::kClipFreq = 200.0f; 34 const float IntelligibilityEnhancer::kClipFreq = 200.0f;
41 const float IntelligibilityEnhancer::kConfigRho = 0.02f; 35 const float IntelligibilityEnhancer::kConfigRho = 0.02f;
42 const float IntelligibilityEnhancer::kKbdAlpha = 1.5f; 36 const float IntelligibilityEnhancer::kKbdAlpha = 1.5f;
43
44 // To disable gain update smoothing, set gain limit to be VERY high.
45 // TODO(ekmeyerson): Add option to disable gain smoothing altogether
46 // to avoid the extra computation.
47 const float IntelligibilityEnhancer::kGainChangeLimit = 0.0125f; 37 const float IntelligibilityEnhancer::kGainChangeLimit = 0.0125f;
48 38
49 using VarianceType = intelligibility::VarianceArray::StepType; 39 using VarianceType = intelligibility::VarianceArray::StepType;
50 40
51 IntelligibilityEnhancer::TransformCallback::TransformCallback( 41 IntelligibilityEnhancer::TransformCallback::TransformCallback(
52 IntelligibilityEnhancer* parent, 42 IntelligibilityEnhancer* parent,
53 IntelligibilityEnhancer::AudioSource source) 43 IntelligibilityEnhancer::AudioSource source)
54 : parent_(parent), source_(source) { 44 : parent_(parent),
55 } 45 source_(source) {}
56 46
57 void IntelligibilityEnhancer::TransformCallback::ProcessAudioBlock( 47 void IntelligibilityEnhancer::TransformCallback::ProcessAudioBlock(
58 const complex<float>* const* in_block, 48 const complex<float>* const* in_block,
59 int in_channels, 49 int in_channels, int frames, int /* out_channels */,
60 int frames,
61 int /* out_channels */,
62 complex<float>* const* out_block) { 50 complex<float>* const* out_block) {
63 DCHECK_EQ(parent_->freqs_, frames); 51 DCHECK_EQ(parent_->freqs_, frames);
64 for (int i = 0; i < in_channels; ++i) { 52 for (int i = 0; i < in_channels; ++i) {
65 parent_->DispatchAudio(source_, in_block[i], out_block[i]); 53 parent_->DispatchAudio(source_, in_block[i], out_block[i]);
66 } 54 }
67 } 55 }
68 56
69 IntelligibilityEnhancer::IntelligibilityEnhancer(int erb_resolution, 57 IntelligibilityEnhancer::IntelligibilityEnhancer(int erb_resolution,
70 int sample_rate_hz, 58 int sample_rate_hz,
71 int channels, 59 int channels,
72 int cv_type, 60 int cv_type, float cv_alpha,
73 float cv_alpha,
74 int cv_win, 61 int cv_win,
75 int analysis_rate, 62 int analysis_rate,
76 int variance_rate, 63 int variance_rate,
77 float gain_limit) 64 float gain_limit)
78 : freqs_(RealFourier::ComplexLength( 65 : freqs_(RealFourier::ComplexLength(RealFourier::FftOrder(
79 RealFourier::FftOrder(sample_rate_hz * kWindowSizeMs / 1000))), 66 sample_rate_hz * kWindowSizeMs / 1000))),
80 window_size_(1 << RealFourier::FftOrder(freqs_)), 67 window_size_(1 << RealFourier::FftOrder(freqs_)),
81 chunk_length_(sample_rate_hz * kChunkSizeMs / 1000), 68 chunk_length_(sample_rate_hz * kChunkSizeMs / 1000),
82 bank_size_(GetBankSize(sample_rate_hz, erb_resolution)), 69 bank_size_(GetBankSize(sample_rate_hz, erb_resolution)),
83 sample_rate_hz_(sample_rate_hz), 70 sample_rate_hz_(sample_rate_hz),
84 erb_resolution_(erb_resolution), 71 erb_resolution_(erb_resolution),
85 channels_(channels), 72 channels_(channels),
86 analysis_rate_(analysis_rate), 73 analysis_rate_(analysis_rate),
87 variance_rate_(variance_rate), 74 variance_rate_(variance_rate),
88 clear_variance_(freqs_, 75 clear_variance_(freqs_, static_cast<VarianceType>(cv_type), cv_win,
89 static_cast<VarianceType>(cv_type),
90 cv_win,
91 cv_alpha), 76 cv_alpha),
92 noise_variance_(freqs_, VarianceType::kStepInfinite, 475, 0.01f), 77 noise_variance_(freqs_, VarianceType::kStepInfinite, 475, 0.01f),
93 filtered_clear_var_(new float[bank_size_]), 78 filtered_clear_var_(new float[bank_size_]),
94 filtered_noise_var_(new float[bank_size_]), 79 filtered_noise_var_(new float[bank_size_]),
95 filter_bank_(nullptr), 80 filter_bank_(nullptr),
96 center_freqs_(new float[bank_size_]), 81 center_freqs_(new float[bank_size_]),
97 rho_(new float[bank_size_]), 82 rho_(new float[bank_size_]),
98 gains_eq_(new float[bank_size_]), 83 gains_eq_(new float[bank_size_]),
99 gain_applier_(freqs_, gain_limit), 84 gain_applier_(freqs_, gain_limit),
100 temp_out_buffer_(nullptr), 85 temp_out_buffer_(nullptr),
101 input_audio_(new float* [channels]), 86 input_audio_(new float*[channels]),
102 kbd_window_(new float[window_size_]), 87 kbd_window_(new float[window_size_]),
103 render_callback_(this, AudioSource::kRenderStream), 88 render_callback_(this, AudioSource::kRenderStream),
104 capture_callback_(this, AudioSource::kCaptureStream), 89 capture_callback_(this, AudioSource::kCaptureStream),
105 block_count_(0), 90 block_count_(0),
106 analysis_step_(0), 91 analysis_step_(0),
107 vad_high_(WebRtcVad_Create()), 92 vad_high_(nullptr),
108 vad_low_(WebRtcVad_Create()), 93 vad_low_(nullptr),
109 vad_tmp_buffer_(new int16_t[chunk_length_]) { 94 vad_tmp_buffer_(new int16_t[chunk_length_]) {
110 DCHECK_LE(kConfigRho, 1.0f); 95 DCHECK_LE(kConfigRho, 1.0f);
111 96
112 CreateErbBank(); 97 CreateErbBank();
113 98
99 WebRtcVad_Create(&vad_high_);
114 WebRtcVad_Init(vad_high_); 100 WebRtcVad_Init(vad_high_);
115 WebRtcVad_set_mode(vad_high_, 0); // High likelihood of speech. 101 WebRtcVad_set_mode(vad_high_, 0); // high likelihood of speech
102 WebRtcVad_Create(&vad_low_);
116 WebRtcVad_Init(vad_low_); 103 WebRtcVad_Init(vad_low_);
117 WebRtcVad_set_mode(vad_low_, 3); // Low likelihood of speech. 104 WebRtcVad_set_mode(vad_low_, 3); // low likelihood of speech
118 105
119 temp_out_buffer_ = static_cast<float**>( 106 temp_out_buffer_ = static_cast<float**>(malloc(
120 malloc(sizeof(*temp_out_buffer_) * channels_ + 107 sizeof(*temp_out_buffer_) * channels_ +
121 sizeof(**temp_out_buffer_) * chunk_length_ * channels_)); 108 sizeof(**temp_out_buffer_) * chunk_length_ * channels_));
122 for (int i = 0; i < channels_; ++i) { 109 for (int i = 0; i < channels_; ++i) {
123 temp_out_buffer_[i] = 110 temp_out_buffer_[i] = reinterpret_cast<float*>(temp_out_buffer_ + channels_)
124 reinterpret_cast<float*>(temp_out_buffer_ + channels_) + 111 + chunk_length_ * i;
125 chunk_length_ * i;
126 } 112 }
127 113
128 // Assumes all rho equal.
129 for (int i = 0; i < bank_size_; ++i) { 114 for (int i = 0; i < bank_size_; ++i) {
130 rho_[i] = kConfigRho * kConfigRho; 115 rho_[i] = kConfigRho * kConfigRho;
131 } 116 }
132 117
133 float freqs_khz = kClipFreq / 1000.0f; 118 float freqs_khz = kClipFreq / 1000.0f;
134 int erb_index = static_cast<int>(ceilf( 119 int erb_index = static_cast<int>(ceilf(11.17f * logf((freqs_khz + 0.312f) /
135 11.17f * logf((freqs_khz + 0.312f) / (freqs_khz + 14.6575f)) + 43.0f)); 120 (freqs_khz + 14.6575f))
121 + 43.0f));
136 start_freq_ = max(1, erb_index * kErbResolution); 122 start_freq_ = max(1, erb_index * kErbResolution);
137 123
138 WindowGenerator::KaiserBesselDerived(kKbdAlpha, window_size_, 124 WindowGenerator::KaiserBesselDerived(kKbdAlpha, window_size_,
139 kbd_window_.get()); 125 kbd_window_.get());
140 render_mangler_.reset(new LappedTransform( 126 render_mangler_.reset(new LappedTransform(channels_, channels_,
141 channels_, channels_, chunk_length_, kbd_window_.get(), window_size_, 127 chunk_length_,
142 window_size_ / 2, &render_callback_)); 128 kbd_window_.get(),
143 capture_mangler_.reset(new LappedTransform( 129 window_size_,
144 channels_, channels_, chunk_length_, kbd_window_.get(), window_size_, 130 window_size_ / 2,
145 window_size_ / 2, &capture_callback_)); 131 &render_callback_));
132 capture_mangler_.reset(new LappedTransform(channels_, channels_,
133 chunk_length_,
134 kbd_window_.get(),
135 window_size_,
136 window_size_ / 2,
137 &capture_callback_));
146 } 138 }
147 139
148 IntelligibilityEnhancer::~IntelligibilityEnhancer() { 140 IntelligibilityEnhancer::~IntelligibilityEnhancer() {
149 WebRtcVad_Free(vad_low_); 141 WebRtcVad_Free(vad_low_);
150 WebRtcVad_Free(vad_high_); 142 WebRtcVad_Free(vad_high_);
151 free(filter_bank_); 143 free(filter_bank_);
152 } 144 }
153 145
154 void IntelligibilityEnhancer::ProcessRenderAudio(float* const* audio) { 146 void IntelligibilityEnhancer::ProcessRenderAudio(float* const* audio) {
155 for (int i = 0; i < chunk_length_; ++i) { 147 for (int i = 0; i < chunk_length_; ++i) {
156 vad_tmp_buffer_[i] = (int16_t)audio[0][i]; 148 vad_tmp_buffer_[i] = (int16_t)audio[0][i];
157 } 149 }
158 has_voice_low_ = WebRtcVad_Process(vad_low_, sample_rate_hz_, 150 has_voice_low_ = WebRtcVad_Process(vad_low_, sample_rate_hz_,
159 vad_tmp_buffer_.get(), chunk_length_) == 1; 151 vad_tmp_buffer_.get(), chunk_length_) == 1;
160 152
161 // Process and enhance chunk of |audio|
162 render_mangler_->ProcessChunk(audio, temp_out_buffer_); 153 render_mangler_->ProcessChunk(audio, temp_out_buffer_);
163
164 for (int i = 0; i < channels_; ++i) { 154 for (int i = 0; i < channels_; ++i) {
165 memcpy(audio[i], temp_out_buffer_[i], 155 memcpy(audio[i], temp_out_buffer_[i],
166 chunk_length_ * sizeof(**temp_out_buffer_)); 156 chunk_length_ * sizeof(**temp_out_buffer_));
167 } 157 }
168 } 158 }
169 159
170 void IntelligibilityEnhancer::ProcessCaptureAudio(float* const* audio) { 160 void IntelligibilityEnhancer::ProcessCaptureAudio(float* const* audio) {
171 for (int i = 0; i < chunk_length_; ++i) { 161 for (int i = 0; i < chunk_length_; ++i) {
172 vad_tmp_buffer_[i] = (int16_t)audio[0][i]; 162 vad_tmp_buffer_[i] = (int16_t)audio[0][i];
173 } 163 }
174 // TODO(bercic): The VAD was always detecting voice in the noise stream, 164 // TODO(bercic): the VAD was always detecting voice in the noise stream,
175 // no matter what the aggressiveness, so it was temporarily disabled here. 165 // no matter what the aggressiveness, so it was temporarily disabled here
176 166
177 #if 0 167 //if (WebRtcVad_Process(vad_high_, sample_rate_hz_, vad_tmp_buffer_.get(),
178 if (WebRtcVad_Process(vad_high_, sample_rate_hz_, vad_tmp_buffer_.get(), 168 // chunk_length_) == 1) {
179 chunk_length_) == 1) { 169 // printf("capture HAS speech\n");
180 printf("capture HAS speech\n"); 170 // return;
181 return; 171 //}
182 } 172 //printf("capture NO speech\n");
183 printf("capture NO speech\n");
184 #endif
185
186 capture_mangler_->ProcessChunk(audio, temp_out_buffer_); 173 capture_mangler_->ProcessChunk(audio, temp_out_buffer_);
187 } 174 }
188 175
189 void IntelligibilityEnhancer::DispatchAudio( 176 void IntelligibilityEnhancer::DispatchAudio(
190 IntelligibilityEnhancer::AudioSource source, 177 IntelligibilityEnhancer::AudioSource source,
191 const complex<float>* in_block, 178 const complex<float>* in_block, complex<float>* out_block) {
192 complex<float>* out_block) {
193 switch (source) { 179 switch (source) {
194 case kRenderStream: 180 case kRenderStream:
195 ProcessClearBlock(in_block, out_block); 181 ProcessClearBlock(in_block, out_block);
196 break; 182 break;
197 case kCaptureStream: 183 case kCaptureStream:
198 ProcessNoiseBlock(in_block, out_block); 184 ProcessNoiseBlock(in_block, out_block);
199 break; 185 break;
200 } 186 }
201 } 187 }
202 188
203 void IntelligibilityEnhancer::ProcessClearBlock(const complex<float>* in_block, 189 void IntelligibilityEnhancer::ProcessClearBlock(const complex<float>* in_block,
204 complex<float>* out_block) { 190 complex<float>* out_block) {
205 float power_target; 191 float power_target;
206 192
207 if (block_count_ < 2) { 193 if (block_count_ < 2) {
208 memset(out_block, 0, freqs_ * sizeof(*out_block)); 194 memset(out_block, 0, freqs_ * sizeof(*out_block));
209 ++block_count_; 195 ++block_count_;
210 return; 196 return;
211 } 197 }
212 198
213 // For now, always assumes enhancement is necessary.
214 // TODO(ekmeyerson): Change to only enhance if necessary,
215 // based on experiments with different cutoffs.
216 if (has_voice_low_ || true) { 199 if (has_voice_low_ || true) {
217 clear_variance_.Step(in_block, false); 200 clear_variance_.Step(in_block, false);
218 power_target = std::accumulate(clear_variance_.variance(), 201 power_target = std::accumulate(clear_variance_.variance(),
219 clear_variance_.variance() + freqs_, 0.0f); 202 clear_variance_.variance() + freqs_, 0.0f);
220 203
221 if (block_count_ % analysis_rate_ == analysis_rate_ - 1) { 204 if (block_count_ % analysis_rate_ == analysis_rate_ - 1) {
222 AnalyzeClearBlock(power_target); 205 AnalyzeClearBlock(power_target);
223 ++analysis_step_; 206 ++analysis_step_;
224 if (analysis_step_ == variance_rate_) { 207 if (analysis_step_ == variance_rate_) {
225 analysis_step_ = 0; 208 analysis_step_ = 0;
226 clear_variance_.Clear(); 209 clear_variance_.Clear();
227 noise_variance_.Clear(); 210 noise_variance_.Clear();
228 } 211 }
229 } 212 }
230 ++block_count_; 213 ++block_count_;
231 } 214 }
232 215
233 /* efidata(n,:) = sqrt(b(n)) * fidata(n,:) */ 216 /* efidata(n,:) = sqrt(b(n)) * fidata(n,:) */
234 gain_applier_.Apply(in_block, out_block); 217 gain_applier_.Apply(in_block, out_block);
235 } 218 }
236 219
237 void IntelligibilityEnhancer::AnalyzeClearBlock(float power_target) { 220 void IntelligibilityEnhancer::AnalyzeClearBlock(float power_target) {
238 FilterVariance(clear_variance_.variance(), filtered_clear_var_.get()); 221 FilterVariance(clear_variance_.variance(), filtered_clear_var_.get());
239 FilterVariance(noise_variance_.variance(), filtered_noise_var_.get()); 222 FilterVariance(noise_variance_.variance(), filtered_noise_var_.get());
240 223
241 // Bisection search for optimal |lambda| 224 /* lambda binary search */
242 225
243 float lambda_bot = -1.0f, lambda_top = -10e-18f, lambda; 226 float lambda_bot = -1.0f, lambda_top = -10e-18f, lambda;
244 float power_bot, power_top, power; 227 float power_bot, power_top, power;
245 SolveForGainsGivenLambda(lambda_top, start_freq_, gains_eq_.get()); 228 SolveEquation14(lambda_top, start_freq_, gains_eq_.get());
246 power_top = 229 power_top = DotProduct(gains_eq_.get(), filtered_clear_var_.get(),
247 DotProduct(gains_eq_.get(), filtered_clear_var_.get(), bank_size_); 230 bank_size_);
248 SolveForGainsGivenLambda(lambda_bot, start_freq_, gains_eq_.get()); 231 SolveEquation14(lambda_bot, start_freq_, gains_eq_.get());
249 power_bot = 232 power_bot = DotProduct(gains_eq_.get(), filtered_clear_var_.get(),
250 DotProduct(gains_eq_.get(), filtered_clear_var_.get(), bank_size_); 233 bank_size_);
251 DCHECK(power_target >= power_bot && power_target <= power_top); 234 DCHECK(power_target >= power_bot && power_target <= power_top);
252 235
253 float power_ratio = 2.0f; // Ratio of achieved power to target power. 236 float power_ratio = 2.0f;
254 const float kConvergeThresh = 0.001f; // TODO(ekmeyerson): Find best values
255 const int kMaxIters = 100; // for these, based on experiments.
256 int iters = 0; 237 int iters = 0;
257 while (fabs(power_ratio - 1.0f) > kConvergeThresh && iters <= kMaxIters) { 238 while (fabs(power_ratio - 1.0f) > 0.001f && iters <= 100) {
258 lambda = lambda_bot + (lambda_top - lambda_bot) / 2.0f; 239 lambda = lambda_bot + (lambda_top - lambda_bot) / 2.0f;
259 SolveForGainsGivenLambda(lambda, start_freq_, gains_eq_.get()); 240 SolveEquation14(lambda, start_freq_, gains_eq_.get());
260 power = DotProduct(gains_eq_.get(), filtered_clear_var_.get(), bank_size_); 241 power = DotProduct(gains_eq_.get(), filtered_clear_var_.get(), bank_size_);
261 if (power < power_target) { 242 if (power < power_target) {
262 lambda_bot = lambda; 243 lambda_bot = lambda;
263 } else { 244 } else {
264 lambda_top = lambda; 245 lambda_top = lambda;
265 } 246 }
266 power_ratio = fabs(power / power_target); 247 power_ratio = fabs(power / power_target);
267 ++iters; 248 ++iters;
268 } 249 }
269 250
270 // (ERB gain) = filterbank' * (freq gain) 251 /* b = filterbank' * b */
271 float* gains = gain_applier_.target(); 252 float* gains = gain_applier_.target();
272 for (int i = 0; i < freqs_; ++i) { 253 for (int i = 0; i < freqs_; ++i) {
273 gains[i] = 0.0f; 254 gains[i] = 0.0f;
274 for (int j = 0; j < bank_size_; ++j) { 255 for (int j = 0; j < bank_size_; ++j) {
275 gains[i] = fmaf(filter_bank_[j][i], gains_eq_[j], gains[i]); 256 gains[i] = fmaf(filter_bank_[j][i], gains_eq_[j], gains[i]);
276 } 257 }
277 } 258 }
278 } 259 }
279 260
280 void IntelligibilityEnhancer::ProcessNoiseBlock(const complex<float>* in_block, 261 void IntelligibilityEnhancer::ProcessNoiseBlock(const complex<float>* in_block,
281 complex<float>* /*out_block*/) { 262 complex<float>* /*out_block*/) {
282 noise_variance_.Step(in_block); 263 noise_variance_.Step(in_block);
283 } 264 }
284 265
285 int IntelligibilityEnhancer::GetBankSize(int sample_rate, int erb_resolution) { 266 int IntelligibilityEnhancer::GetBankSize(int sample_rate, int erb_resolution) {
286 float freq_limit = sample_rate / 2000.0f; 267 float freq_limit = sample_rate / 2000.0f;
287 int erb_scale = ceilf( 268 int erb_scale = ceilf(11.17f * logf((freq_limit + 0.312f) /
288 11.17f * logf((freq_limit + 0.312f) / (freq_limit + 14.6575f)) + 43.0f); 269 (freq_limit + 14.6575f)) + 43.0f);
289 return erb_scale * erb_resolution; 270 return erb_scale * erb_resolution;
290 } 271 }
291 272
292 void IntelligibilityEnhancer::CreateErbBank() { 273 void IntelligibilityEnhancer::CreateErbBank() {
293 int lf = 1, rf = 4; 274 int lf = 1, rf = 4;
294 275
295 for (int i = 0; i < bank_size_; ++i) { 276 for (int i = 0; i < bank_size_; ++i) {
296 float abs_temp = fabsf((i + 1.0f) / static_cast<float>(erb_resolution_)); 277 float abs_temp = fabsf((i + 1.0f) / static_cast<float>(erb_resolution_));
297 center_freqs_[i] = 676170.4f / (47.06538f - expf(0.08950404f * abs_temp)); 278 center_freqs_[i] = 676170.4f / (47.06538f - expf(0.08950404f * abs_temp));
298 center_freqs_[i] -= 14678.49f; 279 center_freqs_[i] -= 14678.49f;
299 } 280 }
300 float last_center_freq = center_freqs_[bank_size_ - 1]; 281 float last_center_freq = center_freqs_[bank_size_ - 1];
301 for (int i = 0; i < bank_size_; ++i) { 282 for (int i = 0; i < bank_size_; ++i) {
302 center_freqs_[i] *= 0.5f * sample_rate_hz_ / last_center_freq; 283 center_freqs_[i] *= 0.5f * sample_rate_hz_ / last_center_freq;
303 } 284 }
304 285
305 filter_bank_ = static_cast<float**>( 286 filter_bank_ = static_cast<float**>(malloc(
306 malloc(sizeof(*filter_bank_) * bank_size_ + 287 sizeof(*filter_bank_) * bank_size_ +
307 sizeof(**filter_bank_) * freqs_ * bank_size_)); 288 sizeof(**filter_bank_) * freqs_ * bank_size_));
308 for (int i = 0; i < bank_size_; ++i) { 289 for (int i = 0; i < bank_size_; ++i) {
309 filter_bank_[i] = 290 filter_bank_[i] = reinterpret_cast<float*>(filter_bank_ + bank_size_) +
310 reinterpret_cast<float*>(filter_bank_ + bank_size_) + freqs_ * i; 291 freqs_ * i;
311 } 292 }
312 293
313 for (int i = 1; i <= bank_size_; ++i) { 294 for (int i = 1; i <= bank_size_; ++i) {
314 int lll, ll, rr, rrr; 295 int lll, ll, rr, rrr;
315 lll = round(center_freqs_[max(1, i - lf) - 1] * freqs_ / 296 lll = round(center_freqs_[max(1, i - lf) - 1] * freqs_ /
316 (0.5f * sample_rate_hz_)); 297 (0.5f * sample_rate_hz_));
317 ll = 298 ll = round(center_freqs_[max(1, i ) - 1] * freqs_ /
318 round(center_freqs_[max(1, i) - 1] * freqs_ / (0.5f * sample_rate_hz_)); 299 (0.5f * sample_rate_hz_));
319 lll = min(freqs_, max(lll, 1)) - 1; 300 lll = min(freqs_, max(lll, 1)) - 1;
320 ll = min(freqs_, max(ll, 1)) - 1; 301 ll = min(freqs_, max(ll, 1)) - 1;
321 302
322 rrr = round(center_freqs_[min(bank_size_, i + rf) - 1] * freqs_ / 303 rrr = round(center_freqs_[min(bank_size_, i + rf) - 1] * freqs_ /
323 (0.5f * sample_rate_hz_)); 304 (0.5f * sample_rate_hz_));
324 rr = round(center_freqs_[min(bank_size_, i + 1) - 1] * freqs_ / 305 rr = round(center_freqs_[min(bank_size_, i + 1) - 1] * freqs_ /
325 (0.5f * sample_rate_hz_)); 306 (0.5f * sample_rate_hz_));
326 rrr = min(freqs_, max(rrr, 1)) - 1; 307 rrr = min(freqs_, max(rrr, 1)) - 1;
327 rr = min(freqs_, max(rr, 1)) - 1; 308 rr = min(freqs_, max(rr, 1)) - 1;
328 309
329 float step, element; 310 float step, element;
330 311
331 step = 1.0f / (ll - lll); 312 step = 1.0f / (ll - lll);
332 element = 0.0f; 313 element = 0.0f;
333 for (int j = lll; j <= ll; ++j) { 314 for (int j = lll; j <= ll; ++j) {
334 filter_bank_[i - 1][j] = element; 315 filter_bank_[i - 1][j] = element;
335 element += step; 316 element += step;
336 } 317 }
337 step = 1.0f / (rrr - rr); 318 step = 1.0f / (rrr - rr);
(...skipping 12 matching lines...) Expand all
350 sum = 0.0f; 331 sum = 0.0f;
351 for (int j = 0; j < bank_size_; ++j) { 332 for (int j = 0; j < bank_size_; ++j) {
352 sum += filter_bank_[j][i]; 333 sum += filter_bank_[j][i];
353 } 334 }
354 for (int j = 0; j < bank_size_; ++j) { 335 for (int j = 0; j < bank_size_; ++j) {
355 filter_bank_[j][i] /= sum; 336 filter_bank_[j][i] /= sum;
356 } 337 }
357 } 338 }
358 } 339 }
359 340
360 void IntelligibilityEnhancer::SolveForGainsGivenLambda(float lambda, 341 void IntelligibilityEnhancer::SolveEquation14(float lambda, int start_freq,
361 int start_freq, 342 float* sols) {
362 float* sols) {
363 bool quadratic = (kConfigRho < 1.0f); 343 bool quadratic = (kConfigRho < 1.0f);
364 const float* var_x0 = filtered_clear_var_.get(); 344 const float* var_x0 = filtered_clear_var_.get();
365 const float* var_n0 = filtered_noise_var_.get(); 345 const float* var_n0 = filtered_noise_var_.get();
366 346
367 for (int n = 0; n < start_freq; ++n) { 347 for (int n = 0; n < start_freq; ++n) {
368 sols[n] = 1.0f; 348 sols[n] = 1.0f;
369 } 349 }
370
371 // Analytic solution for optimal gains. See paper for derivation.
372 for (int n = start_freq - 1; n < bank_size_; ++n) { 350 for (int n = start_freq - 1; n < bank_size_; ++n) {
373 float alpha0, beta0, gamma0; 351 float alpha0, beta0, gamma0;
374 gamma0 = 0.5f * rho_[n] * var_x0[n] * var_n0[n] + 352 gamma0 = 0.5f * rho_[n] * var_x0[n] * var_n0[n] +
375 lambda * var_x0[n] * var_n0[n] * var_n0[n]; 353 lambda * var_x0[n] * var_n0[n] * var_n0[n];
376 beta0 = lambda * var_x0[n] * (2 - rho_[n]) * var_x0[n] * var_n0[n]; 354 beta0 = lambda * var_x0[n] * (2 - rho_[n]) * var_x0[n] * var_n0[n];
377 if (quadratic) { 355 if (quadratic) {
378 alpha0 = lambda * var_x0[n] * (1 - rho_[n]) * var_x0[n] * var_x0[n]; 356 alpha0 = lambda * var_x0[n] * (1 - rho_[n]) * var_x0[n] * var_x0[n];
379 sols[n] = 357 sols[n] = (-beta0 - sqrtf(beta0 * beta0 - 4 * alpha0 * gamma0))
380 (-beta0 - sqrtf(beta0 * beta0 - 4 * alpha0 * gamma0)) / (2 * alpha0); 358 / (2 * alpha0);
381 } else { 359 } else {
382 sols[n] = -gamma0 / beta0; 360 sols[n] = -gamma0 / beta0;
383 } 361 }
384 sols[n] = fmax(0, sols[n]); 362 sols[n] = fmax(0, sols[n]);
385 } 363 }
386 } 364 }
387 365
388 void IntelligibilityEnhancer::FilterVariance(const float* var, float* result) { 366 void IntelligibilityEnhancer::FilterVariance(const float* var, float* result) {
389 for (int i = 0; i < bank_size_; ++i) { 367 for (int i = 0; i < bank_size_; ++i) {
390 result[i] = DotProduct(filter_bank_[i], var, freqs_); 368 result[i] = DotProduct(filter_bank_[i], var, freqs_);
391 } 369 }
392 } 370 }
393 371
394 float IntelligibilityEnhancer::DotProduct(const float* a, 372 float IntelligibilityEnhancer::DotProduct(const float* a, const float* b,
395 const float* b, 373 int length) {
396 int length) {
397 float ret = 0.0f; 374 float ret = 0.0f;
398 375
399 for (int i = 0; i < length; ++i) { 376 for (int i = 0; i < length; ++i) {
400 ret = fmaf(a[i], b[i], ret); 377 ret = fmaf(a[i], b[i], ret);
401 } 378 }
402 return ret; 379 return ret;
403 } 380 }
404 381
405 } // namespace webrtc 382 } // namespace webrtc
383
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698