arduino-audio-tools
Loading...
Searching...
No Matches
Classes | Typedefs | Functions | Variables | Friends
EchoState: Acoustic echo canceller

Classes

struct  EchoState_
 Internal echo canceller state structure. More...
 
struct  fft_state< Allocator >
 FFT state management structure with custom allocator support. More...
 
class  MDFEchoCancellation< Allocator >
 Acoustic echo canceller using MDF algorithm. More...
 

Typedefs

typedef struct EchoState_ EchoState
 
using FloatAllocator = typename std::allocator_traits< Allocator >::template rebind_alloc< float >
 

Functions

constexpr echo_float_t (int16_t mantissa=0, int16_t exponent=0)
 
 fft_state (int size, AudioFFTBase *drv, const Allocator &alloc=Allocator())
 Construct FFT state with specified size and driver.
 
 MDFEchoCancellation (int filterLength, AudioFFTBase &fftDriver, const Allocator &alloc=Allocator())
 
 MDFEchoCancellation (int filterLength, int nbMic, int nbSpeakers, AudioFFTBase &fftDriver, const Allocator &alloc=Allocator())
 
 ~MDFEchoCancellation ()
 
void cancel (const echo_int16_t *rec, const echo_int16_t *play, echo_int16_t *out)
 
void capture (const echo_int16_t *rec, echo_int16_t *out)
 
int control (int request, void *ptr)
 
template<typename Allocator = std::allocator<uint8_t>>
void echo_fft (void *table, echo_word16_t *in, echo_word16_t *out)
 Perform forward FFT.
 
template<typename Allocator = std::allocator<uint8_t>>
void echo_fft_destroy (void *table)
 Destroy FFT state and release resources.
 
template<typename Allocator >
void * echo_fft_init (int size, AudioFFTBase *driver, const Allocator &alloc)
 Initialize FFT state.
 
template<typename Allocator = std::allocator<uint8_t>>
void echo_ifft (void *table, echo_word16_t *in, echo_word16_t *out)
 Perform inverse FFT.
 
template<typename T >
T * echoAlloc (size_t count)
 Allocate memory for array of type T using custom allocator.
 
void echoCancellationImpl (EchoState *st, const echo_int16_t *in, const echo_int16_t *far_end, echo_int16_t *out)
 Core echo cancellation implementation.
 
void echoFatal (const char *str)
 
template<typename T >
void echoFree (T *ptr, size_t count=0)
 Deallocate memory using custom allocator.
 
EchoStateechoStateInitMc (int frame_size, int filter_length, int nb_mic, int nb_speakers)
 Initialize multi-channel echo canceller state.
 
void echoWarning (const char *str)
 
void ensureInitialized ()
 Ensure echo canceller is initialized (lazy initialization)
 
void filterDcNotch16 (const echo_int16_t *in, echo_word16_t radius, echo_word16_t *out, int len, echo_mem_t *mem, int stride)
 Apply DC notch filter to remove DC offset.
 
int getFilterLength ()
 
int getFrameSize ()
 
void getImpulseResponse (echo_int32_t *response)
 
int getImpulseResponseSize ()
 
int getMicChannels ()
 
int getSamplingRate ()
 
int getSpeakerChannels ()
 
EchoStategetState ()
 
void mdfAdjustProp (const echo_word32_t *W, int N, int M, int P, echo_word16_t *prop)
 Adjust proportional adaptation weights.
 
echo_word32_t mdfInnerProd (const echo_word16_t *x, const echo_word16_t *y, int len)
 Compute inner product of two vectors.
 
echo_float_t operator* (const echo_float_t &other) const
 
echo_word32_t operator* (int16_t scalar) const
 
echo_word32_t operator* (int32_t scalar) const
 
void playback (const echo_int16_t *play)
 
void powerSpectrum (const echo_word16_t *X, echo_word32_t *ps, int N)
 Compute power spectrum from FFT output.
 
void powerSpectrumAccum (const echo_word16_t *X, echo_word32_t *ps, int N)
 Accumulate power spectrum from FFT output.
 
void reset ()
 
void setFFTDriver (AudioFFTBase &fftDriver)
 
void setFilterLength (int len)
 
void setMicChannels (int num)
 
void setSamplingRate (int rate)
 
void setSpeakerChannels (int num)
 
