login | register
Wed 03 of Dec, 2008 [02:56 UTC]

voip-info.org

History

Asterisk echo cancellation

Created by: oej,Last modification on Tue 18 of Nov, 2008 [19:14 UTC] by valer77

Echo cancellation in Asterisk


Before you proceed down this path, make sure you have read the notes in Causes of Echo on tracing, and attempting to eliminate, the source of any echo. It should be possible to eliminate any residual echo with the zaptel echo canceller, but it won't be able to cope with line faults; and in any case, you'll get better results if you do all you can to eliminate echo at source.

How does echo cancellation work?

Almost all (if not all) echo cancelling algorithms operate by generating multiple copies of the received signal, each delayed by some small time increment In digital terms, this is the equivalent of a shift register and each delayed signal appears at its own unique "tap". The number of taps determines the size of the echo delay that can be cancelled. These delayed copies are then scaled (or weighted) and subtracted from the original received signal. The trick is scaling the the delayed signals to exactly the extent needed to remove the echo and nothing else. The methods used in determining the tap weights (scaling factors) is what distinguishes one algorithm from another.

Don't trust your ears...

The echo canceller must "train" itself by computing the weights when it first sees a signal at the input. The training time depends upon the algorithm used and the number of taps. For optimal training, the number of taps should be as small as possible, while still adequately cancelling the echo. For the typical scenario being experienced by most of us, the signal delay should be relatively short. Don't let your ears be the guide here! The echo you hear has a lot of additional delay added between the location of the echo canceller and your phone. The real delay you are interested in is the delay from the echo canceller, out through the Digium card, through the network to the source of the echo (see Causes of Echo), and back again, to the echo canceller. This delay is relatively short, probably less than 20-30 ms. Simplistically, you'd need a "tail circuit" (the distance between your echo canceller and the source of the echo) of over 2500 miles to acheive an echo path of 30ms (though the digium card and driver probably add a few ms of latency in each direction, so that may be an exaggeration).

Each tap represents 1 sample, so at 8kHz (which is what all of Asterisk's echo cancellers assume), each tap represents 0.125ms. Asterisk's default of 128taps will therefore handle echo paths of up to 16ms, and is therefore probably good for most things.

The take home, however, is that you may get better performance with fewer taps because the training time is shorter and the canceller will adapt faster. Conversely, if you're having problems with echo on long-distance phone calls, you may need to up this to 256 taps.

Beware that Asterisk only lets you set 32, 64, 128 or 256 taps (this is because the algorithms assume the number of taps will be a power of two). Attempting to use a different number of taps will cause Asterisk to fall back to 128 taps without warning.

Algorithmic echo cancellers will never give performance equal to that obtained by balancing the hybrid. This is due to several reasons, not the least of which is that the expansion/compaction must be undone before cancellation, introducing noise and decreasing accuracy of cancellation, and that the delay characteristics and amplitude characteristics of the echo are never as well defined as they are at the point of origin of the echo (the hybrid). This is really too bad, since we don't get to do anything about the hybrid imbalance.

You should also note that you'll only be able to successfully cancel echo if your echo canceller is on the same side of any VoIP links as the source of the echo - the latency introduced by a VoIP system will be too great for any normal echo canceller to cope with.

Also (and this may be obvious!) your echo canceller needs to be pointing in the right direction! Asterisk's echo cancellers will cancel echo arriving from the PSTN via a digium card, before sending the signal up to Asterisk itself. It will not cancel echo arriving over a VoIP channel (which should have been dealt with at the other end, for the reason noted above).

Gain configuration


Before an echo canceller can work, you need to make sure that the levels of the signals it receives are about right. See Asterisk zapata gain adjustment for how to do this.

Terminology


  • Near-end signal - the received signal, to which we need to apply echo cancellation. Note that, (somewhat confusingly) in the context of an Asterisk setup, this will typically be physically further away than the far-end signal - however, in terms of signal latency, the near-end signal (coming from the PSTN) is "closer" than the far-end signal (coming via a VoIP link).

  • Reference signal - the signal we're about to transmit over the PSTN - that is to say, the far-end signal.

  • Tail circuit - the potential distance between the echo canceller and the source of any echo.

  • Doubletalk - the problem the echocanceller encounters when someone at the near end (them) is talking at the same time as the speaker at the far end (you).

Echo cancellation in Asterisk


The first thing to check if you experience echo cancellation with analogue (eg TDM400) cards is that the PSTN loadzone is set correctly. For instance if running in (default) FCC mode, and you are connected to a UK PSTN line, then you *will* observe harsh echo. You will need to change to using UK mode (see TDM400P) in this instance.

The zaptel drivers provide five echo cancellers (seven in zaptel 1.2)--you can configure which is used in zconfig.h. Unfortunately, there's not much documentation available on how to choose between them, or even how to configure them. I'm going to collect the information I've managed to glean on them on the pages below - however, I'm really not an expert in this field, and it really would be nice if the original authors gave some better documentation ;). Proper selection of an echo canceller is dependent upon knowing the algorithm. You need to know what the training performance, tail length, weighting mechanism, etc. If they were identified, at least one could consult the literature for information about the conditions for which the algorithm is ideal: is it best for room echo associated with speaker phones, is it best for cancelling echo from hybrid imbalance, etc.


