00001 /* 00002 * SpanDSP - a series of DSP components for telephony 00003 * 00004 * modem_echo.h - An echo cancellor, suitable for electrical echos in GSTN modems 00005 * 00006 * Written by Steve Underwood <steveu@coppice.org> 00007 * 00008 * Copyright (C) 2001, 2004 Steve Underwood 00009 * 00010 * Based on a bit from here, a bit from there, eye of toad, 00011 * ear of bat, etc - plus, of course, my own 2 cents. 00012 * 00013 * All rights reserved. 00014 * 00015 * This program is free software; you can redistribute it and/or modify 00016 * it under the terms of the GNU Lesser General Public License version 2.1, 00017 * as published by the Free Software Foundation. 00018 * 00019 * This program is distributed in the hope that it will be useful, 00020 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00021 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00022 * GNU Lesser General Public License for more details. 00023 * 00024 * You should have received a copy of the GNU Lesser General Public 00025 * License along with this program; if not, write to the Free Software 00026 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00027 * 00028 * $Id: modem_echo.h,v 1.11 2008/04/17 14:27:00 steveu Exp $ 00029 */ 00030 00031 /*! \file */ 00032 00033 #if !defined(_SPANDSP_MODEM_ECHO_H_) 00034 #define _SPANDSP_MODEM_ECHO_H_ 00035 00036 /*! \page modem_echo_can_page Line echo cancellation for modems 00037 00038 \section modem_echo_can_page_sec_1 What does it do? 00039 This module aims to cancel electrical echoes (e.g. from 2-4 wire hybrids) 00040 in modem applications. It is not very suitable for speech applications, which 00041 require additional refinements for satisfactory performance. It is, however, more 00042 efficient and better suited to modem applications. 00043 00044 \section modem_echo_can_page_sec_2 How does it work? 00045 The heart of the echo cancellor is an adaptive FIR filter. This is adapted to 00046 match the impulse response of the environment being cancelled. It must be long 00047 enough to adequately cover the duration of that impulse response. The signal 00048 being transmitted into the environment being cancelled is passed through the 00049 FIR filter. The resulting output is an estimate of the echo signal. This is 00050 then subtracted from the received signal, and the result should be an estimate 00051 of the signal which originates within the environment being cancelled (people 00052 talking in the room, or the signal from the far end of a telephone line) free 00053 from the echos of our own transmitted signal. 00054 00055 The FIR filter is adapted using the least mean squares (LMS) algorithm. This 00056 algorithm is attributed to Widrow and Hoff, and was introduced in 1960. It is 00057 the commonest form of filter adaption used in things like modem line equalisers 00058 and line echo cancellers. It works very well if the signal level is constant, 00059 which is true for a modem signal. To ensure good performa certain conditions must 00060 be met: 00061 00062 - The transmitted signal has weak self-correlation. 00063 - There is no signal being generated within the environment being cancelled. 00064 00065 The difficulty is that neither of these can be guaranteed. If the adaption is 00066 performed while transmitting noise (or something fairly noise like, such as 00067 voice) the adaption works very well. If the adaption is performed while 00068 transmitting something highly correlative (e.g. tones, like DTMF), the adaption 00069 can go seriously wrong. The reason is there is only one solution for the 00070 adaption on a near random signal. For a repetitive signal, there are a number of 00071 solutions which converge the adaption, and nothing guides the adaption to choose 00072 the correct one. 00073 00074 \section modem_echo_can_page_sec_3 How do I use it? 00075 The echo cancellor processes both the transmit and receive streams sample by 00076 sample. The processing function is not declared inline. Unfortunately, 00077 cancellation requires many operations per sample, so the call overhead is only a 00078 minor burden. 00079 */ 00080 00081 #include "fir.h" 00082 00083 /*! 00084 Modem line echo canceller descriptor. This defines the working state for a line 00085 echo canceller. 00086 */ 00087 typedef struct 00088 { 00089 int adapt; 00090 int taps; 00091 00092 fir16_state_t fir_state; 00093 /*! Echo FIR taps (16 bit version) */ 00094 int16_t *fir_taps16; 00095 /*! Echo FIR taps (32 bit version) */ 00096 int32_t *fir_taps32; 00097 00098 int tx_power; 00099 int rx_power; 00100 00101 int curr_pos; 00102 } modem_echo_can_state_t; 00103 00104 #if defined(__cplusplus) 00105 extern "C" 00106 { 00107 #endif 00108 00109 /*! Create a modem echo canceller context. 00110 \param len The length of the canceller, in samples. 00111 eturn The new canceller context, or NULL if the canceller could not be created. 00112 */ 00113 modem_echo_can_state_t *modem_echo_can_create(int len); 00114 00115 /*! Free a modem echo canceller context. 00116 \param ec The echo canceller context. 00117 */ 00118 void modem_echo_can_free(modem_echo_can_state_t *ec); 00119 00120 /*! Flush (reinitialise) a modem echo canceller context. 00121 \param ec The echo canceller context. 00122 */ 00123 void modem_echo_can_flush(modem_echo_can_state_t *ec); 00124 00125 /*! Set the adaption mode of a modem echo canceller context. 00126 \param ec The echo canceller context. 00127 \param adapt The mode. 00128 */ 00129 void modem_echo_can_adaption_mode(modem_echo_can_state_t *ec, int adapt); 00130 00131 /*! Process a sample through a modem echo canceller. 00132 \param ec The echo canceller context. 00133 \param tx The transmitted audio sample. 00134 \param rx The received audio sample. 00135 eturn The clean (echo cancelled) received sample. 00136 */ 00137 int16_t modem_echo_can_update(modem_echo_can_state_t *ec, int16_t tx, int16_t rx); 00138 00139 #if defined(__cplusplus) 00140 } 00141 #endif 00142 00143 #endif 00144 /*- End of file ------------------------------------------------------------*/