echo.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * echo.c - A line echo canceller. This code is being developed
  5. * against and partially complies with G168.
  6. *
  7. * Written by Steve Underwood <steveu@coppice.org>
  8. * and David Rowe <david_at_rowetel_dot_com>
  9. *
  10. * Copyright (C) 2001, 2003 Steve Underwood, 2007 David Rowe
  11. *
  12. * Based on a bit from here, a bit from there, eye of toad, ear of
  13. * bat, 15 years of failed attempts by David and a few fried brain
  14. * cells.
  15. *
  16. * All rights reserved.
  17. *
  18. * This program is free software; you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License version 2, as
  20. * published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  30. */
  31. /*! \file */
  32. /* Implementation Notes
  33. David Rowe
  34. April 2007
  35. This code started life as Steve's NLMS algorithm with a tap
  36. rotation algorithm to handle divergence during double talk. I
  37. added a Geigel Double Talk Detector (DTD) [2] and performed some
  38. G168 tests. However I had trouble meeting the G168 requirements,
  39. especially for double talk - there were always cases where my DTD
  40. failed, for example where near end speech was under the 6dB
  41. threshold required for declaring double talk.
  42. So I tried a two path algorithm [1], which has so far given better
  43. results. The original tap rotation/Geigel algorithm is available
  44. in SVN http://svn.rowetel.com/software/oslec/tags/before_16bit.
  45. It's probably possible to make it work if some one wants to put some
  46. serious work into it.
  47. At present no special treatment is provided for tones, which
  48. generally cause NLMS algorithms to diverge. Initial runs of a
  49. subset of the G168 tests for tones (e.g ./echo_test 6) show the
  50. current algorithm is passing OK, which is kind of surprising. The
  51. full set of tests needs to be performed to confirm this result.
  52. One other interesting change is that I have managed to get the NLMS
  53. code to work with 16 bit coefficients, rather than the original 32
  54. bit coefficents. This reduces the MIPs and storage required.
  55. I evaulated the 16 bit port using g168_tests.sh and listening tests
  56. on 4 real-world samples.
  57. I also attempted the implementation of a block based NLMS update
  58. [2] but although this passes g168_tests.sh it didn't converge well
  59. on the real-world samples. I have no idea why, perhaps a scaling
  60. problem. The block based code is also available in SVN
  61. http://svn.rowetel.com/software/oslec/tags/before_16bit. If this
  62. code can be debugged, it will lead to further reduction in MIPS, as
  63. the block update code maps nicely onto DSP instruction sets (it's a
  64. dot product) compared to the current sample-by-sample update.
  65. Steve also has some nice notes on echo cancellers in echo.h
  66. References:
  67. [1] Ochiai, Areseki, and Ogihara, "Echo Canceller with Two Echo
  68. Path Models", IEEE Transactions on communications, COM-25,
  69. No. 6, June
  70. 1977.
  71. http://www.rowetel.com/images/echo/dual_path_paper.pdf
  72. [2] The classic, very useful paper that tells you how to
  73. actually build a real world echo canceller:
  74. Messerschmitt, Hedberg, Cole, Haoui, Winship, "Digital Voice
  75. Echo Canceller with a TMS320020,
  76. http://www.rowetel.com/images/echo/spra129.pdf
  77. [3] I have written a series of blog posts on this work, here is
  78. Part 1: http://www.rowetel.com/blog/?p=18
  79. [4] The source code http://svn.rowetel.com/software/oslec/
  80. [5] A nice reference on LMS filters:
  81. http://en.wikipedia.org/wiki/Least_mean_squares_filter
  82. Credits:
  83. Thanks to Steve Underwood, Jean-Marc Valin, and Ramakrishnan
  84. Muthukrishnan for their suggestions and email discussions. Thanks
  85. also to those people who collected echo samples for me such as
  86. Mark, Pawel, and Pavel.
  87. */
  88. #include <linux/kernel.h>
  89. #include <linux/module.h>
  90. #include <linux/slab.h>
  91. #include "echo.h"
  92. #define MIN_TX_POWER_FOR_ADAPTION 64
  93. #define MIN_RX_POWER_FOR_ADAPTION 64
  94. #define DTD_HANGOVER 600 /* 600 samples, or 75ms */
  95. #define DC_LOG2BETA 3 /* log2() of DC filter Beta */
  96. /* adapting coeffs using the traditional stochastic descent (N)LMS algorithm */
  97. #ifdef __bfin__
  98. static inline void lms_adapt_bg(struct oslec_state *ec, int clean, int shift)
  99. {
  100. int i;
  101. int offset1;
  102. int offset2;
  103. int factor;
  104. int exp;
  105. int16_t *phist;
  106. int n;
  107. if (shift > 0)
  108. factor = clean << shift;
  109. else
  110. factor = clean >> -shift;
  111. /* Update the FIR taps */
  112. offset2 = ec->curr_pos;
  113. offset1 = ec->taps - offset2;
  114. phist = &ec->fir_state_bg.history[offset2];
  115. /* st: and en: help us locate the assembler in echo.s */
  116. /* asm("st:"); */
  117. n = ec->taps;
  118. for (i = 0; i < n; i++) {
  119. exp = *phist++ * factor;
  120. ec->fir_taps16[1][i] += (int16_t) ((exp + (1 << 14)) >> 15);
  121. }
  122. /* asm("en:"); */
  123. /* Note the asm for the inner loop above generated by Blackfin gcc
  124. 4.1.1 is pretty good (note even parallel instructions used):
  125. R0 = W [P0++] (X);
  126. R0 *= R2;
  127. R0 = R0 + R3 (NS) ||
  128. R1 = W [P1] (X) ||
  129. nop;
  130. R0 >>>= 15;
  131. R0 = R0 + R1;
  132. W [P1++] = R0;
  133. A block based update algorithm would be much faster but the
  134. above can't be improved on much. Every instruction saved in
  135. the loop above is 2 MIPs/ch! The for loop above is where the
  136. Blackfin spends most of it's time - about 17 MIPs/ch measured
  137. with speedtest.c with 256 taps (32ms). Write-back and
  138. Write-through cache gave about the same performance.
  139. */
  140. }
  141. /*
  142. IDEAS for further optimisation of lms_adapt_bg():
  143. 1/ The rounding is quite costly. Could we keep as 32 bit coeffs
  144. then make filter pluck the MS 16-bits of the coeffs when filtering?
  145. However this would lower potential optimisation of filter, as I
  146. think the dual-MAC architecture requires packed 16 bit coeffs.
  147. 2/ Block based update would be more efficient, as per comments above,
  148. could use dual MAC architecture.
  149. 3/ Look for same sample Blackfin LMS code, see if we can get dual-MAC
  150. packing.
  151. 4/ Execute the whole e/c in a block of say 20ms rather than sample
  152. by sample. Processing a few samples every ms is inefficient.
  153. */
  154. #else
  155. static inline void lms_adapt_bg(struct oslec_state *ec, int clean, int shift)
  156. {
  157. int i;
  158. int offset1;
  159. int offset2;
  160. int factor;
  161. int exp;
  162. if (shift > 0)
  163. factor = clean << shift;
  164. else
  165. factor = clean >> -shift;
  166. /* Update the FIR taps */
  167. offset2 = ec->curr_pos;
  168. offset1 = ec->taps - offset2;
  169. for (i = ec->taps - 1; i >= offset1; i--) {
  170. exp = (ec->fir_state_bg.history[i - offset1] * factor);
  171. ec->fir_taps16[1][i] += (int16_t) ((exp + (1 << 14)) >> 15);
  172. }
  173. for (; i >= 0; i--) {
  174. exp = (ec->fir_state_bg.history[i + offset2] * factor);
  175. ec->fir_taps16[1][i] += (int16_t) ((exp + (1 << 14)) >> 15);
  176. }
  177. }
  178. #endif
  179. static inline int top_bit(unsigned int bits)
  180. {
  181. if (bits == 0)
  182. return -1;
  183. else
  184. return (int)fls((int32_t) bits) - 1;
  185. }
  186. struct oslec_state *oslec_create(int len, int adaption_mode)
  187. {
  188. struct oslec_state *ec;
  189. int i;
  190. const int16_t *history;
  191. ec = kzalloc(sizeof(*ec), GFP_KERNEL);
  192. if (!ec)
  193. return NULL;
  194. ec->taps = len;
  195. ec->log2taps = top_bit(len);
  196. ec->curr_pos = ec->taps - 1;
  197. ec->fir_taps16[0] =
  198. kcalloc(ec->taps, sizeof(int16_t), GFP_KERNEL);
  199. if (!ec->fir_taps16[0])
  200. goto error_oom_0;
  201. ec->fir_taps16[1] =
  202. kcalloc(ec->taps, sizeof(int16_t), GFP_KERNEL);
  203. if (!ec->fir_taps16[1])
  204. goto error_oom_1;
  205. history = fir16_create(&ec->fir_state, ec->fir_taps16[0], ec->taps);
  206. if (!history)
  207. goto error_state;
  208. history = fir16_create(&ec->fir_state_bg, ec->fir_taps16[1], ec->taps);
  209. if (!history)
  210. goto error_state_bg;
  211. for (i = 0; i < 5; i++)
  212. ec->xvtx[i] = ec->yvtx[i] = ec->xvrx[i] = ec->yvrx[i] = 0;
  213. ec->cng_level = 1000;
  214. oslec_adaption_mode(ec, adaption_mode);
  215. ec->snapshot = kcalloc(ec->taps, sizeof(int16_t), GFP_KERNEL);
  216. if (!ec->snapshot)
  217. goto error_snap;
  218. ec->cond_met = 0;
  219. ec->pstates = 0;
  220. ec->ltxacc = ec->lrxacc = ec->lcleanacc = ec->lclean_bgacc = 0;
  221. ec->ltx = ec->lrx = ec->lclean = ec->lclean_bg = 0;
  222. ec->tx_1 = ec->tx_2 = ec->rx_1 = ec->rx_2 = 0;
  223. ec->lbgn = ec->lbgn_acc = 0;
  224. ec->lbgn_upper = 200;
  225. ec->lbgn_upper_acc = ec->lbgn_upper << 13;
  226. return ec;
  227. error_snap:
  228. fir16_free(&ec->fir_state_bg);
  229. error_state_bg:
  230. fir16_free(&ec->fir_state);
  231. error_state:
  232. kfree(ec->fir_taps16[1]);
  233. error_oom_1:
  234. kfree(ec->fir_taps16[0]);
  235. error_oom_0:
  236. kfree(ec);
  237. return NULL;
  238. }
  239. EXPORT_SYMBOL_GPL(oslec_create);
  240. void oslec_free(struct oslec_state *ec)
  241. {
  242. int i;
  243. fir16_free(&ec->fir_state);
  244. fir16_free(&ec->fir_state_bg);
  245. for (i = 0; i < 2; i++)
  246. kfree(ec->fir_taps16[i]);
  247. kfree(ec->snapshot);
  248. kfree(ec);
  249. }
  250. EXPORT_SYMBOL_GPL(oslec_free);
  251. void oslec_adaption_mode(struct oslec_state *ec, int adaption_mode)
  252. {
  253. ec->adaption_mode = adaption_mode;
  254. }
  255. EXPORT_SYMBOL_GPL(oslec_adaption_mode);
  256. void oslec_flush(struct oslec_state *ec)
  257. {
  258. int i;
  259. ec->ltxacc = ec->lrxacc = ec->lcleanacc = ec->lclean_bgacc = 0;
  260. ec->ltx = ec->lrx = ec->lclean = ec->lclean_bg = 0;
  261. ec->tx_1 = ec->tx_2 = ec->rx_1 = ec->rx_2 = 0;
  262. ec->lbgn = ec->lbgn_acc = 0;
  263. ec->lbgn_upper = 200;
  264. ec->lbgn_upper_acc = ec->lbgn_upper << 13;
  265. ec->nonupdate_dwell = 0;
  266. fir16_flush(&ec->fir_state);
  267. fir16_flush(&ec->fir_state_bg);
  268. ec->fir_state.curr_pos = ec->taps - 1;
  269. ec->fir_state_bg.curr_pos = ec->taps - 1;
  270. for (i = 0; i < 2; i++)
  271. memset(ec->fir_taps16[i], 0, ec->taps * sizeof(int16_t));
  272. ec->curr_pos = ec->taps - 1;
  273. ec->pstates = 0;
  274. }
  275. EXPORT_SYMBOL_GPL(oslec_flush);
  276. void oslec_snapshot(struct oslec_state *ec)
  277. {
  278. memcpy(ec->snapshot, ec->fir_taps16[0], ec->taps * sizeof(int16_t));
  279. }
  280. EXPORT_SYMBOL_GPL(oslec_snapshot);
  281. /* Dual Path Echo Canceller */
  282. int16_t oslec_update(struct oslec_state *ec, int16_t tx, int16_t rx)
  283. {
  284. int32_t echo_value;
  285. int clean_bg;
  286. int tmp;
  287. int tmp1;
  288. /*
  289. * Input scaling was found be required to prevent problems when tx
  290. * starts clipping. Another possible way to handle this would be the
  291. * filter coefficent scaling.
  292. */
  293. ec->tx = tx;
  294. ec->rx = rx;
  295. tx >>= 1;
  296. rx >>= 1;
  297. /*
  298. * Filter DC, 3dB point is 160Hz (I think), note 32 bit precision
  299. * required otherwise values do not track down to 0. Zero at DC, Pole
  300. * at (1-Beta) on real axis. Some chip sets (like Si labs) don't
  301. * need this, but something like a $10 X100P card does. Any DC really
  302. * slows down convergence.
  303. *
  304. * Note: removes some low frequency from the signal, this reduces the
  305. * speech quality when listening to samples through headphones but may
  306. * not be obvious through a telephone handset.
  307. *
  308. * Note that the 3dB frequency in radians is approx Beta, e.g. for Beta
  309. * = 2^(-3) = 0.125, 3dB freq is 0.125 rads = 159Hz.
  310. */
  311. if (ec->adaption_mode & ECHO_CAN_USE_RX_HPF) {
  312. tmp = rx << 15;
  313. /*
  314. * Make sure the gain of the HPF is 1.0. This can still
  315. * saturate a little under impulse conditions, and it might
  316. * roll to 32768 and need clipping on sustained peak level
  317. * signals. However, the scale of such clipping is small, and
  318. * the error due to any saturation should not markedly affect
  319. * the downstream processing.
  320. */
  321. tmp -= (tmp >> 4);
  322. ec->rx_1 += -(ec->rx_1 >> DC_LOG2BETA) + tmp - ec->rx_2;
  323. /*
  324. * hard limit filter to prevent clipping. Note that at this
  325. * stage rx should be limited to +/- 16383 due to right shift
  326. * above
  327. */
  328. tmp1 = ec->rx_1 >> 15;
  329. if (tmp1 > 16383)
  330. tmp1 = 16383;
  331. if (tmp1 < -16383)
  332. tmp1 = -16383;
  333. rx = tmp1;
  334. ec->rx_2 = tmp;
  335. }
  336. /* Block average of power in the filter states. Used for
  337. adaption power calculation. */
  338. {
  339. int new, old;
  340. /* efficient "out with the old and in with the new" algorithm so
  341. we don't have to recalculate over the whole block of
  342. samples. */
  343. new = (int)tx * (int)tx;
  344. old = (int)ec->fir_state.history[ec->fir_state.curr_pos] *
  345. (int)ec->fir_state.history[ec->fir_state.curr_pos];
  346. ec->pstates +=
  347. ((new - old) + (1 << (ec->log2taps - 1))) >> ec->log2taps;
  348. if (ec->pstates < 0)
  349. ec->pstates = 0;
  350. }
  351. /* Calculate short term average levels using simple single pole IIRs */
  352. ec->ltxacc += abs(tx) - ec->ltx;
  353. ec->ltx = (ec->ltxacc + (1 << 4)) >> 5;
  354. ec->lrxacc += abs(rx) - ec->lrx;
  355. ec->lrx = (ec->lrxacc + (1 << 4)) >> 5;
  356. /* Foreground filter */
  357. ec->fir_state.coeffs = ec->fir_taps16[0];
  358. echo_value = fir16(&ec->fir_state, tx);
  359. ec->clean = rx - echo_value;
  360. ec->lcleanacc += abs(ec->clean) - ec->lclean;
  361. ec->lclean = (ec->lcleanacc + (1 << 4)) >> 5;
  362. /* Background filter */
  363. echo_value = fir16(&ec->fir_state_bg, tx);
  364. clean_bg = rx - echo_value;
  365. ec->lclean_bgacc += abs(clean_bg) - ec->lclean_bg;
  366. ec->lclean_bg = (ec->lclean_bgacc + (1 << 4)) >> 5;
  367. /* Background Filter adaption */
  368. /* Almost always adap bg filter, just simple DT and energy
  369. detection to minimise adaption in cases of strong double talk.
  370. However this is not critical for the dual path algorithm.
  371. */
  372. ec->factor = 0;
  373. ec->shift = 0;
  374. if ((ec->nonupdate_dwell == 0)) {
  375. int p, logp, shift;
  376. /* Determine:
  377. f = Beta * clean_bg_rx/P ------ (1)
  378. where P is the total power in the filter states.
  379. The Boffins have shown that if we obey (1) we converge
  380. quickly and avoid instability.
  381. The correct factor f must be in Q30, as this is the fixed
  382. point format required by the lms_adapt_bg() function,
  383. therefore the scaled version of (1) is:
  384. (2^30) * f = (2^30) * Beta * clean_bg_rx/P
  385. factor = (2^30) * Beta * clean_bg_rx/P ----- (2)
  386. We have chosen Beta = 0.25 by experiment, so:
  387. factor = (2^30) * (2^-2) * clean_bg_rx/P
  388. (30 - 2 - log2(P))
  389. factor = clean_bg_rx 2 ----- (3)
  390. To avoid a divide we approximate log2(P) as top_bit(P),
  391. which returns the position of the highest non-zero bit in
  392. P. This approximation introduces an error as large as a
  393. factor of 2, but the algorithm seems to handle it OK.
  394. Come to think of it a divide may not be a big deal on a
  395. modern DSP, so its probably worth checking out the cycles
  396. for a divide versus a top_bit() implementation.
  397. */
  398. p = MIN_TX_POWER_FOR_ADAPTION + ec->pstates;
  399. logp = top_bit(p) + ec->log2taps;
  400. shift = 30 - 2 - logp;
  401. ec->shift = shift;
  402. lms_adapt_bg(ec, clean_bg, shift);
  403. }
  404. /* very simple DTD to make sure we dont try and adapt with strong
  405. near end speech */
  406. ec->adapt = 0;
  407. if ((ec->lrx > MIN_RX_POWER_FOR_ADAPTION) && (ec->lrx > ec->ltx))
  408. ec->nonupdate_dwell = DTD_HANGOVER;
  409. if (ec->nonupdate_dwell)
  410. ec->nonupdate_dwell--;
  411. /* Transfer logic */
  412. /* These conditions are from the dual path paper [1], I messed with
  413. them a bit to improve performance. */
  414. if ((ec->adaption_mode & ECHO_CAN_USE_ADAPTION) &&
  415. (ec->nonupdate_dwell == 0) &&
  416. /* (ec->Lclean_bg < 0.875*ec->Lclean) */
  417. (8 * ec->lclean_bg < 7 * ec->lclean) &&
  418. /* (ec->Lclean_bg < 0.125*ec->Ltx) */
  419. (8 * ec->lclean_bg < ec->ltx)) {
  420. if (ec->cond_met == 6) {
  421. /*
  422. * BG filter has had better results for 6 consecutive
  423. * samples
  424. */
  425. ec->adapt = 1;
  426. memcpy(ec->fir_taps16[0], ec->fir_taps16[1],
  427. ec->taps * sizeof(int16_t));
  428. } else
  429. ec->cond_met++;
  430. } else
  431. ec->cond_met = 0;
  432. /* Non-Linear Processing */
  433. ec->clean_nlp = ec->clean;
  434. if (ec->adaption_mode & ECHO_CAN_USE_NLP) {
  435. /*
  436. * Non-linear processor - a fancy way to say "zap small
  437. * signals, to avoid residual echo due to (uLaw/ALaw)
  438. * non-linearity in the channel.".
  439. */
  440. if ((16 * ec->lclean < ec->ltx)) {
  441. /*
  442. * Our e/c has improved echo by at least 24 dB (each
  443. * factor of 2 is 6dB, so 2*2*2*2=16 is the same as
  444. * 6+6+6+6=24dB)
  445. */
  446. if (ec->adaption_mode & ECHO_CAN_USE_CNG) {
  447. ec->cng_level = ec->lbgn;
  448. /*
  449. * Very elementary comfort noise generation.
  450. * Just random numbers rolled off very vaguely
  451. * Hoth-like. DR: This noise doesn't sound
  452. * quite right to me - I suspect there are some
  453. * overflow issues in the filtering as it's too
  454. * "crackly".
  455. * TODO: debug this, maybe just play noise at
  456. * high level or look at spectrum.
  457. */
  458. ec->cng_rndnum =
  459. 1664525U * ec->cng_rndnum + 1013904223U;
  460. ec->cng_filter =
  461. ((ec->cng_rndnum & 0xFFFF) - 32768 +
  462. 5 * ec->cng_filter) >> 3;
  463. ec->clean_nlp =
  464. (ec->cng_filter * ec->cng_level * 8) >> 14;
  465. } else if (ec->adaption_mode & ECHO_CAN_USE_CLIP) {
  466. /* This sounds much better than CNG */
  467. if (ec->clean_nlp > ec->lbgn)
  468. ec->clean_nlp = ec->lbgn;
  469. if (ec->clean_nlp < -ec->lbgn)
  470. ec->clean_nlp = -ec->lbgn;
  471. } else {
  472. /*
  473. * just mute the residual, doesn't sound very
  474. * good, used mainly in G168 tests
  475. */
  476. ec->clean_nlp = 0;
  477. }
  478. } else {
  479. /*
  480. * Background noise estimator. I tried a few
  481. * algorithms here without much luck. This very simple
  482. * one seems to work best, we just average the level
  483. * using a slow (1 sec time const) filter if the
  484. * current level is less than a (experimentally
  485. * derived) constant. This means we dont include high
  486. * level signals like near end speech. When combined
  487. * with CNG or especially CLIP seems to work OK.
  488. */
  489. if (ec->lclean < 40) {
  490. ec->lbgn_acc += abs(ec->clean) - ec->lbgn;
  491. ec->lbgn = (ec->lbgn_acc + (1 << 11)) >> 12;
  492. }
  493. }
  494. }
  495. /* Roll around the taps buffer */
  496. if (ec->curr_pos <= 0)
  497. ec->curr_pos = ec->taps;
  498. ec->curr_pos--;
  499. if (ec->adaption_mode & ECHO_CAN_DISABLE)
  500. ec->clean_nlp = rx;
  501. /* Output scaled back up again to match input scaling */
  502. return (int16_t) ec->clean_nlp << 1;
  503. }
  504. EXPORT_SYMBOL_GPL(oslec_update);
  505. /* This function is separated from the echo canceller is it is usually called
  506. as part of the tx process. See rx HP (DC blocking) filter above, it's
  507. the same design.
  508. Some soft phones send speech signals with a lot of low frequency
  509. energy, e.g. down to 20Hz. This can make the hybrid non-linear
  510. which causes the echo canceller to fall over. This filter can help
  511. by removing any low frequency before it gets to the tx port of the
  512. hybrid.
  513. It can also help by removing and DC in the tx signal. DC is bad
  514. for LMS algorithms.
  515. This is one of the classic DC removal filters, adjusted to provide
  516. sufficient bass rolloff to meet the above requirement to protect hybrids
  517. from things that upset them. The difference between successive samples
  518. produces a lousy HPF, and then a suitably placed pole flattens things out.
  519. The final result is a nicely rolled off bass end. The filtering is
  520. implemented with extended fractional precision, which noise shapes things,
  521. giving very clean DC removal.
  522. */
  523. int16_t oslec_hpf_tx(struct oslec_state *ec, int16_t tx)
  524. {
  525. int tmp;
  526. int tmp1;
  527. if (ec->adaption_mode & ECHO_CAN_USE_TX_HPF) {
  528. tmp = tx << 15;
  529. /*
  530. * Make sure the gain of the HPF is 1.0. The first can still
  531. * saturate a little under impulse conditions, and it might
  532. * roll to 32768 and need clipping on sustained peak level
  533. * signals. However, the scale of such clipping is small, and
  534. * the error due to any saturation should not markedly affect
  535. * the downstream processing.
  536. */
  537. tmp -= (tmp >> 4);
  538. ec->tx_1 += -(ec->tx_1 >> DC_LOG2BETA) + tmp - ec->tx_2;
  539. tmp1 = ec->tx_1 >> 15;
  540. if (tmp1 > 32767)
  541. tmp1 = 32767;
  542. if (tmp1 < -32767)
  543. tmp1 = -32767;
  544. tx = tmp1;
  545. ec->tx_2 = tmp;
  546. }
  547. return tx;
  548. }
  549. EXPORT_SYMBOL_GPL(oslec_hpf_tx);
  550. MODULE_LICENSE("GPL");
  551. MODULE_AUTHOR("David Rowe");
  552. MODULE_DESCRIPTION("Open Source Line Echo Canceller");
  553. MODULE_VERSION("0.3.0");