1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | #include <string.h> |
12 | #include <math.h> |
13 | |
14 | #include <stdlib.h> |
15 | #include "noise_suppression.h" |
16 | #include "ns_core.h" |
17 | #include "windows_private.h" |
18 | #include "fft4g.h" |
19 | #include "signal_processing_library.h" |
20 | |
21 | |
22 | void WebRtcNs_set_feature_extraction_parameters(NSinst_t* inst) { |
23 | |
24 | inst->featureExtractionParams.binSizeLrt = (float)0.1; |
25 | inst->featureExtractionParams.binSizeSpecFlat = (float)0.05; |
26 | inst->featureExtractionParams.binSizeSpecDiff = (float)0.1; |
27 | |
28 | |
29 | inst->featureExtractionParams.rangeAvgHistLrt = (float)1.0; |
30 | |
31 | |
32 | |
33 | inst->featureExtractionParams.factor1ModelPars = (float)1.20; |
34 | inst->featureExtractionParams.factor2ModelPars = (float)0.9; |
35 | |
36 | |
37 | |
38 | inst->featureExtractionParams.thresPosSpecFlat = (float)0.6; |
39 | |
40 | |
41 | inst->featureExtractionParams.limitPeakSpacingSpecFlat = |
42 | 2 * inst->featureExtractionParams.binSizeSpecFlat; |
43 | inst->featureExtractionParams.limitPeakSpacingSpecDiff = |
44 | 2 * inst->featureExtractionParams.binSizeSpecDiff; |
45 | |
46 | |
47 | inst->featureExtractionParams.limitPeakWeightsSpecFlat = (float)0.5; |
48 | inst->featureExtractionParams.limitPeakWeightsSpecDiff = (float)0.5; |
49 | |
50 | |
51 | inst->featureExtractionParams.thresFluctLrt = (float)0.05; |
52 | |
53 | |
54 | inst->featureExtractionParams.maxLrt = (float)1.0; |
55 | inst->featureExtractionParams.minLrt = (float)0.20; |
56 | |
57 | inst->featureExtractionParams.maxSpecFlat = (float)0.95; |
58 | inst->featureExtractionParams.minSpecFlat = (float)0.10; |
59 | |
60 | inst->featureExtractionParams.maxSpecDiff = (float)1.0; |
61 | inst->featureExtractionParams.minSpecDiff = (float)0.16; |
62 | |
63 | |
64 | inst->featureExtractionParams.thresWeightSpecFlat = (int)(0.3 |
65 | * (inst->modelUpdatePars[1])); |
66 | inst->featureExtractionParams.thresWeightSpecDiff = (int)(0.3 |
67 | * (inst->modelUpdatePars[1])); |
68 | } |
69 | |
70 | |
71 | int WebRtcNs_InitCore(NSinst_t* inst, WebRtc_UWord32 fs) { |
72 | int i; |
73 | |
74 | |
75 | |
76 | if (inst == NULL((void*)0)) { |
77 | return -1; |
78 | } |
79 | |
80 | |
81 | if (fs == 8000 || fs == 16000 || fs == 32000) { |
82 | inst->fs = fs; |
83 | } else { |
84 | return -1; |
85 | } |
86 | inst->windShift = 0; |
87 | if (fs == 8000) { |
88 | |
89 | inst->blockLen = 80; |
90 | inst->blockLen10ms = 80; |
91 | inst->anaLen = 128; |
92 | inst->window = kBlocks80w128; |
93 | inst->outLen = 0; |
94 | } else if (fs == 16000) { |
95 | |
96 | inst->blockLen = 160; |
97 | inst->blockLen10ms = 160; |
98 | inst->anaLen = 256; |
99 | inst->window = kBlocks160w256; |
100 | inst->outLen = 0; |
101 | } else if (fs == 32000) { |
102 | |
103 | inst->blockLen = 160; |
104 | inst->blockLen10ms = 160; |
105 | inst->anaLen = 256; |
106 | inst->window = kBlocks160w256; |
107 | inst->outLen = 0; |
108 | } |
109 | inst->magnLen = inst->anaLen / 2 + 1; |
110 | |
111 | |
112 | inst->ip[0] = 0; |
113 | memset(inst->dataBuf, 0, sizeof(float) * ANAL_BLOCKL_MAX256); |
114 | WebRtc_rdft(inst->anaLen, 1, inst->dataBuf, inst->ip, inst->wfft); |
115 | |
116 | memset(inst->dataBuf, 0, sizeof(float) * ANAL_BLOCKL_MAX256); |
117 | memset(inst->syntBuf, 0, sizeof(float) * ANAL_BLOCKL_MAX256); |
118 | |
119 | |
120 | memset(inst->dataBufHB, 0, sizeof(float) * ANAL_BLOCKL_MAX256); |
121 | |
122 | |
123 | memset(inst->quantile, 0, sizeof(float) * HALF_ANAL_BLOCKL129); |
124 | for (i = 0; i < SIMULT3 * HALF_ANAL_BLOCKL129; i++) { |
125 | inst->lquantile[i] = (float)8.0; |
126 | inst->density[i] = (float)0.3; |
127 | } |
128 | |
129 | for (i = 0; i < SIMULT3; i++) { |
130 | inst->counter[i] = (int)floor((float)(END_STARTUP_LONG200 * (i + 1)) / (float)SIMULT3); |
131 | } |
132 | |
133 | inst->updates = 0; |
134 | |
135 | |
136 | for (i = 0; i < HALF_ANAL_BLOCKL129; i++) { |
137 | inst->smooth[i] = (float)1.0; |
138 | } |
139 | |
140 | |
141 | inst->aggrMode = 0; |
142 | |
143 | |
144 | inst->priorSpeechProb = (float)0.5; |
145 | for (i = 0; i < HALF_ANAL_BLOCKL129; i++) { |
146 | inst->magnPrev[i] = (float)0.0; |
147 | inst->noisePrev[i] = (float)0.0; |
148 | inst->logLrtTimeAvg[i] = LRT_FEATURE_THR(float)0.5; |
149 | inst->magnAvgPause[i] = (float)0.0; |
150 | inst->speechProbHB[i] = (float)0.0; |
151 | inst->initMagnEst[i] = (float)0.0; |
152 | } |
153 | |
154 | |
155 | inst->featureData[0] = SF_FEATURE_THR(float)0.5; |
156 | inst->featureData[1] = (float)0.0; |
157 | inst->featureData[2] = (float)0.0; |
158 | inst->featureData[3] = LRT_FEATURE_THR(float)0.5; |
159 | inst->featureData[4] = SF_FEATURE_THR(float)0.5; |
160 | inst->featureData[5] = (float)0.0; |
161 | inst->featureData[6] = (float)0.0; |
162 | |
163 | |
164 | for (i = 0; i < HIST_PAR_EST1000; i++) { |
165 | inst->histLrt[i] = 0; |
166 | inst->histSpecFlat[i] = 0; |
167 | inst->histSpecDiff[i] = 0; |
168 | } |
169 | |
170 | inst->blockInd = -1; |
171 | inst->priorModelPars[0] = LRT_FEATURE_THR(float)0.5; |
172 | inst->priorModelPars[1] = (float)0.5; |
173 | |
174 | inst->priorModelPars[2] = (float)1.0; |
175 | |
176 | inst->priorModelPars[3] = (float)0.5; |
177 | |
178 | inst->priorModelPars[4] = (float)1.0; |
179 | inst->priorModelPars[5] = (float)0.0; |
180 | |
181 | inst->priorModelPars[6] = (float)0.0; |
182 | |
183 | |
184 | inst->modelUpdatePars[0] = 2; |
185 | |
186 | inst->modelUpdatePars[1] = 500; |
187 | inst->modelUpdatePars[2] = 0; |
188 | |
189 | inst->modelUpdatePars[3] = inst->modelUpdatePars[1]; |
190 | |
191 | inst->signalEnergy = 0.0; |
192 | inst->sumMagn = 0.0; |
193 | inst->whiteNoiseLevel = 0.0; |
194 | inst->pinkNoiseNumerator = 0.0; |
195 | inst->pinkNoiseExp = 0.0; |
196 | |
197 | WebRtcNs_set_feature_extraction_parameters(inst); |
198 | |
199 | |
200 | WebRtcNs_set_policy_core(inst, 0); |
201 | |
202 | |
203 | memset(inst->outBuf, 0, sizeof(float) * 3 * BLOCKL_MAX160); |
204 | |
205 | inst->initFlag = 1; |
206 | return 0; |
207 | } |
208 | |
209 | int WebRtcNs_set_policy_core(NSinst_t* inst, int mode) { |
210 | |
211 | if (mode < 0 || mode > 3) { |
212 | return (-1); |
213 | } |
214 | |
215 | inst->aggrMode = mode; |
216 | if (mode == 0) { |
217 | inst->overdrive = (float)1.0; |
218 | inst->denoiseBound = (float)0.5; |
219 | inst->gainmap = 0; |
220 | } else if (mode == 1) { |
221 | |
222 | inst->overdrive = (float)1.0; |
223 | inst->denoiseBound = (float)0.25; |
224 | inst->gainmap = 1; |
225 | } else if (mode == 2) { |
226 | |
227 | inst->overdrive = (float)1.1; |
228 | inst->denoiseBound = (float)0.125; |
229 | inst->gainmap = 1; |
230 | } else if (mode == 3) { |
231 | |
232 | inst->overdrive = (float)1.25; |
233 | inst->denoiseBound = (float)0.09; |
234 | inst->gainmap = 1; |
235 | } |
236 | return 0; |
237 | } |
238 | |
239 | |
240 | void WebRtcNs_NoiseEstimation(NSinst_t* inst, float* magn, float* noise) { |
241 | int i, s, offset; |
242 | float lmagn[HALF_ANAL_BLOCKL129], delta; |
243 | |
244 | if (inst->updates < END_STARTUP_LONG200) { |
245 | inst->updates++; |
246 | } |
247 | |
248 | for (i = 0; i < inst->magnLen; i++) { |
249 | lmagn[i] = (float)log(magn[i]); |
250 | } |
251 | |
252 | |
253 | for (s = 0; s < SIMULT3; s++) { |
254 | offset = s * inst->magnLen; |
255 | |
256 | |
257 | for (i = 0; i < inst->magnLen; i++) { |
258 | |
259 | if (inst->density[offset + i] > 1.0) { |
260 | delta = FACTOR(float)40.0 * (float)1.0 / inst->density[offset + i]; |
261 | } else { |
262 | delta = FACTOR(float)40.0; |
263 | } |
264 | |
265 | |
266 | if (lmagn[i] > inst->lquantile[offset + i]) { |
267 | inst->lquantile[offset + i] += QUANTILE(float)0.25 * delta |
268 | / (float)(inst->counter[s] + 1); |
269 | } else { |
270 | inst->lquantile[offset + i] -= ((float)1.0 - QUANTILE(float)0.25) * delta |
271 | / (float)(inst->counter[s] + 1); |
272 | } |
273 | |
274 | |
275 | if (fabs(lmagn[i] - inst->lquantile[offset + i]) < WIDTH(float)0.01) { |
276 | inst->density[offset + i] = ((float)inst->counter[s] * inst->density[offset |
277 | + i] + (float)1.0 / ((float)2.0 * WIDTH(float)0.01)) / (float)(inst->counter[s] + 1); |
278 | } |
279 | } |
280 | |
281 | if (inst->counter[s] >= END_STARTUP_LONG200) { |
282 | inst->counter[s] = 0; |
283 | if (inst->updates >= END_STARTUP_LONG200) { |
284 | for (i = 0; i < inst->magnLen; i++) { |
285 | inst->quantile[i] = (float)exp(inst->lquantile[offset + i]); |
286 | } |
287 | } |
288 | } |
289 | |
290 | inst->counter[s]++; |
291 | } |
292 | |
293 | |
294 | if (inst->updates < END_STARTUP_LONG200) { |
295 | |
296 | for (i = 0; i < inst->magnLen; i++) { |
297 | inst->quantile[i] = (float)exp(inst->lquantile[offset + i]); |
298 | } |
299 | } |
300 | |
301 | for (i = 0; i < inst->magnLen; i++) { |
302 | noise[i] = inst->quantile[i]; |
303 | } |
304 | } |
305 | |
306 | |
307 | |
308 | |
309 | |
310 | |
311 | void WebRtcNs_FeatureParameterExtraction(NSinst_t* inst, int flag) { |
312 | int i, useFeatureSpecFlat, useFeatureSpecDiff, numHistLrt; |
313 | int maxPeak1, maxPeak2; |
314 | int weightPeak1SpecFlat, weightPeak2SpecFlat, weightPeak1SpecDiff, weightPeak2SpecDiff; |
315 | |
316 | float binMid, featureSum; |
317 | float posPeak1SpecFlat, posPeak2SpecFlat, posPeak1SpecDiff, posPeak2SpecDiff; |
318 | float fluctLrt, avgHistLrt, avgSquareHistLrt, avgHistLrtCompl; |
319 | |
320 | |
321 | |
322 | |
323 | |
324 | |
325 | |
326 | if (flag == 0) { |
327 | |
328 | if ((inst->featureData[3] < HIST_PAR_EST1000 * inst->featureExtractionParams.binSizeLrt) |
329 | && (inst->featureData[3] >= 0.0)) { |
330 | i = (int)(inst->featureData[3] / inst->featureExtractionParams.binSizeLrt); |
331 | inst->histLrt[i]++; |
332 | } |
333 | |
334 | if ((inst->featureData[0] < HIST_PAR_EST1000 |
335 | * inst->featureExtractionParams.binSizeSpecFlat) |
336 | && (inst->featureData[0] >= 0.0)) { |
337 | i = (int)(inst->featureData[0] / inst->featureExtractionParams.binSizeSpecFlat); |
338 | inst->histSpecFlat[i]++; |
339 | } |
340 | |
341 | if ((inst->featureData[4] < HIST_PAR_EST1000 |
342 | * inst->featureExtractionParams.binSizeSpecDiff) |
343 | && (inst->featureData[4] >= 0.0)) { |
344 | i = (int)(inst->featureData[4] / inst->featureExtractionParams.binSizeSpecDiff); |
345 | inst->histSpecDiff[i]++; |
346 | } |
347 | } |
348 | |
349 | |
350 | if (flag == 1) { |
351 | |
352 | avgHistLrt = 0.0; |
353 | avgHistLrtCompl = 0.0; |
354 | avgSquareHistLrt = 0.0; |
355 | numHistLrt = 0; |
356 | for (i = 0; i < HIST_PAR_EST1000; i++) { |
357 | binMid = ((float)i + (float)0.5) * inst->featureExtractionParams.binSizeLrt; |
358 | if (binMid <= inst->featureExtractionParams.rangeAvgHistLrt) { |
359 | avgHistLrt += inst->histLrt[i] * binMid; |
360 | numHistLrt += inst->histLrt[i]; |
361 | } |
362 | avgSquareHistLrt += inst->histLrt[i] * binMid * binMid; |
363 | avgHistLrtCompl += inst->histLrt[i] * binMid; |
364 | } |
365 | if (numHistLrt > 0) { |
366 | avgHistLrt = avgHistLrt / ((float)numHistLrt); |
367 | } |
368 | avgHistLrtCompl = avgHistLrtCompl / ((float)inst->modelUpdatePars[1]); |
369 | avgSquareHistLrt = avgSquareHistLrt / ((float)inst->modelUpdatePars[1]); |
370 | fluctLrt = avgSquareHistLrt - avgHistLrt * avgHistLrtCompl; |
371 | |
372 | if (fluctLrt < inst->featureExtractionParams.thresFluctLrt) { |
373 | |
374 | inst->priorModelPars[0] = inst->featureExtractionParams.maxLrt; |
375 | } else { |
376 | inst->priorModelPars[0] = inst->featureExtractionParams.factor1ModelPars |
377 | * avgHistLrt; |
378 | |
379 | if (inst->priorModelPars[0] < inst->featureExtractionParams.minLrt) { |
380 | inst->priorModelPars[0] = inst->featureExtractionParams.minLrt; |
381 | } |
382 | if (inst->priorModelPars[0] > inst->featureExtractionParams.maxLrt) { |
383 | inst->priorModelPars[0] = inst->featureExtractionParams.maxLrt; |
384 | } |
385 | } |
386 | |
387 | |
388 | |
389 | |
390 | maxPeak1 = 0; |
391 | maxPeak2 = 0; |
392 | posPeak1SpecFlat = 0.0; |
393 | posPeak2SpecFlat = 0.0; |
394 | weightPeak1SpecFlat = 0; |
395 | weightPeak2SpecFlat = 0; |
396 | |
397 | |
398 | for (i = 0; i < HIST_PAR_EST1000; i++) { |
399 | binMid = ((float)i + (float)0.5) * inst->featureExtractionParams.binSizeSpecFlat; |
400 | if (inst->histSpecFlat[i] > maxPeak1) { |
401 | |
402 | maxPeak2 = maxPeak1; |
403 | weightPeak2SpecFlat = weightPeak1SpecFlat; |
404 | posPeak2SpecFlat = posPeak1SpecFlat; |
405 | |
406 | maxPeak1 = inst->histSpecFlat[i]; |
407 | weightPeak1SpecFlat = inst->histSpecFlat[i]; |
408 | posPeak1SpecFlat = binMid; |
409 | } else if (inst->histSpecFlat[i] > maxPeak2) { |
410 | |
411 | maxPeak2 = inst->histSpecFlat[i]; |
412 | weightPeak2SpecFlat = inst->histSpecFlat[i]; |
413 | posPeak2SpecFlat = binMid; |
414 | } |
415 | } |
416 | |
417 | |
418 | maxPeak1 = 0; |
419 | maxPeak2 = 0; |
420 | posPeak1SpecDiff = 0.0; |
421 | posPeak2SpecDiff = 0.0; |
422 | weightPeak1SpecDiff = 0; |
423 | weightPeak2SpecDiff = 0; |
424 | |
425 | for (i = 0; i < HIST_PAR_EST1000; i++) { |
426 | binMid = ((float)i + (float)0.5) * inst->featureExtractionParams.binSizeSpecDiff; |
427 | if (inst->histSpecDiff[i] > maxPeak1) { |
428 | |
429 | maxPeak2 = maxPeak1; |
430 | weightPeak2SpecDiff = weightPeak1SpecDiff; |
431 | posPeak2SpecDiff = posPeak1SpecDiff; |
432 | |
433 | maxPeak1 = inst->histSpecDiff[i]; |
434 | weightPeak1SpecDiff = inst->histSpecDiff[i]; |
435 | posPeak1SpecDiff = binMid; |
436 | } else if (inst->histSpecDiff[i] > maxPeak2) { |
437 | |
438 | maxPeak2 = inst->histSpecDiff[i]; |
439 | weightPeak2SpecDiff = inst->histSpecDiff[i]; |
440 | posPeak2SpecDiff = binMid; |
441 | } |
442 | } |
443 | |
444 | |
445 | useFeatureSpecFlat = 1; |
446 | |
447 | if ((fabs(posPeak2SpecFlat - posPeak1SpecFlat) |
448 | < inst->featureExtractionParams.limitPeakSpacingSpecFlat) |
449 | && (weightPeak2SpecFlat |
450 | > inst->featureExtractionParams.limitPeakWeightsSpecFlat |
451 | * weightPeak1SpecFlat)) { |
452 | weightPeak1SpecFlat += weightPeak2SpecFlat; |
453 | posPeak1SpecFlat = (float)0.5 * (posPeak1SpecFlat + posPeak2SpecFlat); |
454 | } |
455 | |
456 | if (weightPeak1SpecFlat < inst->featureExtractionParams.thresWeightSpecFlat |
457 | || posPeak1SpecFlat < inst->featureExtractionParams.thresPosSpecFlat) { |
458 | useFeatureSpecFlat = 0; |
459 | } |
460 | |
461 | if (useFeatureSpecFlat == 1) { |
462 | |
463 | inst->priorModelPars[1] = inst->featureExtractionParams.factor2ModelPars |
464 | * posPeak1SpecFlat; |
465 | |
466 | if (inst->priorModelPars[1] < inst->featureExtractionParams.minSpecFlat) { |
467 | inst->priorModelPars[1] = inst->featureExtractionParams.minSpecFlat; |
468 | } |
469 | if (inst->priorModelPars[1] > inst->featureExtractionParams.maxSpecFlat) { |
470 | inst->priorModelPars[1] = inst->featureExtractionParams.maxSpecFlat; |
471 | } |
472 | } |
473 | |
474 | |
475 | |
476 | useFeatureSpecDiff = 1; |
477 | |
478 | if ((fabs(posPeak2SpecDiff - posPeak1SpecDiff) |
479 | < inst->featureExtractionParams.limitPeakSpacingSpecDiff) |
480 | && (weightPeak2SpecDiff |
481 | > inst->featureExtractionParams.limitPeakWeightsSpecDiff |
482 | * weightPeak1SpecDiff)) { |
483 | weightPeak1SpecDiff += weightPeak2SpecDiff; |
484 | posPeak1SpecDiff = (float)0.5 * (posPeak1SpecDiff + posPeak2SpecDiff); |
485 | } |
486 | |
487 | inst->priorModelPars[3] = inst->featureExtractionParams.factor1ModelPars |
488 | * posPeak1SpecDiff; |
489 | |
490 | if (weightPeak1SpecDiff < inst->featureExtractionParams.thresWeightSpecDiff) { |
491 | useFeatureSpecDiff = 0; |
492 | } |
493 | |
494 | if (inst->priorModelPars[3] < inst->featureExtractionParams.minSpecDiff) { |
495 | inst->priorModelPars[3] = inst->featureExtractionParams.minSpecDiff; |
496 | } |
497 | if (inst->priorModelPars[3] > inst->featureExtractionParams.maxSpecDiff) { |
498 | inst->priorModelPars[3] = inst->featureExtractionParams.maxSpecDiff; |
499 | } |
500 | |
501 | |
502 | |
503 | |
504 | if (fluctLrt < inst->featureExtractionParams.thresFluctLrt) { |
505 | useFeatureSpecDiff = 0; |
506 | } |
507 | |
508 | |
509 | |
510 | |
511 | |
512 | featureSum = (float)(1 + useFeatureSpecFlat + useFeatureSpecDiff); |
513 | inst->priorModelPars[4] = (float)1.0 / featureSum; |
514 | inst->priorModelPars[5] = ((float)useFeatureSpecFlat) / featureSum; |
515 | inst->priorModelPars[6] = ((float)useFeatureSpecDiff) / featureSum; |
516 | |
517 | |
518 | if (inst->modelUpdatePars[0] >= 1) { |
519 | for (i = 0; i < HIST_PAR_EST1000; i++) { |
520 | inst->histLrt[i] = 0; |
521 | inst->histSpecFlat[i] = 0; |
522 | inst->histSpecDiff[i] = 0; |
523 | } |
524 | } |
525 | } |
526 | } |
527 | |
528 | |
529 | |
530 | |
531 | void WebRtcNs_ComputeSpectralFlatness(NSinst_t* inst, float* magnIn) { |
532 | int i; |
533 | int shiftLP = 1; |
534 | float avgSpectralFlatnessNum, avgSpectralFlatnessDen, spectralTmp; |
535 | |
536 | |
537 | |
538 | avgSpectralFlatnessNum = 0.0; |
539 | avgSpectralFlatnessDen = inst->sumMagn; |
540 | for (i = 0; i < shiftLP; i++) { |
541 | avgSpectralFlatnessDen -= magnIn[i]; |
542 | } |
543 | |
544 | for (i = shiftLP; i < inst->magnLen; i++) { |
545 | if (magnIn[i] > 0.0) { |
546 | avgSpectralFlatnessNum += (float)log(magnIn[i]); |
547 | } else { |
548 | inst->featureData[0] -= SPECT_FL_TAVG(float)0.30 * inst->featureData[0]; |
549 | return; |
550 | } |
551 | } |
552 | |
553 | avgSpectralFlatnessDen = avgSpectralFlatnessDen / inst->magnLen; |
554 | avgSpectralFlatnessNum = avgSpectralFlatnessNum / inst->magnLen; |
555 | |
556 | |
557 | spectralTmp = (float)exp(avgSpectralFlatnessNum) / avgSpectralFlatnessDen; |
558 | |
559 | |
560 | inst->featureData[0] += SPECT_FL_TAVG(float)0.30 * (spectralTmp - inst->featureData[0]); |
561 | |
562 | } |
563 | |
564 | |
565 | |
566 | |
567 | |
568 | void WebRtcNs_ComputeSpectralDifference(NSinst_t* inst, float* magnIn) { |
569 | |
570 | int i; |
571 | float avgPause, avgMagn, covMagnPause, varPause, varMagn, avgDiffNormMagn; |
572 | |
573 | avgPause = 0.0; |
574 | avgMagn = inst->sumMagn; |
575 | |
576 | for (i = 0; i < inst->magnLen; i++) { |
577 | |
578 | avgPause += inst->magnAvgPause[i]; |
579 | } |
580 | avgPause = avgPause / ((float)inst->magnLen); |
581 | avgMagn = avgMagn / ((float)inst->magnLen); |
582 | |
583 | covMagnPause = 0.0; |
584 | varPause = 0.0; |
585 | varMagn = 0.0; |
586 | |
587 | for (i = 0; i < inst->magnLen; i++) { |
588 | covMagnPause += (magnIn[i] - avgMagn) * (inst->magnAvgPause[i] - avgPause); |
589 | varPause += (inst->magnAvgPause[i] - avgPause) * (inst->magnAvgPause[i] - avgPause); |
590 | varMagn += (magnIn[i] - avgMagn) * (magnIn[i] - avgMagn); |
591 | } |
592 | covMagnPause = covMagnPause / ((float)inst->magnLen); |
593 | varPause = varPause / ((float)inst->magnLen); |
594 | varMagn = varMagn / ((float)inst->magnLen); |
595 | |
596 | inst->featureData[6] += inst->signalEnergy; |
597 | |
598 | avgDiffNormMagn = varMagn - (covMagnPause * covMagnPause) / (varPause + (float)0.0001); |
599 | |
600 | avgDiffNormMagn = (float)(avgDiffNormMagn / (inst->featureData[5] + (float)0.0001)); |
601 | inst->featureData[4] += SPECT_DIFF_TAVG(float)0.30 * (avgDiffNormMagn - inst->featureData[4]); |
602 | } |
603 | |
604 | |
605 | |
606 | |
607 | |
608 | |
609 | |
610 | void WebRtcNs_SpeechNoiseProb(NSinst_t* inst, float* probSpeechFinal, float* snrLocPrior, |
611 | float* snrLocPost) { |
612 | int i, sgnMap; |
613 | float invLrt, gainPrior, indPrior; |
614 | float logLrtTimeAvgKsum, besselTmp; |
615 | float indicator0, indicator1, indicator2; |
616 | float tmpFloat1, tmpFloat2; |
617 | float weightIndPrior0, weightIndPrior1, weightIndPrior2; |
618 | float threshPrior0, threshPrior1, threshPrior2; |
619 | float widthPrior, widthPrior0, widthPrior1, widthPrior2; |
620 | |
621 | widthPrior0 = WIDTH_PR_MAP(float)4.0; |
622 | widthPrior1 = (float)2.0 * WIDTH_PR_MAP(float)4.0; |
623 | |
624 | widthPrior2 = (float)2.0 * WIDTH_PR_MAP(float)4.0; |
625 | |
626 | |
627 | threshPrior0 = inst->priorModelPars[0]; |
628 | threshPrior1 = inst->priorModelPars[1]; |
629 | threshPrior2 = inst->priorModelPars[3]; |
630 | |
631 | |
632 | sgnMap = (int)(inst->priorModelPars[2]); |
633 | |
634 | |
635 | weightIndPrior0 = inst->priorModelPars[4]; |
636 | weightIndPrior1 = inst->priorModelPars[5]; |
637 | weightIndPrior2 = inst->priorModelPars[6]; |
638 | |
639 | |
640 | |
641 | logLrtTimeAvgKsum = 0.0; |
642 | for (i = 0; i < inst->magnLen; i++) { |
643 | tmpFloat1 = (float)1.0 + (float)2.0 * snrLocPrior[i]; |
644 | tmpFloat2 = (float)2.0 * snrLocPrior[i] / (tmpFloat1 + (float)0.0001); |
645 | besselTmp = (snrLocPost[i] + (float)1.0) * tmpFloat2; |
646 | inst->logLrtTimeAvg[i] += LRT_TAVG(float)0.50 * (besselTmp - (float)log(tmpFloat1) |
647 | - inst->logLrtTimeAvg[i]); |
648 | logLrtTimeAvgKsum += inst->logLrtTimeAvg[i]; |
649 | } |
650 | logLrtTimeAvgKsum = (float)logLrtTimeAvgKsum / (inst->magnLen); |
651 | inst->featureData[3] = logLrtTimeAvgKsum; |
652 | |
653 | |
654 | |
655 | |
656 | |
657 | |
658 | |
659 | widthPrior = widthPrior0; |
660 | |
661 | if (logLrtTimeAvgKsum < threshPrior0) { |
662 | widthPrior = widthPrior1; |
663 | } |
664 | |
665 | indicator0 = (float)0.5 * ((float)tanh(widthPrior * |
666 | (logLrtTimeAvgKsum - threshPrior0)) + (float)1.0); |
667 | |
668 | |
669 | tmpFloat1 = inst->featureData[0]; |
670 | widthPrior = widthPrior0; |
671 | |
672 | if (sgnMap == 1 && (tmpFloat1 > threshPrior1)) { |
673 | widthPrior = widthPrior1; |
674 | } |
675 | if (sgnMap == -1 && (tmpFloat1 < threshPrior1)) { |
676 | widthPrior = widthPrior1; |
677 | } |
678 | |
679 | indicator1 = (float)0.5 * ((float)tanh((float)sgnMap * |
680 | widthPrior * (threshPrior1 - tmpFloat1)) + (float)1.0); |
681 | |
682 | |
683 | tmpFloat1 = inst->featureData[4]; |
684 | widthPrior = widthPrior0; |
685 | |
686 | if (tmpFloat1 < threshPrior2) { |
687 | widthPrior = widthPrior2; |
688 | } |
689 | |
690 | indicator2 = (float)0.5 * ((float)tanh(widthPrior * (tmpFloat1 - threshPrior2)) |
691 | + (float)1.0); |
692 | |
693 | |
694 | indPrior = weightIndPrior0 * indicator0 + weightIndPrior1 * indicator1 + weightIndPrior2 |
695 | * indicator2; |
696 | |
697 | |
698 | |
699 | inst->priorSpeechProb += PRIOR_UPDATE(float)0.10 * (indPrior - inst->priorSpeechProb); |
700 | |
701 | if (inst->priorSpeechProb > 1.0) { |
702 | inst->priorSpeechProb = (float)1.0; |
703 | } |
704 | if (inst->priorSpeechProb < 0.01) { |
705 | inst->priorSpeechProb = (float)0.01; |
706 | } |
707 | |
708 | |
709 | gainPrior = ((float)1.0 - inst->priorSpeechProb) / (inst->priorSpeechProb + (float)0.0001); |
710 | for (i = 0; i < inst->magnLen; i++) { |
711 | invLrt = (float)exp(-inst->logLrtTimeAvg[i]); |
712 | invLrt = (float)gainPrior * invLrt; |
713 | probSpeechFinal[i] = (float)1.0 / ((float)1.0 + invLrt); |
714 | } |
715 | } |
716 | |
717 | int WebRtcNs_ProcessCore(NSinst_t* inst, |
718 | short* speechFrame, |
719 | short* speechFrameHB, |
720 | short* outFrame, |
721 | short* outFrameHB) { |
722 | |
723 | |
724 | int flagHB = 0; |
725 | int i; |
726 | const int kStartBand = 5; |
727 | int updateParsFlag; |
728 | |
729 | float energy1, energy2, gain, factor, factor1, factor2; |
730 | float signalEnergy, sumMagn; |
731 | float snrPrior, currentEstimateStsa; |
732 | float tmpFloat1, tmpFloat2, tmpFloat3, probSpeech, probNonSpeech; |
733 | float gammaNoiseTmp, gammaNoiseOld; |
734 | float noiseUpdateTmp, fTmp, dTmp; |
735 | float fin[BLOCKL_MAX160], fout[BLOCKL_MAX160]; |
736 | float winData[ANAL_BLOCKL_MAX256]; |
737 | float magn[HALF_ANAL_BLOCKL129], noise[HALF_ANAL_BLOCKL129]; |
738 | float theFilter[HALF_ANAL_BLOCKL129], theFilterTmp[HALF_ANAL_BLOCKL129]; |
739 | float snrLocPost[HALF_ANAL_BLOCKL129], snrLocPrior[HALF_ANAL_BLOCKL129]; |
740 | float probSpeechFinal[HALF_ANAL_BLOCKL129] = { 0 }; |
741 | float previousEstimateStsa[HALF_ANAL_BLOCKL129]; |
742 | float real[ANAL_BLOCKL_MAX256], imag[HALF_ANAL_BLOCKL129]; |
743 | |
744 | float sum_log_i = 0.0; |
745 | float sum_log_i_square = 0.0; |
746 | float sum_log_magn = 0.0; |
747 | float sum_log_i_log_magn = 0.0; |
748 | float parametric_noise = 0.0; |
749 | float parametric_exp = 0.0; |
750 | float parametric_num = 0.0; |
751 | |
752 | |
753 | int deltaBweHB = 1; |
754 | int deltaGainHB = 1; |
755 | float decayBweHB = 1.0; |
756 | float gainMapParHB = 1.0; |
757 | float gainTimeDomainHB = 1.0; |
758 | float avgProbSpeechHB, avgProbSpeechHBTmp, avgFilterGainHB, gainModHB; |
759 | |
760 | |
761 | if (inst->initFlag != 1) { |
| |
762 | return (-1); |
763 | } |
764 | |
765 | if (inst->fs == 32000) { |
| |
766 | if (speechFrameHB == NULL((void*)0)) { |
767 | return -1; |
768 | } |
769 | flagHB = 1; |
770 | |
771 | deltaBweHB = (int)inst->magnLen / 4; |
772 | deltaGainHB = deltaBweHB; |
773 | } |
774 | |
775 | updateParsFlag = inst->modelUpdatePars[0]; |
776 | |
777 | |
778 | |
779 | |
780 | for (i = 0; i < inst->blockLen10ms; i++) { |
| 3 | | Loop condition is false. Execution continues on line 785 | |
|
781 | fin[i] = (float)speechFrame[i]; |
782 | } |
783 | |
784 | memcpy(inst->dataBuf, inst->dataBuf + inst->blockLen10ms, |
785 | sizeof(float) * (inst->anaLen - inst->blockLen10ms)); |
786 | memcpy(inst->dataBuf + inst->anaLen - inst->blockLen10ms, fin, |
787 | sizeof(float) * inst->blockLen10ms); |
788 | |
789 | if (flagHB == 1) { |
| |
790 | |
791 | for (i = 0; i < inst->blockLen10ms; i++) { |
792 | fin[i] = (float)speechFrameHB[i]; |
793 | } |
794 | |
795 | memcpy(inst->dataBufHB, inst->dataBufHB + inst->blockLen10ms, |
796 | sizeof(float) * (inst->anaLen - inst->blockLen10ms)); |
797 | memcpy(inst->dataBufHB + inst->anaLen - inst->blockLen10ms, fin, |
798 | sizeof(float) * inst->blockLen10ms); |
799 | } |
800 | |
801 | |
802 | if (inst->outLen == 0) { |
| |
803 | |
804 | energy1 = 0.0; |
805 | for (i = 0; i < inst->anaLen; i++) { |
| 6 | | Loop condition is false. Execution continues on line 809 | |
|
806 | winData[i] = inst->window[i] * inst->dataBuf[i]; |
807 | energy1 += winData[i] * winData[i]; |
808 | } |
809 | if (energy1 == 0.0) { |
| |
810 | |
811 | |
812 | |
813 | |
814 | |
815 | |
816 | |
817 | |
818 | |
819 | |
820 | for (i = inst->windShift; i < inst->blockLen + inst->windShift; i++) { |
821 | fout[i - inst->windShift] = inst->syntBuf[i]; |
822 | } |
823 | |
824 | memcpy(inst->syntBuf, inst->syntBuf + inst->blockLen, |
825 | sizeof(float) * (inst->anaLen - inst->blockLen)); |
826 | memset(inst->syntBuf + inst->anaLen - inst->blockLen, 0, |
827 | sizeof(float) * inst->blockLen); |
828 | |
829 | |
830 | inst->outLen = inst->blockLen - inst->blockLen10ms; |
831 | if (inst->blockLen > inst->blockLen10ms) { |
832 | for (i = 0; i < inst->outLen; i++) { |
833 | inst->outBuf[i] = fout[i + inst->blockLen10ms]; |
834 | } |
835 | } |
836 | |
837 | for (i = 0; i < inst->blockLen10ms; i++) { |
838 | dTmp = fout[i]; |
839 | if (dTmp < WEBRTC_SPL_WORD16_MIN-32768) { |
840 | dTmp = WEBRTC_SPL_WORD16_MIN-32768; |
841 | } else if (dTmp > WEBRTC_SPL_WORD16_MAX32767) { |
842 | dTmp = WEBRTC_SPL_WORD16_MAX32767; |
843 | } |
844 | outFrame[i] = (short)dTmp; |
845 | } |
846 | |
847 | |
848 | if (flagHB == 1) { |
849 | for (i = 0; i < inst->blockLen10ms; i++) { |
850 | dTmp = inst->dataBufHB[i]; |
851 | if (dTmp < WEBRTC_SPL_WORD16_MIN-32768) { |
852 | dTmp = WEBRTC_SPL_WORD16_MIN-32768; |
853 | } else if (dTmp > WEBRTC_SPL_WORD16_MAX32767) { |
854 | dTmp = WEBRTC_SPL_WORD16_MAX32767; |
855 | } |
856 | outFrameHB[i] = (short)dTmp; |
857 | } |
858 | } |
859 | |
860 | return 0; |
861 | } |
862 | |
863 | |
864 | inst->blockInd++; |
865 | |
866 | WebRtc_rdft(inst->anaLen, 1, winData, inst->ip, inst->wfft); |
867 | |
868 | imag[0] = 0; |
869 | real[0] = winData[0]; |
870 | magn[0] = (float)(fabs(real[0]) + 1.0f); |
871 | imag[inst->magnLen - 1] = 0; |
872 | real[inst->magnLen - 1] = winData[1]; |
873 | magn[inst->magnLen - 1] = (float)(fabs(real[inst->magnLen - 1]) + 1.0f); |
874 | signalEnergy = (float)(real[0] * real[0]) + |
875 | (float)(real[inst->magnLen - 1] * real[inst->magnLen - 1]); |
876 | sumMagn = magn[0] + magn[inst->magnLen - 1]; |
877 | if (inst->blockInd < END_STARTUP_SHORT50) { |
| |
878 | inst->initMagnEst[0] += magn[0]; |
879 | inst->initMagnEst[inst->magnLen - 1] += magn[inst->magnLen - 1]; |
880 | tmpFloat2 = log((float)(inst->magnLen - 1)); |
881 | sum_log_i = tmpFloat2; |
882 | sum_log_i_square = tmpFloat2 * tmpFloat2; |
883 | tmpFloat1 = log(magn[inst->magnLen - 1]); |
884 | sum_log_magn = tmpFloat1; |
885 | sum_log_i_log_magn = tmpFloat2 * tmpFloat1; |
886 | } |
887 | for (i = 1; i < inst->magnLen - 1; i++) { |
| 9 | | Loop condition is false. Execution continues on line 908 | |
|
888 | real[i] = winData[2 * i]; |
889 | imag[i] = winData[2 * i + 1]; |
890 | |
891 | fTmp = real[i] * real[i]; |
892 | fTmp += imag[i] * imag[i]; |
893 | signalEnergy += fTmp; |
894 | magn[i] = ((float)sqrt(fTmp)) + 1.0f; |
895 | sumMagn += magn[i]; |
896 | if (inst->blockInd < END_STARTUP_SHORT50) { |
897 | inst->initMagnEst[i] += magn[i]; |
898 | if (i >= kStartBand) { |
899 | tmpFloat2 = log((float)i); |
900 | sum_log_i += tmpFloat2; |
901 | sum_log_i_square += tmpFloat2 * tmpFloat2; |
902 | tmpFloat1 = log(magn[i]); |
903 | sum_log_magn += tmpFloat1; |
904 | sum_log_i_log_magn += tmpFloat2 * tmpFloat1; |
905 | } |
906 | } |
907 | } |
908 | signalEnergy = signalEnergy / ((float)inst->magnLen); |
909 | inst->signalEnergy = signalEnergy; |
910 | inst->sumMagn = sumMagn; |
911 | |
912 | |
913 | WebRtcNs_ComputeSpectralFlatness(inst, magn); |
914 | |
915 | WebRtcNs_NoiseEstimation(inst, magn, noise); |
916 | |
917 | if (inst->blockInd < END_STARTUP_SHORT50) { |
| |
918 | |
919 | inst->whiteNoiseLevel += sumMagn / ((float)inst->magnLen) * inst->overdrive; |
920 | |
921 | tmpFloat1 = sum_log_i_square * ((float)(inst->magnLen - kStartBand)); |
922 | tmpFloat1 -= (sum_log_i * sum_log_i); |
923 | tmpFloat2 = (sum_log_i_square * sum_log_magn - sum_log_i * sum_log_i_log_magn); |
924 | tmpFloat3 = tmpFloat2 / tmpFloat1; |
925 | |
926 | if (tmpFloat3 < 0.0f) { |
927 | tmpFloat3 = 0.0f; |
928 | } |
929 | inst->pinkNoiseNumerator += tmpFloat3; |
930 | tmpFloat2 = (sum_log_i * sum_log_magn); |
931 | tmpFloat2 -= ((float)(inst->magnLen - kStartBand)) * sum_log_i_log_magn; |
932 | tmpFloat3 = tmpFloat2 / tmpFloat1; |
933 | |
934 | if (tmpFloat3 < 0.0f) { |
935 | tmpFloat3 = 0.0f; |
936 | } |
937 | if (tmpFloat3 > 1.0f) { |
938 | tmpFloat3 = 1.0f; |
939 | } |
940 | inst->pinkNoiseExp += tmpFloat3; |
941 | |
942 | |
943 | if (inst->pinkNoiseExp == 0.0f) { |
944 | |
945 | parametric_noise = inst->whiteNoiseLevel; |
946 | } else { |
947 | |
948 | parametric_num = exp(inst->pinkNoiseNumerator / (float)(inst->blockInd + 1)); |
949 | parametric_num *= (float)(inst->blockInd + 1); |
950 | parametric_exp = inst->pinkNoiseExp / (float)(inst->blockInd + 1); |
951 | parametric_noise = parametric_num / pow((float)kStartBand, parametric_exp); |
952 | } |
953 | for (i = 0; i < inst->magnLen; i++) { |
954 | |
955 | if ((inst->pinkNoiseExp > 0.0f) && (i >= kStartBand)) { |
956 | |
957 | parametric_noise = parametric_num / pow((float)i, parametric_exp); |
958 | } |
959 | theFilterTmp[i] = (inst->initMagnEst[i] - inst->overdrive * parametric_noise); |
960 | theFilterTmp[i] /= (inst->initMagnEst[i] + (float)0.0001); |
961 | |
962 | noise[i] *= (inst->blockInd); |
963 | tmpFloat2 = parametric_noise * (END_STARTUP_SHORT50 - inst->blockInd); |
964 | noise[i] += (tmpFloat2 / (float)(inst->blockInd + 1)); |
965 | noise[i] /= END_STARTUP_SHORT50; |
966 | } |
967 | } |
968 | |
969 | |
970 | if (inst->blockInd < END_STARTUP_LONG200) { |
| |
971 | inst->featureData[5] *= inst->blockInd; |
972 | inst->featureData[5] += signalEnergy; |
973 | inst->featureData[5] /= (inst->blockInd + 1); |
974 | } |
975 | |
976 | #ifdef PROCESS_FLOW_0 |
977 | if (inst->blockInd > END_STARTUP_LONG200) { |
978 | |
979 | for (i = 0; i < inst->magnLen; i++) { |
980 | noise[i] = (float)0.6 * inst->noisePrev[i] + (float)0.4 * noise[i]; |
981 | } |
982 | for (i = 0; i < inst->magnLen; i++) { |
983 | |
984 | theFilter[i] = (magn[i] - inst->overdrive * noise[i]) / (magn[i] + (float)0.0001); |
985 | } |
986 | } |
987 | #else |
988 | |
989 | |
990 | |
991 | |
992 | |
993 | |
994 | for (i = 0; i < inst->magnLen; i++) { |
| 12 | | Loop condition is true. Entering loop body | |
|
| 14 | | Loop condition is false. Execution continues on line 1025 | |
|
995 | |
996 | snrLocPost[i] = (float)0.0; |
997 | if (magn[i] > noise[i]) { |
| |
998 | snrLocPost[i] = magn[i] / (noise[i] + (float)0.0001) - (float)1.0; |
999 | } |
1000 | |
1001 | |
1002 | previousEstimateStsa[i] = inst->magnPrev[i] / (inst->noisePrev[i] + (float)0.0001) |
1003 | * (inst->smooth[i]); |
1004 | |
1005 | |
1006 | snrLocPrior[i] = DD_PR_SNR(float)0.98 * previousEstimateStsa[i] + ((float)1.0 - DD_PR_SNR(float)0.98) |
1007 | * snrLocPost[i]; |
1008 | |
1009 | } |
1010 | #ifdef PROCESS_FLOW_1 |
1011 | for (i = 0; i < inst->magnLen; i++) { |
1012 | |
1013 | tmpFloat1 = inst->overdrive + snrLocPrior[i]; |
1014 | tmpFloat2 = (float)snrLocPrior[i] / tmpFloat1; |
1015 | theFilter[i] = (float)tmpFloat2; |
1016 | } |
1017 | #endif |
1018 | |
1019 | |
1020 | |
1021 | |
1022 | |
1023 | #ifdef PROCESS_FLOW_2 |
1024 | |
1025 | WebRtcNs_ComputeSpectralDifference(inst, magn); |
1026 | |
1027 | |
1028 | if (updateParsFlag >= 1) { |
| 15 | | Assuming 'updateParsFlag' is >= 1 | |
|
| |
1029 | |
1030 | inst->modelUpdatePars[3]--; |
1031 | |
1032 | if (inst->modelUpdatePars[3] > 0) { |
| |
1033 | WebRtcNs_FeatureParameterExtraction(inst, 0); |
1034 | } |
1035 | |
1036 | if (inst->modelUpdatePars[3] == 0) { |
| |
1037 | WebRtcNs_FeatureParameterExtraction(inst, 1); |
1038 | inst->modelUpdatePars[3] = inst->modelUpdatePars[1]; |
1039 | |
1040 | if (updateParsFlag == 1) { |
1041 | inst->modelUpdatePars[0] = 0; |
1042 | } else { |
1043 | |
1044 | |
1045 | inst->featureData[6] = inst->featureData[6] |
1046 | / ((float)inst->modelUpdatePars[1]); |
1047 | inst->featureData[5] = (float)0.5 * (inst->featureData[6] |
1048 | + inst->featureData[5]); |
1049 | inst->featureData[6] = (float)0.0; |
1050 | } |
1051 | } |
1052 | } |
1053 | |
1054 | WebRtcNs_SpeechNoiseProb(inst, probSpeechFinal, snrLocPrior, snrLocPost); |
1055 | |
1056 | gammaNoiseTmp = NOISE_UPDATE(float)0.90; |
1057 | for (i = 0; i < inst->magnLen; i++) { |
| 19 | | Loop condition is true. Entering loop body | |
|
| 23 | | Loop condition is false. Execution continues on line 1094 | |
|
1058 | probSpeech = probSpeechFinal[i]; |
1059 | probNonSpeech = (float)1.0 - probSpeech; |
1060 | |
1061 | |
1062 | noiseUpdateTmp = gammaNoiseTmp * inst->noisePrev[i] + ((float)1.0 - gammaNoiseTmp) |
1063 | * (probNonSpeech * magn[i] + probSpeech * inst->noisePrev[i]); |
1064 | |
1065 | |
1066 | gammaNoiseOld = gammaNoiseTmp; |
1067 | gammaNoiseTmp = NOISE_UPDATE(float)0.90; |
1068 | |
1069 | if (probSpeech > PROB_RANGE(float)0.20) { |
| |
1070 | gammaNoiseTmp = SPEECH_UPDATE(float)0.99; |
1071 | } |
1072 | |
1073 | if (probSpeech < PROB_RANGE(float)0.20) { |
| |
1074 | inst->magnAvgPause[i] += GAMMA_PAUSE(float)0.05 * (magn[i] - inst->magnAvgPause[i]); |
1075 | } |
1076 | |
1077 | if (gammaNoiseTmp == gammaNoiseOld) { |
| |
1078 | noise[i] = noiseUpdateTmp; |
1079 | } else { |
1080 | noise[i] = gammaNoiseTmp * inst->noisePrev[i] + ((float)1.0 - gammaNoiseTmp) |
1081 | * (probNonSpeech * magn[i] + probSpeech * inst->noisePrev[i]); |
1082 | |
1083 | |
1084 | if (noiseUpdateTmp < noise[i]) { |
1085 | noise[i] = noiseUpdateTmp; |
1086 | } |
1087 | } |
1088 | } |
1089 | |
1090 | |
1091 | |
1092 | |
1093 | |
1094 | for (i = 0; i < inst->magnLen; i++) { |
| 24 | | Loop condition is true. Entering loop body | |
|
| 26 | | Loop condition is false. Execution continues on line 1113 | |
|
1095 | |
1096 | currentEstimateStsa = (float)0.0; |
1097 | if (magn[i] > noise[i]) { |
| |
1098 | currentEstimateStsa = magn[i] / (noise[i] + (float)0.0001) - (float)1.0; |
1099 | } |
1100 | |
1101 | |
1102 | snrPrior = DD_PR_SNR(float)0.98 * previousEstimateStsa[i] + ((float)1.0 - DD_PR_SNR(float)0.98) |
1103 | * currentEstimateStsa; |
1104 | |
1105 | tmpFloat1 = inst->overdrive + snrPrior; |
1106 | tmpFloat2 = (float)snrPrior / tmpFloat1; |
1107 | theFilter[i] = (float)tmpFloat2; |
1108 | } |
1109 | |
1110 | #endif |
1111 | #endif |
1112 | |
1113 | for (i = 0; i < inst->magnLen; i++) { |
| 27 | | Loop condition is true. Entering loop body | |
|
1114 | |
1115 | if (theFilter[i] < inst->denoiseBound) { |
| |
1116 | theFilter[i] = inst->denoiseBound; |
1117 | } |
1118 | |
1119 | if (theFilter[i] > (float)1.0) { |
| |
1120 | theFilter[i] = 1.0; |
1121 | } |
1122 | if (inst->blockInd < END_STARTUP_SHORT50) { |
| |
1123 | |
1124 | if (theFilterTmp[i] < inst->denoiseBound) { |
| 31 | | The left operand of '<' is a garbage value |
|
1125 | theFilterTmp[i] = inst->denoiseBound; |
1126 | } |
1127 | |
1128 | if (theFilterTmp[i] > (float)1.0) { |
1129 | theFilterTmp[i] = 1.0; |
1130 | } |
1131 | |
1132 | theFilter[i] *= (inst->blockInd); |
1133 | theFilterTmp[i] *= (END_STARTUP_SHORT50 - inst->blockInd); |
1134 | theFilter[i] += theFilterTmp[i]; |
1135 | theFilter[i] /= (END_STARTUP_SHORT50); |
1136 | } |
1137 | |
1138 | #ifdef PROCESS_FLOW_0 |
1139 | inst->smooth[i] *= SMOOTH(float)0.75; |
1140 | inst->smooth[i] += ((float)1.0 - SMOOTH(float)0.75) * theFilter[i]; |
1141 | #else |
1142 | inst->smooth[i] = theFilter[i]; |
1143 | #endif |
1144 | real[i] *= inst->smooth[i]; |
1145 | imag[i] *= inst->smooth[i]; |
1146 | } |
1147 | |
1148 | for (i = 0; i < inst->magnLen; i++) { |
1149 | inst->noisePrev[i] = noise[i]; |
1150 | inst->magnPrev[i] = magn[i]; |
1151 | } |
1152 | |
1153 | winData[0] = real[0]; |
1154 | winData[1] = real[inst->magnLen - 1]; |
1155 | for (i = 1; i < inst->magnLen - 1; i++) { |
1156 | winData[2 * i] = real[i]; |
1157 | winData[2 * i + 1] = imag[i]; |
1158 | } |
1159 | WebRtc_rdft(inst->anaLen, -1, winData, inst->ip, inst->wfft); |
1160 | |
1161 | for (i = 0; i < inst->anaLen; i++) { |
1162 | real[i] = 2.0f * winData[i] / inst->anaLen; |
1163 | } |
1164 | |
1165 | |
1166 | factor = (float)1.0; |
1167 | if (inst->gainmap == 1 && inst->blockInd > END_STARTUP_LONG200) { |
1168 | factor1 = (float)1.0; |
1169 | factor2 = (float)1.0; |
1170 | |
1171 | energy2 = 0.0; |
1172 | for (i = 0; i < inst->anaLen; i++) { |
1173 | energy2 += (float)real[i] * (float)real[i]; |
1174 | } |
1175 | gain = (float)sqrt(energy2 / (energy1 + (float)1.0)); |
1176 | |
1177 | #ifdef PROCESS_FLOW_2 |
1178 | |
1179 | if (gain > B_LIM(float)0.5) { |
1180 | factor1 = (float)1.0 + (float)1.3 * (gain - B_LIM(float)0.5); |
1181 | if (gain * factor1 > (float)1.0) { |
1182 | factor1 = (float)1.0 / gain; |
1183 | } |
1184 | } |
1185 | if (gain < B_LIM(float)0.5) { |
1186 | |
1187 | |
1188 | if (gain <= inst->denoiseBound) { |
1189 | gain = inst->denoiseBound; |
1190 | } |
1191 | factor2 = (float)1.0 - (float)0.3 * (B_LIM(float)0.5 - gain); |
1192 | } |
1193 | |
1194 | |
1195 | factor = inst->priorSpeechProb * factor1 + ((float)1.0 - inst->priorSpeechProb) |
1196 | * factor2; |
1197 | #else |
1198 | if (gain > B_LIM(float)0.5) { |
1199 | factor = (float)1.0 + (float)1.3 * (gain - B_LIM(float)0.5); |
1200 | } else { |
1201 | factor = (float)1.0 + (float)2.0 * (gain - B_LIM(float)0.5); |
1202 | } |
1203 | if (gain * factor > (float)1.0) { |
1204 | factor = (float)1.0 / gain; |
1205 | } |
1206 | #endif |
1207 | } |
1208 | |
1209 | |
1210 | for (i = 0; i < inst->anaLen; i++) { |
1211 | inst->syntBuf[i] += factor * inst->window[i] * (float)real[i]; |
1212 | } |
1213 | |
1214 | for (i = inst->windShift; i < inst->blockLen + inst->windShift; i++) { |
1215 | fout[i - inst->windShift] = inst->syntBuf[i]; |
1216 | } |
1217 | |
1218 | memcpy(inst->syntBuf, inst->syntBuf + inst->blockLen, |
1219 | sizeof(float) * (inst->anaLen - inst->blockLen)); |
1220 | memset(inst->syntBuf + inst->anaLen - inst->blockLen, 0, |
1221 | sizeof(float) * inst->blockLen); |
1222 | |
1223 | |
1224 | inst->outLen = inst->blockLen - inst->blockLen10ms; |
1225 | if (inst->blockLen > inst->blockLen10ms) { |
1226 | for (i = 0; i < inst->outLen; i++) { |
1227 | inst->outBuf[i] = fout[i + inst->blockLen10ms]; |
1228 | } |
1229 | } |
1230 | } |
1231 | else { |
1232 | for (i = 0; i < inst->blockLen10ms; i++) { |
1233 | fout[i] = inst->outBuf[i]; |
1234 | } |
1235 | memcpy(inst->outBuf, inst->outBuf + inst->blockLen10ms, |
1236 | sizeof(float) * (inst->outLen - inst->blockLen10ms)); |
1237 | memset(inst->outBuf + inst->outLen - inst->blockLen10ms, 0, |
1238 | sizeof(float) * inst->blockLen10ms); |
1239 | inst->outLen -= inst->blockLen10ms; |
1240 | } |
1241 | |
1242 | |
1243 | for (i = 0; i < inst->blockLen10ms; i++) { |
1244 | dTmp = fout[i]; |
1245 | if (dTmp < WEBRTC_SPL_WORD16_MIN-32768) { |
1246 | dTmp = WEBRTC_SPL_WORD16_MIN-32768; |
1247 | } else if (dTmp > WEBRTC_SPL_WORD16_MAX32767) { |
1248 | dTmp = WEBRTC_SPL_WORD16_MAX32767; |
1249 | } |
1250 | outFrame[i] = (short)dTmp; |
1251 | } |
1252 | |
1253 | |
1254 | if (flagHB == 1) { |
1255 | for (i = 0; i < inst->magnLen; i++) { |
1256 | inst->speechProbHB[i] = probSpeechFinal[i]; |
1257 | } |
1258 | |
1259 | |
1260 | avgProbSpeechHB = 0.0; |
1261 | for (i = inst->magnLen - deltaBweHB - 1; i < inst->magnLen - 1; i++) { |
1262 | avgProbSpeechHB += inst->speechProbHB[i]; |
1263 | } |
1264 | avgProbSpeechHB = avgProbSpeechHB / ((float)deltaBweHB); |
1265 | |
1266 | |
1267 | avgFilterGainHB = 0.0; |
1268 | for (i = inst->magnLen - deltaGainHB - 1; i < inst->magnLen - 1; i++) { |
1269 | avgFilterGainHB += inst->smooth[i]; |
1270 | } |
1271 | avgFilterGainHB = avgFilterGainHB / ((float)(deltaGainHB)); |
1272 | avgProbSpeechHBTmp = (float)2.0 * avgProbSpeechHB - (float)1.0; |
1273 | |
1274 | gainModHB = (float)0.5 * ((float)1.0 + (float)tanh(gainMapParHB * avgProbSpeechHBTmp)); |
1275 | |
1276 | gainTimeDomainHB = (float)0.5 * gainModHB + (float)0.5 * avgFilterGainHB; |
1277 | if (avgProbSpeechHB >= (float)0.5) { |
1278 | gainTimeDomainHB = (float)0.25 * gainModHB + (float)0.75 * avgFilterGainHB; |
1279 | } |
1280 | gainTimeDomainHB = gainTimeDomainHB * decayBweHB; |
1281 | |
1282 | |
1283 | if (gainTimeDomainHB < inst->denoiseBound) { |
1284 | gainTimeDomainHB = inst->denoiseBound; |
1285 | } |
1286 | |
1287 | if (gainTimeDomainHB > (float)1.0) { |
1288 | gainTimeDomainHB = 1.0; |
1289 | } |
1290 | |
1291 | for (i = 0; i < inst->blockLen10ms; i++) { |
1292 | dTmp = gainTimeDomainHB * inst->dataBufHB[i]; |
1293 | if (dTmp < WEBRTC_SPL_WORD16_MIN-32768) { |
1294 | dTmp = WEBRTC_SPL_WORD16_MIN-32768; |
1295 | } else if (dTmp > WEBRTC_SPL_WORD16_MAX32767) { |
1296 | dTmp = WEBRTC_SPL_WORD16_MAX32767; |
1297 | } |
1298 | outFrameHB[i] = (short)dTmp; |
1299 | } |
1300 | } |
1301 | |
1302 | |
1303 | return 0; |
1304 | } |