echo_david.h

Go to the documentation of this file.
00001 /*
00002  * SpanDSP - a series of DSP components for telephony
00003  *
00004  * echo.h - A line echo canceller.  This code is being developed
00005  *          against and partially complies with G168.
00006  *
00007  * Written by Steve Underwood <steveu@coppice.org> 
00008  *         and David Rowe <david_at_rowetel_dot_com>
00009  *
00010  * Copyright (C) 2001 Steve Underwood and 2007 David Rowe
00011  *
00012  * All rights reserved.
00013  *
00014  * This program is free software; you can redistribute it and/or modify
00015  * it under the terms of the GNU General Public License version 2, as
00016  * published by the Free Software Foundation.
00017  *
00018  * This program is distributed in the hope that it will be useful,
00019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021  * GNU General Public License for more details.
00022  *
00023  * You should have received a copy of the GNU General Public License
00024  * along with this program; if not, write to the Free Software
00025  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00026  *
00027  * $Id: echo.h,v 1.9 2006/10/24 13:45:28 steveu Exp $
00028  */
00029 
00030 /*! \file */
00031 
00032 #if !defined(_SPANDSP_ECHO_H_)
00033 #define _SPANDSP_ECHO_H_
00034 
00035 /*! \page echo_can_page Line echo cancellation for voice
00036 
00037 \section echo_can_page_sec_1 What does it do?
00038 This module aims to provide G.168-2002 compliant echo cancellation, to remove
00039 electrical echoes (e.g. from 2-4 wire hybrids) from voice calls.
00040 
00041 \section echo_can_page_sec_2 How does it work?
00042 The heart of the echo cancellor is FIR filter. This is adapted to match the echo
00043 impulse response of the telephone line. It must be long enough to adequately cover
00044 the duration of that impulse response. The signal transmitted to the telephone line
00045 is passed through the FIR filter. Once the FIR is properly adapted, the resulting
00046 output is an estimate of the echo signal received from the line. This is subtracted
00047 from the received signal. The result is an estimate of the signal which originated
00048 at the far end of the line, free from echos of our own transmitted signal. 
00049 
00050 The least mean squares (LMS) algorithm is attributed to Widrow and Hoff, and was
00051 introduced in 1960. It is the commonest form of filter adaption used in things
00052 like modem line equalisers and line echo cancellers. There it works very well.
00053 However, it only works well for signals of constant amplitude. It works very poorly
00054 for things like speech echo cancellation, where the signal level varies widely.
00055 This is quite easy to fix. If the signal level is normalised - similar to applying
00056 AGC - LMS can work as well for a signal of varying amplitude as it does for a modem
00057 signal. This normalised least mean squares (NLMS) algorithm is the commonest one used
00058 for speech echo cancellation. Many other algorithms exist - e.g. RLS (essentially
00059 the same as Kalman filtering), FAP, etc. Some perform significantly better than NLMS.
00060 However, factors such as computational complexity and patents favour the use of NLMS.
00061 
00062 A simple refinement to NLMS can improve its performance with speech. NLMS tends
00063 to adapt best to the strongest parts of a signal. If the signal is white noise,
00064 the NLMS algorithm works very well. However, speech has more low frequency than
00065 high frequency content. Pre-whitening (i.e. filtering the signal to flatten
00066 its spectrum) the echo signal improves the adapt rate for speech, and ensures the
00067 final residual signal is not heavily biased towards high frequencies. A very low
00068 complexity filter is adequate for this, so pre-whitening adds little to the
00069 compute requirements of the echo canceller.
00070 
00071 An FIR filter adapted using pre-whitened NLMS performs well, provided certain
00072 conditions are met: 
00073 
00074     - The transmitted signal has poor self-correlation.
00075     - There is no signal being generated within the environment being cancelled.
00076 
00077 The difficulty is that neither of these can be guaranteed.
00078 
00079 If the adaption is performed while transmitting noise (or something fairly noise
00080 like, such as voice) the adaption works very well. If the adaption is performed
00081 while transmitting something highly correlative (typically narrow band energy
00082 such as signalling tones or DTMF), the adaption can go seriously wrong. The reason
00083 is there is only one solution for the adaption on a near random signal - the impulse
00084 response of the line. For a repetitive signal, there are any number of solutions
00085 which converge the adaption, and nothing guides the adaption to choose the generalised
00086 one. Allowing an untrained canceller to converge on this kind of narrowband
00087 energy probably a good thing, since at least it cancels the tones. Allowing a well
00088 converged canceller to continue converging on such energy is just a way to ruin
00089 its generalised adaption. A narrowband detector is needed, so adapation can be
00090 suspended at appropriate times.
00091 
00092 The adaption process is based on trying to eliminate the received signal. When
00093 there is any signal from within the environment being cancelled it may upset the
00094 adaption process. Similarly, if the signal we are transmitting is small, noise
00095 may dominate and disturb the adaption process. If we can ensure that the
00096 adaption is only performed when we are transmitting a significant signal level,
00097 and the environment is not, things will be OK. Clearly, it is easy to tell when
00098 we are sending a significant signal. Telling, if the environment is generating a
00099 significant signal, and doing it with sufficient speed that the adaption will
00100 not have diverged too much more we stop it, is a little harder. 
00101 
00102 The key problem in detecting when the environment is sourcing significant energy
00103 is that we must do this very quickly. Given a reasonably long sample of the
00104 received signal, there are a number of strategies which may be used to assess
00105 whether that signal contains a strong far end component. However, by the time
00106 that assessment is complete the far end signal will have already caused major
00107 mis-convergence in the adaption process. An assessment algorithm is needed which
00108 produces a fairly accurate result from a very short burst of far end energy. 
00109 
00110 \section echo_can_page_sec_3 How do I use it?
00111 The echo cancellor processes both the transmit and receive streams sample by
00112 sample. The processing function is not declared inline. Unfortunately,
00113 cancellation requires many operations per sample, so the call overhead is only a
00114 minor burden. 
00115 */
00116 
00117 #include "fir.h"
00118 
00119 /* Mask bits for the adaption mode */
00120 enum
00121 {
00122     ECHO_CAN_USE_ADAPTION = 0x01,
00123     ECHO_CAN_USE_NLP = 0x02,
00124     ECHO_CAN_USE_CNG = 0x04,
00125     ECHO_CAN_USE_CLIP = 0x08,
00126     ECHO_CAN_USE_SUPPRESSOR = 0x10,
00127     ECHO_CAN_USE_TX_HPF = 0x20,
00128     ECHO_CAN_USE_RX_HPF = 0x40,
00129     ECHO_CAN_DISABLE = 0x80,
00130 };
00131 
00132 /*!
00133     G.168 echo canceller descriptor. This defines the working state for a line
00134     echo canceller.
00135 */
00136 typedef struct
00137 {
00138     int16_t tx;
00139     int16_t rx;
00140     int16_t clean;
00141     int16_t clean_nlp;
00142 
00143     int nonupdate_dwell;
00144     int curr_pos;       
00145     int taps;
00146     int log2taps;
00147     int adaption_mode;
00148 
00149     int cond_met;
00150     int32_t Pstates;
00151     int16_t adapt;
00152     int32_t factor;
00153     int16_t shift;
00154 
00155     /* Average levels and averaging filter states */ 
00156     int Ltxacc;
00157     int Lrxacc;
00158     int Lcleanacc;
00159     int Lclean_bgacc;
00160     int Ltx;
00161     int Lrx;
00162     int Lclean;
00163     int Lclean_bg;
00164     int Lbgn;
00165     int Lbgn_acc;
00166     int Lbgn_upper;
00167     int Lbgn_upper_acc;
00168 
00169     /* Foreground and background filter states */
00170     fir16_state_t fir_state;
00171     fir16_state_t fir_state_bg;
00172     int16_t *fir_taps16[2];
00173     
00174     /* DC and near DC blocking filter states */
00175     int32_t tx_hpf[2];
00176     int32_t rx_hpf[2];
00177    
00178     /* Parameters for the optional Hoth noise generator */
00179     int cng_level;
00180     int cng_rndnum;
00181     int cng_filter;
00182     
00183     /* Snapshot sample of coeffs used for development */
00184     int16_t *snapshot;       
00185 } echo_can_state_t;
00186 
00187 #if defined(__cplusplus)
00188 extern "C"
00189 {
00190 #endif
00191 
00192 /*! Create a voice echo canceller context.
00193     \param len The length of the canceller, in samples.
00194     \return The new canceller context, or NULL if the canceller could not be created.
00195 */
00196 echo_can_state_t *echo_can_create(int len, int adaption_mode);
00197 
00198 /*! Free a voice echo canceller context.
00199     \param ec The echo canceller context.
00200 */
00201 void echo_can_free(echo_can_state_t *ec);
00202 
00203 /*! Flush (reinitialise) a voice echo canceller context.
00204     \param ec The echo canceller context.
00205 */
00206 void echo_can_flush(echo_can_state_t *ec);
00207 
00208 /*! Set the adaption mode of a voice echo canceller context.
00209     \param ec The echo canceller context.
00210     \param adaption_mode The mode.
00211 */
00212 void echo_can_adaption_mode(echo_can_state_t *ec, int adaption_mode);
00213 
00214 /*! Process a sample through a voice echo canceller.
00215     \param ec The echo canceller context.
00216     \param tx The transmitted audio sample.
00217     \param rx The received audio sample.
00218     \return The clean (echo cancelled) received sample.
00219 */
00220 int16_t echo_can_update(echo_can_state_t *ec, int16_t tx, int16_t rx);
00221 
00222 /*! Process to high pass filter the tx signal.
00223     \param ec The echo canceller context.
00224     \param tx The transmitted auio sample.
00225     \return The HP filtered transmit sample, send this to your D/A.
00226 */
00227 int16_t echo_can_hpf_tx(echo_can_state_t *ec, int16_t tx);
00228 
00229 void echo_can_snapshot(echo_can_state_t *ec);
00230 
00231 #if defined(__cplusplus)
00232 }
00233 #endif
00234 
00235 #endif
00236 /*- End of file ------------------------------------------------------------*/

Generated on Tue Oct 7 20:25:45 2008 for spandsp by  doxygen 1.5.6