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