2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
12 * The core AEC algorithm, which is presented with time-aligned signals.
15 #include "webrtc/modules/audio_processing/aec/aec_core.h"
19 #include <stddef.h> // size_t
23 #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
24 #include "webrtc/modules/audio_processing/aec/aec_core_internal.h"
25 #include "webrtc/modules/audio_processing/aec/aec_rdft.h"
26 #include "webrtc/modules/audio_processing/utility/delay_estimator_wrapper.h"
27 #include "webrtc/modules/audio_processing/utility/ring_buffer.h"
28 #include "webrtc/system_wrappers/interface/cpu_features_wrapper.h"
29 #include "webrtc/typedefs.h"
31 // Buffer size (samples)
32 static const size_t kBufSizePartitions = 250; // 1 second of audio in 16 kHz.
35 static const int subCountLen = 4;
36 static const int countLen = 50;
38 // Quantities to control H band scaling for SWB input
39 static const int flagHbandCn = 1; // flag for adding comfort noise in H band
40 static const float cnScaleHband =
41 (float)0.4; // scale for comfort noise in H band
42 // Initial bin for averaging nlp gain in low band
43 static const int freqAvgIc = PART_LEN / 2;
45 // Matlab code to produce table:
46 // win = sqrt(hanning(63)); win = [0 ; win(1:32)];
47 // fprintf(1, '\t%.14f, %.14f, %.14f,\n', win);
48 static const float sqrtHanning[65] = {
49 0.00000000000000f, 0.02454122852291f, 0.04906767432742f, 0.07356456359967f,
50 0.09801714032956f, 0.12241067519922f, 0.14673047445536f, 0.17096188876030f,
51 0.19509032201613f, 0.21910124015687f, 0.24298017990326f, 0.26671275747490f,
52 0.29028467725446f, 0.31368174039889f, 0.33688985339222f, 0.35989503653499f,
53 0.38268343236509f, 0.40524131400499f, 0.42755509343028f, 0.44961132965461f,
54 0.47139673682600f, 0.49289819222978f, 0.51410274419322f, 0.53499761988710f,
55 0.55557023301960f, 0.57580819141785f, 0.59569930449243f, 0.61523159058063f,
56 0.63439328416365f, 0.65317284295378f, 0.67155895484702f, 0.68954054473707f,
57 0.70710678118655f, 0.72424708295147f, 0.74095112535496f, 0.75720884650648f,
58 0.77301045336274f, 0.78834642762661f, 0.80320753148064f, 0.81758481315158f,
59 0.83146961230255f, 0.84485356524971f, 0.85772861000027f, 0.87008699110871f,
60 0.88192126434835f, 0.89322430119552f, 0.90398929312344f, 0.91420975570353f,
61 0.92387953251129f, 0.93299279883474f, 0.94154406518302f, 0.94952818059304f,
62 0.95694033573221f, 0.96377606579544f, 0.97003125319454f, 0.97570213003853f,
63 0.98078528040323f, 0.98527764238894f, 0.98917650996478f, 0.99247953459871f,
64 0.99518472667220f, 0.99729045667869f, 0.99879545620517f, 0.99969881869620f,
67 // Matlab code to produce table:
68 // weightCurve = [0 ; 0.3 * sqrt(linspace(0,1,64))' + 0.1];
69 // fprintf(1, '\t%.4f, %.4f, %.4f, %.4f, %.4f, %.4f,\n', weightCurve);
70 const float WebRtcAec_weightCurve[65] = {
71 0.0000f, 0.1000f, 0.1378f, 0.1535f, 0.1655f, 0.1756f, 0.1845f, 0.1926f,
72 0.2000f, 0.2069f, 0.2134f, 0.2195f, 0.2254f, 0.2309f, 0.2363f, 0.2414f,
73 0.2464f, 0.2512f, 0.2558f, 0.2604f, 0.2648f, 0.2690f, 0.2732f, 0.2773f,
74 0.2813f, 0.2852f, 0.2890f, 0.2927f, 0.2964f, 0.3000f, 0.3035f, 0.3070f,
75 0.3104f, 0.3138f, 0.3171f, 0.3204f, 0.3236f, 0.3268f, 0.3299f, 0.3330f,
76 0.3360f, 0.3390f, 0.3420f, 0.3449f, 0.3478f, 0.3507f, 0.3535f, 0.3563f,
77 0.3591f, 0.3619f, 0.3646f, 0.3673f, 0.3699f, 0.3726f, 0.3752f, 0.3777f,
78 0.3803f, 0.3828f, 0.3854f, 0.3878f, 0.3903f, 0.3928f, 0.3952f, 0.3976f,
81 // Matlab code to produce table:
82 // overDriveCurve = [sqrt(linspace(0,1,65))' + 1];
83 // fprintf(1, '\t%.4f, %.4f, %.4f, %.4f, %.4f, %.4f,\n', overDriveCurve);
84 const float WebRtcAec_overDriveCurve[65] = {
85 1.0000f, 1.1250f, 1.1768f, 1.2165f, 1.2500f, 1.2795f, 1.3062f, 1.3307f,
86 1.3536f, 1.3750f, 1.3953f, 1.4146f, 1.4330f, 1.4507f, 1.4677f, 1.4841f,
87 1.5000f, 1.5154f, 1.5303f, 1.5449f, 1.5590f, 1.5728f, 1.5863f, 1.5995f,
88 1.6124f, 1.6250f, 1.6374f, 1.6495f, 1.6614f, 1.6731f, 1.6847f, 1.6960f,
89 1.7071f, 1.7181f, 1.7289f, 1.7395f, 1.7500f, 1.7603f, 1.7706f, 1.7806f,
90 1.7906f, 1.8004f, 1.8101f, 1.8197f, 1.8292f, 1.8385f, 1.8478f, 1.8570f,
91 1.8660f, 1.8750f, 1.8839f, 1.8927f, 1.9014f, 1.9100f, 1.9186f, 1.9270f,
92 1.9354f, 1.9437f, 1.9520f, 1.9601f, 1.9682f, 1.9763f, 1.9843f, 1.9922f,
95 // Target suppression levels for nlp modes.
96 // log{0.001, 0.00001, 0.00000001}
97 static const float kTargetSupp[3] = {-6.9f, -11.5f, -18.4f};
99 // Two sets of parameters, one for the extended filter mode.
100 static const float kExtendedMinOverDrive[3] = {3.0f, 6.0f, 15.0f};
101 static const float kNormalMinOverDrive[3] = {1.0f, 2.0f, 5.0f};
102 static const float kExtendedSmoothingCoefficients[2][2] = {{0.9f, 0.1f},
104 static const float kNormalSmoothingCoefficients[2][2] = {{0.9f, 0.1f},
107 // Number of partitions forming the NLP's "preferred" bands.
112 #ifdef WEBRTC_AEC_DEBUG_DUMP
113 extern int webrtc_aec_instance_count;
116 // "Private" function prototypes.
117 static void ProcessBlock(AecCore* aec);
119 static void NonLinearProcessing(AecCore* aec, short* output, short* outputH);
121 static void GetHighbandGain(const float* lambda, float* nlpGainHband);
123 // Comfort_noise also computes noise for H band returned in comfortNoiseHband
124 static void ComfortNoise(AecCore* aec,
125 float efw[2][PART_LEN1],
126 complex_t* comfortNoiseHband,
127 const float* noisePow,
128 const float* lambda);
130 static void InitLevel(PowerLevel* level);
131 static void InitStats(Stats* stats);
132 static void InitMetrics(AecCore* aec);
133 static void UpdateLevel(PowerLevel* level, float in[2][PART_LEN1]);
134 static void UpdateMetrics(AecCore* aec);
135 // Convert from time domain to frequency domain. Note that |time_data| are
137 static void TimeToFrequency(float time_data[PART_LEN2],
138 float freq_data[2][PART_LEN1],
141 __inline static float MulRe(float aRe, float aIm, float bRe, float bIm) {
142 return aRe * bRe - aIm * bIm;
145 __inline static float MulIm(float aRe, float aIm, float bRe, float bIm) {
146 return aRe * bIm + aIm * bRe;
149 static int CmpFloat(const void* a, const void* b) {
150 const float* da = (const float*)a;
151 const float* db = (const float*)b;
153 return (*da > *db) - (*da < *db);
156 int WebRtcAec_CreateAec(AecCore** aecInst) {
157 AecCore* aec = malloc(sizeof(AecCore));
163 aec->nearFrBuf = WebRtc_CreateBuffer(FRAME_LEN + PART_LEN, sizeof(int16_t));
164 if (!aec->nearFrBuf) {
165 WebRtcAec_FreeAec(aec);
170 aec->outFrBuf = WebRtc_CreateBuffer(FRAME_LEN + PART_LEN, sizeof(int16_t));
171 if (!aec->outFrBuf) {
172 WebRtcAec_FreeAec(aec);
177 aec->nearFrBufH = WebRtc_CreateBuffer(FRAME_LEN + PART_LEN, sizeof(int16_t));
178 if (!aec->nearFrBufH) {
179 WebRtcAec_FreeAec(aec);
184 aec->outFrBufH = WebRtc_CreateBuffer(FRAME_LEN + PART_LEN, sizeof(int16_t));
185 if (!aec->outFrBufH) {
186 WebRtcAec_FreeAec(aec);
191 // Create far-end buffers.
193 WebRtc_CreateBuffer(kBufSizePartitions, sizeof(float) * 2 * PART_LEN1);
195 WebRtcAec_FreeAec(aec);
199 aec->far_buf_windowed =
200 WebRtc_CreateBuffer(kBufSizePartitions, sizeof(float) * 2 * PART_LEN1);
201 if (!aec->far_buf_windowed) {
202 WebRtcAec_FreeAec(aec);
206 #ifdef WEBRTC_AEC_DEBUG_DUMP
208 WebRtc_CreateBuffer(kBufSizePartitions, sizeof(int16_t) * PART_LEN);
209 if (!aec->far_time_buf) {
210 WebRtcAec_FreeAec(aec);
216 sprintf(filename, "aec_far%d.pcm", webrtc_aec_instance_count);
217 aec->farFile = fopen(filename, "wb");
218 sprintf(filename, "aec_near%d.pcm", webrtc_aec_instance_count);
219 aec->nearFile = fopen(filename, "wb");
220 sprintf(filename, "aec_out%d.pcm", webrtc_aec_instance_count);
221 aec->outFile = fopen(filename, "wb");
222 sprintf(filename, "aec_out_linear%d.pcm", webrtc_aec_instance_count);
223 aec->outLinearFile = fopen(filename, "wb");
226 aec->delay_estimator_farend =
227 WebRtc_CreateDelayEstimatorFarend(PART_LEN1, kHistorySizeBlocks);
228 if (aec->delay_estimator_farend == NULL) {
229 WebRtcAec_FreeAec(aec);
233 aec->delay_estimator = WebRtc_CreateDelayEstimator(
234 aec->delay_estimator_farend, kLookaheadBlocks);
235 if (aec->delay_estimator == NULL) {
236 WebRtcAec_FreeAec(aec);
244 int WebRtcAec_FreeAec(AecCore* aec) {
249 WebRtc_FreeBuffer(aec->nearFrBuf);
250 WebRtc_FreeBuffer(aec->outFrBuf);
252 WebRtc_FreeBuffer(aec->nearFrBufH);
253 WebRtc_FreeBuffer(aec->outFrBufH);
255 WebRtc_FreeBuffer(aec->far_buf);
256 WebRtc_FreeBuffer(aec->far_buf_windowed);
257 #ifdef WEBRTC_AEC_DEBUG_DUMP
258 WebRtc_FreeBuffer(aec->far_time_buf);
259 fclose(aec->farFile);
260 fclose(aec->nearFile);
261 fclose(aec->outFile);
262 fclose(aec->outLinearFile);
264 WebRtc_FreeDelayEstimator(aec->delay_estimator);
265 WebRtc_FreeDelayEstimatorFarend(aec->delay_estimator_farend);
271 static void FilterFar(AecCore* aec, float yf[2][PART_LEN1]) {
273 for (i = 0; i < aec->num_partitions; i++) {
275 int xPos = (i + aec->xfBufBlockPos) * PART_LEN1;
276 int pos = i * PART_LEN1;
278 if (i + aec->xfBufBlockPos >= aec->num_partitions) {
279 xPos -= aec->num_partitions * (PART_LEN1);
282 for (j = 0; j < PART_LEN1; j++) {
283 yf[0][j] += MulRe(aec->xfBuf[0][xPos + j],
284 aec->xfBuf[1][xPos + j],
285 aec->wfBuf[0][pos + j],
286 aec->wfBuf[1][pos + j]);
287 yf[1][j] += MulIm(aec->xfBuf[0][xPos + j],
288 aec->xfBuf[1][xPos + j],
289 aec->wfBuf[0][pos + j],
290 aec->wfBuf[1][pos + j]);
295 static void ScaleErrorSignal(AecCore* aec, float ef[2][PART_LEN1]) {
296 const float mu = aec->extended_filter_enabled ? kExtendedMu : aec->normal_mu;
297 const float error_threshold = aec->extended_filter_enabled
298 ? kExtendedErrorThreshold
299 : aec->normal_error_threshold;
302 for (i = 0; i < (PART_LEN1); i++) {
303 ef[0][i] /= (aec->xPow[i] + 1e-10f);
304 ef[1][i] /= (aec->xPow[i] + 1e-10f);
305 abs_ef = sqrtf(ef[0][i] * ef[0][i] + ef[1][i] * ef[1][i]);
307 if (abs_ef > error_threshold) {
308 abs_ef = error_threshold / (abs_ef + 1e-10f);
319 // Time-unconstrined filter adaptation.
320 // TODO(andrew): consider for a low-complexity mode.
321 // static void FilterAdaptationUnconstrained(AecCore* aec, float *fft,
322 // float ef[2][PART_LEN1]) {
324 // for (i = 0; i < aec->num_partitions; i++) {
325 // int xPos = (i + aec->xfBufBlockPos)*(PART_LEN1);
328 // if (i + aec->xfBufBlockPos >= aec->num_partitions) {
329 // xPos -= aec->num_partitions * PART_LEN1;
332 // pos = i * PART_LEN1;
334 // for (j = 0; j < PART_LEN1; j++) {
335 // aec->wfBuf[0][pos + j] += MulRe(aec->xfBuf[0][xPos + j],
336 // -aec->xfBuf[1][xPos + j],
337 // ef[0][j], ef[1][j]);
338 // aec->wfBuf[1][pos + j] += MulIm(aec->xfBuf[0][xPos + j],
339 // -aec->xfBuf[1][xPos + j],
340 // ef[0][j], ef[1][j]);
345 static void FilterAdaptation(AecCore* aec, float* fft, float ef[2][PART_LEN1]) {
347 for (i = 0; i < aec->num_partitions; i++) {
348 int xPos = (i + aec->xfBufBlockPos) * (PART_LEN1);
351 if (i + aec->xfBufBlockPos >= aec->num_partitions) {
352 xPos -= aec->num_partitions * PART_LEN1;
357 for (j = 0; j < PART_LEN; j++) {
359 fft[2 * j] = MulRe(aec->xfBuf[0][xPos + j],
360 -aec->xfBuf[1][xPos + j],
363 fft[2 * j + 1] = MulIm(aec->xfBuf[0][xPos + j],
364 -aec->xfBuf[1][xPos + j],
368 fft[1] = MulRe(aec->xfBuf[0][xPos + PART_LEN],
369 -aec->xfBuf[1][xPos + PART_LEN],
373 aec_rdft_inverse_128(fft);
374 memset(fft + PART_LEN, 0, sizeof(float) * PART_LEN);
378 float scale = 2.0f / PART_LEN2;
379 for (j = 0; j < PART_LEN; j++) {
383 aec_rdft_forward_128(fft);
385 aec->wfBuf[0][pos] += fft[0];
386 aec->wfBuf[0][pos + PART_LEN] += fft[1];
388 for (j = 1; j < PART_LEN; j++) {
389 aec->wfBuf[0][pos + j] += fft[2 * j];
390 aec->wfBuf[1][pos + j] += fft[2 * j + 1];
395 static void OverdriveAndSuppress(AecCore* aec,
396 float hNl[PART_LEN1],
398 float efw[2][PART_LEN1]) {
400 for (i = 0; i < PART_LEN1; i++) {
402 if (hNl[i] > hNlFb) {
403 hNl[i] = WebRtcAec_weightCurve[i] * hNlFb +
404 (1 - WebRtcAec_weightCurve[i]) * hNl[i];
406 hNl[i] = powf(hNl[i], aec->overDriveSm * WebRtcAec_overDriveCurve[i]);
408 // Suppress error signal
412 // Ooura fft returns incorrect sign on imaginary component. It matters here
413 // because we are making an additive change with comfort noise.
418 WebRtcAec_FilterFar_t WebRtcAec_FilterFar;
419 WebRtcAec_ScaleErrorSignal_t WebRtcAec_ScaleErrorSignal;
420 WebRtcAec_FilterAdaptation_t WebRtcAec_FilterAdaptation;
421 WebRtcAec_OverdriveAndSuppress_t WebRtcAec_OverdriveAndSuppress;
422 WebRtcAec_ComfortNoise_t WebRtcAec_ComfortNoise;
424 int WebRtcAec_InitAec(AecCore* aec, int sampFreq) {
427 aec->sampFreq = sampFreq;
429 if (sampFreq == 8000) {
430 aec->normal_mu = 0.6f;
431 aec->normal_error_threshold = 2e-6f;
433 aec->normal_mu = 0.5f;
434 aec->normal_error_threshold = 1.5e-6f;
437 if (WebRtc_InitBuffer(aec->nearFrBuf) == -1) {
441 if (WebRtc_InitBuffer(aec->outFrBuf) == -1) {
445 if (WebRtc_InitBuffer(aec->nearFrBufH) == -1) {
449 if (WebRtc_InitBuffer(aec->outFrBufH) == -1) {
453 // Initialize far-end buffers.
454 if (WebRtc_InitBuffer(aec->far_buf) == -1) {
457 if (WebRtc_InitBuffer(aec->far_buf_windowed) == -1) {
460 #ifdef WEBRTC_AEC_DEBUG_DUMP
461 if (WebRtc_InitBuffer(aec->far_time_buf) == -1) {
465 aec->system_delay = 0;
467 if (WebRtc_InitDelayEstimatorFarend(aec->delay_estimator_farend) != 0) {
470 if (WebRtc_InitDelayEstimator(aec->delay_estimator) != 0) {
473 aec->delay_logging_enabled = 0;
474 memset(aec->delay_histogram, 0, sizeof(aec->delay_histogram));
476 aec->reported_delay_enabled = 1;
477 aec->extended_filter_enabled = 0;
478 aec->num_partitions = kNormalNumPartitions;
480 // Update the delay estimator with filter length. We use half the
481 // |num_partitions| to take the echo path into account. In practice we say
482 // that the echo has a duration of maximum half |num_partitions|, which is not
483 // true, but serves as a crude measure.
484 WebRtc_set_allowed_offset(aec->delay_estimator, aec->num_partitions / 2);
485 // TODO(bjornv): I currently hard coded the enable. Once we've established
486 // that AECM has no performance regression, robust_validation will be enabled
487 // all the time and the APIs to turn it on/off will be removed. Hence, remove
489 WebRtc_enable_robust_validation(aec->delay_estimator, 1);
491 // Default target suppression mode.
494 // Sampling frequency multiplier
495 // SWB is processed as 160 frame size
496 if (aec->sampFreq == 32000) {
497 aec->mult = (short)aec->sampFreq / 16000;
499 aec->mult = (short)aec->sampFreq / 8000;
502 aec->farBufWritePos = 0;
503 aec->farBufReadPos = 0;
509 // Initialize buffers
510 memset(aec->dBuf, 0, sizeof(aec->dBuf));
511 memset(aec->eBuf, 0, sizeof(aec->eBuf));
513 memset(aec->dBufH, 0, sizeof(aec->dBufH));
515 memset(aec->xPow, 0, sizeof(aec->xPow));
516 memset(aec->dPow, 0, sizeof(aec->dPow));
517 memset(aec->dInitMinPow, 0, sizeof(aec->dInitMinPow));
518 aec->noisePow = aec->dInitMinPow;
519 aec->noiseEstCtr = 0;
521 // Initial comfort noise power
522 for (i = 0; i < PART_LEN1; i++) {
523 aec->dMinPow[i] = 1.0e6f;
526 // Holds the last block written to
527 aec->xfBufBlockPos = 0;
528 // TODO: Investigate need for these initializations. Deleting them doesn't
529 // change the output at all and yields 0.4% overall speedup.
530 memset(aec->xfBuf, 0, sizeof(complex_t) * kExtendedNumPartitions * PART_LEN1);
531 memset(aec->wfBuf, 0, sizeof(complex_t) * kExtendedNumPartitions * PART_LEN1);
532 memset(aec->sde, 0, sizeof(complex_t) * PART_LEN1);
533 memset(aec->sxd, 0, sizeof(complex_t) * PART_LEN1);
535 aec->xfwBuf, 0, sizeof(complex_t) * kExtendedNumPartitions * PART_LEN1);
536 memset(aec->se, 0, sizeof(float) * PART_LEN1);
538 // To prevent numerical instability in the first block.
539 for (i = 0; i < PART_LEN1; i++) {
542 for (i = 0; i < PART_LEN1; i++) {
546 memset(aec->hNs, 0, sizeof(aec->hNs));
547 memset(aec->outBuf, 0, sizeof(float) * PART_LEN);
550 aec->hNlFbLocalMin = 1;
551 aec->hNlXdAvgMin = 1;
555 aec->overDriveSm = 2;
557 aec->stNearState = 0;
559 aec->divergeState = 0;
562 aec->delayEstCtr = 0;
564 // Metrics disabled by default
565 aec->metricsMode = 0;
568 // Assembly optimization
569 WebRtcAec_FilterFar = FilterFar;
570 WebRtcAec_ScaleErrorSignal = ScaleErrorSignal;
571 WebRtcAec_FilterAdaptation = FilterAdaptation;
572 WebRtcAec_OverdriveAndSuppress = OverdriveAndSuppress;
573 WebRtcAec_ComfortNoise = ComfortNoise;
575 #if defined(WEBRTC_ARCH_X86_FAMILY)
576 if (WebRtc_GetCPUInfo(kSSE2)) {
577 WebRtcAec_InitAec_SSE2();
581 #if defined(MIPS_FPU_LE)
582 WebRtcAec_InitAec_mips();
590 void WebRtcAec_BufferFarendPartition(AecCore* aec, const float* farend) {
591 float fft[PART_LEN2];
592 float xf[2][PART_LEN1];
594 // Check if the buffer is full, and in that case flush the oldest data.
595 if (WebRtc_available_write(aec->far_buf) < 1) {
596 WebRtcAec_MoveFarReadPtr(aec, 1);
598 // Convert far-end partition to the frequency domain without windowing.
599 memcpy(fft, farend, sizeof(float) * PART_LEN2);
600 TimeToFrequency(fft, xf, 0);
601 WebRtc_WriteBuffer(aec->far_buf, &xf[0][0], 1);
603 // Convert far-end partition to the frequency domain with windowing.
604 memcpy(fft, farend, sizeof(float) * PART_LEN2);
605 TimeToFrequency(fft, xf, 1);
606 WebRtc_WriteBuffer(aec->far_buf_windowed, &xf[0][0], 1);
609 int WebRtcAec_MoveFarReadPtr(AecCore* aec, int elements) {
610 int elements_moved = WebRtc_MoveReadPtr(aec->far_buf_windowed, elements);
611 WebRtc_MoveReadPtr(aec->far_buf, elements);
612 #ifdef WEBRTC_AEC_DEBUG_DUMP
613 WebRtc_MoveReadPtr(aec->far_time_buf, elements);
615 aec->system_delay -= elements_moved * PART_LEN;
616 return elements_moved;
619 void WebRtcAec_ProcessFrame(AecCore* aec,
620 const short* nearend,
621 const short* nearendH,
625 int out_elements = 0;
627 // For each frame the process is as follows:
628 // 1) If the system_delay indicates on being too small for processing a
629 // frame we stuff the buffer with enough data for 10 ms.
630 // 2) Adjust the buffer to the system delay, by moving the read pointer.
631 // 3) TODO(bjornv): Investigate if we need to add this:
632 // If we can't move read pointer due to buffer size limitations we
633 // flush/stuff the buffer.
634 // 4) Process as many partitions as possible.
635 // 5) Update the |system_delay| with respect to a full frame of FRAME_LEN
636 // samples. Even though we will have data left to process (we work with
637 // partitions) we consider updating a whole frame, since that's the
638 // amount of data we input and output in audio_processing.
639 // 6) Update the outputs.
641 // TODO(bjornv): Investigate how we should round the delay difference; right
642 // now we know that incoming |knownDelay| is underestimated when it's less
643 // than |aec->knownDelay|. We therefore, round (-32) in that direction. In
644 // the other direction, we don't have this situation, but might flush one
645 // partition too little. This can cause non-causality, which should be
646 // investigated. Maybe, allow for a non-symmetric rounding, like -16.
647 int move_elements = (aec->knownDelay - knownDelay - 32) / PART_LEN;
648 int moved_elements = 0;
650 // TODO(bjornv): Change the near-end buffer handling to be the same as for
651 // far-end, that is, with a near_pre_buf.
652 // Buffer the near-end frame.
653 WebRtc_WriteBuffer(aec->nearFrBuf, nearend, FRAME_LEN);
655 if (aec->sampFreq == 32000) {
656 WebRtc_WriteBuffer(aec->nearFrBufH, nearendH, FRAME_LEN);
659 // 1) At most we process |aec->mult|+1 partitions in 10 ms. Make sure we
660 // have enough far-end data for that by stuffing the buffer if the
661 // |system_delay| indicates others.
662 if (aec->system_delay < FRAME_LEN) {
663 // We don't have enough data so we rewind 10 ms.
664 WebRtcAec_MoveFarReadPtr(aec, -(aec->mult + 1));
667 // 2) Compensate for a possible change in the system delay.
668 WebRtc_MoveReadPtr(aec->far_buf_windowed, move_elements);
669 moved_elements = WebRtc_MoveReadPtr(aec->far_buf, move_elements);
670 aec->knownDelay -= moved_elements * PART_LEN;
671 #ifdef WEBRTC_AEC_DEBUG_DUMP
672 WebRtc_MoveReadPtr(aec->far_time_buf, move_elements);
675 // 4) Process as many blocks as possible.
676 while (WebRtc_available_read(aec->nearFrBuf) >= PART_LEN) {
680 // 5) Update system delay with respect to the entire frame.
681 aec->system_delay -= FRAME_LEN;
683 // 6) Update output frame.
684 // Stuff the out buffer if we have less than a frame to output.
685 // This should only happen for the first frame.
686 out_elements = (int)WebRtc_available_read(aec->outFrBuf);
687 if (out_elements < FRAME_LEN) {
688 WebRtc_MoveReadPtr(aec->outFrBuf, out_elements - FRAME_LEN);
689 if (aec->sampFreq == 32000) {
690 WebRtc_MoveReadPtr(aec->outFrBufH, out_elements - FRAME_LEN);
693 // Obtain an output frame.
694 WebRtc_ReadBuffer(aec->outFrBuf, NULL, out, FRAME_LEN);
696 if (aec->sampFreq == 32000) {
697 WebRtc_ReadBuffer(aec->outFrBufH, NULL, outH, FRAME_LEN);
701 int WebRtcAec_GetDelayMetricsCore(AecCore* self, int* median, int* std) {
703 int delay_values = 0;
704 int num_delay_values = 0;
706 const int kMsPerBlock = PART_LEN / (self->mult * 8);
709 assert(self != NULL);
710 assert(median != NULL);
713 if (self->delay_logging_enabled == 0) {
718 // Get number of delay values since last update.
719 for (i = 0; i < kHistorySizeBlocks; i++) {
720 num_delay_values += self->delay_histogram[i];
722 if (num_delay_values == 0) {
723 // We have no new delay value data. Even though -1 is a valid estimate, it
724 // will practically never be used since multiples of |kMsPerBlock| will
725 // always be returned.
731 delay_values = num_delay_values >> 1; // Start value for median count down.
732 // Get median of delay values since last update.
733 for (i = 0; i < kHistorySizeBlocks; i++) {
734 delay_values -= self->delay_histogram[i];
735 if (delay_values < 0) {
740 // Account for lookahead.
741 *median = (my_median - kLookaheadBlocks) * kMsPerBlock;
743 // Calculate the L1 norm, with median value as central moment.
744 for (i = 0; i < kHistorySizeBlocks; i++) {
745 l1_norm += (float)abs(i - my_median) * self->delay_histogram[i];
747 *std = (int)(l1_norm / (float)num_delay_values + 0.5f) * kMsPerBlock;
750 memset(self->delay_histogram, 0, sizeof(self->delay_histogram));
755 int WebRtcAec_echo_state(AecCore* self) { return self->echoState; }
757 void WebRtcAec_GetEchoStats(AecCore* self,
762 assert(erle != NULL);
763 assert(a_nlp != NULL);
769 #ifdef WEBRTC_AEC_DEBUG_DUMP
770 void* WebRtcAec_far_time_buf(AecCore* self) { return self->far_time_buf; }
773 void WebRtcAec_SetConfigCore(AecCore* self,
777 assert(nlp_mode >= 0 && nlp_mode < 3);
778 self->nlp_mode = nlp_mode;
779 self->metricsMode = metrics_mode;
780 if (self->metricsMode) {
783 self->delay_logging_enabled = delay_logging;
784 if (self->delay_logging_enabled) {
785 memset(self->delay_histogram, 0, sizeof(self->delay_histogram));
789 void WebRtcAec_enable_reported_delay(AecCore* self, int enable) {
790 self->reported_delay_enabled = enable;
793 int WebRtcAec_reported_delay_enabled(AecCore* self) {
794 return self->reported_delay_enabled;
797 void WebRtcAec_enable_delay_correction(AecCore* self, int enable) {
798 self->extended_filter_enabled = enable;
799 self->num_partitions = enable ? kExtendedNumPartitions : kNormalNumPartitions;
800 // Update the delay estimator with filter length. See InitAEC() for details.
801 WebRtc_set_allowed_offset(self->delay_estimator, self->num_partitions / 2);
804 int WebRtcAec_delay_correction_enabled(AecCore* self) {
805 return self->extended_filter_enabled;
808 int WebRtcAec_system_delay(AecCore* self) { return self->system_delay; }
810 void WebRtcAec_SetSystemDelay(AecCore* self, int delay) {
812 self->system_delay = delay;
815 static void ProcessBlock(AecCore* aec) {
817 float d[PART_LEN], y[PART_LEN], e[PART_LEN], dH[PART_LEN];
820 float fft[PART_LEN2];
821 float xf[2][PART_LEN1], yf[2][PART_LEN1], ef[2][PART_LEN1];
822 float df[2][PART_LEN1];
823 float far_spectrum = 0.0f;
824 float near_spectrum = 0.0f;
825 float abs_far_spectrum[PART_LEN1];
826 float abs_near_spectrum[PART_LEN1];
828 const float gPow[2] = {0.9f, 0.1f};
830 // Noise estimate constants.
831 const int noiseInitBlocks = 500 * aec->mult;
832 const float step = 0.1f;
833 const float ramp = 1.0002f;
834 const float gInitNoise[2] = {0.999f, 0.001f};
836 int16_t nearend[PART_LEN];
837 int16_t* nearend_ptr = NULL;
838 int16_t output[PART_LEN];
839 int16_t outputH[PART_LEN];
841 float* xf_ptr = NULL;
843 memset(dH, 0, sizeof(dH));
844 if (aec->sampFreq == 32000) {
845 // Get the upper band first so we can reuse |nearend|.
846 WebRtc_ReadBuffer(aec->nearFrBufH, (void**)&nearend_ptr, nearend, PART_LEN);
847 for (i = 0; i < PART_LEN; i++) {
848 dH[i] = (float)(nearend_ptr[i]);
850 memcpy(aec->dBufH + PART_LEN, dH, sizeof(float) * PART_LEN);
852 WebRtc_ReadBuffer(aec->nearFrBuf, (void**)&nearend_ptr, nearend, PART_LEN);
854 // ---------- Ooura fft ----------
855 // Concatenate old and new nearend blocks.
856 for (i = 0; i < PART_LEN; i++) {
857 d[i] = (float)(nearend_ptr[i]);
859 memcpy(aec->dBuf + PART_LEN, d, sizeof(float) * PART_LEN);
861 #ifdef WEBRTC_AEC_DEBUG_DUMP
863 int16_t farend[PART_LEN];
864 int16_t* farend_ptr = NULL;
865 WebRtc_ReadBuffer(aec->far_time_buf, (void**)&farend_ptr, farend, 1);
866 (void)fwrite(farend_ptr, sizeof(int16_t), PART_LEN, aec->farFile);
867 (void)fwrite(nearend_ptr, sizeof(int16_t), PART_LEN, aec->nearFile);
871 // We should always have at least one element stored in |far_buf|.
872 assert(WebRtc_available_read(aec->far_buf) > 0);
873 WebRtc_ReadBuffer(aec->far_buf, (void**)&xf_ptr, &xf[0][0], 1);
876 memcpy(fft, aec->dBuf, sizeof(float) * PART_LEN2);
877 TimeToFrequency(fft, df, 0);
880 for (i = 0; i < PART_LEN1; i++) {
881 far_spectrum = (xf_ptr[i] * xf_ptr[i]) +
882 (xf_ptr[PART_LEN1 + i] * xf_ptr[PART_LEN1 + i]);
884 gPow[0] * aec->xPow[i] + gPow[1] * aec->num_partitions * far_spectrum;
885 // Calculate absolute spectra
886 abs_far_spectrum[i] = sqrtf(far_spectrum);
888 near_spectrum = df[0][i] * df[0][i] + df[1][i] * df[1][i];
889 aec->dPow[i] = gPow[0] * aec->dPow[i] + gPow[1] * near_spectrum;
890 // Calculate absolute spectra
891 abs_near_spectrum[i] = sqrtf(near_spectrum);
894 // Estimate noise power. Wait until dPow is more stable.
895 if (aec->noiseEstCtr > 50) {
896 for (i = 0; i < PART_LEN1; i++) {
897 if (aec->dPow[i] < aec->dMinPow[i]) {
899 (aec->dPow[i] + step * (aec->dMinPow[i] - aec->dPow[i])) * ramp;
901 aec->dMinPow[i] *= ramp;
906 // Smooth increasing noise power from zero at the start,
907 // to avoid a sudden burst of comfort noise.
908 if (aec->noiseEstCtr < noiseInitBlocks) {
910 for (i = 0; i < PART_LEN1; i++) {
911 if (aec->dMinPow[i] > aec->dInitMinPow[i]) {
912 aec->dInitMinPow[i] = gInitNoise[0] * aec->dInitMinPow[i] +
913 gInitNoise[1] * aec->dMinPow[i];
915 aec->dInitMinPow[i] = aec->dMinPow[i];
918 aec->noisePow = aec->dInitMinPow;
920 aec->noisePow = aec->dMinPow;
923 // Block wise delay estimation used for logging
924 if (aec->delay_logging_enabled) {
925 int delay_estimate = 0;
926 if (WebRtc_AddFarSpectrumFloat(
927 aec->delay_estimator_farend, abs_far_spectrum, PART_LEN1) == 0) {
928 delay_estimate = WebRtc_DelayEstimatorProcessFloat(
929 aec->delay_estimator, abs_near_spectrum, PART_LEN1);
930 if (delay_estimate >= 0) {
931 // Update delay estimate buffer.
932 aec->delay_histogram[delay_estimate]++;
937 // Update the xfBuf block position.
938 aec->xfBufBlockPos--;
939 if (aec->xfBufBlockPos == -1) {
940 aec->xfBufBlockPos = aec->num_partitions - 1;
944 memcpy(aec->xfBuf[0] + aec->xfBufBlockPos * PART_LEN1,
946 sizeof(float) * PART_LEN1);
947 memcpy(aec->xfBuf[1] + aec->xfBufBlockPos * PART_LEN1,
949 sizeof(float) * PART_LEN1);
951 memset(yf, 0, sizeof(yf));
954 WebRtcAec_FilterFar(aec, yf);
956 // Inverse fft to obtain echo estimate and error.
958 fft[1] = yf[0][PART_LEN];
959 for (i = 1; i < PART_LEN; i++) {
960 fft[2 * i] = yf[0][i];
961 fft[2 * i + 1] = yf[1][i];
963 aec_rdft_inverse_128(fft);
965 scale = 2.0f / PART_LEN2;
966 for (i = 0; i < PART_LEN; i++) {
967 y[i] = fft[PART_LEN + i] * scale; // fft scaling
970 for (i = 0; i < PART_LEN; i++) {
975 memcpy(aec->eBuf + PART_LEN, e, sizeof(float) * PART_LEN);
976 memset(fft, 0, sizeof(float) * PART_LEN);
977 memcpy(fft + PART_LEN, e, sizeof(float) * PART_LEN);
978 // TODO(bjornv): Change to use TimeToFrequency().
979 aec_rdft_forward_128(fft);
984 ef[0][PART_LEN] = fft[1];
985 for (i = 1; i < PART_LEN; i++) {
986 ef[0][i] = fft[2 * i];
987 ef[1][i] = fft[2 * i + 1];
990 if (aec->metricsMode == 1) {
991 // Note that the first PART_LEN samples in fft (before transformation) are
992 // zero. Hence, the scaling by two in UpdateLevel() should not be
993 // performed. That scaling is taken care of in UpdateMetrics() instead.
994 UpdateLevel(&aec->linoutlevel, ef);
997 // Scale error signal inversely with far power.
998 WebRtcAec_ScaleErrorSignal(aec, ef);
999 WebRtcAec_FilterAdaptation(aec, fft, ef);
1000 NonLinearProcessing(aec, output, outputH);
1002 if (aec->metricsMode == 1) {
1003 // Update power levels and echo metrics
1004 UpdateLevel(&aec->farlevel, (float(*)[PART_LEN1])xf_ptr);
1005 UpdateLevel(&aec->nearlevel, df);
1009 // Store the output block.
1010 WebRtc_WriteBuffer(aec->outFrBuf, output, PART_LEN);
1012 if (aec->sampFreq == 32000) {
1013 WebRtc_WriteBuffer(aec->outFrBufH, outputH, PART_LEN);
1016 #ifdef WEBRTC_AEC_DEBUG_DUMP
1018 int16_t eInt16[PART_LEN];
1019 for (i = 0; i < PART_LEN; i++) {
1020 eInt16[i] = (int16_t)WEBRTC_SPL_SAT(
1021 WEBRTC_SPL_WORD16_MAX, e[i], WEBRTC_SPL_WORD16_MIN);
1024 (void)fwrite(eInt16, sizeof(int16_t), PART_LEN, aec->outLinearFile);
1025 (void)fwrite(output, sizeof(int16_t), PART_LEN, aec->outFile);
1030 static void NonLinearProcessing(AecCore* aec, short* output, short* outputH) {
1031 float efw[2][PART_LEN1], dfw[2][PART_LEN1], xfw[2][PART_LEN1];
1032 complex_t comfortNoiseHband[PART_LEN1];
1033 float fft[PART_LEN2];
1038 // Coherence and non-linear filter
1039 float cohde[PART_LEN1], cohxd[PART_LEN1];
1040 float hNlDeAvg, hNlXdAvg;
1041 float hNl[PART_LEN1];
1042 float hNlPref[kPrefBandSize];
1043 float hNlFb = 0, hNlFbLow = 0;
1044 const float prefBandQuant = 0.75f, prefBandQuantLow = 0.5f;
1045 const int prefBandSize = kPrefBandSize / aec->mult;
1046 const int minPrefBand = 4 / aec->mult;
1048 // Near and error power sums
1049 float sdSum = 0, seSum = 0;
1051 // Power estimate smoothing coefficients.
1052 const float* ptrGCoh = aec->extended_filter_enabled
1053 ? kExtendedSmoothingCoefficients[aec->mult - 1]
1054 : kNormalSmoothingCoefficients[aec->mult - 1];
1055 const float* min_overdrive = aec->extended_filter_enabled
1056 ? kExtendedMinOverDrive
1057 : kNormalMinOverDrive;
1060 float wfEnMax = 0, wfEn = 0;
1061 const int delayEstInterval = 10 * aec->mult;
1063 float* xfw_ptr = NULL;
1066 if (aec->delayEstCtr == delayEstInterval) {
1067 aec->delayEstCtr = 0;
1070 // initialize comfort noise for H band
1071 memset(comfortNoiseHband, 0, sizeof(comfortNoiseHband));
1072 nlpGainHband = (float)0.0;
1075 // Measure energy in each filter partition to determine delay.
1076 // TODO: Spread by computing one partition per block?
1077 if (aec->delayEstCtr == 0) {
1080 for (i = 0; i < aec->num_partitions; i++) {
1081 pos = i * PART_LEN1;
1083 for (j = 0; j < PART_LEN1; j++) {
1084 wfEn += aec->wfBuf[0][pos + j] * aec->wfBuf[0][pos + j] +
1085 aec->wfBuf[1][pos + j] * aec->wfBuf[1][pos + j];
1088 if (wfEn > wfEnMax) {
1095 // We should always have at least one element stored in |far_buf|.
1096 assert(WebRtc_available_read(aec->far_buf_windowed) > 0);
1098 WebRtc_ReadBuffer(aec->far_buf_windowed, (void**)&xfw_ptr, &xfw[0][0], 1);
1100 // TODO(bjornv): Investigate if we can reuse |far_buf_windowed| instead of
1103 memcpy(aec->xfwBuf, xfw_ptr, sizeof(float) * 2 * PART_LEN1);
1106 memcpy(xfw, aec->xfwBuf + aec->delayIdx * PART_LEN1, sizeof(xfw));
1108 // Windowed near fft
1109 for (i = 0; i < PART_LEN; i++) {
1110 fft[i] = aec->dBuf[i] * sqrtHanning[i];
1111 fft[PART_LEN + i] = aec->dBuf[PART_LEN + i] * sqrtHanning[PART_LEN - i];
1113 aec_rdft_forward_128(fft);
1116 dfw[1][PART_LEN] = 0;
1118 dfw[0][PART_LEN] = fft[1];
1119 for (i = 1; i < PART_LEN; i++) {
1120 dfw[0][i] = fft[2 * i];
1121 dfw[1][i] = fft[2 * i + 1];
1124 // Windowed error fft
1125 for (i = 0; i < PART_LEN; i++) {
1126 fft[i] = aec->eBuf[i] * sqrtHanning[i];
1127 fft[PART_LEN + i] = aec->eBuf[PART_LEN + i] * sqrtHanning[PART_LEN - i];
1129 aec_rdft_forward_128(fft);
1131 efw[1][PART_LEN] = 0;
1133 efw[0][PART_LEN] = fft[1];
1134 for (i = 1; i < PART_LEN; i++) {
1135 efw[0][i] = fft[2 * i];
1136 efw[1][i] = fft[2 * i + 1];
1140 for (i = 0; i < PART_LEN1; i++) {
1141 aec->sd[i] = ptrGCoh[0] * aec->sd[i] +
1142 ptrGCoh[1] * (dfw[0][i] * dfw[0][i] + dfw[1][i] * dfw[1][i]);
1143 aec->se[i] = ptrGCoh[0] * aec->se[i] +
1144 ptrGCoh[1] * (efw[0][i] * efw[0][i] + efw[1][i] * efw[1][i]);
1145 // We threshold here to protect against the ill-effects of a zero farend.
1146 // The threshold is not arbitrarily chosen, but balances protection and
1147 // adverse interaction with the algorithm's tuning.
1148 // TODO: investigate further why this is so sensitive.
1150 ptrGCoh[0] * aec->sx[i] +
1152 WEBRTC_SPL_MAX(xfw[0][i] * xfw[0][i] + xfw[1][i] * xfw[1][i], 15);
1155 ptrGCoh[0] * aec->sde[i][0] +
1156 ptrGCoh[1] * (dfw[0][i] * efw[0][i] + dfw[1][i] * efw[1][i]);
1158 ptrGCoh[0] * aec->sde[i][1] +
1159 ptrGCoh[1] * (dfw[0][i] * efw[1][i] - dfw[1][i] * efw[0][i]);
1162 ptrGCoh[0] * aec->sxd[i][0] +
1163 ptrGCoh[1] * (dfw[0][i] * xfw[0][i] + dfw[1][i] * xfw[1][i]);
1165 ptrGCoh[0] * aec->sxd[i][1] +
1166 ptrGCoh[1] * (dfw[0][i] * xfw[1][i] - dfw[1][i] * xfw[0][i]);
1168 sdSum += aec->sd[i];
1169 seSum += aec->se[i];
1172 // Divergent filter safeguard.
1173 if (aec->divergeState == 0) {
1174 if (seSum > sdSum) {
1175 aec->divergeState = 1;
1178 if (seSum * 1.05f < sdSum) {
1179 aec->divergeState = 0;
1183 if (aec->divergeState == 1) {
1184 memcpy(efw, dfw, sizeof(efw));
1187 if (!aec->extended_filter_enabled) {
1188 // Reset if error is significantly larger than nearend (13 dB).
1189 if (seSum > (19.95f * sdSum)) {
1190 memset(aec->wfBuf, 0, sizeof(aec->wfBuf));
1194 // Subband coherence
1195 for (i = 0; i < PART_LEN1; i++) {
1197 (aec->sde[i][0] * aec->sde[i][0] + aec->sde[i][1] * aec->sde[i][1]) /
1198 (aec->sd[i] * aec->se[i] + 1e-10f);
1200 (aec->sxd[i][0] * aec->sxd[i][0] + aec->sxd[i][1] * aec->sxd[i][1]) /
1201 (aec->sx[i] * aec->sd[i] + 1e-10f);
1205 for (i = minPrefBand; i < prefBandSize + minPrefBand; i++) {
1206 hNlXdAvg += cohxd[i];
1208 hNlXdAvg /= prefBandSize;
1209 hNlXdAvg = 1 - hNlXdAvg;
1212 for (i = minPrefBand; i < prefBandSize + minPrefBand; i++) {
1213 hNlDeAvg += cohde[i];
1215 hNlDeAvg /= prefBandSize;
1217 if (hNlXdAvg < 0.75f && hNlXdAvg < aec->hNlXdAvgMin) {
1218 aec->hNlXdAvgMin = hNlXdAvg;
1221 if (hNlDeAvg > 0.98f && hNlXdAvg > 0.9f) {
1222 aec->stNearState = 1;
1223 } else if (hNlDeAvg < 0.95f || hNlXdAvg < 0.8f) {
1224 aec->stNearState = 0;
1227 if (aec->hNlXdAvgMin == 1) {
1229 aec->overDrive = min_overdrive[aec->nlp_mode];
1231 if (aec->stNearState == 1) {
1232 memcpy(hNl, cohde, sizeof(hNl));
1234 hNlFbLow = hNlDeAvg;
1236 for (i = 0; i < PART_LEN1; i++) {
1237 hNl[i] = 1 - cohxd[i];
1240 hNlFbLow = hNlXdAvg;
1244 if (aec->stNearState == 1) {
1246 memcpy(hNl, cohde, sizeof(hNl));
1248 hNlFbLow = hNlDeAvg;
1251 for (i = 0; i < PART_LEN1; i++) {
1252 hNl[i] = WEBRTC_SPL_MIN(cohde[i], 1 - cohxd[i]);
1255 // Select an order statistic from the preferred bands.
1256 // TODO: Using quicksort now, but a selection algorithm may be preferred.
1257 memcpy(hNlPref, &hNl[minPrefBand], sizeof(float) * prefBandSize);
1258 qsort(hNlPref, prefBandSize, sizeof(float), CmpFloat);
1259 hNlFb = hNlPref[(int)floor(prefBandQuant * (prefBandSize - 1))];
1260 hNlFbLow = hNlPref[(int)floor(prefBandQuantLow * (prefBandSize - 1))];
1264 // Track the local filter minimum to determine suppression overdrive.
1265 if (hNlFbLow < 0.6f && hNlFbLow < aec->hNlFbLocalMin) {
1266 aec->hNlFbLocalMin = hNlFbLow;
1267 aec->hNlFbMin = hNlFbLow;
1271 aec->hNlFbLocalMin =
1272 WEBRTC_SPL_MIN(aec->hNlFbLocalMin + 0.0008f / aec->mult, 1);
1273 aec->hNlXdAvgMin = WEBRTC_SPL_MIN(aec->hNlXdAvgMin + 0.0006f / aec->mult, 1);
1275 if (aec->hNlNewMin == 1) {
1278 if (aec->hNlMinCtr == 2) {
1282 WEBRTC_SPL_MAX(kTargetSupp[aec->nlp_mode] /
1283 ((float)log(aec->hNlFbMin + 1e-10f) + 1e-10f),
1284 min_overdrive[aec->nlp_mode]);
1287 // Smooth the overdrive.
1288 if (aec->overDrive < aec->overDriveSm) {
1289 aec->overDriveSm = 0.99f * aec->overDriveSm + 0.01f * aec->overDrive;
1291 aec->overDriveSm = 0.9f * aec->overDriveSm + 0.1f * aec->overDrive;
1294 WebRtcAec_OverdriveAndSuppress(aec, hNl, hNlFb, efw);
1296 // Add comfort noise.
1297 WebRtcAec_ComfortNoise(aec, efw, comfortNoiseHband, aec->noisePow, hNl);
1299 // TODO(bjornv): Investigate how to take the windowing below into account if
1301 if (aec->metricsMode == 1) {
1302 // Note that we have a scaling by two in the time domain |eBuf|.
1303 // In addition the time domain signal is windowed before transformation,
1304 // losing half the energy on the average. We take care of the first
1305 // scaling only in UpdateMetrics().
1306 UpdateLevel(&aec->nlpoutlevel, efw);
1308 // Inverse error fft.
1310 fft[1] = efw[0][PART_LEN];
1311 for (i = 1; i < PART_LEN; i++) {
1312 fft[2 * i] = efw[0][i];
1313 // Sign change required by Ooura fft.
1314 fft[2 * i + 1] = -efw[1][i];
1316 aec_rdft_inverse_128(fft);
1318 // Overlap and add to obtain output.
1319 scale = 2.0f / PART_LEN2;
1320 for (i = 0; i < PART_LEN; i++) {
1321 fft[i] *= scale; // fft scaling
1322 fft[i] = fft[i] * sqrtHanning[i] + aec->outBuf[i];
1324 // Saturation protection
1325 output[i] = (short)WEBRTC_SPL_SAT(
1326 WEBRTC_SPL_WORD16_MAX, fft[i], WEBRTC_SPL_WORD16_MIN);
1328 fft[PART_LEN + i] *= scale; // fft scaling
1329 aec->outBuf[i] = fft[PART_LEN + i] * sqrtHanning[PART_LEN - i];
1333 if (aec->sampFreq == 32000) {
1336 // average nlp over low band: average over second half of freq spectrum
1338 GetHighbandGain(hNl, &nlpGainHband);
1340 // Inverse comfort_noise
1341 if (flagHbandCn == 1) {
1342 fft[0] = comfortNoiseHband[0][0];
1343 fft[1] = comfortNoiseHband[PART_LEN][0];
1344 for (i = 1; i < PART_LEN; i++) {
1345 fft[2 * i] = comfortNoiseHband[i][0];
1346 fft[2 * i + 1] = comfortNoiseHband[i][1];
1348 aec_rdft_inverse_128(fft);
1349 scale = 2.0f / PART_LEN2;
1352 // compute gain factor
1353 for (i = 0; i < PART_LEN; i++) {
1354 dtmp = (float)aec->dBufH[i];
1355 dtmp = (float)dtmp * nlpGainHband; // for variable gain
1357 // add some comfort noise where Hband is attenuated
1358 if (flagHbandCn == 1) {
1359 fft[i] *= scale; // fft scaling
1360 dtmp += cnScaleHband * fft[i];
1363 // Saturation protection
1364 outputH[i] = (short)WEBRTC_SPL_SAT(
1365 WEBRTC_SPL_WORD16_MAX, dtmp, WEBRTC_SPL_WORD16_MIN);
1369 // Copy the current block to the old position.
1370 memcpy(aec->dBuf, aec->dBuf + PART_LEN, sizeof(float) * PART_LEN);
1371 memcpy(aec->eBuf, aec->eBuf + PART_LEN, sizeof(float) * PART_LEN);
1373 // Copy the current block to the old position for H band
1374 if (aec->sampFreq == 32000) {
1375 memcpy(aec->dBufH, aec->dBufH + PART_LEN, sizeof(float) * PART_LEN);
1378 memmove(aec->xfwBuf + PART_LEN1,
1380 sizeof(aec->xfwBuf) - sizeof(complex_t) * PART_LEN1);
1383 static void GetHighbandGain(const float* lambda, float* nlpGainHband) {
1386 nlpGainHband[0] = (float)0.0;
1387 for (i = freqAvgIc; i < PART_LEN1 - 1; i++) {
1388 nlpGainHband[0] += lambda[i];
1390 nlpGainHband[0] /= (float)(PART_LEN1 - 1 - freqAvgIc);
1393 static void ComfortNoise(AecCore* aec,
1394 float efw[2][PART_LEN1],
1395 complex_t* comfortNoiseHband,
1396 const float* noisePow,
1397 const float* lambda) {
1399 float rand[PART_LEN];
1400 float noise, noiseAvg, tmp, tmpAvg;
1401 int16_t randW16[PART_LEN];
1402 complex_t u[PART_LEN1];
1404 const float pi2 = 6.28318530717959f;
1406 // Generate a uniform random array on [0 1]
1407 WebRtcSpl_RandUArray(randW16, PART_LEN, &aec->seed);
1408 for (i = 0; i < PART_LEN; i++) {
1409 rand[i] = ((float)randW16[i]) / 32768;
1415 for (i = 1; i < PART_LEN1; i++) {
1416 tmp = pi2 * rand[i - 1];
1418 noise = sqrtf(noisePow[i]);
1419 u[i][0] = noise * cosf(tmp);
1420 u[i][1] = -noise * sinf(tmp);
1424 for (i = 0; i < PART_LEN1; i++) {
1425 // This is the proper weighting to match the background noise power
1426 tmp = sqrtf(WEBRTC_SPL_MAX(1 - lambda[i] * lambda[i], 0));
1427 // tmp = 1 - lambda[i];
1428 efw[0][i] += tmp * u[i][0];
1429 efw[1][i] += tmp * u[i][1];
1432 // For H band comfort noise
1433 // TODO: don't compute noise and "tmp" twice. Use the previous results.
1437 if (aec->sampFreq == 32000 && flagHbandCn == 1) {
1439 // average noise scale
1440 // average over second half of freq spectrum (i.e., 4->8khz)
1441 // TODO: we shouldn't need num. We know how many elements we're summing.
1442 for (i = PART_LEN1 >> 1; i < PART_LEN1; i++) {
1444 noiseAvg += sqrtf(noisePow[i]);
1446 noiseAvg /= (float)num;
1448 // average nlp scale
1449 // average over second half of freq spectrum (i.e., 4->8khz)
1450 // TODO: we shouldn't need num. We know how many elements we're summing.
1452 for (i = PART_LEN1 >> 1; i < PART_LEN1; i++) {
1454 tmpAvg += sqrtf(WEBRTC_SPL_MAX(1 - lambda[i] * lambda[i], 0));
1456 tmpAvg /= (float)num;
1458 // Use average noise for H band
1459 // TODO: we should probably have a new random vector here.
1463 for (i = 1; i < PART_LEN1; i++) {
1464 tmp = pi2 * rand[i - 1];
1466 // Use average noise for H band
1467 u[i][0] = noiseAvg * (float)cos(tmp);
1468 u[i][1] = -noiseAvg * (float)sin(tmp);
1472 for (i = 0; i < PART_LEN1; i++) {
1473 // Use average NLP weight for H band
1474 comfortNoiseHband[i][0] = tmpAvg * u[i][0];
1475 comfortNoiseHband[i][1] = tmpAvg * u[i][1];
1480 static void InitLevel(PowerLevel* level) {
1481 const float kBigFloat = 1E17f;
1483 level->averagelevel = 0;
1484 level->framelevel = 0;
1485 level->minlevel = kBigFloat;
1488 level->frcounter = 0;
1489 level->sfrcounter = 0;
1492 static void InitStats(Stats* stats) {
1493 stats->instant = kOffsetLevel;
1494 stats->average = kOffsetLevel;
1495 stats->max = kOffsetLevel;
1496 stats->min = kOffsetLevel * (-1);
1499 stats->himean = kOffsetLevel;
1501 stats->hicounter = 0;
1504 static void InitMetrics(AecCore* self) {
1505 self->stateCounter = 0;
1506 InitLevel(&self->farlevel);
1507 InitLevel(&self->nearlevel);
1508 InitLevel(&self->linoutlevel);
1509 InitLevel(&self->nlpoutlevel);
1511 InitStats(&self->erl);
1512 InitStats(&self->erle);
1513 InitStats(&self->aNlp);
1514 InitStats(&self->rerl);
1517 static void UpdateLevel(PowerLevel* level, float in[2][PART_LEN1]) {
1518 // Do the energy calculation in the frequency domain. The FFT is performed on
1519 // a segment of PART_LEN2 samples due to overlap, but we only want the energy
1520 // of half that data (the last PART_LEN samples). Parseval's relation states
1521 // that the energy is preserved according to
1523 // \sum_{n=0}^{N-1} |x(n)|^2 = 1/N * \sum_{n=0}^{N-1} |X(n)|^2
1526 // where N = PART_LEN2. Since we are only interested in calculating the energy
1527 // for the last PART_LEN samples we approximate by calculating ENERGY and
1530 // \sum_{n=N/2}^{N-1} |x(n)|^2 ~= ENERGY / 2
1532 // Since we deal with real valued time domain signals we only store frequency
1533 // bins [0, PART_LEN], which is what |in| consists of. To calculate ENERGY we
1534 // need to add the contribution from the missing part in
1535 // [PART_LEN+1, PART_LEN2-1]. These values are, up to a phase shift, identical
1536 // with the values in [1, PART_LEN-1], hence multiply those values by 2. This
1537 // is the values in the for loop below, but multiplication by 2 and division
1540 // TODO(bjornv): Investigate reusing energy calculations performed at other
1541 // places in the code.
1543 // Imaginary parts are zero at end points and left out of the calculation.
1544 float energy = (in[0][0] * in[0][0]) / 2;
1545 energy += (in[0][PART_LEN] * in[0][PART_LEN]) / 2;
1547 for (k = 1; k < PART_LEN; k++) {
1548 energy += (in[0][k] * in[0][k] + in[1][k] * in[1][k]);
1550 energy /= PART_LEN2;
1552 level->sfrsum += energy;
1553 level->sfrcounter++;
1555 if (level->sfrcounter > subCountLen) {
1556 level->framelevel = level->sfrsum / (subCountLen * PART_LEN);
1558 level->sfrcounter = 0;
1559 if (level->framelevel > 0) {
1560 if (level->framelevel < level->minlevel) {
1561 level->minlevel = level->framelevel; // New minimum.
1563 level->minlevel *= (1 + 0.001f); // Small increase.
1567 level->frsum += level->framelevel;
1568 if (level->frcounter > countLen) {
1569 level->averagelevel = level->frsum / countLen;
1571 level->frcounter = 0;
1576 static void UpdateMetrics(AecCore* aec) {
1579 const float actThresholdNoisy = 8.0f;
1580 const float actThresholdClean = 40.0f;
1581 const float safety = 0.99995f;
1582 const float noisyPower = 300000.0f;
1585 float echo, suppressedEcho;
1587 if (aec->echoState) { // Check if echo is likely present
1588 aec->stateCounter++;
1591 if (aec->farlevel.frcounter == 0) {
1593 if (aec->farlevel.minlevel < noisyPower) {
1594 actThreshold = actThresholdClean;
1596 actThreshold = actThresholdNoisy;
1599 if ((aec->stateCounter > (0.5f * countLen * subCountLen)) &&
1600 (aec->farlevel.sfrcounter == 0)
1602 // Estimate in active far-end segments only
1604 (aec->farlevel.averagelevel >
1605 (actThreshold * aec->farlevel.minlevel))) {
1607 // Subtract noise power
1608 echo = aec->nearlevel.averagelevel - safety * aec->nearlevel.minlevel;
1611 dtmp = 10 * (float)log10(aec->farlevel.averagelevel /
1612 aec->nearlevel.averagelevel +
1614 dtmp2 = 10 * (float)log10(aec->farlevel.averagelevel / echo + 1e-10f);
1616 aec->erl.instant = dtmp;
1617 if (dtmp > aec->erl.max) {
1618 aec->erl.max = dtmp;
1621 if (dtmp < aec->erl.min) {
1622 aec->erl.min = dtmp;
1626 aec->erl.sum += dtmp;
1627 aec->erl.average = aec->erl.sum / aec->erl.counter;
1630 if (dtmp > aec->erl.average) {
1631 aec->erl.hicounter++;
1632 aec->erl.hisum += dtmp;
1633 aec->erl.himean = aec->erl.hisum / aec->erl.hicounter;
1637 dtmp = 10 * (float)log10(aec->nearlevel.averagelevel /
1638 (2 * aec->linoutlevel.averagelevel) +
1641 // subtract noise power
1642 suppressedEcho = 2 * (aec->linoutlevel.averagelevel -
1643 safety * aec->linoutlevel.minlevel);
1645 dtmp2 = 10 * (float)log10(echo / suppressedEcho + 1e-10f);
1647 aec->aNlp.instant = dtmp2;
1648 if (dtmp > aec->aNlp.max) {
1649 aec->aNlp.max = dtmp;
1652 if (dtmp < aec->aNlp.min) {
1653 aec->aNlp.min = dtmp;
1656 aec->aNlp.counter++;
1657 aec->aNlp.sum += dtmp;
1658 aec->aNlp.average = aec->aNlp.sum / aec->aNlp.counter;
1661 if (dtmp > aec->aNlp.average) {
1662 aec->aNlp.hicounter++;
1663 aec->aNlp.hisum += dtmp;
1664 aec->aNlp.himean = aec->aNlp.hisum / aec->aNlp.hicounter;
1669 // subtract noise power
1670 suppressedEcho = 2 * (aec->nlpoutlevel.averagelevel -
1671 safety * aec->nlpoutlevel.minlevel);
1673 dtmp = 10 * (float)log10(aec->nearlevel.averagelevel /
1674 (2 * aec->nlpoutlevel.averagelevel) +
1676 dtmp2 = 10 * (float)log10(echo / suppressedEcho + 1e-10f);
1679 aec->erle.instant = dtmp;
1680 if (dtmp > aec->erle.max) {
1681 aec->erle.max = dtmp;
1684 if (dtmp < aec->erle.min) {
1685 aec->erle.min = dtmp;
1688 aec->erle.counter++;
1689 aec->erle.sum += dtmp;
1690 aec->erle.average = aec->erle.sum / aec->erle.counter;
1693 if (dtmp > aec->erle.average) {
1694 aec->erle.hicounter++;
1695 aec->erle.hisum += dtmp;
1696 aec->erle.himean = aec->erle.hisum / aec->erle.hicounter;
1700 aec->stateCounter = 0;
1704 static void TimeToFrequency(float time_data[PART_LEN2],
1705 float freq_data[2][PART_LEN1],
1709 // TODO(bjornv): Should we have a different function/wrapper for windowed FFT?
1711 for (i = 0; i < PART_LEN; i++) {
1712 time_data[i] *= sqrtHanning[i];
1713 time_data[PART_LEN + i] *= sqrtHanning[PART_LEN - i];
1717 aec_rdft_forward_128(time_data);
1719 freq_data[1][0] = 0;
1720 freq_data[1][PART_LEN] = 0;
1721 freq_data[0][0] = time_data[0];
1722 freq_data[0][PART_LEN] = time_data[1];
1723 for (i = 1; i < PART_LEN; i++) {
1724 freq_data[0][i] = time_data[2 * i];
1725 freq_data[1][i] = time_data[2 * i + 1];