1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * SpanDSP - a series of DSP components for telephony
5 * ec_disable_detector.h - A detector which should eventually meet the
6 * G.164/G.165 requirements for detecting the
7 * 2100Hz echo cancellor disable tone.
9 * Written by Steve Underwood <steveu@coppice.org>
11 * Copyright (C) 2001 Steve Underwood
13 * All rights reserved.
16 #include "dsp_biquad.h"
18 struct ec_disable_detector_state
{
19 struct biquad2_state notch
;
23 int tone_cycle_duration
;
33 echo_can_disable_detector_init(struct ec_disable_detector_state
*det
)
36 /* This is actually centred at 2095Hz, but gets the balance we want, due
37 to the asymmetric walls of the notch */
38 biquad2_init(&det
->notch
,
39 (int32_t)(-0.7600000 * 32768.0),
40 (int32_t)(-0.1183852 * 32768.0),
41 (int32_t)(-0.5104039 * 32768.0),
42 (int32_t)(0.1567596 * 32768.0),
43 (int32_t)(1.0000000 * 32768.0));
45 det
->channel_level
= 0;
47 det
->tone_present
= FALSE
;
48 det
->tone_cycle_duration
= 0;
52 /*- End of function --------------------------------------------------------*/
55 echo_can_disable_detector_update(struct ec_disable_detector_state
*det
,
60 notched
= biquad2(&det
->notch
, amp
);
61 /* Estimate the overall energy in the channel, and the energy in
62 the notch (i.e. overall channel energy - tone energy => noise).
63 Use abs instead of multiply for speed (is it really faster?).
64 Damp the overall energy a little more for a stable result.
65 Damp the notch energy a little less, so we don't damp out the
66 blip every time the phase reverses */
67 det
->channel_level
+= ((abs(amp
) - det
->channel_level
) >> 5);
68 det
->notch_level
+= ((abs(notched
) - det
->notch_level
) >> 4);
69 if (det
->channel_level
> 280) {
70 /* There is adequate energy in the channel.
71 Is it mostly at 2100Hz? */
72 if (det
->notch_level
* 6 < det
->channel_level
) {
73 /* The notch says yes, so we have the tone. */
74 if (!det
->tone_present
) {
75 /* Do we get a kick every 450+-25ms? */
76 if (det
->tone_cycle_duration
>= 425 * 8
77 && det
->tone_cycle_duration
<= 475 * 8) {
79 if (det
->good_cycles
> 2)
82 det
->tone_cycle_duration
= 0;
84 det
->tone_present
= TRUE
;
86 det
->tone_present
= FALSE
;
87 det
->tone_cycle_duration
++;
89 det
->tone_present
= FALSE
;
90 det
->tone_cycle_duration
= 0;
95 /*- End of function --------------------------------------------------------*/
96 /*- End of file ------------------------------------------------------------*/