ecdis.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * ec_disable_detector.h - A detector which should eventually meet the
  5. * G.164/G.165 requirements for detecting the
  6. * 2100Hz echo cancellor disable tone.
  7. *
  8. * Written by Steve Underwood <steveu@coppice.org>
  9. *
  10. * Copyright (C) 2001 Steve Underwood
  11. *
  12. * All rights reserved.
  13. *
  14. */
  15. /*
  16. * See http://www.asterisk.org for more information about
  17. * the Asterisk project. Please do not directly contact
  18. * any of the maintainers of this project for assistance;
  19. * the project provides a web site, mailing lists and IRC
  20. * channels for your use.
  21. *
  22. * This program is free software, distributed under the terms of
  23. * the GNU General Public License Version 2 as published by the
  24. * Free Software Foundation. See the LICENSE file included with
  25. * this program for more details.
  26. */
  27. #include "biquad.h"
  28. #define FALSE 0
  29. #define TRUE (!FALSE)
  30. static inline void echo_can_disable_detector_init (echo_can_disable_detector_state_t *det)
  31. {
  32. /* Elliptic notch */
  33. /* This is actually centred at 2095Hz, but gets the balance we want, due
  34. to the asymmetric walls of the notch */
  35. biquad2_init (&det->notch,
  36. (int32_t) (-0.7600000*32768.0),
  37. (int32_t) (-0.1183852*32768.0),
  38. (int32_t) (-0.5104039*32768.0),
  39. (int32_t) ( 0.1567596*32768.0),
  40. (int32_t) ( 1.0000000*32768.0));
  41. det->channel_level = 0;
  42. det->notch_level = 0;
  43. det->tone_present = FALSE;
  44. det->tone_cycle_duration = 0;
  45. det->good_cycles = 0;
  46. det->hit = 0;
  47. }
  48. /*- End of function --------------------------------------------------------*/
  49. static inline int echo_can_disable_detector_update (echo_can_disable_detector_state_t *det,
  50. int16_t amp)
  51. {
  52. int16_t notched;
  53. notched = biquad2 (&det->notch, amp);
  54. /* Estimate the overall energy in the channel, and the energy in
  55. the notch (i.e. overall channel energy - tone energy => noise).
  56. Use abs instead of multiply for speed (is it really faster?).
  57. Damp the overall energy a little more for a stable result.
  58. Damp the notch energy a little less, so we don't damp out the
  59. blip every time the phase reverses */
  60. det->channel_level += ((abs(amp) - det->channel_level) >> 5);
  61. det->notch_level += ((abs(notched) - det->notch_level) >> 4);
  62. if (det->channel_level >= 70) {
  63. /* There is adequate energy in the channel. Is it mostly at 2100Hz? */
  64. if (det->notch_level*6 < det->channel_level) {
  65. det->tone_cycle_duration++;
  66. /* The notch says yes, so we have the tone. */
  67. if (!det->tone_present) {
  68. /* Do we get a kick every 450+-25ms? */
  69. if ((det->tone_cycle_duration >= (425 * 8)) &&
  70. (det->tone_cycle_duration <= (475 * 8))) {
  71. /* It's ANS/PR (CED with polarity reversals), so wait
  72. for at least three cycles before returning a hit. */
  73. det->good_cycles++;
  74. if (det->good_cycles > 2)
  75. det->hit = TRUE;
  76. }
  77. det->tone_cycle_duration = 0;
  78. det->tone_present = TRUE;
  79. } else if (det->tone_cycle_duration >= 600 * 8) {
  80. /* It's ANS (CED without polarity reversals)
  81. so return a hit. */
  82. det->hit = TRUE;
  83. }
  84. } else {
  85. det->tone_present = FALSE;
  86. }
  87. } else {
  88. det->tone_present = FALSE;
  89. det->tone_cycle_duration = 0;
  90. det->good_cycles = 0;
  91. }
  92. return det->hit;
  93. }
  94. /*- End of function --------------------------------------------------------*/
  95. /*- End of file ------------------------------------------------------------*/