void spectralMulAccum (const echo_word16_t *X, const echo_word32_t *Y, echo_word16_t *acc, int N, int M)
 Accumulate spectral multiplication across multiple frames.
 
float spxCos (float x)
 Compute cosine function.
 
float spxExp (float x)
 Compute exponential function.
 
echo_int16_t spxIlog2 (echo_uint32_t x)
 
float spxSqrt (float x)
 
void weightedSpectralMulConj (const echo_float_t *w, const echo_float_t p, const echo_word16_t *X, const echo_word16_t *Y, echo_word32_t *prod, int N)
 Compute weighted spectral multiplication with conjugate.
 

Variables

int adapted
 
Allocator allocator
 
echo_word16_t beta0
 
echo_word16_t beta_max
 
int C
 
int cancel_count
 
echo_word32_t Davg1
 
echo_word32_t Davg2
 
AudioFFTBasedriver
 
echo_float_t Dvar1
 
echo_float_t Dvar2
 
int16_t e
 
echo_word16_t * e
 
echo_word16_t * E
 
echo_word32_t * Eh
 
AudioFFTBasefft_driver
 
void * fft_table
 
int filter_length
 
echo_word16_t * foreground
 
int frame_size
 
bool initialized = false
 
echo_word16_t * input
 
int K
 
echo_word16_t * last_y
 
echo_word16_t leak_estimate
 
int16_t m
 
int M
 
echo_word16_t * memD
 
echo_word16_t * memE
 
echo_word16_t * memX
 
int N
 
int nb_mic
 
int nb_speakers
 
echo_mem_t * notch_mem
 
echo_word16_t notch_radius
 
echo_float_t Pey
 
echo_word32_t * PHI
 
echo_int16_t * play_buf
 
int play_buf_pos
 
int play_buf_started
 
echo_word32_t * power
 
echo_float_tpower_1
 
echo_word16_t preemph
 
echo_word16_t * prop
 
echo_float_t Pyy
 
echo_word32_t * Rf
 
echo_int32_t sampling_rate
 
int saturated
 
int screwed_up
 
echo_word16_t spec_average
 
EchoStatestate = nullptr
 
echo_word32_t sum_adapt
 
std::vector< float, FloatAllocatortemp_img
 
std::vector< float, FloatAllocatortemp_real
 
echo_word32_t * W
 
echo_word16_t * window
 
int window_size
 
echo_word16_t * wtmp
 
echo_word16_t * x
 
echo_word16_t * X
 
echo_word32_t * Xf
 
echo_word16_t * y
 
echo_word16_t * Y
 
echo_word32_t * Yf
 
echo_word32_t * Yh
 

Friends

echo_word32_t operator* (int32_t scalar, const echo_float_t &f)
 

Detailed Description

This is the acoustic echo canceller module.

Typedef Documentation

◆ FloatAllocator

template<typename Allocator = std::allocator<uint8_t>>
using FloatAllocator = typename std::allocator_traits<Allocator>::template rebind_alloc<float>

Rebound allocator for float vectors

Function Documentation

◆ fft_state()

template<typename Allocator = std::allocator<uint8_t>>
fft_state ( int  size,
AudioFFTBase drv,
const Allocator alloc = Allocator() 
)
inline

Construct FFT state with specified size and driver.

Parameters
sizeFFT size (number of points)
drvPointer to FFT driver implementation
allocAllocator instance for memory management

◆ MDFEchoCancellation() [1/2]

template<typename Allocator = std::allocator<uint8_t>>
MDFEchoCancellation ( int  filterLength,
AudioFFTBase fftDriver,
const Allocator alloc = Allocator() 
)
inline

Initialize echo canceller with single channel (mono)

Parameters
filterLengthLength of echo cancellation filter in samples
fftDriverFFT driver instance from AudioFFT
allocAllocator instance (optional)

◆ MDFEchoCancellation() [2/2]

template<typename Allocator = std::allocator<uint8_t>>
MDFEchoCancellation ( int  filterLength,
int  nbMic,
int  nbSpeakers,
AudioFFTBase fftDriver,
const Allocator alloc = Allocator() 
)
inline

Initialize echo canceller with multiple channels

Parameters
filterLengthLength of echo cancellation filter in samples
nbMicNumber of microphone channels
nbSpeakersNumber of speaker channels
fftDriverFFT driver instance from AudioFFT
allocAllocator instance (optional)

