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 <pulsecore/macro.h>
21 #include <pulse/xmalloc.h>
25 // use double if your CPU does software-emulation of float
37 /* dB values for 16bit PCM */
38 /* MxdB_PCM = 32767 * 10 ^(x / 20) */
39 #define M10dB_PCM 10362.0f
40 #define M20dB_PCM 3277.0f
41 #define M25dB_PCM 1843.0f
42 #define M30dB_PCM 1026.0f
43 #define M35dB_PCM 583.0f
44 #define M40dB_PCM 328.0f
45 #define M45dB_PCM 184.0f
46 #define M50dB_PCM 104.0f
47 #define M55dB_PCM 58.0f
48 #define M60dB_PCM 33.0f
49 #define M65dB_PCM 18.0f
50 #define M70dB_PCM 10.0f
51 #define M75dB_PCM 6.0f
52 #define M80dB_PCM 3.0f
53 #define M85dB_PCM 2.0f
54 #define M90dB_PCM 1.0f
56 #define MAXPCM 32767.0f
58 /* Design constants (Change to fine tune the algorithms */
60 /* The following values are for hardware AEC and studio quality
63 /* NLMS filter length in taps (samples). A longer filter length gives
64 * better Echo Cancellation, but maybe slower convergence speed and
65 * needs more CPU power (Order of NLMS is linear) */
66 #define NLMS_LEN (100*WIDEB*8)
68 /* Vector w visualization length in taps (samples).
69 * Must match argv value for wdisplay.tcl */
70 #define DUMP_LEN (40*WIDEB*8)
72 /* minimum energy in xf. Range: M70dB_PCM to M50dB_PCM. Should be equal
73 * to microphone ambient Noise level */
74 #define NoiseFloor M55dB_PCM
76 /* Leaky hangover in taps.
78 #define Thold (60 * WIDEB * 8)
80 // Adrian soft decision DTD
81 // left point. X is ratio, Y is stepsize
84 // right point. STEPX2=2.0 is good double talk, 3.0 is good single talk.
87 #define ALPHAFAST (1.0f / 100.0f)
88 #define ALPHASLOW (1.0f / 20000.0f)
92 /* Ageing multiplier for LMS memory vector w */
95 /* Double Talk Detector Speaker/Microphone Threshold. Range <=1
96 * Large value (M0dB) is good for Single-Talk Echo cancellation,
97 * small value (M12dB) is good for Doulbe-Talk AEC */
98 #define GeigelThreshold M6dB
100 /* for Non Linear Processor. Range >0 to 1. Large value (M0dB) is good
101 * for Double-Talk, small value (M12dB) is good for Single-Talk */
102 #define NLPAttenuation M12dB
104 /* Below this line there are no more design constants */
106 typedef struct IIR_HP IIR_HP
;
108 /* Exponential Smoothing or IIR Infinite Impulse Response Filter */
113 static IIR_HP
* IIR_HP_init(void) {
114 IIR_HP
*i
= pa_xnew(IIR_HP
, 1);
119 static REAL
IIR_HP_highpass(IIR_HP
*i
, REAL in
) {
120 const REAL a0
= 0.01f
; /* controls Transfer Frequency */
121 /* Highpass = Signal - Lowpass. Lowpass = Exponential Smoothing */
122 i
->x
+= a0
* (in
- i
->x
);
126 typedef struct FIR_HP_300Hz FIR_HP_300Hz
;
129 /* 17 taps FIR Finite Impulse Response filter
130 * Coefficients calculated with
131 * www.dsptutor.freeuk.com/KaiserFilterDesign/KaiserFilterDesign.html
138 memset(this, 0, sizeof(FIR_HP_300Hz
));
141 REAL
highpass(REAL in
) {
143 // Kaiser Window FIR Filter, Filter type: High pass
144 // Passband: 300.0 - 4000.0 Hz, Order: 16
145 // Transition band: 75.0 Hz, Stopband attenuation: 10.0 dB
146 -0.034870606, -0.039650206, -0.044063766, -0.04800318,
147 -0.051370874, -0.054082647, -0.056070227, -0.057283327,
148 0.8214126, -0.057283327, -0.056070227, -0.054082647,
149 -0.051370874, -0.04800318, -0.044063766, -0.039650206,
152 memmove(z
+ 1, z
, 17 * sizeof(REAL
));
154 REAL sum0
= 0.0, sum1
= 0.0;
157 for (j
= 0; j
< 18; j
+= 2) {
158 // optimize: partial loop unrolling
160 sum1
+= a
[j
+ 1] * z
[j
+ 1];
168 /* 35 taps FIR Finite Impulse Response filter
169 * Passband 150Hz to 4kHz for 8kHz sample rate, 300Hz to 8kHz for 16kHz
171 * Coefficients calculated with
172 * www.dsptutor.freeuk.com/KaiserFilterDesign/KaiserFilterDesign.html
174 struct FIR_HP_300Hz
{
178 static FIR_HP_300Hz
* FIR_HP_300Hz_init(void) {
179 FIR_HP_300Hz
*ret
= pa_xnew(FIR_HP_300Hz
, 1);
180 memset(ret
, 0, sizeof(FIR_HP_300Hz
));
184 static REAL
FIR_HP_300Hz_highpass(FIR_HP_300Hz
*f
, REAL in
) {
185 REAL sum0
= 0.0, sum1
= 0.0;
188 // Kaiser Window FIR Filter, Filter type: High pass
189 // Passband: 150.0 - 4000.0 Hz, Order: 34
190 // Transition band: 34.0 Hz, Stopband attenuation: 10.0 dB
191 -0.016165324, -0.017454365, -0.01871232, -0.019931411,
192 -0.021104068, -0.022222936, -0.02328091, -0.024271343,
193 -0.025187887, -0.02602462, -0.026776174, -0.027437767,
194 -0.028004972, -0.028474221, -0.028842418, -0.029107114,
195 -0.02926664, 0.8524841, -0.02926664, -0.029107114,
196 -0.028842418, -0.028474221, -0.028004972, -0.027437767,
197 -0.026776174, -0.02602462, -0.025187887, -0.024271343,
198 -0.02328091, -0.022222936, -0.021104068, -0.019931411,
199 -0.01871232, -0.017454365, -0.016165324, 0.0
201 memmove(f
->z
+ 1, f
->z
, 35 * sizeof(REAL
));
204 for (j
= 0; j
< 36; j
+= 2) {
205 // optimize: partial loop unrolling
206 sum0
+= a
[j
] * f
->z
[j
];
207 sum1
+= a
[j
+ 1] * f
->z
[j
+ 1];
213 typedef struct IIR1 IIR1
;
215 /* Recursive single pole IIR Infinite Impulse response High-pass filter
217 * Reference: The Scientist and Engineer's Guide to Digital Processing
219 * output[N] = A0 * input[N] + A1 * input[N-1] + B1 * output[N-1]
221 * X = exp(-2.0 * pi * Fc)
225 * Fc = cutoff freq / sample rate
234 memset(this, 0, sizeof(IIR1
));
238 static IIR1
* IIR1_init(REAL Fc
) {
239 IIR1
*i
= pa_xnew(IIR1
, 1);
240 i
->b1
= expf(-2.0f
* M_PI
* Fc
);
241 i
->a0
= (1.0f
+ i
->b1
) / 2.0f
;
248 static REAL
IIR1_highpass(IIR1
*i
, REAL in
) {
249 REAL out
= i
->a0
* in
+ i
->a1
* i
->in0
+ i
->b1
* i
->out0
;
257 /* Recursive two pole IIR Infinite Impulse Response filter
258 * Coefficients calculated with
259 * http://www.dsptutor.freeuk.com/IIRFilterDesign/IIRFiltDes102.html
266 memset(this, 0, sizeof(IIR2
));
269 REAL
highpass(REAL in
) {
270 // Butterworth IIR filter, Filter type: HP
271 // Passband: 2000 - 4000.0 Hz, Order: 2
272 const REAL a
[] = { 0.29289323f
, -0.58578646f
, 0.29289323f
};
273 const REAL b
[] = { 1.3007072E-16f
, 0.17157288f
};
275 a
[0] * in
+ a
[1] * x
[0] + a
[2] * x
[1] - b
[0] * y
[0] - b
[1] * y
[1];
287 // Extention in taps to reduce mem copies
288 #define NLMS_EXT (10*8)
290 // block size in taps to optimize DTD calculation
293 typedef struct AEC AEC
;
296 // Time domain Filters
297 IIR_HP
*acMic
, *acSpk
; // DC-level remove Highpass)
298 FIR_HP_300Hz
*cutoff
; // 150Hz cut-off Highpass
299 REAL gain
; // Mic signal amplify
300 IIR1
*Fx
, *Fe
; // pre-whitening Highpass for x, e
302 // Adrian soft decision DTD (Double Talk Detector)
307 REAL x
[NLMS_LEN
+ NLMS_EXT
]; // tap delayed loudspeaker signal
308 REAL xf
[NLMS_LEN
+ NLMS_EXT
]; // pre-whitening tap delayed signal
309 REAL w_arr
[NLMS_LEN
+16]; // tap weights
310 REAL
*w
; // this will be a 16-byte aligned pointer into w_arr
311 int j
; // optimize: less memory copies
312 double dotp_xf_xf
; // double to avoid loss of precision
313 float delta
; // noise floor to stabilize NLMS
316 float aes_y2
; // not in use!
318 // w vector visualization
319 REAL ws
[DUMP_LEN
]; // tap weights sums
320 int fdwdisplay
; // TCP file descriptor
321 int dumpcnt
; // wdisplay output counter
323 // variables are public for visualization
327 // vfuncs that are picked based on processor features available
328 REAL (*dotp
) (REAL
[], REAL
[]);
331 /* Double-Talk Detector
333 * in d: microphone sample (PCM as REALing point value)
334 * in x: loudspeaker sample (PCM as REALing point value)
335 * return: from 0 for doubletalk to 1.0 for single talk
337 static float AEC_dtd(AEC
*a
, REAL d
, REAL x
);
339 static void AEC_leaky(AEC
*a
);
341 /* Normalized Least Mean Square Algorithm pre-whitening (NLMS-pw)
342 * The LMS algorithm was developed by Bernard Widrow
343 * book: Haykin, Adaptive Filter Theory, 4. edition, Prentice Hall, 2002
345 * in d: microphone sample (16bit PCM value)
346 * in x_: loudspeaker sample (16bit PCM value)
347 * in stepsize: NLMS adaptation variable
348 * return: echo cancelled microphone sample
350 static REAL
AEC_nlms_pw(AEC
*a
, REAL d
, REAL x_
, float stepsize
);
352 AEC
* AEC_init(int RATE
, int have_vector
);
354 /* Acoustic Echo Cancellation and Suppression of one sample
355 * in d: microphone signal with echo
356 * in x: loudspeaker signal
357 * return: echo cancelled microphone signal
359 int AEC_doAEC(AEC
*a
, int d_
, int x_
);
361 PA_GCC_UNUSED
static float AEC_getambient(AEC
*a
) {
364 static void AEC_setambient(AEC
*a
, float Min_xf
) {
365 a
->dotp_xf_xf
-= a
->delta
; // subtract old delta
366 a
->delta
= (NLMS_LEN
-1) * Min_xf
* Min_xf
;
367 a
->dotp_xf_xf
+= a
->delta
; // add new delta
369 PA_GCC_UNUSED
static void AEC_setgain(AEC
*a
, float gain_
) {
373 void AEC_openwdisplay(AEC
*a
);
375 PA_GCC_UNUSED
static void AEC_setaes(AEC
*a
, float aes_y2_
) {