?? echo.h
字號:
/* * SpanDSP - a series of DSP components for telephony * * echo.h - An echo cancellor, suitable for electrical and acoustic * cancellation. This code does not currently comply with * any relevant standards (e.g. G.164/5/7/8). * * Written by Steve Underwood <steveu@coppice.org> * * Copyright (C) 2001 Steve Underwood * * Based on a bit from here, a bit from there, eye of toad, * ear of bat, etc - plus, of course, my own 2 cents. * * All rights reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2, as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * $Id: echo.h,v 1.10 2007/04/05 19:20:49 steveu Exp $ *//*! \file */#if !defined(_SPANDSP_ECHO_H_)#define _SPANDSP_ECHO_H_/*! \page echo_can_page Line echo cancellation for voice\section echo_can_page_sec_1 What does it do?This module aims to provide G.168-2002 compliant echo cancellation, to removeelectrical echoes (e.g. from 2-4 wire hybrids) from voice calls.\section echo_can_page_sec_2 How does it work?The heart of the echo cancellor is FIR filter. This is adapted to match the echoimpulse response of the telephone line. It must be long enough to adequately coverthe duration of that impulse response. The signal transmitted to the telephone lineis passed through the FIR filter. Once the FIR is properly adapted, the resultingoutput is an estimate of the echo signal received from the line. This is subtractedfrom the received signal. The result is an estimate of the signal which originatedat the far end of the line, free from echos of our own transmitted signal. The least mean squares (LMS) algorithm is attributed to Widrow and Hoff, and wasintroduced in 1960. It is the commonest form of filter adaption used in thingslike modem line equalisers and line echo cancellers. There it works very well.However, it only works well for signals of constant amplitude. It works very poorlyfor things like speech echo cancellation, where the signal level varies widely.This is quite easy to fix. If the signal level is normalised - similar to applyingAGC - LMS can work as well for a signal of varying amplitude as it does for a modemsignal. This normalised least mean squares (NLMS) algorithm is the commonest one usedfor speech echo cancellation. Many other algorithms exist - e.g. RLS (essentiallythe same as Kalman filtering), FAP, etc. Some perform significantly better than NLMS.However, factors such as computational complexity and patents favour the use of NLMS.A simple refinement to NLMS can improve its performance with speech. NLMS tendsto adapt best to the strongest parts of a signal. If the signal is white noise,the NLMS algorithm works very well. However, speech has more low frequency thanhigh frequency content. Pre-whitening (i.e. filtering the signal to flattenits spectrum) the echo signal improves the adapt rate for speech, and ensures thefinal residual signal is not heavily biased towards high frequencies. A very lowcomplexity filter is adequate for this, so pre-whitening adds little to thecompute requirements of the echo canceller.An FIR filter adapted using pre-whitened NLMS performs well, provided certainconditions are met: - The transmitted signal has poor self-correlation. - There is no signal being generated within the environment being cancelled.The difficulty is that neither of these can be guaranteed.If the adaption is performed while transmitting noise (or something fairly noiselike, such as voice) the adaption works very well. If the adaption is performedwhile transmitting something highly correlative (typically narrow band energysuch as signalling tones or DTMF), the adaption can go seriously wrong. The reasonis there is only one solution for the adaption on a near random signal - the impulseresponse of the line. For a repetitive signal, there are any number of solutionswhich converge the adaption, and nothing guides the adaption to choose the generalisedone. Allowing an untrained canceller to converge on this kind of narrowbandenergy probably a good thing, since at least it cancels the tones. Allowing a wellconverged canceller to continue converging on such energy is just a way to ruinits generalised adaption. A narrowband detector is needed, so adapation can besuspended at appropriate times.The adaption process is based on trying to eliminate the received signal. Whenthere is any signal from within the environment being cancelled it may upset theadaption process. Similarly, if the signal we are transmitting is small, noisemay dominate and disturb the adaption process. If we can ensure that theadaption is only performed when we are transmitting a significant signal level,and the environment is not, things will be OK. Clearly, it is easy to tell whenwe are sending a significant signal. Telling, if the environment is generating asignificant signal, and doing it with sufficient speed that the adaption willnot have diverged too much more we stop it, is a little harder. The key problem in detecting when the environment is sourcing significant energyis that we must do this very quickly. Given a reasonably long sample of thereceived signal, there are a number of strategies which may be used to assesswhether that signal contains a strong far end component. However, by the timethat assessment is complete the far end signal will have already caused majormis-convergence in the adaption process. An assessment algorithm is needed whichproduces a fairly accurate result from a very short burst of far end energy. \section echo_can_page_sec_3 How do I use it?The echo cancellor processes both the transmit and receive streams sample bysample. The processing function is not declared inline. Unfortunately,cancellation requires many operations per sample, so the call overhead is only aminor burden. */#include "fir.h"#define NONUPDATE_DWELL_TIME 600 /* 600 samples, or 75ms *//* Mask bits for the adaption mode */#define ECHO_CAN_USE_NLP 0x01#define ECHO_CAN_USE_SUPPRESSOR 0x02#define ECHO_CAN_USE_CNG 0x04#define ECHO_CAN_USE_ADAPTION 0x08/*! G.168 echo canceller descriptor. This defines the working state for a line echo canceller.*/typedef struct{ int tx_power[4]; int rx_power[3]; int clean_rx_power; int rx_power_threshold; int nonupdate_dwell; fir16_state_t fir_state; /*! Echo FIR taps (16 bit version) */ int16_t *fir_taps16[4]; /*! Echo FIR taps (32 bit version) */ int32_t *fir_taps32; int curr_pos; int taps; int tap_mask; int adaption_mode; int32_t supp_test1; int32_t supp_test2; int32_t supp1; int32_t supp2; int vad; int cng; /* Parameters for the Hoth noise generator */ int cng_level; int cng_rndnum; int cng_filter; int16_t geigel_max; int geigel_lag; int dtd_onset; int tap_set; int tap_rotate_counter; int32_t latest_correction; /* Indication of the magnitude of the latest adaption, or a code to indicate why adaption was skipped, for test purposes */ int32_t last_acf[28]; int narrowband_count; int narrowband_score;} echo_can_state_t;/*! Create a voice echo canceller context. \param len The length of the canceller, in samples. \return The new canceller context, or NULL if the canceller could not be created.*/echo_can_state_t *echo_can_create(int len, int adaption_mode);/*! Free a voice echo canceller context. \param ec The echo canceller context.*/void echo_can_free(echo_can_state_t *ec);/*! Flush (reinitialise) a voice echo canceller context. \param ec The echo canceller context.*/void echo_can_flush(echo_can_state_t *ec);/*! Set the adaption mode of a voice echo canceller context. \param ec The echo canceller context. \param adapt The mode.*/void echo_can_adaption_mode(echo_can_state_t *ec, int adaption_mode);/*! Process a sample through a voice echo canceller. \param ec The echo canceller context. \param tx The transmitted audio sample. \param rx The received audio sample. \return The clean (echo cancelled) received sample.*/int16_t echo_can_update(echo_can_state_t *ec, int16_t tx, int16_t rx);#endif/*- End of file ------------------------------------------------------------*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -