3 * Copyright (C) DFS Deutsche Flugsicherung (2004, 2005).
7 * Acoustic Echo Cancellation Leaky NLMS-pw algorithm
9 * Version 0.3 filter created with www.dsptutor.freeuk.com
10 * Version 0.3.1 Allow change of stability parameter delta
11 * Version 0.4 Leaky Normalized LMS - pre whitening algorithm
14 #ifndef _AEC_H /* include only once */
20 #include <pulse/gccmacro.h>
21 #include <pulse/xmalloc.h>
23 #include <pulsecore/macro.h>
27 // use double if your CPU does software-emulation of float
39 /* dB values for 16bit PCM */
40 /* MxdB_PCM = 32767 * 10 ^(x / 20) */
41 #define M10dB_PCM 10362.0f
42 #define M20dB_PCM 3277.0f
43 #define M25dB_PCM 1843.0f
44 #define M30dB_PCM 1026.0f
45 #define M35dB_PCM 583.0f
46 #define M40dB_PCM 328.0f
47 #define M45dB_PCM 184.0f
48 #define M50dB_PCM 104.0f
49 #define M55dB_PCM 58.0f
50 #define M60dB_PCM 33.0f
51 #define M65dB_PCM 18.0f
52 #define M70dB_PCM 10.0f
53 #define M75dB_PCM 6.0f
54 #define M80dB_PCM 3.0f
55 #define M85dB_PCM 2.0f
56 #define M90dB_PCM 1.0f
58 #define MAXPCM 32767.0f
60 /* Design constants (Change to fine tune the algorithms */
62 /* The following values are for hardware AEC and studio quality
65 /* NLMS filter length in taps (samples). A longer filter length gives
66 * better Echo Cancellation, but maybe slower convergence speed and
67 * needs more CPU power (Order of NLMS is linear) */
68 #define NLMS_LEN (100*WIDEB*8)
70 /* Vector w visualization length in taps (samples).
71 * Must match argv value for wdisplay.tcl */
72 #define DUMP_LEN (40*WIDEB*8)
74 /* minimum energy in xf. Range: M70dB_PCM to M50dB_PCM. Should be equal
75 * to microphone ambient Noise level */
76 #define NoiseFloor M55dB_PCM
78 /* Leaky hangover in taps.
80 #define Thold (60 * WIDEB * 8)
82 // Adrian soft decision DTD
83 // left point. X is ratio, Y is stepsize
86 // right point. STEPX2=2.0 is good double talk, 3.0 is good single talk.
89 #define ALPHAFAST (1.0f / 100.0f)
90 #define ALPHASLOW (1.0f / 20000.0f)
94 /* Ageing multiplier for LMS memory vector w */
97 /* Double Talk Detector Speaker/Microphone Threshold. Range <=1
98 * Large value (M0dB) is good for Single-Talk Echo cancellation,
99 * small value (M12dB) is good for Doulbe-Talk AEC */
100 #define GeigelThreshold M6dB
102 /* for Non Linear Processor. Range >0 to 1. Large value (M0dB) is good
103 * for Double-Talk, small value (M12dB) is good for Single-Talk */
104 #define NLPAttenuation M12dB
106 /* Below this line there are no more design constants */
108 typedef struct IIR_HP IIR_HP
;
110 /* Exponential Smoothing or IIR Infinite Impulse Response Filter */
115 static IIR_HP
* IIR_HP_init(void) {
116 IIR_HP
*i
= pa_xnew(IIR_HP
, 1);
121 static REAL
IIR_HP_highpass(IIR_HP
*i
, REAL in
) {
122 const REAL a0
= 0.01f
; /* controls Transfer Frequency */
123 /* Highpass = Signal - Lowpass. Lowpass = Exponential Smoothing */
124 i
->x
+= a0
* (in
- i
->x
);
128 typedef struct FIR_HP_300Hz FIR_HP_300Hz
;
131 /* 17 taps FIR Finite Impulse Response filter
132 * Coefficients calculated with
133 * www.dsptutor.freeuk.com/KaiserFilterDesign/KaiserFilterDesign.html
140 memset(this, 0, sizeof(FIR_HP_300Hz
));
143 REAL
highpass(REAL in
) {
145 // Kaiser Window FIR Filter, Filter type: High pass
146 // Passband: 300.0 - 4000.0 Hz, Order: 16
147 // Transition band: 75.0 Hz, Stopband attenuation: 10.0 dB
148 -0.034870606, -0.039650206, -0.044063766, -0.04800318,
149 -0.051370874, -0.054082647, -0.056070227, -0.057283327,
150 0.8214126, -0.057283327, -0.056070227, -0.054082647,
151 -0.051370874, -0.04800318, -0.044063766, -0.039650206,
154 memmove(z
+ 1, z
, 17 * sizeof(REAL
));
156 REAL sum0
= 0.0, sum1
= 0.0;
159 for (j
= 0; j
< 18; j
+= 2) {
160 // optimize: partial loop unrolling
162 sum1
+= a
[j
+ 1] * z
[j
+ 1];
170 /* 35 taps FIR Finite Impulse Response filter
171 * Passband 150Hz to 4kHz for 8kHz sample rate, 300Hz to 8kHz for 16kHz
173 * Coefficients calculated with
174 * www.dsptutor.freeuk.com/KaiserFilterDesign/KaiserFilterDesign.html
176 struct FIR_HP_300Hz
{
180 static FIR_HP_300Hz
* FIR_HP_300Hz_init(void) {
181 FIR_HP_300Hz
*ret
= pa_xnew(FIR_HP_300Hz
, 1);
182 memset(ret
, 0, sizeof(FIR_HP_300Hz
));
186 static REAL
FIR_HP_300Hz_highpass(FIR_HP_300Hz
*f
, REAL in
) {
187 REAL sum0
= 0.0, sum1
= 0.0;
190 // Kaiser Window FIR Filter, Filter type: High pass
191 // Passband: 150.0 - 4000.0 Hz, Order: 34
192 // Transition band: 34.0 Hz, Stopband attenuation: 10.0 dB
193 -0.016165324, -0.017454365, -0.01871232, -0.019931411,
194 -0.021104068, -0.022222936, -0.02328091, -0.024271343,
195 -0.025187887, -0.02602462, -0.026776174, -0.027437767,
196 -0.028004972, -0.028474221, -0.028842418, -0.029107114,
197 -0.02926664, 0.8524841, -0.02926664, -0.029107114,
198 -0.028842418, -0.028474221, -0.028004972, -0.027437767,
199 -0.026776174, -0.02602462, -0.025187887, -0.024271343,
200 -0.02328091, -0.022222936, -0.021104068, -0.019931411,
201 -0.01871232, -0.017454365, -0.016165324, 0.0
203 memmove(f
->z
+ 1, f
->z
, 35 * sizeof(REAL
));
206 for (j
= 0; j
< 36; j
+= 2) {
207 // optimize: partial loop unrolling
208 sum0
+= a
[j
] * f
->z
[j
];
209 sum1
+= a
[j
+ 1] * f
->z
[j
+ 1];
215 typedef struct IIR1 IIR1
;
217 /* Recursive single pole IIR Infinite Impulse response High-pass filter
219 * Reference: The Scientist and Engineer's Guide to Digital Processing
221 * output[N] = A0 * input[N] + A1 * input[N-1] + B1 * output[N-1]
223 * X = exp(-2.0 * pi * Fc)
227 * Fc = cutoff freq / sample rate
236 memset(this, 0, sizeof(IIR1
));
240 static IIR1
* IIR1_init(REAL Fc
) {
241 IIR1
*i
= pa_xnew(IIR1
, 1);
242 i
->b1
= expf(-2.0f
* M_PI
* Fc
);
243 i
->a0
= (1.0f
+ i
->b1
) / 2.0f
;
250 static REAL
IIR1_highpass(IIR1
*i
, REAL in
) {
251 REAL out
= i
->a0
* in
+ i
->a1
* i
->in0
+ i
->b1
* i
->out0
;
259 /* Recursive two pole IIR Infinite Impulse Response filter
260 * Coefficients calculated with
261 * http://www.dsptutor.freeuk.com/IIRFilterDesign/IIRFiltDes102.html
268 memset(this, 0, sizeof(IIR2
));
271 REAL
highpass(REAL in
) {
272 // Butterworth IIR filter, Filter type: HP
273 // Passband: 2000 - 4000.0 Hz, Order: 2
274 const REAL a
[] = { 0.29289323f
, -0.58578646f
, 0.29289323f
};
275 const REAL b
[] = { 1.3007072E-16f
, 0.17157288f
};
277 a
[0] * in
+ a
[1] * x
[0] + a
[2] * x
[1] - b
[0] * y
[0] - b
[1] * y
[1];
289 // Extention in taps to reduce mem copies
290 #define NLMS_EXT (10*8)
292 // block size in taps to optimize DTD calculation
295 typedef struct AEC AEC
;
298 // Time domain Filters
299 IIR_HP
*acMic
, *acSpk
; // DC-level remove Highpass)
300 FIR_HP_300Hz
*cutoff
; // 150Hz cut-off Highpass
301 REAL gain
; // Mic signal amplify
302 IIR1
*Fx
, *Fe
; // pre-whitening Highpass for x, e
304 // Adrian soft decision DTD (Double Talk Detector)
309 REAL x
[NLMS_LEN
+ NLMS_EXT
]; // tap delayed loudspeaker signal
310 REAL xf
[NLMS_LEN
+ NLMS_EXT
]; // pre-whitening tap delayed signal
311 REAL w_arr
[NLMS_LEN
+ (16 / sizeof(REAL
))]; // tap weights
312 REAL
*w
; // this will be a 16-byte aligned pointer into w_arr
313 int j
; // optimize: less memory copies
314 double dotp_xf_xf
; // double to avoid loss of precision
315 float delta
; // noise floor to stabilize NLMS
318 float aes_y2
; // not in use!
320 // w vector visualization
321 REAL ws
[DUMP_LEN
]; // tap weights sums
322 int fdwdisplay
; // TCP file descriptor
323 int dumpcnt
; // wdisplay output counter
325 // variables are public for visualization
329 // vfuncs that are picked based on processor features available
330 REAL (*dotp
) (REAL
[], REAL
[]);
333 /* Double-Talk Detector
335 * in d: microphone sample (PCM as REALing point value)
336 * in x: loudspeaker sample (PCM as REALing point value)
337 * return: from 0 for doubletalk to 1.0 for single talk
339 static float AEC_dtd(AEC
*a
, REAL d
, REAL x
);
341 static void AEC_leaky(AEC
*a
);
343 /* Normalized Least Mean Square Algorithm pre-whitening (NLMS-pw)
344 * The LMS algorithm was developed by Bernard Widrow
345 * book: Haykin, Adaptive Filter Theory, 4. edition, Prentice Hall, 2002
347 * in d: microphone sample (16bit PCM value)
348 * in x_: loudspeaker sample (16bit PCM value)
349 * in stepsize: NLMS adaptation variable
350 * return: echo cancelled microphone sample
352 static REAL
AEC_nlms_pw(AEC
*a
, REAL d
, REAL x_
, float stepsize
);
354 AEC
* AEC_init(int RATE
, int have_vector
);
355 void AEC_done(AEC
*a
);
357 /* Acoustic Echo Cancellation and Suppression of one sample
358 * in d: microphone signal with echo
359 * in x: loudspeaker signal
360 * return: echo cancelled microphone signal
362 int AEC_doAEC(AEC
*a
, int d_
, int x_
);
364 PA_GCC_UNUSED
static float AEC_getambient(AEC
*a
) {
367 static void AEC_setambient(AEC
*a
, float Min_xf
) {
368 a
->dotp_xf_xf
-= a
->delta
; // subtract old delta
369 a
->delta
= (NLMS_LEN
-1) * Min_xf
* Min_xf
;
370 a
->dotp_xf_xf
+= a
->delta
; // add new delta
372 PA_GCC_UNUSED
static void AEC_setgain(AEC
*a
, float gain_
) {
376 void AEC_openwdisplay(AEC
*a
);
378 PA_GCC_UNUSED
static void AEC_setaes(AEC
*a
, float aes_y2_
) {