1 /* 2 * SpanDSP - a series of DSP components for telephony 3 * 4 * echo.h - An echo cancellor, suitable for electrical and acoustic 5 * cancellation. This code does not currently comply with 6 * any relevant standards (e.g. G.164/5/7/8). 7 * 8 * Written by Steve Underwood <steveu@coppice.org> 9 * 10 * Copyright (C) 2001 Steve Underwood 11 * 12 * Based on a bit from here, a bit from there, eye of toad, 13 * ear of bat, etc - plus, of course, my own 2 cents. 14 * 15 * All rights reserved. 16 * 17 * This program is free software; you can redistribute it and/or modify 18 * it under the terms of the GNU Lesser General Public License version 2.1, 19 * as published by the Free Software Foundation. 20 * 21 * This program is distributed in the hope that it will be useful, 22 * but WITHOUT ANY WARRANTY; without even the implied warranty of 23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 24 * GNU Lesser General Public License for more details. 25 * 26 * You should have received a copy of the GNU Lesser General Public 27 * License along with this program; if not, write to the Free Software 28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 29 * 30 * $Id: echo.h,v 1.13 2008/04/17 14:27:00 steveu Exp $ 31 */ 32 33 /*! \file */ 34 35 #if !defined(_SPANDSP_ECHO_H_) 36 #define _SPANDSP_ECHO_H_ 37 38 /*! \page echo_can_page Line echo cancellation for voice 39 40 \section echo_can_page_sec_1 What does it do? 41 This module aims to provide G.168-2002 compliant echo cancellation, to remove 42 electrical echoes (e.g. from 2-4 wire hybrids) from voice calls. 43 44 \section echo_can_page_sec_2 How does it work? 45 The heart of the echo cancellor is FIR filter. This is adapted to match the echo 46 impulse response of the telephone line. It must be long enough to adequately cover 47 the duration of that impulse response. The signal transmitted to the telephone line 48 is passed through the FIR filter. Once the FIR is properly adapted, the resulting 49 output is an estimate of the echo signal received from the line. This is subtracted 50 from the received signal. The result is an estimate of the signal which originated 51 at the far end of the line, free from echos of our own transmitted signal. 52 53 The least mean squares (LMS) algorithm is attributed to Widrow and Hoff, and was 54 introduced in 1960. It is the commonest form of filter adaption used in things 55 like modem line equalisers and line echo cancellers. There it works very well. 56 However, it only works well for signals of constant amplitude. It works very poorly 57 for things like speech echo cancellation, where the signal level varies widely. 58 This is quite easy to fix. If the signal level is normalised - similar to applying 59 AGC - LMS can work as well for a signal of varying amplitude as it does for a modem 60 signal. This normalised least mean squares (NLMS) algorithm is the commonest one used 61 for speech echo cancellation. Many other algorithms exist - e.g. RLS (essentially 62 the same as Kalman filtering), FAP, etc. Some perform significantly better than NLMS. 63 However, factors such as computational complexity and patents favour the use of NLMS. 64 65 A simple refinement to NLMS can improve its performance with speech. NLMS tends 66 to adapt best to the strongest parts of a signal. If the signal is white noise, 67 the NLMS algorithm works very well. However, speech has more low frequency than 68 high frequency content. Pre-whitening (i.e. filtering the signal to flatten 69 its spectrum) the echo signal improves the adapt rate for speech, and ensures the 70 final residual signal is not heavily biased towards high frequencies. A very low 71 complexity filter is adequate for this, so pre-whitening adds little to the 72 compute requirements of the echo canceller. 73 74 An FIR filter adapted using pre-whitened NLMS performs well, provided certain 75 conditions are met: 76 77 - The transmitted signal has poor self-correlation. 78 - There is no signal being generated within the environment being cancelled. 79 80 The difficulty is that neither of these can be guaranteed. 81 82 If the adaption is performed while transmitting noise (or something fairly noise 83 like, such as voice) the adaption works very well. If the adaption is performed 84 while transmitting something highly correlative (typically narrow band energy 85 such as signalling tones or DTMF), the adaption can go seriously wrong. The reason 86 is there is only one solution for the adaption on a near random signal - the impulse 87 response of the line. For a repetitive signal, there are any number of solutions 88 which converge the adaption, and nothing guides the adaption to choose the generalised 89 one. Allowing an untrained canceller to converge on this kind of narrowband 90 energy probably a good thing, since at least it cancels the tones. Allowing a well 91 converged canceller to continue converging on such energy is just a way to ruin 92 its generalised adaption. A narrowband detector is needed, so adapation can be 93 suspended at appropriate times. 94 95 The adaption process is based on trying to eliminate the received signal. When 96 there is any signal from within the environment being cancelled it may upset the 97 adaption process. Similarly, if the signal we are transmitting is small, noise 98 may dominate and disturb the adaption process. If we can ensure that the 99 adaption is only performed when we are transmitting a significant signal level, 100 and the environment is not, things will be OK. Clearly, it is easy to tell when 101 we are sending a significant signal. Telling, if the environment is generating a 102 significant signal, and doing it with sufficient speed that the adaption will 103 not have diverged too much more we stop it, is a little harder. 104 105 The key problem in detecting when the environment is sourcing significant energy 106 is that we must do this very quickly. Given a reasonably long sample of the 107 received signal, there are a number of strategies which may be used to assess 108 whether that signal contains a strong far end component. However, by the time 109 that assessment is complete the far end signal will have already caused major 110 mis-convergence in the adaption process. An assessment algorithm is needed which 111 produces a fairly accurate result from a very short burst of far end energy. 112 113 \section echo_can_page_sec_3 How do I use it? 114 The echo cancellor processes both the transmit and receive streams sample by 115 sample. The processing function is not declared inline. Unfortunately, 116 cancellation requires many operations per sample, so the call overhead is only a 117 minor burden. 118 */ 119 120 #include "fir.h" 121 122 #define NONUPDATE_DWELL_TIME 600 /* 600 samples, or 75ms */ 123 124 /* Mask bits for the adaption mode */ 125 #define ECHO_CAN_USE_NLP 0x01 126 #define ECHO_CAN_USE_SUPPRESSOR 0x02 127 #define ECHO_CAN_USE_CNG 0x04 128 #define ECHO_CAN_USE_ADAPTION 0x08 129 130 /*! 131 G.168 echo canceller descriptor. This defines the working state for a line 132 echo canceller. 133 */ 134 typedef struct 135 { 136 int tx_power[4]; 137 int rx_power[3]; 138 int clean_rx_power; 139 140 int rx_power_threshold; 141 int nonupdate_dwell; 142 143 fir16_state_t fir_state; 144 /*! Echo FIR taps (16 bit version) */ 145 int16_t *fir_taps16[4]; 146 /*! Echo FIR taps (32 bit version) */ 147 int32_t *fir_taps32; 148 149 int curr_pos; 150 151 int taps; 152 int tap_mask; 153 int adaption_mode; 154 155 int32_t supp_test1; 156 int32_t supp_test2; 157 int32_t supp1; 158 int32_t supp2; 159 int vad; 160 int cng; 161 /* Parameters for the Hoth noise generator */ 162 int cng_level; 163 int cng_rndnum; 164 int cng_filter; 165 166 int16_t geigel_max; 167 int geigel_lag; 168 int dtd_onset; 169 int tap_set; 170 int tap_rotate_counter; 171 172 int32_t latest_correction; /* Indication of the magnitude of the latest 173 adaption, or a code to indicate why adaption 174 was skipped, for test purposes */ 175 int32_t last_acf[28]; 176 int narrowband_count; 177 int narrowband_score; 178 } echo_can_state_t; 179 180 #if defined(__cplusplus) 181 extern "C" 182 { 183 #endif 184 185 /*! Create a voice echo canceller context. 186 \param len The length of the canceller, in samples. 187 \return The new canceller context, or NULL if the canceller could not be created. 188 */ 189 echo_can_state_t *echo_can_create(int len, int adaption_mode); 190 191 /*! Free a voice echo canceller context. 192 \param ec The echo canceller context. 193 */ 194 void echo_can_free(echo_can_state_t *ec); 195 196 /*! Flush (reinitialise) a voice echo canceller context. 197 \param ec The echo canceller context. 198 */ 199 void echo_can_flush(echo_can_state_t *ec); 200 201 /*! Set the adaption mode of a voice echo canceller context. 202 \param ec The echo canceller context. 203 \param adaption_mode The mode. 204 */ 205 void echo_can_adaption_mode(echo_can_state_t *ec, int adaption_mode); 206 207 /*! Process a sample through a voice echo canceller. 208 \param ec The echo canceller context. 209 \param tx The transmitted audio sample. 210 \param rx The received audio sample. 211 \return The clean (echo cancelled) received sample. 212 */ 213 int16_t echo_can_update(echo_can_state_t *ec, int16_t tx, int16_t rx); 214 215 #if defined(__cplusplus) 216 } 217 #endif 218 219 #endif 220 /*- End of file ------------------------------------------------------------*/ 221