Mercurial > hg > audiostuff
comparison intercom/aec.h @ 2:13be24d74cd2
import intercom-0.4.1
author | Peter Meerwald <pmeerw@cosy.sbg.ac.at> |
---|---|
date | Fri, 25 Jun 2010 09:57:52 +0200 |
parents | |
children | c6c5a16ce2f2 |
comparison
equal
deleted
inserted
replaced
1:9cadc470e3da | 2:13be24d74cd2 |
---|---|
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 // use double if your CPU does software-emulation of float | |
17 typedef float REAL; | |
18 | |
19 /* dB Values */ | |
20 const REAL M0dB = 1.0f; | |
21 const REAL M3dB = 0.71f; | |
22 const REAL M6dB = 0.50f; | |
23 const REAL M9dB = 0.35f; | |
24 const REAL M12dB = 0.25f; | |
25 const REAL M18dB = 0.125f; | |
26 const REAL M24dB = 0.063f; | |
27 | |
28 /* dB values for 16bit PCM */ | |
29 /* MxdB_PCM = 32767 * 10 ^(x / 20) */ | |
30 const REAL M10dB_PCM = 10362.0f; | |
31 const REAL M20dB_PCM = 3277.0f; | |
32 const REAL M25dB_PCM = 1843.0f; | |
33 const REAL M30dB_PCM = 1026.0f; | |
34 const REAL M35dB_PCM = 583.0f; | |
35 const REAL M40dB_PCM = 328.0f; | |
36 const REAL M45dB_PCM = 184.0f; | |
37 const REAL M50dB_PCM = 104.0f; | |
38 const REAL M55dB_PCM = 58.0f; | |
39 const REAL M60dB_PCM = 33.0f; | |
40 const REAL M65dB_PCM = 18.0f; | |
41 const REAL M70dB_PCM = 10.0f; | |
42 const REAL M75dB_PCM = 6.0f; | |
43 const REAL M80dB_PCM = 3.0f; | |
44 const REAL M85dB_PCM = 2.0f; | |
45 const REAL M90dB_PCM = 1.0f; | |
46 | |
47 const REAL MAXPCM = 32767.0f; | |
48 | |
49 /* Design constants (Change to fine tune the algorithms */ | |
50 | |
51 /* The following values are for hardware AEC and studio quality | |
52 * microphone */ | |
53 | |
54 /* NLMS filter length in taps (samples). A longer filter length gives | |
55 * better Echo Cancellation, but maybe slower convergence speed and | |
56 * needs more CPU power (Order of NLMS is linear) */ | |
57 #define NLMS_LEN (100*WIDEB*8) | |
58 | |
59 /* Vector w visualization length in taps (samples). | |
60 * Must match argv value for wdisplay.tcl */ | |
61 #define DUMP_LEN (40*WIDEB*8) | |
62 | |
63 /* minimum energy in xf. Range: M70dB_PCM to M50dB_PCM. Should be equal | |
64 * to microphone ambient Noise level */ | |
65 const REAL NoiseFloor = M55dB_PCM; | |
66 | |
67 /* Leaky hangover in taps. | |
68 */ | |
69 const int Thold = 60 * WIDEB * 8; | |
70 | |
71 // Adrian soft decision DTD | |
72 // left point. X is ratio, Y is stepsize | |
73 const float STEPX1 = 1.0, STEPY1 = 1.0; | |
74 // right point. STEPX2=2.0 is good double talk, 3.0 is good single talk. | |
75 const float STEPX2 = 2.5, STEPY2 = 0; | |
76 const float ALPHAFAST = 1.0f / 100.0f; | |
77 const float ALPHASLOW = 1.0f / 20000.0f; | |
78 | |
79 | |
80 | |
81 /* Ageing multiplier for LMS memory vector w */ | |
82 const REAL Leaky = 0.9999f; | |
83 | |
84 /* Double Talk Detector Speaker/Microphone Threshold. Range <=1 | |
85 * Large value (M0dB) is good for Single-Talk Echo cancellation, | |
86 * small value (M12dB) is good for Doulbe-Talk AEC */ | |
87 const REAL GeigelThreshold = M6dB; | |
88 | |
89 /* for Non Linear Processor. Range >0 to 1. Large value (M0dB) is good | |
90 * for Double-Talk, small value (M12dB) is good for Single-Talk */ | |
91 const REAL NLPAttenuation = M12dB; | |
92 | |
93 /* Below this line there are no more design constants */ | |
94 | |
95 | |
96 /* Exponential Smoothing or IIR Infinite Impulse Response Filter */ | |
97 class IIR_HP { | |
98 REAL x; | |
99 | |
100 public: | |
101 IIR_HP() { | |
102 x = 0.0f; | |
103 } | |
104 | |
105 REAL highpass(REAL in) { | |
106 const REAL a0 = 0.01f; /* controls Transfer Frequency */ | |
107 /* Highpass = Signal - Lowpass. Lowpass = Exponential Smoothing */ | |
108 x += a0 * (in - x); | |
109 return in - x; | |
110 }; | |
111 }; | |
112 | |
113 #if WIDEB==1 | |
114 /* 17 taps FIR Finite Impulse Response filter | |
115 * Coefficients calculated with | |
116 * www.dsptutor.freeuk.com/KaiserFilterDesign/KaiserFilterDesign.html | |
117 */ | |
118 class FIR_HP_300Hz { | |
119 REAL z[18]; | |
120 | |
121 public: | |
122 FIR_HP_300Hz() { | |
123 memset(this, 0, sizeof(FIR_HP_300Hz)); | |
124 } | |
125 | |
126 REAL highpass(REAL in) { | |
127 const REAL a[18] = { | |
128 // Kaiser Window FIR Filter, Filter type: High pass | |
129 // Passband: 300.0 - 4000.0 Hz, Order: 16 | |
130 // Transition band: 75.0 Hz, Stopband attenuation: 10.0 dB | |
131 -0.034870606, -0.039650206, -0.044063766, -0.04800318, | |
132 -0.051370874, -0.054082647, -0.056070227, -0.057283327, | |
133 0.8214126, -0.057283327, -0.056070227, -0.054082647, | |
134 -0.051370874, -0.04800318, -0.044063766, -0.039650206, | |
135 -0.034870606, 0.0 | |
136 }; | |
137 memmove(z + 1, z, 17 * sizeof(REAL)); | |
138 z[0] = in; | |
139 REAL sum0 = 0.0, sum1 = 0.0; | |
140 int j; | |
141 | |
142 for (j = 0; j < 18; j += 2) { | |
143 // optimize: partial loop unrolling | |
144 sum0 += a[j] * z[j]; | |
145 sum1 += a[j + 1] * z[j + 1]; | |
146 } | |
147 return sum0 + sum1; | |
148 } | |
149 }; | |
150 | |
151 #else | |
152 | |
153 /* 35 taps FIR Finite Impulse Response filter | |
154 * Passband 150Hz to 4kHz for 8kHz sample rate, 300Hz to 8kHz for 16kHz | |
155 * sample rate. | |
156 * Coefficients calculated with | |
157 * www.dsptutor.freeuk.com/KaiserFilterDesign/KaiserFilterDesign.html | |
158 */ | |
159 class FIR_HP_300Hz { | |
160 REAL z[36]; | |
161 | |
162 public: | |
163 FIR_HP_300Hz() { | |
164 memset(this, 0, sizeof(FIR_HP_300Hz)); | |
165 } | |
166 | |
167 REAL highpass(REAL in) { | |
168 const REAL a[36] = { | |
169 // Kaiser Window FIR Filter, Filter type: High pass | |
170 // Passband: 150.0 - 4000.0 Hz, Order: 34 | |
171 // Transition band: 34.0 Hz, Stopband attenuation: 10.0 dB | |
172 -0.016165324, -0.017454365, -0.01871232, -0.019931411, | |
173 -0.021104068, -0.022222936, -0.02328091, -0.024271343, | |
174 -0.025187887, -0.02602462, -0.026776174, -0.027437767, | |
175 -0.028004972, -0.028474221, -0.028842418, -0.029107114, | |
176 -0.02926664, 0.8524841, -0.02926664, -0.029107114, | |
177 -0.028842418, -0.028474221, -0.028004972, -0.027437767, | |
178 -0.026776174, -0.02602462, -0.025187887, -0.024271343, | |
179 -0.02328091, -0.022222936, -0.021104068, -0.019931411, | |
180 -0.01871232, -0.017454365, -0.016165324, 0.0 | |
181 }; | |
182 memmove(z + 1, z, 35 * sizeof(REAL)); | |
183 z[0] = in; | |
184 REAL sum0 = 0.0, sum1 = 0.0; | |
185 int j; | |
186 | |
187 for (j = 0; j < 36; j += 2) { | |
188 // optimize: partial loop unrolling | |
189 sum0 += a[j] * z[j]; | |
190 sum1 += a[j + 1] * z[j + 1]; | |
191 } | |
192 return sum0 + sum1; | |
193 } | |
194 }; | |
195 #endif | |
196 | |
197 /* Recursive single pole IIR Infinite Impulse response High-pass filter | |
198 * | |
199 * Reference: The Scientist and Engineer's Guide to Digital Processing | |
200 * | |
201 * output[N] = A0 * input[N] + A1 * input[N-1] + B1 * output[N-1] | |
202 * | |
203 * X = exp(-2.0 * pi * Fc) | |
204 * A0 = (1 + X) / 2 | |
205 * A1 = -(1 + X) / 2 | |
206 * B1 = X | |
207 * Fc = cutoff freq / sample rate | |
208 */ | |
209 class IIR1 { | |
210 REAL in0, out0; | |
211 REAL a0, a1, b1; | |
212 | |
213 public: | |
214 IIR1() { | |
215 memset(this, 0, sizeof(IIR1)); | |
216 } | |
217 | |
218 void init(REAL Fc) { | |
219 b1 = expf(-2.0f * M_PI * Fc); | |
220 a0 = (1.0f + b1) / 2.0f; | |
221 a1 = -a0; | |
222 in0 = 0.0f; | |
223 out0 = 0.0f; | |
224 } | |
225 | |
226 REAL highpass(REAL in) { | |
227 REAL out = a0 * in + a1 * in0 + b1 * out0; | |
228 in0 = in; | |
229 out0 = out; | |
230 return out; | |
231 } | |
232 }; | |
233 | |
234 | |
235 /* Recursive two pole IIR Infinite Impulse Response filter | |
236 * Coefficients calculated with | |
237 * http://www.dsptutor.freeuk.com/IIRFilterDesign/IIRFiltDes102.html | |
238 */ | |
239 class IIR2 { | |
240 REAL x[2], y[2]; | |
241 | |
242 public: | |
243 IIR2() { | |
244 memset(this, 0, sizeof(IIR2)); | |
245 } | |
246 | |
247 REAL highpass(REAL in) { | |
248 // Butterworth IIR filter, Filter type: HP | |
249 // Passband: 2000 - 4000.0 Hz, Order: 2 | |
250 const REAL a[] = { 0.29289323f, -0.58578646f, 0.29289323f }; | |
251 const REAL b[] = { 1.3007072E-16f, 0.17157288f }; | |
252 REAL out = | |
253 a[0] * in + a[1] * x[0] + a[2] * x[1] - b[0] * y[0] - b[1] * y[1]; | |
254 | |
255 x[1] = x[0]; | |
256 x[0] = in; | |
257 y[1] = y[0]; | |
258 y[0] = out; | |
259 return out; | |
260 } | |
261 }; | |
262 | |
263 | |
264 // Extention in taps to reduce mem copies | |
265 #define NLMS_EXT (10*8) | |
266 | |
267 // block size in taps to optimize DTD calculation | |
268 #define DTD_LEN 16 | |
269 | |
270 | |
271 class AEC { | |
272 // Time domain Filters | |
273 IIR_HP acMic, acSpk; // DC-level remove Highpass) | |
274 FIR_HP_300Hz cutoff; // 150Hz cut-off Highpass | |
275 REAL gain; // Mic signal amplify | |
276 IIR1 Fx, Fe; // pre-whitening Highpass for x, e | |
277 | |
278 // Adrian soft decision DTD (Double Talk Detector) | |
279 REAL dfast, xfast; | |
280 REAL dslow, xslow; | |
281 | |
282 // NLMS-pw | |
283 REAL x[NLMS_LEN + NLMS_EXT]; // tap delayed loudspeaker signal | |
284 REAL xf[NLMS_LEN + NLMS_EXT]; // pre-whitening tap delayed signal | |
285 REAL w[NLMS_LEN]; // tap weights | |
286 int j; // optimize: less memory copies | |
287 double dotp_xf_xf; // double to avoid loss of precision | |
288 float delta; // noise floor to stabilize NLMS | |
289 | |
290 // AES | |
291 float aes_y2; // not in use! | |
292 | |
293 // w vector visualization | |
294 REAL ws[DUMP_LEN]; // tap weights sums | |
295 int fdwdisplay; // TCP file descriptor | |
296 int dumpcnt; // wdisplay output counter | |
297 | |
298 /* Double-Talk Detector | |
299 * | |
300 * in d: microphone sample (PCM as REALing point value) | |
301 * in x: loudspeaker sample (PCM as REALing point value) | |
302 * return: from 0 for doubletalk to 1.0 for single talk | |
303 */ | |
304 float dtd(REAL d, REAL x); | |
305 | |
306 void AEC::leaky(); | |
307 | |
308 /* Normalized Least Mean Square Algorithm pre-whitening (NLMS-pw) | |
309 * The LMS algorithm was developed by Bernard Widrow | |
310 * book: Haykin, Adaptive Filter Theory, 4. edition, Prentice Hall, 2002 | |
311 * | |
312 * in d: microphone sample (16bit PCM value) | |
313 * in x_: loudspeaker sample (16bit PCM value) | |
314 * in stepsize: NLMS adaptation variable | |
315 * return: echo cancelled microphone sample | |
316 */ | |
317 REAL nlms_pw(REAL d, REAL x_, float stepsize); | |
318 | |
319 public: | |
320 // variables are public for visualization | |
321 int hangover; | |
322 float stepsize; | |
323 AEC(); | |
324 | |
325 /* Acoustic Echo Cancellation and Suppression of one sample | |
326 * in d: microphone signal with echo | |
327 * in x: loudspeaker signal | |
328 * return: echo cancelled microphone signal | |
329 */ | |
330 int AEC::doAEC(int d_, int x_); | |
331 | |
332 float AEC::getambient() { | |
333 return dfast; | |
334 }; | |
335 void AEC::setambient(float Min_xf) { | |
336 dotp_xf_xf -= delta; // subtract old delta | |
337 delta = (NLMS_LEN-1) * Min_xf * Min_xf; | |
338 dotp_xf_xf += delta; // add new delta | |
339 }; | |
340 void AEC::setgain(float gain_) { | |
341 gain = gain_; | |
342 }; | |
343 void AEC::openwdisplay(); | |
344 void AEC::setaes(float aes_y2_) { | |
345 aes_y2 = aes_y2_; | |
346 }; | |
347 double AEC::max_dotp_xf_xf(double u); | |
348 }; | |
349 | |
350 #define _AEC_H | |
351 #endif |