echo.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * SpanDSP - a series of DSP components for telephony
  4. *
  5. * echo.c - A line echo canceller. This code is being developed
  6. * against and partially complies with G168.
  7. *
  8. * Written by Steve Underwood <steveu@coppice.org>
  9. * and David Rowe <david_at_rowetel_dot_com>
  10. *
  11. * Copyright (C) 2001 Steve Underwood and 2007 David Rowe
  12. *
  13. * All rights reserved.
  14. */
  15. #ifndef __ECHO_H
  16. #define __ECHO_H
  17. /*
  18. Line echo cancellation for voice
  19. What does it do?
  20. This module aims to provide G.168-2002 compliant echo cancellation, to remove
  21. electrical echoes (e.g. from 2-4 wire hybrids) from voice calls.
  22. How does it work?
  23. The heart of the echo cancellor is FIR filter. This is adapted to match the
  24. echo impulse response of the telephone line. It must be long enough to
  25. adequately cover the duration of that impulse response. The signal transmitted
  26. to the telephone line is passed through the FIR filter. Once the FIR is
  27. properly adapted, the resulting output is an estimate of the echo signal
  28. received from the line. This is subtracted from the received signal. The result
  29. is an estimate of the signal which originated at the far end of the line, free
  30. from echos of our own transmitted signal.
  31. The least mean squares (LMS) algorithm is attributed to Widrow and Hoff, and
  32. was introduced in 1960. It is the commonest form of filter adaption used in
  33. things like modem line equalisers and line echo cancellers. There it works very
  34. well. However, it only works well for signals of constant amplitude. It works
  35. very poorly for things like speech echo cancellation, where the signal level
  36. varies widely. This is quite easy to fix. If the signal level is normalised -
  37. similar to applying AGC - LMS can work as well for a signal of varying
  38. amplitude as it does for a modem signal. This normalised least mean squares
  39. (NLMS) algorithm is the commonest one used for speech echo cancellation. Many
  40. other algorithms exist - e.g. RLS (essentially the same as Kalman filtering),
  41. FAP, etc. Some perform significantly better than NLMS. However, factors such
  42. as computational complexity and patents favour the use of NLMS.
  43. A simple refinement to NLMS can improve its performance with speech. NLMS tends
  44. to adapt best to the strongest parts of a signal. If the signal is white noise,
  45. the NLMS algorithm works very well. However, speech has more low frequency than
  46. high frequency content. Pre-whitening (i.e. filtering the signal to flatten its
  47. spectrum) the echo signal improves the adapt rate for speech, and ensures the
  48. final residual signal is not heavily biased towards high frequencies. A very
  49. low complexity filter is adequate for this, so pre-whitening adds little to the
  50. compute requirements of the echo canceller.
  51. An FIR filter adapted using pre-whitened NLMS performs well, provided certain
  52. conditions are met:
  53. - The transmitted signal has poor self-correlation.
  54. - There is no signal being generated within the environment being
  55. cancelled.
  56. The difficulty is that neither of these can be guaranteed.
  57. If the adaption is performed while transmitting noise (or something fairly
  58. noise like, such as voice) the adaption works very well. If the adaption is
  59. performed while transmitting something highly correlative (typically narrow
  60. band energy such as signalling tones or DTMF), the adaption can go seriously
  61. wrong. The reason is there is only one solution for the adaption on a near
  62. random signal - the impulse response of the line. For a repetitive signal,
  63. there are any number of solutions which converge the adaption, and nothing
  64. guides the adaption to choose the generalised one. Allowing an untrained
  65. canceller to converge on this kind of narrowband energy probably a good thing,
  66. since at least it cancels the tones. Allowing a well converged canceller to
  67. continue converging on such energy is just a way to ruin its generalised
  68. adaption. A narrowband detector is needed, so adapation can be suspended at
  69. appropriate times.
  70. The adaption process is based on trying to eliminate the received signal. When
  71. there is any signal from within the environment being cancelled it may upset
  72. the adaption process. Similarly, if the signal we are transmitting is small,
  73. noise may dominate and disturb the adaption process. If we can ensure that the
  74. adaption is only performed when we are transmitting a significant signal level,
  75. and the environment is not, things will be OK. Clearly, it is easy to tell when
  76. we are sending a significant signal. Telling, if the environment is generating
  77. a significant signal, and doing it with sufficient speed that the adaption will
  78. not have diverged too much more we stop it, is a little harder.
  79. The key problem in detecting when the environment is sourcing significant
  80. energy is that we must do this very quickly. Given a reasonably long sample of
  81. the received signal, there are a number of strategies which may be used to
  82. assess whether that signal contains a strong far end component. However, by the
  83. time that assessment is complete the far end signal will have already caused
  84. major mis-convergence in the adaption process. An assessment algorithm is
  85. needed which produces a fairly accurate result from a very short burst of far
  86. end energy.
  87. How do I use it?
  88. The echo cancellor processes both the transmit and receive streams sample by
  89. sample. The processing function is not declared inline. Unfortunately,
  90. cancellation requires many operations per sample, so the call overhead is only
  91. a minor burden.
  92. */
  93. #include "fir.h"
  94. #include "oslec.h"
  95. /*
  96. G.168 echo canceller descriptor. This defines the working state for a line
  97. echo canceller.
  98. */
  99. struct oslec_state {
  100. int16_t tx;
  101. int16_t rx;
  102. int16_t clean;
  103. int16_t clean_nlp;
  104. int nonupdate_dwell;
  105. int curr_pos;
  106. int taps;
  107. int log2taps;
  108. int adaption_mode;
  109. int cond_met;
  110. int32_t pstates;
  111. int16_t adapt;
  112. int32_t factor;
  113. int16_t shift;
  114. /* Average levels and averaging filter states */
  115. int ltxacc;
  116. int lrxacc;
  117. int lcleanacc;
  118. int lclean_bgacc;
  119. int ltx;
  120. int lrx;
  121. int lclean;
  122. int lclean_bg;
  123. int lbgn;
  124. int lbgn_acc;
  125. int lbgn_upper;
  126. int lbgn_upper_acc;
  127. /* foreground and background filter states */
  128. struct fir16_state_t fir_state;
  129. struct fir16_state_t fir_state_bg;
  130. int16_t *fir_taps16[2];
  131. /* DC blocking filter states */
  132. int tx_1;
  133. int tx_2;
  134. int rx_1;
  135. int rx_2;
  136. /* optional High Pass Filter states */
  137. int32_t xvtx[5];
  138. int32_t yvtx[5];
  139. int32_t xvrx[5];
  140. int32_t yvrx[5];
  141. /* Parameters for the optional Hoth noise generator */
  142. int cng_level;
  143. int cng_rndnum;
  144. int cng_filter;
  145. /* snapshot sample of coeffs used for development */
  146. int16_t *snapshot;
  147. };
  148. #endif /* __ECHO_H */