linux/drivers/misc/echo/echo.c

// SPDX-License-Identifier: GPL-2.0-only
/*
 * SpanDSP - a series of DSP components for telephony
 *
 * echo.c - A line echo canceller.  This code is being developed
 *          against and partially complies with G168.
 *
 * Written by Steve Underwood <[email protected]>
 *         and David Rowe <david_at_rowetel_dot_com>
 *
 * Copyright (C) 2001, 2003 Steve Underwood, 2007 David Rowe
 *
 * Based on a bit from here, a bit from there, eye of toad, ear of
 * bat, 15 years of failed attempts by David and a few fried brain
 * cells.
 *
 * All rights reserved.
 */

/*! \file */

/* Implementation Notes
   David Rowe
   April 2007

   This code started life as Steve's NLMS algorithm with a tap
   rotation algorithm to handle divergence during double talk.  I
   added a Geigel Double Talk Detector (DTD) [2] and performed some
   G168 tests.  However I had trouble meeting the G168 requirements,
   especially for double talk - there were always cases where my DTD
   failed, for example where near end speech was under the 6dB
   threshold required for declaring double talk.

   So I tried a two path algorithm [1], which has so far given better
   results.  The original tap rotation/Geigel algorithm is available
   in SVN http://svn.rowetel.com/software/oslec/tags/before_16bit.
   It's probably possible to make it work if some one wants to put some
   serious work into it.

   At present no special treatment is provided for tones, which
   generally cause NLMS algorithms to diverge.  Initial runs of a
   subset of the G168 tests for tones (e.g ./echo_test 6) show the
   current algorithm is passing OK, which is kind of surprising.  The
   full set of tests needs to be performed to confirm this result.

   One other interesting change is that I have managed to get the NLMS
   code to work with 16 bit coefficients, rather than the original 32
   bit coefficents.  This reduces the MIPs and storage required.
   I evaulated the 16 bit port using g168_tests.sh and listening tests
   on 4 real-world samples.

   I also attempted the implementation of a block based NLMS update
   [2] but although this passes g168_tests.sh it didn't converge well
   on the real-world samples.  I have no idea why, perhaps a scaling
   problem.  The block based code is also available in SVN
   http://svn.rowetel.com/software/oslec/tags/before_16bit.  If this
   code can be debugged, it will lead to further reduction in MIPS, as
   the block update code maps nicely onto DSP instruction sets (it's a
   dot product) compared to the current sample-by-sample update.

   Steve also has some nice notes on echo cancellers in echo.h

   References:

   [1] Ochiai, Areseki, and Ogihara, "Echo Canceller with Two Echo
       Path Models", IEEE Transactions on communications, COM-25,
       No. 6, June
       1977.
       https://www.rowetel.com/images/echo/dual_path_paper.pdf

   [2] The classic, very useful paper that tells you how to
       actually build a real world echo canceller:
	 Messerschmitt, Hedberg, Cole, Haoui, Winship, "Digital Voice
	 Echo Canceller with a TMS320020,
	 https://www.rowetel.com/images/echo/spra129.pdf

   [3] I have written a series of blog posts on this work, here is
       Part 1: http://www.rowetel.com/blog/?p=18

   [4] The source code http://svn.rowetel.com/software/oslec/

   [5] A nice reference on LMS filters:
	 https://en.wikipedia.org/wiki/Least_mean_squares_filter

   Credits:

   Thanks to Steve Underwood, Jean-Marc Valin, and Ramakrishnan
   Muthukrishnan for their suggestions and email discussions.  Thanks
   also to those people who collected echo samples for me such as
   Mark, Pawel, and Pavel.
*/

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>

#include "echo.h"

#define MIN_TX_POWER_FOR_ADAPTION
#define MIN_RX_POWER_FOR_ADAPTION
#define DTD_HANGOVER
#define DC_LOG2BETA

/* adapting coeffs using the traditional stochastic descent (N)LMS algorithm */

static inline void lms_adapt_bg(struct oslec_state *ec, int clean, int shift)
{}

static inline int top_bit(unsigned int bits)
{}

struct oslec_state *oslec_create(int len, int adaption_mode)
{}
EXPORT_SYMBOL_GPL();

void oslec_free(struct oslec_state *ec)
{}
EXPORT_SYMBOL_GPL();

void oslec_adaption_mode(struct oslec_state *ec, int adaption_mode)
{}
EXPORT_SYMBOL_GPL();

void oslec_flush(struct oslec_state *ec)
{}
EXPORT_SYMBOL_GPL();

void oslec_snapshot(struct oslec_state *ec)
{}
EXPORT_SYMBOL_GPL();

/* Dual Path Echo Canceller */

int16_t oslec_update(struct oslec_state *ec, int16_t tx, int16_t rx)
{}
EXPORT_SYMBOL_GPL();

/* This function is separated from the echo canceller is it is usually called
   as part of the tx process.  See rx HP (DC blocking) filter above, it's
   the same design.

   Some soft phones send speech signals with a lot of low frequency
   energy, e.g. down to 20Hz.  This can make the hybrid non-linear
   which causes the echo canceller to fall over.  This filter can help
   by removing any low frequency before it gets to the tx port of the
   hybrid.

   It can also help by removing and DC in the tx signal.  DC is bad
   for LMS algorithms.

   This is one of the classic DC removal filters, adjusted to provide
   sufficient bass rolloff to meet the above requirement to protect hybrids
   from things that upset them. The difference between successive samples
   produces a lousy HPF, and then a suitably placed pole flattens things out.
   The final result is a nicely rolled off bass end. The filtering is
   implemented with extended fractional precision, which noise shapes things,
   giving very clean DC removal.
*/

int16_t oslec_hpf_tx(struct oslec_state *ec, int16_t tx)
{}
EXPORT_SYMBOL_GPL();

MODULE_LICENSE();
MODULE_AUTHOR();
MODULE_DESCRIPTION();
MODULE_VERSION();