]> code.delx.au - pulseaudio/blob - src/modules/echo-cancel/adrian-aec.h
echo-cancel: orc-ify some bits for optimisation
[pulseaudio] / src / modules / echo-cancel / adrian-aec.h
1 /* aec.h
2 *
3 * Copyright (C) DFS Deutsche Flugsicherung (2004, 2005).
4 * All Rights Reserved.
5 * Author: Andre Adrian
6 *
7 * Acoustic Echo Cancellation Leaky NLMS-pw algorithm
8 *
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
12 */
13
14 #ifndef _AEC_H /* include only once */
15
16 #ifdef HAVE_CONFIG_H
17 #include <config.h>
18 #endif
19
20 #include <pulsecore/macro.h>
21 #include <pulse/xmalloc.h>
22
23 #define WIDEB 2
24
25 // use double if your CPU does software-emulation of float
26 #define REAL float
27
28 /* dB Values */
29 #define M0dB 1.0f
30 #define M3dB 0.71f
31 #define M6dB 0.50f
32 #define M9dB 0.35f
33 #define M12dB 0.25f
34 #define M18dB 0.125f
35 #define M24dB 0.063f
36
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
55
56 #define MAXPCM 32767.0f
57
58 /* Design constants (Change to fine tune the algorithms */
59
60 /* The following values are for hardware AEC and studio quality
61 * microphone */
62
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)
67
68 /* Vector w visualization length in taps (samples).
69 * Must match argv value for wdisplay.tcl */
70 #define DUMP_LEN (40*WIDEB*8)
71
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
75
76 /* Leaky hangover in taps.
77 */
78 #define Thold (60 * WIDEB * 8)
79
80 // Adrian soft decision DTD
81 // left point. X is ratio, Y is stepsize
82 #define STEPX1 1.0
83 #define STEPY1 1.0
84 // right point. STEPX2=2.0 is good double talk, 3.0 is good single talk.
85 #define STEPX2 2.5
86 #define STEPY2 0
87 #define ALPHAFAST (1.0f / 100.0f)
88 #define ALPHASLOW (1.0f / 20000.0f)
89
90
91
92 /* Ageing multiplier for LMS memory vector w */
93 #define Leaky 0.9999f
94
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
99
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
103
104 /* Below this line there are no more design constants */
105
106 typedef struct IIR_HP IIR_HP;
107
108 /* Exponential Smoothing or IIR Infinite Impulse Response Filter */
109 struct IIR_HP {
110 REAL x;
111 };
112
113 static IIR_HP* IIR_HP_init(void) {
114 IIR_HP *i = pa_xnew(IIR_HP, 1);
115 i->x = 0.0f;
116 return i;
117 }
118
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);
123 return in - i->x;
124 };
125
126 typedef struct FIR_HP_300Hz FIR_HP_300Hz;
127
128 #if WIDEB==1
129 /* 17 taps FIR Finite Impulse Response filter
130 * Coefficients calculated with
131 * www.dsptutor.freeuk.com/KaiserFilterDesign/KaiserFilterDesign.html
132 */
133 class FIR_HP_300Hz {
134 REAL z[18];
135
136 public:
137 FIR_HP_300Hz() {
138 memset(this, 0, sizeof(FIR_HP_300Hz));
139 }
140
141 REAL highpass(REAL in) {
142 const REAL a[18] = {
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,
150 -0.034870606, 0.0
151 };
152 memmove(z + 1, z, 17 * sizeof(REAL));
153 z[0] = in;
154 REAL sum0 = 0.0, sum1 = 0.0;
155 int j;
156
157 for (j = 0; j < 18; j += 2) {
158 // optimize: partial loop unrolling
159 sum0 += a[j] * z[j];
160 sum1 += a[j + 1] * z[j + 1];
161 }
162 return sum0 + sum1;
163 }
164 };
165
166 #else
167
168 /* 35 taps FIR Finite Impulse Response filter
169 * Passband 150Hz to 4kHz for 8kHz sample rate, 300Hz to 8kHz for 16kHz
170 * sample rate.
171 * Coefficients calculated with
172 * www.dsptutor.freeuk.com/KaiserFilterDesign/KaiserFilterDesign.html
173 */
174 struct FIR_HP_300Hz {
175 REAL z[36];
176 };
177
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));
181 return ret;
182 }
183
184 static REAL FIR_HP_300Hz_highpass(FIR_HP_300Hz *f, REAL in) {
185 REAL sum0 = 0.0, sum1 = 0.0;
186 int j;
187 const REAL a[36] = {
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
200 };
201 memmove(f->z + 1, f->z, 35 * sizeof(REAL));
202 f->z[0] = in;
203
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];
208 }
209 return sum0 + sum1;
210 }
211 #endif
212
213 typedef struct IIR1 IIR1;
214
215 /* Recursive single pole IIR Infinite Impulse response High-pass filter
216 *
217 * Reference: The Scientist and Engineer's Guide to Digital Processing
218 *
219 * output[N] = A0 * input[N] + A1 * input[N-1] + B1 * output[N-1]
220 *
221 * X = exp(-2.0 * pi * Fc)
222 * A0 = (1 + X) / 2
223 * A1 = -(1 + X) / 2
224 * B1 = X
225 * Fc = cutoff freq / sample rate
226 */
227 struct IIR1 {
228 REAL in0, out0;
229 REAL a0, a1, b1;
230 };
231
232 #if 0
233 IIR1() {
234 memset(this, 0, sizeof(IIR1));
235 }
236 #endif
237
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;
242 i->a1 = -(i->a0);
243 i->in0 = 0.0f;
244 i->out0 = 0.0f;
245 return i;
246 }
247
248 static REAL IIR1_highpass(IIR1 *i, REAL in) {
249 REAL out = i->a0 * in + i->a1 * i->in0 + i->b1 * i->out0;
250 i->in0 = in;
251 i->out0 = out;
252 return out;
253 }
254
255
256 #if 0
257 /* Recursive two pole IIR Infinite Impulse Response filter
258 * Coefficients calculated with
259 * http://www.dsptutor.freeuk.com/IIRFilterDesign/IIRFiltDes102.html
260 */
261 class IIR2 {
262 REAL x[2], y[2];
263
264 public:
265 IIR2() {
266 memset(this, 0, sizeof(IIR2));
267 }
268
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 };
274 REAL out =
275 a[0] * in + a[1] * x[0] + a[2] * x[1] - b[0] * y[0] - b[1] * y[1];
276
277 x[1] = x[0];
278 x[0] = in;
279 y[1] = y[0];
280 y[0] = out;
281 return out;
282 }
283 };
284 #endif
285
286
287 // Extention in taps to reduce mem copies
288 #define NLMS_EXT (10*8)
289
290 // block size in taps to optimize DTD calculation
291 #define DTD_LEN 16
292
293 typedef struct AEC AEC;
294
295 struct 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
301
302 // Adrian soft decision DTD (Double Talk Detector)
303 REAL dfast, xfast;
304 REAL dslow, xslow;
305
306 // NLMS-pw
307 REAL x[NLMS_LEN + NLMS_EXT]; // tap delayed loudspeaker signal
308 REAL xf[NLMS_LEN + NLMS_EXT]; // pre-whitening tap delayed signal
309 PA_DECLARE_ALIGNED(16, REAL, w[NLMS_LEN]); // tap weights
310 int j; // optimize: less memory copies
311 double dotp_xf_xf; // double to avoid loss of precision
312 float delta; // noise floor to stabilize NLMS
313
314 // AES
315 float aes_y2; // not in use!
316
317 // w vector visualization
318 REAL ws[DUMP_LEN]; // tap weights sums
319 int fdwdisplay; // TCP file descriptor
320 int dumpcnt; // wdisplay output counter
321
322 // variables are public for visualization
323 int hangover;
324 float stepsize;
325
326 // vfuncs that are picked based on processor features available
327 REAL (*dotp) (REAL[], REAL[]);
328 };
329
330 /* Double-Talk Detector
331 *
332 * in d: microphone sample (PCM as REALing point value)
333 * in x: loudspeaker sample (PCM as REALing point value)
334 * return: from 0 for doubletalk to 1.0 for single talk
335 */
336 static float AEC_dtd(AEC *a, REAL d, REAL x);
337
338 static void AEC_leaky(AEC *a);
339
340 /* Normalized Least Mean Square Algorithm pre-whitening (NLMS-pw)
341 * The LMS algorithm was developed by Bernard Widrow
342 * book: Haykin, Adaptive Filter Theory, 4. edition, Prentice Hall, 2002
343 *
344 * in d: microphone sample (16bit PCM value)
345 * in x_: loudspeaker sample (16bit PCM value)
346 * in stepsize: NLMS adaptation variable
347 * return: echo cancelled microphone sample
348 */
349 static REAL AEC_nlms_pw(AEC *a, REAL d, REAL x_, float stepsize);
350
351 AEC* AEC_init(int RATE, int have_vector);
352
353 /* Acoustic Echo Cancellation and Suppression of one sample
354 * in d: microphone signal with echo
355 * in x: loudspeaker signal
356 * return: echo cancelled microphone signal
357 */
358 int AEC_doAEC(AEC *a, int d_, int x_);
359
360 static float AEC_getambient(AEC *a) {
361 return a->dfast;
362 };
363 static void AEC_setambient(AEC *a, float Min_xf) {
364 a->dotp_xf_xf -= a->delta; // subtract old delta
365 a->delta = (NLMS_LEN-1) * Min_xf * Min_xf;
366 a->dotp_xf_xf += a->delta; // add new delta
367 };
368 static void AEC_setgain(AEC *a, float gain_) {
369 a->gain = gain_;
370 };
371 #if 0
372 void AEC_openwdisplay(AEC *a);
373 #endif
374 static void AEC_setaes(AEC *a, float aes_y2_) {
375 a->aes_y2 = aes_y2_;
376 };
377 static double AEC_max_dotp_xf_xf(AEC *a, double u);
378
379 #define _AEC_H
380 #endif