Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * SpanDSP - a series of DSP components for telephony
0004  *
0005  * ec_disable_detector.h - A detector which should eventually meet the
0006  *                         G.164/G.165 requirements for detecting the
0007  *                         2100Hz echo cancellor disable tone.
0008  *
0009  * Written by Steve Underwood <steveu@coppice.org>
0010  *
0011  * Copyright (C) 2001 Steve Underwood
0012  *
0013  * All rights reserved.
0014  */
0015 
0016 #include "dsp_biquad.h"
0017 
0018 struct ec_disable_detector_state {
0019     struct biquad2_state notch;
0020     int notch_level;
0021     int channel_level;
0022     int tone_present;
0023     int tone_cycle_duration;
0024     int good_cycles;
0025     int hit;
0026 };
0027 
0028 
0029 #define FALSE 0
0030 #define TRUE (!FALSE)
0031 
0032 static inline void
0033 echo_can_disable_detector_init(struct ec_disable_detector_state *det)
0034 {
0035     /* Elliptic notch */
0036     /* This is actually centred at 2095Hz, but gets the balance we want, due
0037        to the asymmetric walls of the notch */
0038     biquad2_init(&det->notch,
0039              (int32_t)(-0.7600000 * 32768.0),
0040              (int32_t)(-0.1183852 * 32768.0),
0041              (int32_t)(-0.5104039 * 32768.0),
0042              (int32_t)(0.1567596 * 32768.0),
0043              (int32_t)(1.0000000 * 32768.0));
0044 
0045     det->channel_level = 0;
0046     det->notch_level = 0;
0047     det->tone_present = FALSE;
0048     det->tone_cycle_duration = 0;
0049     det->good_cycles = 0;
0050     det->hit = 0;
0051 }
0052 /*- End of function --------------------------------------------------------*/
0053 
0054 static inline int
0055 echo_can_disable_detector_update(struct ec_disable_detector_state *det,
0056                  int16_t amp)
0057 {
0058     int16_t notched;
0059 
0060     notched = biquad2(&det->notch, amp);
0061     /* Estimate the overall energy in the channel, and the energy in
0062        the notch (i.e. overall channel energy - tone energy => noise).
0063        Use abs instead of multiply for speed (is it really faster?).
0064        Damp the overall energy a little more for a stable result.
0065        Damp the notch energy a little less, so we don't damp out the
0066        blip every time the phase reverses */
0067     det->channel_level += ((abs(amp) - det->channel_level) >> 5);
0068     det->notch_level += ((abs(notched) - det->notch_level) >> 4);
0069     if (det->channel_level > 280) {
0070         /* There is adequate energy in the channel.
0071            Is it mostly at 2100Hz? */
0072         if (det->notch_level * 6 < det->channel_level) {
0073             /* The notch says yes, so we have the tone. */
0074             if (!det->tone_present) {
0075                 /* Do we get a kick every 450+-25ms? */
0076                 if (det->tone_cycle_duration >= 425 * 8
0077                     && det->tone_cycle_duration <= 475 * 8) {
0078                     det->good_cycles++;
0079                     if (det->good_cycles > 2)
0080                         det->hit = TRUE;
0081                 }
0082                 det->tone_cycle_duration = 0;
0083             }
0084             det->tone_present = TRUE;
0085         } else
0086             det->tone_present = FALSE;
0087         det->tone_cycle_duration++;
0088     } else {
0089         det->tone_present = FALSE;
0090         det->tone_cycle_duration = 0;
0091         det->good_cycles = 0;
0092     }
0093     return det->hit;
0094 }
0095 /*- End of function --------------------------------------------------------*/
0096 /*- End of file ------------------------------------------------------------*/