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