◆ ~MDFEchoCancellation()

template<typename Allocator = std::allocator<uint8_t>>
~MDFEchoCancellation ( )
inline

Destructor

◆ cancel()

template<typename Allocator = std::allocator<uint8_t>>
void cancel ( const echo_int16_t *  rec,
const echo_int16_t *  play,
echo_int16_t *  out 
)
inline

Perform echo cancellation

Parameters
recRecorded signal (with echo)
playPlayback signal (reference)
outOutput signal (echo removed)

◆ capture()

template<typename Allocator = std::allocator<uint8_t>>
void capture ( const echo_int16_t *  rec,
echo_int16_t *  out 
)
inline

Process captured audio with buffered playback

Parameters
recRecorded signal
outOutput signal (echo removed)

◆ control()

template<typename Allocator = std::allocator<uint8_t>>
int control ( int  request,
void *  ptr 
)
inline

Control/query echo canceller parameters

Parameters
requestRequest type (ECHO_GET_FRAME_SIZE, etc.)
ptrPointer to parameter value
Returns
0 on success, -1 on error

◆ echo_fft()

template<typename Allocator = std::allocator<uint8_t>>
void echo_fft ( void *  table,
echo_word16_t *  in,
echo_word16_t *  out 
)
inline

Perform forward FFT.

Template Parameters
AllocatorCustom allocator type (must match the one used in echo_fft_init)
Parameters
tableOpaque pointer to FFT state
inInput time-domain signal (size N)
outOutput frequency-domain signal in packed format (size N) Format: [DC, real1, imag1, real2, imag2, ..., Nyquist]

◆ echo_fft_destroy()

template<typename Allocator = std::allocator<uint8_t>>
void echo_fft_destroy ( void *  table)
inline

Destroy FFT state and release resources.

Template Parameters
AllocatorCustom allocator type (must match the one used in echo_fft_init)
Parameters
tableOpaque pointer to FFT state returned by echo_fft_init

◆ echo_fft_init()

template<typename Allocator >
void * echo_fft_init ( int  size,
AudioFFTBase driver,
const Allocator alloc 
)
inline

Initialize FFT state.

Template Parameters
AllocatorCustom allocator type for memory management
Parameters
sizeFFT size (number of points)
driverPointer to FFT driver implementing AudioFFTBase interface
allocAllocator instance for memory management
Returns
Opaque pointer to FFT state, or nullptr on failure

◆ echo_ifft()

template<typename Allocator = std::allocator<uint8_t>>
void echo_ifft ( void *  table,
echo_word16_t *  in,
echo_word16_t *  out 
)
inline

Perform inverse FFT.

Template Parameters
AllocatorCustom allocator type (must match the one used in echo_fft_init)
Parameters
tableOpaque pointer to FFT state
inInput frequency-domain signal in packed format (size N) Format: [DC, real1, imag1, real2, imag2, ..., Nyquist]
outOutput time-domain signal (size N)

◆ echoAlloc()

template<typename Allocator = std::allocator<uint8_t>>
template<typename T >
T * echoAlloc ( size_t  count)
inlineprotected

Allocate memory for array of type T using custom allocator.

Template Parameters
TType of objects to allocate
Parameters
countNumber of objects to allocate
Returns
Pointer to allocated and zero-initialized memory
Note
Memory is zero-initialized to match calloc behavior

◆ echoCancellationImpl()

template<typename Allocator = std::allocator<uint8_t>>
void echoCancellationImpl ( EchoState st,
const echo_int16_t *  in,
const echo_int16_t *  far_end,
echo_int16_t *  out 
)
inlineprotected

Core echo cancellation implementation.

Implements the complete MDF echo cancellation algorithm including:

  • Pre-processing (DC notch filter, pre-emphasis)
  • FFT transformation of input signals
  • Adaptive filter update in frequency domain
  • Echo prediction and subtraction
  • Two-path filter switching for double-talk handling
  • Post-processing (de-emphasis)
Parameters
stEcho state structure containing all internal state
inInput signal from microphones (interleaved if multi-channel)
far_endReference signal from speakers (interleaved if multi-channel)
outOutput signal with echo removed (interleaved if multi-channel)

◆ echoFree()

template<typename Allocator = std::allocator<uint8_t>>
template<typename T >
void echoFree ( T *  ptr,
size_t  count = 0 
)
inlineprotected

