162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * SpanDSP - a series of DSP components for telephony 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * ec_disable_detector.h - A detector which should eventually meet the 662306a36Sopenharmony_ci * G.164/G.165 requirements for detecting the 762306a36Sopenharmony_ci * 2100Hz echo cancellor disable tone. 862306a36Sopenharmony_ci * 962306a36Sopenharmony_ci * Written by Steve Underwood <steveu@coppice.org> 1062306a36Sopenharmony_ci * 1162306a36Sopenharmony_ci * Copyright (C) 2001 Steve Underwood 1262306a36Sopenharmony_ci * 1362306a36Sopenharmony_ci * All rights reserved. 1462306a36Sopenharmony_ci */ 1562306a36Sopenharmony_ci 1662306a36Sopenharmony_ci#include "dsp_biquad.h" 1762306a36Sopenharmony_ci 1862306a36Sopenharmony_cistruct ec_disable_detector_state { 1962306a36Sopenharmony_ci struct biquad2_state notch; 2062306a36Sopenharmony_ci int notch_level; 2162306a36Sopenharmony_ci int channel_level; 2262306a36Sopenharmony_ci int tone_present; 2362306a36Sopenharmony_ci int tone_cycle_duration; 2462306a36Sopenharmony_ci int good_cycles; 2562306a36Sopenharmony_ci int hit; 2662306a36Sopenharmony_ci}; 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_ci#define FALSE 0 3062306a36Sopenharmony_ci#define TRUE (!FALSE) 3162306a36Sopenharmony_ci 3262306a36Sopenharmony_cistatic inline void 3362306a36Sopenharmony_ciecho_can_disable_detector_init(struct ec_disable_detector_state *det) 3462306a36Sopenharmony_ci{ 3562306a36Sopenharmony_ci /* Elliptic notch */ 3662306a36Sopenharmony_ci /* This is actually centred at 2095Hz, but gets the balance we want, due 3762306a36Sopenharmony_ci to the asymmetric walls of the notch */ 3862306a36Sopenharmony_ci biquad2_init(&det->notch, 3962306a36Sopenharmony_ci (int32_t)(-0.7600000 * 32768.0), 4062306a36Sopenharmony_ci (int32_t)(-0.1183852 * 32768.0), 4162306a36Sopenharmony_ci (int32_t)(-0.5104039 * 32768.0), 4262306a36Sopenharmony_ci (int32_t)(0.1567596 * 32768.0), 4362306a36Sopenharmony_ci (int32_t)(1.0000000 * 32768.0)); 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_ci det->channel_level = 0; 4662306a36Sopenharmony_ci det->notch_level = 0; 4762306a36Sopenharmony_ci det->tone_present = FALSE; 4862306a36Sopenharmony_ci det->tone_cycle_duration = 0; 4962306a36Sopenharmony_ci det->good_cycles = 0; 5062306a36Sopenharmony_ci det->hit = 0; 5162306a36Sopenharmony_ci} 5262306a36Sopenharmony_ci/*- End of function --------------------------------------------------------*/ 5362306a36Sopenharmony_ci 5462306a36Sopenharmony_cistatic inline int 5562306a36Sopenharmony_ciecho_can_disable_detector_update(struct ec_disable_detector_state *det, 5662306a36Sopenharmony_ci int16_t amp) 5762306a36Sopenharmony_ci{ 5862306a36Sopenharmony_ci int16_t notched; 5962306a36Sopenharmony_ci 6062306a36Sopenharmony_ci notched = biquad2(&det->notch, amp); 6162306a36Sopenharmony_ci /* Estimate the overall energy in the channel, and the energy in 6262306a36Sopenharmony_ci the notch (i.e. overall channel energy - tone energy => noise). 6362306a36Sopenharmony_ci Use abs instead of multiply for speed (is it really faster?). 6462306a36Sopenharmony_ci Damp the overall energy a little more for a stable result. 6562306a36Sopenharmony_ci Damp the notch energy a little less, so we don't damp out the 6662306a36Sopenharmony_ci blip every time the phase reverses */ 6762306a36Sopenharmony_ci det->channel_level += ((abs(amp) - det->channel_level) >> 5); 6862306a36Sopenharmony_ci det->notch_level += ((abs(notched) - det->notch_level) >> 4); 6962306a36Sopenharmony_ci if (det->channel_level > 280) { 7062306a36Sopenharmony_ci /* There is adequate energy in the channel. 7162306a36Sopenharmony_ci Is it mostly at 2100Hz? */ 7262306a36Sopenharmony_ci if (det->notch_level * 6 < det->channel_level) { 7362306a36Sopenharmony_ci /* The notch says yes, so we have the tone. */ 7462306a36Sopenharmony_ci if (!det->tone_present) { 7562306a36Sopenharmony_ci /* Do we get a kick every 450+-25ms? */ 7662306a36Sopenharmony_ci if (det->tone_cycle_duration >= 425 * 8 7762306a36Sopenharmony_ci && det->tone_cycle_duration <= 475 * 8) { 7862306a36Sopenharmony_ci det->good_cycles++; 7962306a36Sopenharmony_ci if (det->good_cycles > 2) 8062306a36Sopenharmony_ci det->hit = TRUE; 8162306a36Sopenharmony_ci } 8262306a36Sopenharmony_ci det->tone_cycle_duration = 0; 8362306a36Sopenharmony_ci } 8462306a36Sopenharmony_ci det->tone_present = TRUE; 8562306a36Sopenharmony_ci } else 8662306a36Sopenharmony_ci det->tone_present = FALSE; 8762306a36Sopenharmony_ci det->tone_cycle_duration++; 8862306a36Sopenharmony_ci } else { 8962306a36Sopenharmony_ci det->tone_present = FALSE; 9062306a36Sopenharmony_ci det->tone_cycle_duration = 0; 9162306a36Sopenharmony_ci det->good_cycles = 0; 9262306a36Sopenharmony_ci } 9362306a36Sopenharmony_ci return det->hit; 9462306a36Sopenharmony_ci} 9562306a36Sopenharmony_ci/*- End of function --------------------------------------------------------*/ 9662306a36Sopenharmony_ci/*- End of file ------------------------------------------------------------*/ 97