dsp_ecdis.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  27. *
  28. */
  29. #include "dsp_biquad.h"
  30. struct ec_disable_detector_state {
  31. struct biquad2_state notch;
  32. int notch_level;
  33. int channel_level;
  34. int tone_present;
  35. int tone_cycle_duration;
  36. int good_cycles;
  37. int hit;
  38. };
  39. #define FALSE 0
  40. #define TRUE (!FALSE)
  41. static inline void
  42. echo_can_disable_detector_init(struct ec_disable_detector_state *det)
  43. {
  44. /* Elliptic notch */
  45. /* This is actually centred at 2095Hz, but gets the balance we want, due
  46. to the asymmetric walls of the notch */
  47. biquad2_init(&det->notch,
  48. (int32_t) (-0.7600000*32768.0),
  49. (int32_t) (-0.1183852*32768.0),
  50. (int32_t) (-0.5104039*32768.0),
  51. (int32_t) (0.1567596*32768.0),
  52. (int32_t) (1.0000000*32768.0));
  53. det->channel_level = 0;
  54. det->notch_level = 0;
  55. det->tone_present = FALSE;
  56. det->tone_cycle_duration = 0;
  57. det->good_cycles = 0;
  58. det->hit = 0;
  59. }
  60. /*- End of function --------------------------------------------------------*/
  61. static inline int
  62. echo_can_disable_detector_update(struct ec_disable_detector_state *det,
  63. int16_t amp)
  64. {
  65. int16_t notched;
  66. notched = biquad2(&det->notch, amp);
  67. /* Estimate the overall energy in the channel, and the energy in
  68. the notch (i.e. overall channel energy - tone energy => noise).
  69. Use abs instead of multiply for speed (is it really faster?).
  70. Damp the overall energy a little more for a stable result.
  71. Damp the notch energy a little less, so we don't damp out the
  72. blip every time the phase reverses */
  73. det->channel_level += ((abs(amp) - det->channel_level) >> 5);
  74. det->notch_level += ((abs(notched) - det->notch_level) >> 4);
  75. if (det->channel_level > 280) {
  76. /* There is adequate energy in the channel.
  77. Is it mostly at 2100Hz? */
  78. if (det->notch_level*6 < det->channel_level) {
  79. /* The notch says yes, so we have the tone. */
  80. if (!det->tone_present) {
  81. /* Do we get a kick every 450+-25ms? */
  82. if (det->tone_cycle_duration >= 425*8
  83. && det->tone_cycle_duration <= 475*8) {
  84. det->good_cycles++;
  85. if (det->good_cycles > 2)
  86. det->hit = TRUE;
  87. }
  88. det->tone_cycle_duration = 0;
  89. }
  90. det->tone_present = TRUE;
  91. } else
  92. det->tone_present = FALSE;
  93. det->tone_cycle_duration++;
  94. } else {
  95. det->tone_present = FALSE;
  96. det->tone_cycle_duration = 0;
  97. det->good_cycles = 0;
  98. }
  99. return det->hit;
  100. }
  101. /*- End of function --------------------------------------------------------*/
  102. /*- End of file ------------------------------------------------------------*/