Deallocate memory using custom allocator.

Template Parameters
TType of objects to deallocate
Parameters
ptrPointer to memory to deallocate
countNumber of objects (currently unused but kept for API compatibility)
Note
For POD types used in speexdsp, count parameter is not strictly required

◆ echoStateInitMc()

template<typename Allocator = std::allocator<uint8_t>>
EchoState * echoStateInitMc ( int  frame_size,
int  filter_length,
int  nb_mic,
int  nb_speakers 
)
inlineprotected

Initialize multi-channel echo canceller state.

Parameters
frame_sizeNumber of samples per frame
filter_lengthTotal filter length in samples
nb_micNumber of microphone channels
nb_speakersNumber of speaker channels
Returns
Pointer to initialized echo state, or nullptr on failure

◆ filterDcNotch16()

template<typename Allocator = std::allocator<uint8_t>>
void filterDcNotch16 ( const echo_int16_t *  in,
echo_word16_t  radius,
echo_word16_t *  out,
int  len,
echo_mem_t *  mem,
int  stride 
)
inlineprotected

Apply DC notch filter to remove DC offset.

Parameters
inInput signal
radiusFilter radius parameter
outOutput filtered signal
lenLength of signal
memFilter memory state (2 values)
strideStride for accessing input array

◆ getFilterLength()

template<typename Allocator = std::allocator<uint8_t>>
int getFilterLength ( )
inline

Get filter length

◆ getFrameSize()

template<typename Allocator = std::allocator<uint8_t>>
int getFrameSize ( )
inline

Get frame size

◆ getImpulseResponse()

template<typename Allocator = std::allocator<uint8_t>>
void getImpulseResponse ( echo_int32_t *  response)
inline

Get impulse response

Parameters
responseBuffer to store impulse response (must be pre-allocated)

◆ getImpulseResponseSize()

template<typename Allocator = std::allocator<uint8_t>>
int getImpulseResponseSize ( )
inline

Get impulse response size

◆ getMicChannels()

template<typename Allocator = std::allocator<uint8_t>>
int getMicChannels ( )
inline

Get number of microphone channels

◆ getSamplingRate()

template<typename Allocator = std::allocator<uint8_t>>
int getSamplingRate ( )
inline

Get sampling rate

◆ getSpeakerChannels()

template<typename Allocator = std::allocator<uint8_t>>
int getSpeakerChannels ( )
inline

Get number of speaker channels

◆ getState()

template<typename Allocator = std::allocator<uint8_t>>
EchoState * getState ( )
inline

Get underlying state (for advanced usage)

◆ mdfAdjustProp()

template<typename Allocator = std::allocator<uint8_t>>
void mdfAdjustProp ( const echo_word32_t *  W,
int  N,
int  M,
int  P,
echo_word16_t *  prop 
)
inlineprotected

Adjust proportional adaptation weights.

Parameters
WFilter weights in frequency domain
NFFT size
MNumber of filter blocks
PNumber of channels
propOutput proportional weights (normalized)

◆ mdfInnerProd()

template<typename Allocator = std::allocator<uint8_t>>
echo_word32_t mdfInnerProd ( const echo_word16_t *  x,
const echo_word16_t *  y,
int  len 
)
inlineprotected

Compute inner product of two vectors.

Parameters
xFirst input vector
ySecond input vector
lenLength of vectors
Returns
Sum of element-wise products

◆ playback()

template<typename Allocator = std::allocator<uint8_t>>
void playback ( const echo_int16_t *  play)
inline

Buffer playback signal for later processing

Parameters
playPlayback signal to buffer

◆ powerSpectrum()

template<typename Allocator = std::allocator<uint8_t>>
void powerSpectrum ( const echo_word16_t *  X,
echo_word32_t *  ps,
int  N 
)
inlineprotected

Compute power spectrum from FFT output.

Parameters
XFFT output in packed format
psOutput power spectrum
NFFT size

◆ powerSpectrumAccum()

template<typename Allocator = std::allocator<uint8_t>>
void powerSpectrumAccum ( const echo_word16_t *  X,
echo_word32_t *  ps,
int  N 
)
inlineprotected

Accumulate power spectrum from FFT output.

Parameters
XFFT output in packed format
psAccumulated power spectrum (updated in place)
NFFT size

◆ reset()

template<typename Allocator = std::allocator<uint8_t>>
void reset ( )
inline

