Debian Codegraph @

drivers/dahdi/dahdi_echocan_kb1.c

raw symbols SCIP
1 /*
2 * ECHO_CAN_KB1
3 *
4 * by Kris Boutilier
5 *
6 * Based upon mec2.h
7 *
8 * Copyright (C) 2002-2012, Digium, Inc.
9 *
10 * Additional background on the techniques used in this code can be found in:
11 *
12 * Messerschmitt, David; Hedberg, David; Cole, Christopher; Haoui, Amine;
13 * Winship, Peter; "Digital Voice Echo Canceller with a TMS32020,"
14 * in Digital Signal Processing Applications with the TMS320 Family,
15 * pp. 415-437, Texas Instruments, Inc., 1986.
16 *
17 * A pdf of which is available by searching on the document title at http://www.ti.com/
18 *
19 */
20
21 /*
22 * See http://www.asterisk.org for more information about
23 * the Asterisk project. Please do not directly contact
24 * any of the maintainers of this project for assistance;
25 * the project provides a web site, mailing lists and IRC
26 * channels for your use.
27 *
28 * This program is free software, distributed under the terms of
29 * the GNU General Public License Version 2 as published by the
30 * Free Software Foundation. See the LICENSE file included with
31 * this program for more details.
32 */
33
34 #include <linux/kernel.h>
35 #include <linux/slab.h>
36 #include <linux/errno.h>
37 #include <linux/module.h>
38 #include <linux/init.h>
39 #include <linux/ctype.h>
40 #include <linux/moduleparam.h>
41
42 #include <dahdi/kernel.h>
43
44 static int debug;
45 static int aggressive;
46
47 /* Uncomment to provide summary statistics for overall echo can performance every 4000 samples */
48 /* #define MEC2_STATS 4000 */
49
50 /* Uncomment to generate per-sample statistics - this will severely degrade system performance and audio quality */
51 /* #define MEC2_STATS_DETAILED */
52
53 /* Get optimized routines for math */
54 #include "arith.h"
55
56 /*
57 Important constants for tuning kb1 echo can
58 */
59
60 /* Convergence (aka. adaptation) speed -- higher means slower */
61 #define DEFAULT_BETA1_I 2048
62
63 /* Constants for various power computations */
64 #define DEFAULT_SIGMA_LY_I 7
65 #define DEFAULT_SIGMA_LU_I 7
66 #define DEFAULT_ALPHA_ST_I 5 /* near-end speech detection sensitivity factor */
67 #define DEFAULT_ALPHA_YT_I 5
68
69 #define DEFAULT_CUTOFF_I 128
70
71 /* Define the near-end speech hangover counter: if near-end speech
72 * is declared, hcntr is set equal to hangt (see pg. 432)
73 */
74 #define DEFAULT_HANGT 600 /* in samples, so 600 samples = 75ms */
75
76 /* define the residual error suppression threshold */
77 #define DEFAULT_SUPPR_I 16 /* 16 = -24db */
78
79 /* This is the minimum reference signal power estimate level
80 * that will result in filter adaptation.
81 * If this is too low then background noise will cause the filter
82 * coefficients to constantly be updated.
83 */
84 #define MIN_UPDATE_THRESH_I 4096
85
86 /* The number of samples used to update coefficients using the
87 * the block update method (M). It should be related back to the
88 * length of the echo can.
89 * ie. it only updates coefficients when (sample number MOD default_m) = 0
90 *
91 * Getting this wrong may cause an oops. Consider yourself warned!
92 */
93 #define DEFAULT_M 16 /* every 16th sample */
94
95 /* If AGGRESSIVE supression is enabled, then we start cancelling residual
96 * echos again even while there is potentially the very end of a near-side
97 * signal present.
98 * This defines how many samples of DEFAULT_HANGT can remain before we
99 * kick back in
100 */
101 #define AGGRESSIVE_HCNTR 160 /* in samples, so 160 samples = 20ms */
102
103
104 /***************************************************************/
105 /* The following knobs are not implemented in the current code */
106
107 /* we need a dynamic level of suppression varying with the ratio of the
108 power of the echo to the power of the reference signal this is
109 done so that we have a smoother background.
110 we have a higher suppression when the power ratio is closer to
111 suppr_ceil and reduces logarithmically as we approach suppr_floor.
112 */
113 #define SUPPR_FLOOR -64
114 #define SUPPR_CEIL -24
115
116 /* in a second departure, we calculate the residual error suppression
117 * as a percentage of the reference signal energy level. The threshold
118 * is defined in terms of dB below the reference signal.
119 */
120 #define RES_SUPR_FACTOR -20
121
122 #ifndef NULL
123 #define NULL 0
124 #endif
125 #ifndef FALSE
126 #define FALSE 0
127 #endif
128 #ifndef TRUE
129 #define TRUE (!FALSE)
130 #endif
131
132 /* Generic circular buffer definition */
133 typedef struct {
134 /* Pointer to the relative 'start' of the buffer */
135 int idx_d;
136 /* The absolute size of the buffer */
137 int size_d;
138 /* The actual sample - twice as large as we need, however we do store values at idx_d and idx_d+size_d */
139 short *buf_d;
140 } echo_can_cb_s;
141
142 static int echo_can_create(struct dahdi_chan *chan, struct dahdi_echocanparams *ecp,
143 struct dahdi_echocanparam *p, struct dahdi_echocan_state **ec);
144 static void echo_can_free(struct dahdi_chan *chan, struct dahdi_echocan_state *ec);
145 static void echo_can_process(struct dahdi_echocan_state *ec, short *isig, const short *iref, u32 size);
146 static int echo_can_traintap(struct dahdi_echocan_state *ec, int pos, short val);
147 static void echocan_NLP_toggle(struct dahdi_echocan_state *ec, unsigned int enable);
148 static const char *name = "KB1";
149 static const char *ec_name(const struct dahdi_chan *chan) { return name; }
150
151 static const struct dahdi_echocan_factory my_factory = {
152 .get_name = ec_name,
153 .owner = THIS_MODULE,
154 .echocan_create = echo_can_create,
155 };
156
157 static const struct dahdi_echocan_features my_features = {
158 .NLP_toggle = 1,
159 };
160
161 static const struct dahdi_echocan_ops my_ops = {
162 .echocan_free = echo_can_free,
163 .echocan_process = echo_can_process,
164 .echocan_traintap = echo_can_traintap,
165 .echocan_NLP_toggle = echocan_NLP_toggle,
166 };
167
168 struct ec_pvt {
169 struct dahdi_echocan_state dahdi;
170 /* an arbitrary ID for this echo can - this really should be settable from the calling channel... */
171 int id;
172
173 /* absolute time - aka. sample number index - essentially the number of samples since this can was init'ed */
174 int i_d;
175
176 /* Pre-computed constants */
177 /* ---------------------- */
178 /* Number of filter coefficents */
179 int N_d;
180 /* Rate of adaptation of filter */
181 int beta2_i;
182
183 /* Accumulators for power computations */
184 /* ----------------------------------- */
185 /* reference signal power estimate - aka. Average absolute value of y(k) */
186 int Ly_i;
187 /* ... */
188 int Lu_i;
189
190 /* Accumulators for signal detectors */
191 /* --------------------------------- */
192 /* Power estimate of the recent past of the near-end hybrid signal - aka. Short-time average of: 2 x |s(i)| */
193 int s_tilde_i;
194 /* Power estimate of the recent past of the far-end receive signal - aka. Short-time average of: |y(i)| */
195 int y_tilde_i;
196
197 /* Near end speech detection counter - stores Hangover counter time remaining, in samples */
198 int HCNTR_d;
199
200 /* Circular buffers and coefficients */
201 /* --------------------------------- */
202 /* ... */
203 int *a_i;
204 /* ... */
205 short *a_s;
206 /* Reference samples of far-end receive signal */
207 echo_can_cb_s y_s;
208 /* Reference samples of near-end signal */
209 echo_can_cb_s s_s;
210 /* Reference samples of near-end signal minus echo estimate */
211 echo_can_cb_s u_s;
212 /* Reference samples of far-end receive signal used to calculate short-time average */
213 echo_can_cb_s y_tilde_s;
214
215 /* Peak far-end receive signal */
216 /* --------------------------- */
217 /* Highest y_tilde value in the sample buffer */
218 short max_y_tilde;
219 /* Index of the sample containing the max_y_tilde value */
220 int max_y_tilde_pos;
221
222 #ifdef MEC2_STATS
223 /* Storage for performance statistics */
224 int cntr_nearend_speech_frames;
225 int cntr_residualcorrected_frames;
226 int cntr_residualcorrected_framesskipped;
227 int cntr_coeff_updates;
228 int cntr_coeff_missedupdates;
229
230 int avg_Lu_i_toolow;
231 int avg_Lu_i_ok;
232 #endif
233 unsigned int aggressive:1;
234 int use_nlp;
235 };
236
237 #define dahdi_to_pvt(a) container_of(a, struct ec_pvt, dahdi)
238
239 static inline void init_cb_s(echo_can_cb_s *cb, int len, void *where)
240 {
241 cb->buf_d = (short *)where;
242 cb->idx_d = 0;
243 cb->size_d = len;
244 }
245
246 static inline void add_cc_s(echo_can_cb_s *cb, short newval)
247 {
248 /* Can't use modulus because N+M isn't a power of two (generally) */
249 cb->idx_d--;
250 if (cb->idx_d < (int)0)
251 /* Whoops - the pointer to the 'start' wrapped around so reset it to the top of the buffer */
252 cb->idx_d += cb->size_d;
253
254 /* Load two copies into memory */
255 cb->buf_d[cb->idx_d] = newval;
256 cb->buf_d[cb->idx_d + cb->size_d] = newval;
257 }
258
259 static inline short get_cc_s(echo_can_cb_s *cb, int pos)
260 {
261 /* Load two copies into memory */
262 return cb->buf_d[cb->idx_d + pos];
263 }
264
265 static inline void init_cc(struct ec_pvt *pvt, int N, int maxy, int maxu)
266 {
267 char *ptr = (char *) pvt;
268 unsigned long tmp;
269
270 /* Double-word align past end of state */
271 ptr += sizeof(*pvt);
272 tmp = (unsigned long)ptr;
273 tmp += 3;
274 tmp &= ~3L;
275 ptr = (void *)tmp;
276
277 /* Reset parameters */
278 pvt->N_d = N;
279 pvt->beta2_i = DEFAULT_BETA1_I;
280
281 /* Allocate coefficient memory */
282 pvt->a_i = (int *) ptr;
283 ptr += (sizeof(int) * pvt->N_d);
284 pvt->a_s = (short *) ptr;
285 ptr += (sizeof(short) * pvt->N_d);
286
287 /* Reset Y circular buffer (short version) */
288 init_cb_s(&pvt->y_s, maxy, ptr);
289 ptr += (sizeof(short) * (maxy) * 2);
290
291 /* Reset Sigma circular buffer (short version for FIR filter) */
292 init_cb_s(&pvt->s_s, (1 << DEFAULT_ALPHA_ST_I), ptr);
293 ptr += (sizeof(short) * (1 << DEFAULT_ALPHA_ST_I) * 2);
294
295 init_cb_s(&pvt->u_s, maxu, ptr);
296 ptr += (sizeof(short) * maxu * 2);
297
298 /* Allocate a buffer for the reference signal power computation */
299 init_cb_s(&pvt->y_tilde_s, pvt->N_d, ptr);
300
301 /* Reset the absolute time index */
302 pvt->i_d = (int)0;
303
304 /* Reset the power computations (for y and u) */
305 pvt->Ly_i = DEFAULT_CUTOFF_I;
306 pvt->Lu_i = DEFAULT_CUTOFF_I;
307
308 #ifdef MEC2_STATS
309 /* set the identity */
310 pvt->id = (int)&ptr;
311
312 /* Reset performance stats */
313 pvt->cntr_nearend_speech_frames = (int)0;
314 pvt->cntr_residualcorrected_frames = (int)0;
315 pvt->cntr_residualcorrected_framesskipped = (int)0;
316 pvt->cntr_coeff_updates = (int)0;
317 pvt->cntr_coeff_missedupdates = (int)0;
318
319 pvt->avg_Lu_i_toolow = (int)0;
320 pvt->avg_Lu_i_ok = (int)0;
321 #endif
322
323 /* Reset the near-end speech detector */
324 pvt->s_tilde_i = (int)0;
325 pvt->y_tilde_i = (int)0;
326 pvt->HCNTR_d = (int)0;
327
328 }
329
330 static void echo_can_free(struct dahdi_chan *chan, struct dahdi_echocan_state *ec)
331 {
332 struct ec_pvt *pvt = dahdi_to_pvt(ec);
333
334 kfree(pvt);
335 }
336
337 static inline short sample_update(struct ec_pvt *pvt, short iref, short isig)
338 {
339 /* Declare local variables that are used more than once */
340 /* ... */
341 int k;
342 /* ... */
343 int rs;
344 /* ... */
345 short u;
346 /* ... */
347 int Py_i;
348 /* ... */
349 int two_beta_i;
350
351 /* flow A on pg. 428 */
352 /* eq. (16): high-pass filter the input to generate the next value;
353 * push the current value into the circular buffer
354 *
355 * sdc_im1_d = sdc_d;
356 * sdc_d = sig;
357 * s_i_d = sdc_d;
358 * s_d = s_i_d;
359 * s_i_d = (float)(1.0 - gamma_d) * s_i_d
360 * + (float)(0.5 * (1.0 - gamma_d)) * (sdc_d - sdc_im1_d);
361 */
362
363 /* Update the Far-end receive signal circular buffers and accumulators */
364 /* ------------------------------------------------------------------- */
365 /* Delete the oldest sample from the power estimate accumulator */
366 pvt->y_tilde_i -= abs(get_cc_s(&pvt->y_s, (1 << DEFAULT_ALPHA_YT_I) - 1)) >> DEFAULT_ALPHA_YT_I;
367 /* Add the new sample to the power estimate accumulator */
368 pvt->y_tilde_i += abs(iref) >> DEFAULT_ALPHA_ST_I;
369 /* Push a copy of the new sample into its circular buffer */
370 add_cc_s(&pvt->y_s, iref);
371
372
373 /* eq. (2): compute r in fixed-point */
374 rs = CONVOLVE2(pvt->a_s,
375 pvt->y_s.buf_d + pvt->y_s.idx_d,
376 pvt->N_d);
377 rs >>= 15;
378
379 /* eq. (3): compute the output value (see figure 3) and the error
380 * note: the error is the same as the output signal when near-end
381 * speech is not present
382 */
383 u = isig - rs;
384
385 /* Push a copy of the output value sample into its circular buffer */
386 add_cc_s(&pvt->u_s, u);
387
388
389 /* Update the Near-end hybrid signal circular buffers and accumulators */
390 /* ------------------------------------------------------------------- */
391 /* Delete the oldest sample from the power estimate accumulator */
392 pvt->s_tilde_i -= abs(get_cc_s(&pvt->s_s, (1 << DEFAULT_ALPHA_ST_I) - 1));
393 /* Add the new sample to the power estimate accumulator */
394 pvt->s_tilde_i += abs(isig);
395 /* Push a copy of the new sample into it's circular buffer */
396 add_cc_s(&pvt->s_s, isig);
397
398
399 /* Push a copy of the current short-time average of the far-end receive signal into it's circular buffer */
400 add_cc_s(&pvt->y_tilde_s, pvt->y_tilde_i);
401
402 /* flow B on pg. 428 */
403
404 /* If the hangover timer isn't running then compute the new convergence factor, otherwise set Py_i to 32768 */
405 if (!pvt->HCNTR_d) {
406 Py_i = (pvt->Ly_i >> DEFAULT_SIGMA_LY_I) * (pvt->Ly_i >> DEFAULT_SIGMA_LY_I);
407 Py_i >>= 15;
408 } else {
409 Py_i = (1 << 15);
410 }
411
412 #if 0
413 /* Vary rate of adaptation depending on position in the file
414 * Do not do this for the first (DEFAULT_UPDATE_TIME) secs after speech
415 * has begun of the file to allow the echo cancellor to estimate the
416 * channel accurately
417 * Still needs conversion!
418 */
419
420 if (pvt->start_speech_d != 0) {
421 if (pvt->i_d > (DEFAULT_T0 + pvt->start_speech_d)*(SAMPLE_FREQ)) {
422 pvt->beta2_d = max_cc_float(MIN_BETA, DEFAULT_BETA1 * exp((-1/DEFAULT_TAU)*((pvt->i_d/(float)SAMPLE_FREQ) - DEFAULT_T0 - pvt->start_speech_d)));
423 }
424 } else {
425 pvt->beta2_d = DEFAULT_BETA1;
426 }
427 #endif
428
429 /* Fixed point, inverted */
430 pvt->beta2_i = DEFAULT_BETA1_I;
431
432 /* Fixed point version, inverted */
433 two_beta_i = (pvt->beta2_i * Py_i) >> 15;
434 if (!two_beta_i)
435 two_beta_i++;
436
437 /* Update the Suppressed signal power estimate accumulator */
438 /* ------------------------------------------------------- */
439 /* Delete the oldest sample from the power estimate accumulator */
440 pvt->Lu_i -= abs(get_cc_s(&pvt->u_s, (1 << DEFAULT_SIGMA_LU_I) - 1));
441 /* Add the new sample to the power estimate accumulator */
442 pvt->Lu_i += abs(u);
443
444 /* Update the Far-end reference signal power estimate accumulator */
445 /* -------------------------------------------------------------- */
446 /* eq. (10): update power estimate of the reference */
447 /* Delete the oldest sample from the power estimate accumulator */
448 pvt->Ly_i -= abs(get_cc_s(&pvt->y_s, (1 << DEFAULT_SIGMA_LY_I) - 1)) ;
449 /* Add the new sample to the power estimate accumulator */
450 pvt->Ly_i += abs(iref);
451
452 if (pvt->Ly_i < DEFAULT_CUTOFF_I)
453 pvt->Ly_i = DEFAULT_CUTOFF_I;
454
455
456 /* Update the Peak far-end receive signal detected */
457 /* ----------------------------------------------- */
458 if (pvt->y_tilde_i > pvt->max_y_tilde) {
459 /* New highest y_tilde with full life */
460 pvt->max_y_tilde = pvt->y_tilde_i;
461 pvt->max_y_tilde_pos = pvt->N_d - 1;
462 } else if (--pvt->max_y_tilde_pos < 0) {
463 /* Time to find new max y tilde... */
464 pvt->max_y_tilde = MAX16(pvt->y_tilde_s.buf_d + pvt->y_tilde_s.idx_d, pvt->N_d, &pvt->max_y_tilde_pos);
465 }
466
467 /* Determine if near end speech was detected in this sample */
468 /* -------------------------------------------------------- */
469 if (((pvt->s_tilde_i >> (DEFAULT_ALPHA_ST_I - 1)) > pvt->max_y_tilde)
470 && (pvt->max_y_tilde > 0)) {
471 /* Then start the Hangover counter */
472 pvt->HCNTR_d = DEFAULT_HANGT;
473 #ifdef MEC2_STATS_DETAILED
474 printk(KERN_INFO "Reset near end speech timer with: s_tilde_i %d, stmnt %d, max_y_tilde %d\n", pvt->s_tilde_i, (pvt->s_tilde_i >> (DEFAULT_ALPHA_ST_I - 1)), pvt->max_y_tilde);
475 #endif
476 #ifdef MEC2_STATS
477 ++pvt->cntr_nearend_speech_frames;
478 #endif
479 } else if (pvt->HCNTR_d > (int)0) {
480 /* otherwise, if it's still non-zero, decrement the Hangover counter by one sample */
481 #ifdef MEC2_STATS
482 ++pvt->cntr_nearend_speech_frames;
483 #endif
484 pvt->HCNTR_d--;
485 }
486
487 /* Update coefficients if no near-end speech in this sample (ie. HCNTR_d = 0)
488 * and we have enough signal to bother trying to update.
489 * --------------------------------------------------------------------------
490 */
491 if (!pvt->HCNTR_d && /* no near-end speech present */
492 !(pvt->i_d % DEFAULT_M)) { /* we only update on every DEFAULM_M'th sample from the stream */
493 if (pvt->Lu_i > MIN_UPDATE_THRESH_I) { /* there is sufficient energy above the noise floor to contain meaningful data */
494 /* so loop over all the filter coefficients */
495 #ifdef MEC2_STATS_DETAILED
496 printk(KERN_INFO "updating coefficients with: pvt->Lu_i %9d\n", pvt->Lu_i);
497 #endif
498 #ifdef MEC2_STATS
499 pvt->avg_Lu_i_ok = pvt->avg_Lu_i_ok + pvt->Lu_i;
500 ++pvt->cntr_coeff_updates;
501 #endif
502 for (k = 0; k < pvt->N_d; k++) {
503 /* eq. (7): compute an expectation over M_d samples */
504 int grad2;
505 grad2 = CONVOLVE2(pvt->u_s.buf_d + pvt->u_s.idx_d,
506 pvt->y_s.buf_d + pvt->y_s.idx_d + k,
507 DEFAULT_M);
508 /* eq. (7): update the coefficient */
509 pvt->a_i[k] += grad2 / two_beta_i;
510 pvt->a_s[k] = pvt->a_i[k] >> 16;
511 }
512 } else {
513 #ifdef MEC2_STATS_DETAILED
514 printk(KERN_INFO "insufficient signal to update coefficients pvt->Lu_i %5d < %5d\n", pvt->Lu_i, MIN_UPDATE_THRESH_I);
515 #endif
516 #ifdef MEC2_STATS
517 pvt->avg_Lu_i_toolow = pvt->avg_Lu_i_toolow + pvt->Lu_i;
518 ++pvt->cntr_coeff_missedupdates;
519 #endif
520 }
521 }
522
523 /* paragraph below eq. (15): if no near-end speech in the sample and
524 * the reference signal power estimate > cutoff threshold
525 * then perform residual error suppression
526 */
527 #ifdef MEC2_STATS_DETAILED
528 if (pvt->HCNTR_d == 0)
529 printk(KERN_INFO "possibly correcting frame with pvt->Ly_i %9d pvt->Lu_i %9d and expression %d\n", pvt->Ly_i, pvt->Lu_i, (pvt->Ly_i/(pvt->Lu_i + 1)));
530 #endif
531
532 #ifndef NO_ECHO_SUPPRESSOR
533 if (pvt->use_nlp) {
534 if (pvt->aggressive) {
535 if ((pvt->HCNTR_d < AGGRESSIVE_HCNTR) && (pvt->Ly_i > (pvt->Lu_i << 1))) {
536 for (k = 0; k < 2; k++) {
537 u = u * (pvt->Lu_i >> DEFAULT_SIGMA_LU_I) / ((pvt->Ly_i >> (DEFAULT_SIGMA_LY_I)) + 1);
538 }
539 #ifdef MEC2_STATS_DETAILED
540 printk(KERN_INFO "aggresively correcting frame with pvt->Ly_i %9d pvt->Lu_i %9d expression %d\n", pvt->Ly_i, pvt->Lu_i, (pvt->Ly_i/(pvt->Lu_i + 1)));
541 #endif
542 #ifdef MEC2_STATS
543 ++pvt->cntr_residualcorrected_frames;
544 #endif
545 }
546 } else {
547 if (pvt->HCNTR_d == 0) {
548 if ((pvt->Ly_i/(pvt->Lu_i + 1)) > DEFAULT_SUPPR_I) {
549 for (k = 0; k < 1; k++) {
550 u = u * (pvt->Lu_i >> DEFAULT_SIGMA_LU_I) / ((pvt->Ly_i >> (DEFAULT_SIGMA_LY_I + 2)) + 1);
551 }
552 #ifdef MEC2_STATS_DETAILED
553 printk(KERN_INFO "correcting frame with pvt->Ly_i %9d pvt->Lu_i %9d expression %d\n", pvt->Ly_i, pvt->Lu_i, (pvt->Ly_i/(pvt->Lu_i + 1)));
554 #endif
555 #ifdef MEC2_STATS
556 ++pvt->cntr_residualcorrected_frames;
557 #endif
558 }
559 #ifdef MEC2_STATS
560 else {
561 ++pvt->cntr_residualcorrected_framesskipped;
562 }
563 #endif
564 }
565 }
566 }
567 #endif
568
569 #if 0
570 /* This will generate a non-linear supression factor, once converted */
571 if ((pvt->HCNTR_d == 0) &&
572 ((pvt->Lu_d/pvt->Ly_d) < DEFAULT_SUPPR) &&
573 (pvt->Lu_d/pvt->Ly_d > EC_MIN_DB_VALUE)) {
574 suppr_factor = (10 / (float)(SUPPR_FLOOR - SUPPR_CEIL)) * log(pvt->Lu_d/pvt->Ly_d)
575 - SUPPR_CEIL / (float)(SUPPR_FLOOR - SUPPR_CEIL);
576 u_suppr = pow(10.0, (suppr_factor) * RES_SUPR_FACTOR / 10.0) * u_suppr;
577 }
578 #endif
579
580 #ifdef MEC2_STATS
581 /* Periodically dump performance stats */
582 if ((pvt->i_d % MEC2_STATS) == 0) {
583 /* make sure to avoid div0's! */
584 if (pvt->cntr_coeff_missedupdates > 0)
585 pvt->avg_Lu_i_toolow = (int)(pvt->avg_Lu_i_toolow / pvt->cntr_coeff_missedupdates);
586 else
587 pvt->avg_Lu_i_toolow = -1;
588
589 if (pvt->cntr_coeff_updates > 0)
590 pvt->avg_Lu_i_ok = (pvt->avg_Lu_i_ok / pvt->cntr_coeff_updates);
591 else
592 pvt->avg_Lu_i_ok = -1;
593
594 printk( KERN_INFO "%d: Near end speech: %5d Residuals corrected/skipped: %5d/%5d Coefficients updated ok/low sig: %3d/%3d Lu_i avg ok/low sig %6d/%5d\n",
595 pvt->id,
596 pvt->cntr_nearend_speech_frames,
597 pvt->cntr_residualcorrected_frames, pvt->cntr_residualcorrected_framesskipped,
598 pvt->cntr_coeff_updates, pvt->cntr_coeff_missedupdates,
599 pvt->avg_Lu_i_ok, pvt->avg_Lu_i_toolow);
600
601 pvt->cntr_nearend_speech_frames = 0;
602 pvt->cntr_residualcorrected_frames = 0;
603 pvt->cntr_residualcorrected_framesskipped = 0;
604 pvt->cntr_coeff_updates = 0;
605 pvt->cntr_coeff_missedupdates = 0;
606 pvt->avg_Lu_i_ok = 0;
607 pvt->avg_Lu_i_toolow = 0;
608 }
609 #endif
610
611 /* Increment the sample index and return the corrected sample */
612 pvt->i_d++;
613 return u;
614 }
615
616 static void echo_can_process(struct dahdi_echocan_state *ec, short *isig, const short *iref, u32 size)
617 {
618 struct ec_pvt *pvt = dahdi_to_pvt(ec);
619 u32 x;
620 short result;
621
622 for (x = 0; x < size; x++) {
623 result = sample_update(pvt, *iref, *isig);
624 *isig++ = result;
625 ++iref;
626 }
627 }
628
629 static int echo_can_create(struct dahdi_chan *chan, struct dahdi_echocanparams *ecp,
630 struct dahdi_echocanparam *p, struct dahdi_echocan_state **ec)
631 {
632 int maxy;
633 int maxu;
634 size_t size;
635 unsigned int x;
636 char *c;
637 struct ec_pvt *pvt;
638
639 maxy = ecp->tap_length + DEFAULT_M;
640 maxu = DEFAULT_M;
641 if (maxy < (1 << DEFAULT_ALPHA_YT_I))
642 maxy = (1 << DEFAULT_ALPHA_YT_I);
643 if (maxy < (1 << DEFAULT_SIGMA_LY_I))
644 maxy = (1 << DEFAULT_SIGMA_LY_I);
645 if (maxu < (1 << DEFAULT_SIGMA_LU_I))
646 maxu = (1 << DEFAULT_SIGMA_LU_I);
647
648 size = sizeof(*ec) +
649 4 + /* align */
650 sizeof(int) * ecp->tap_length + /* a_i */
651 sizeof(short) * ecp->tap_length + /* a_s */
652 2 * sizeof(short) * (maxy) + /* y_s */
653 2 * sizeof(short) * (1 << DEFAULT_ALPHA_ST_I) + /* s_s */
654 2 * sizeof(short) * (maxu) + /* u_s */
655 2 * sizeof(short) * ecp->tap_length; /* y_tilde_s */
656
657 pvt = kzalloc(size, GFP_KERNEL);
658 if (!pvt)
659 return -ENOMEM;
660
661 pvt->dahdi.ops = &my_ops;
662
663 pvt->aggressive = aggressive;
664 pvt->dahdi.features = my_features;
665
666 for (x = 0; x < ecp->param_count; x++) {
667 for (c = p[x].name; *c; c++)
668 *c = tolower(*c);
669 if (!strcmp(p[x].name, "aggressive")) {
670 pvt->aggressive = p[x].value ? 1 : 0;
671 } else {
672 printk(KERN_WARNING "Unknown parameter supplied to KB1 echo canceler: '%s'\n", p[x].name);
673 kfree(pvt);
674
675 return -EINVAL;
676 }
677 }
678
679 init_cc(pvt, ecp->tap_length, maxy, maxu);
680 /* Non-linear processor - a fancy way to say "zap small signals, to avoid
681 accumulating noise". */
682 pvt->use_nlp = TRUE;
683
684 *ec = &pvt->dahdi;
685 return 0;
686 }
687
688 static int echo_can_traintap(struct dahdi_echocan_state *ec, int pos, short val)
689 {
690 struct ec_pvt *pvt = dahdi_to_pvt(ec);
691
692 /* Set the hangover counter to the length of the can to
693 * avoid adjustments occuring immediately after initial forced training
694 */
695 pvt->HCNTR_d = pvt->N_d << 1;
696
697 if (pos >= pvt->N_d)
698 return 1;
699
700 pvt->a_i[pos] = val << 17;
701 pvt->a_s[pos] = val << 1;
702
703 if (++pos >= pvt->N_d)
704 return 1;
705
706 return 0;
707 }
708
709 static void echocan_NLP_toggle(struct dahdi_echocan_state *ec, unsigned int enable)
710 {
711 struct ec_pvt *pvt = dahdi_to_pvt(ec);
712
713 pvt->use_nlp = enable ? 1 : 0;
714 }
715
716 static int __init mod_init(void)
717 {
718 if (dahdi_register_echocan_factory(&my_factory)) {
719 module_printk(KERN_ERR, "could not register with DAHDI core\n");
720
721 return -EPERM;
722 }
723
724 module_printk(KERN_NOTICE, "Registered echo canceler '%s'\n",
725 my_factory.get_name(NULL));
726
727 return 0;
728 }
729
730 static void __exit mod_exit(void)
731 {
732 dahdi_unregister_echocan_factory(&my_factory);
733 }
734
735 module_param(debug, int, S_IRUGO | S_IWUSR);
736 module_param(aggressive, int, S_IRUGO | S_IWUSR);
737
738 MODULE_DESCRIPTION("DAHDI 'KB1' Echo Canceler");
739 MODULE_AUTHOR("Kris Boutilier");
740 MODULE_LICENSE("GPL v2");
741
742 module_init(mod_init);
743 module_exit(mod_exit);