zaptel 1.0.x offers only the first five of the algorithms listed above. The default echo canceller used by later versions of 1.0.x (eg, 1.0.9.2) is mark2. The default echo canceller used by zaptel 1.2 (up to 1.2.5, at least) is kb1. None of these algorithms has changed since zaptel version 1.2.1(?) 2005-11-30.

fxotune


The FXO module typically has quite a few tunable parameters that affect the electronical characteristics of the device. When tuned right, the device will generate less echo. Those parameters also tend to be quite country-dependent, and hence the first approximation would be to set opermode. See below.

This will work with basically all analog FXO cards (with recent drivers), except the X100P, whose hardware does not have such tuning (anybody tried it with Sangoma A200 cards?)

Basically fxotune pretunes the FXO Module on a TDM400 to deal with the line quality reducing convergence and decreasing the ammount of echo generated in the first place. Hence the echo canceller will have much less echo to cancel later on.

See the Asterisk fxotune page for more info :)

Fax-tone detection


The zaptel drivers provide code which listens for a fax tone (a 2100Hz monotone), and automatically disable the echo cancellers if such a tone is heard in either direction.

This functionality can't be switched off at runtime (though it can be disabled at compile time by setting NO_ECHOCAN_DISABLE)

Stephen R. Besch, November 2003
Updates (with apologies) — Richard van der Hoff, July 2005

See Also


Asterisk | Tips & Tricks | FAQ

Comments

Comments Filter
222

333Echo shown as Off

by timking, Friday 27 of April, 2007 [15:29:33 UTC]
Echo cancellation is only shown if the channel is in use.
222

333Echo cancellation: Currently off

by mpdias, Monday 23 of April, 2007 [11:16:18 UTC]
Despite having the zapata.conf file with echocancel= true and echotraining= true, when I debug the channel in Asterisk CLI "zap show channel 4" it always says "Echo Cancellation: 128 taps, currently OFF". The last line in zapata.conf file of each channel is the definition of the channel...What could be the reason for the echo canceler not to be working? Any help would be much appreciated...

my zapata.conf file (channel 4):

echotraining = yes
callprogress = off
busydetect = yes
signalling = fxs_ks
echocancel = yes
context = incoming
echocancelwhenbridged = yes
channel = 4


222

333Echo Problem PSTN ISDN/DIGIUM/ASTERISK

by jimmmm2, Monday 25 of April, 2005 [20:40:03 UTC]
We are having a problem that whenever some customers use an analog line to dial into our asterisk box, we occasionally get echo.

Because we are using Speech Recognition we need to eliminate the echo at the digium side.

We are currently looking into the mec3.h program.

1) Does anyone have experience with this and how to remove these types of echo at all times.

ie


a)Double talk
b)Complex echo
c)Residual Error Suppression
d)Length of the filter

2) If it does, can you give us information on how to use it to cancel echo when an Analog line dials into our ISDN?

ie Please can you tell us how to compile the mec3.h program for the Digium trunk?

3) or Do you know of a better echo canceller that we should use to get rid of the echo's described above?



222

333Re: Be sure to try echo cancellation on both FXO and FXS lines

by , Wednesday 12 of January, 2005 [00:51:50 UTC]
could you provide you zapata.conf file to illustrat how you did this?

thnx
222

333echotraining=800

by meowmeow64, Thursday 26 of August, 2004 [19:53:40 UTC]
rc2 version of asterisk echo training eliminated echo dramatically in my adtran TA750+T100p+TDM400p(4fxo) setup.
setting echotraining=800 in zapata.conf and echocancel=128(default)

(:biggrin:)
222

333Be sure to try echo cancellation on both FXO and FXS lines

by allanon, Thursday 05 of February, 2004 [22:32:40 UTC]
We had a problem with echo on inside lines (extensions), and tried putting echo cancellation on just the inside lines. It did nothing. Our customers couldn't hear any echo, so we didn't immediately try putting echo cancellation on the outside lines.
Once we put echo cancellation on the outside lines, the echo on the inside lines completely went away!