Reset echo canceller state

◆ setFFTDriver()

template<typename Allocator = std::allocator<uint8_t>>
void setFFTDriver ( AudioFFTBase fftDriver)
inline

Set FFT driver (must be called before first use)

Parameters
fftDriverReference to FFT driver instance

◆ setFilterLength()

template<typename Allocator = std::allocator<uint8_t>>
void setFilterLength ( int  len)
inline

Set filter length (must be called before first use)

Parameters
lenLength of echo cancellation filter in samples

◆ setMicChannels()

template<typename Allocator = std::allocator<uint8_t>>
void setMicChannels ( int  num)
inline

Set number of microphone channels (must be called before first use)

Parameters
numNumber of microphone channels

◆ setSamplingRate()

template<typename Allocator = std::allocator<uint8_t>>
void setSamplingRate ( int  rate)
inline

Set sampling rate

Parameters
rateSampling rate in Hz

◆ setSpeakerChannels()

template<typename Allocator = std::allocator<uint8_t>>
void setSpeakerChannels ( int  num)
inline

Set number of speaker channels (must be called before first use)

Parameters
numNumber of speaker channels

◆ spectralMulAccum()

template<typename Allocator = std::allocator<uint8_t>>
void spectralMulAccum ( const echo_word16_t *  X,
const echo_word32_t *  Y,
echo_word16_t *  acc,
int  N,
int  M 
)
inlineprotected

Accumulate spectral multiplication across multiple frames.

Parameters
XInput spectrum (M frames)
YFilter weights in frequency domain
accOutput accumulated result
NFFT size
MNumber of frames to accumulate

◆ spxCos()

template<typename Allocator = std::allocator<uint8_t>>
float spxCos ( float  x)
inlineprotected

Compute cosine function.

Parameters
xInput angle in radians
Returns
Cosine of x

◆ spxExp()

template<typename Allocator = std::allocator<uint8_t>>
float spxExp ( float  x)
inlineprotected

Compute exponential function.

Parameters
xInput value
Returns
e raised to the power of x

◆ weightedSpectralMulConj()

template<typename Allocator = std::allocator<uint8_t>>
void weightedSpectralMulConj ( const echo_float_t w,
const echo_float_t  p,
const echo_word16_t *  X,
const echo_word16_t *  Y,
echo_word32_t *  prod,
int  N 
)
inlineprotected

Compute weighted spectral multiplication with conjugate.

Parameters
wWeight array for each frequency bin
pGlobal scaling factor
XFirst input spectrum
YSecond input spectrum (conjugated)
prodOutput product
NFFT size

Variable Documentation

◆ allocator

template<typename Allocator = std::allocator<uint8_t>>
Allocator allocator
protected

Allocator instance for memory management

◆ driver

template<typename Allocator = std::allocator<uint8_t>>
AudioFFTBase* driver

Pointer to FFT driver implementation

◆ fft_driver

template<typename Allocator = std::allocator<uint8_t>>
AudioFFTBase* fft_driver
protected

Pointer to FFT driver instance

◆ filter_length

template<typename Allocator = std::allocator<uint8_t>>
int filter_length
protected

Length of echo cancellation filter

◆ frame_size

int frame_size

Number of samples processed each time

◆ initialized

template<typename Allocator = std::allocator<uint8_t>>
bool initialized = false
protected

Lazy initialization flag

◆ K

int K

Number of input channels (microphones)

◆ N

template<typename Allocator = std::allocator<uint8_t>>
int N

FFT size

◆ nb_mic

template<typename Allocator = std::allocator<uint8_t>>
int nb_mic
protected

Number of microphone channels

◆ nb_speakers

template<typename Allocator = std::allocator<uint8_t>>
int nb_speakers
protected

Number of speaker channels

◆ sampling_rate

echo_int32_t sampling_rate

Number of output channels (loudspeakers)

◆ state

template<typename Allocator = std::allocator<uint8_t>>
EchoState* state = nullptr
protected

Pointer to internal echo canceller state

◆ temp_img

template<typename Allocator = std::allocator<uint8_t>>
std::vector<float, FloatAllocator> temp_img

Temporary buffer for imaginary components

◆ temp_real

template<typename Allocator = std::allocator<uint8_t>>
std::vector<float, FloatAllocator> temp_real

Temporary buffer for real components