LIBML  Version 3.2.4
LIBML DSP Software Library
Functions
Least Mean Square (LMS) Filters
Collaboration diagram for Least Mean Square (LMS) Filters:

Functions

void tpt_lms_f32 (f32_t *aOutData, f32_t *aErrData, const tpt_lms_f32_t *aFilter, const f32_t *aInData, const f32_t *aRefData, uint32_t aCount)
 Processing function for floating-point LMS filter. More...
 
void tpt_lms_init_f32 (tpt_lms_f32_t *aFilter, uint16_t aTaps, f32_t *aCoeffs, f32_t *aState, f32_t aMu, uint32_t aCount)
 Initialization function for floating-point LMS filter. More...
 
void tpt_lms_init_q15 (tpt_lms_q15_t *aFilter, uint16_t aTaps, q15_t *aCoeffs, q15_t *aState, q15_t aMu, uint32_t aCount, uint8_t aPostShift)
 Initialization function for the Q15 LMS filter. More...
 
void tpt_lms_init_q31 (tpt_lms_q31_t *aFilter, uint16_t aTaps, q31_t *aCoeffs, q31_t *aState, q31_t aMu, uint32_t aCount, uint8_t aPostShift)
 Initialization function for Q31 LMS filter. More...
 
void tpt_lms_q15 (q15_t *aOutData, q15_t *aErrData, const tpt_lms_q15_t *aFilter, const q15_t *aInData, const q15_t *aRefData, uint32_t aCount)
 Processing function for Q15 LMS filter. More...
 
void tpt_lms_q31 (q31_t *aOutData, q31_t *aErrData, const tpt_lms_q31_t *aFilter, const q31_t *aInData, const q31_t *aRefData, uint32_t aCount)
 Processing function for Q31 LMS filter. More...
 

Detailed Description

LMS filters are a class of adaptive filters that are able to "learn" an unknown transfer functions. LMS filters use a gradient descent method in which the filter coefficients are updated based on the instantaneous error signal. Adaptive filters are often used in communication systems, equalizers, and noise removal. The DSP Library contains LMS filter functions that operate on Q15, Q31, and floating-point data types. The library also contains normalized LMS filters in which the filter coefficient adaptation is indepedent of the level of the input signal.

An LMS filter consists of two components as shown below. The first component is a standard transversal or FIR filter. The second component is a coefficient update mechanism. The LMS filter has two input signals. The "input" feeds the FIR filter while the "reference input" corresponds to the desired output of the FIR filter. That is, the FIR filter coefficients are updated so that the output of the FIR filter matches the reference input. The filter coefficient update mechanism is based on the difference between the FIR filter output and the reference input. This "error signal" tends towards zero as the filter adapts. The LMS processing functions accept the input and reference input signals and generate the filter output and error signal.

Internal structure of the Least Mean Square filter

The functions operate on blocks of data and each call to the function processes aCount samples through the filter. aInData points to input signal, aRefData points to reference signal, aOutData points to output signal and aErrData points to error signal. All arrays contain aCount values.

The functions operate on a block-by-block basis. Internally, the filter coefficients b[n] are updated on a sample-by-sample basis. The convergence of the LMS filter is slower compared to the normalized LMS algorithm.

Algorithm
The output signal y[n] is computed by a standard FIR filter:
    y[n] = b[0] * x[n] + b[1] * x[n - 1] + b[2] * x[n - 2] + ...
         + b[uTaps - 1] * x[n - uTaps + 1]
  
The error signal equals the difference between the reference signal d[n] and the filter output:
    e[n] = d[n] - y[n].
  
After each sample of the error signal is computed, the filter coefficients b[k] are updated on a sample-by-sample basis:
    b[k] = b[k] + e[n] * fMu * x[n - k],  for k=0, 1, ..., uTaps - 1
  
where fMu is the step size and controls the rate of coefficient convergence.
In the APIs, pCoeffs points to a coefficient array of size uTaps. Coefficients are stored in time reversed order.
    { b[uTaps - 1], b[uTaps - 2], b[N - 2], ..., b[1], b[0] }
  
pState points to a state array of size uTaps + aCount - 1. Samples in the state buffer are stored in the order:
    { x[n - uTaps + 1], x[n - uTaps], x[n - uTaps - 1], ..., x[0],
      x[1], x[2], ..., x[aCount - 1] }
  
Note that the length of the state buffer exceeds the length of the coefficient array by aCount-1 samples. The increased state buffer length allows circular addressing, which is traditionally used in FIR filters, to be avoided and yields a significant speed improvement. The state variables are updated after each block of data is processed.
Instance Structure
The coefficients and state variables for a filter are stored together in an instance data structure. A separate instance structure must be defined for each filter and coefficient and state arrays cannot be shared among instances. There are separate instance structure declarations for each of the 3 supported data types.
Initialization Functions
There is also an associated initialization function for each data type. The initialization function performs the following operations:
  • Sets the values of the internal structure fields.
  • Zeros out the values in the state buffer. To do this manually without calling the init function, assign the follow subfields of the instance structure: uTaps, pCoeffs, iMu/fMu, uPostShift (not for f32), pState. Also set all of the values in pState to zero.
Use of the initialization function is optional. However, if the initialization function is used, then the instance structure cannot be placed into a const data section. To place an instance structure into a const data section, the instance structure must be manually initialized. Set the values in the state buffer to zeros before static initialization. The code below statically initializes each of the 3 different data type filter instance structures
    tpt_lms_f32_t aFilter = { aTaps, aState, aCoeffs, aMu };
    tpt_lms_q31_t aFilter = { aTaps, aState, aCoeffs, aMu, aPostShift };
    tpt_lms_q15_t aFilter = { aTaps, aState, aCoeffs, aMu, aPostShift };
  
where aTaps is the number of filter coefficients in the filter; aState is the address of the state buffer; aCoeffs is the address of the coefficient buffer; aMu is the step size parameter; and aPostShift is the shift applied to coefficients.
Fixed-Point Behavior
Care must be taken when using the Q15 and Q31 versions of the LMS filter. The following issues must be considered:
  • Scaling of coefficients
  • Overflow and saturation
Scaling of Coefficients
Filter coefficients are represented as fractional values and coefficients are restricted to lie in the range [-1 +1). The fixed-point functions have an additional scaling parameter uPostShift. At the output of the filter's accumulator is a shift register which shifts the result by uPostShift bits. This essentially scales the filter coefficients by 2 ^ uPostShift and allows the filter coefficients to exceed the range [+1 -1). The value of uPostShift is set by the user based on the expected gain through the system being modeled.
Overflow and Saturation
Overflow and saturation behavior of the fixed-point Q15 and Q31 versions are described separately as part of the function specific documentation below.

Function Documentation

◆ tpt_lms_f32()

void tpt_lms_f32 ( f32_t aOutData,
f32_t aErrData,
const tpt_lms_f32_t aFilter,
const f32_t aInData,
const f32_t aRefData,
uint32_t  aCount 
)

Processing function for floating-point LMS filter.

Parameters
[out]aOutDatapoints to the block of output data.
[out]aErrDatapoints to the block of error data.
[in]aFilterpoints to an instance of the floating-point LMS filter structure.
[in]aInDatapoints to the block of input data.
[in]aRefDatapoints to the block of reference data.
[in]aCountnumber of samples to process
Returns
none

◆ tpt_lms_init_f32()

void tpt_lms_init_f32 ( tpt_lms_f32_t aFilter,
uint16_t  aTaps,
f32_t aCoeffs,
f32_t aState,
f32_t  aMu,
uint32_t  aCount 
)

Initialization function for floating-point LMS filter.

Parameters
[in]aFilterpoints to an instance of the floating-point LMS filter structure.
[in]aTapsnumber of filter coefficients
[in]aCoeffspoints to coefficient buffer.
[in]aStatepoints to state buffer.
[in]aMustep size that controls filter coefficient updates
[in]aCountnumber of samples to process
Returns
none
Details
pCoeffs points to the array of filter coefficients stored in time reversed order:
    { b[aTaps - 1], b[aTaps - 2], ..., b[1], b[0] }
 
The initial filter coefficients serve as a starting point for the adaptive filter. aState points to an array of length aTaps + aCount - 1 samples, where aCount is the number of input samples processed by each call to tpt_lms_f32().

◆ tpt_lms_init_q15()

void tpt_lms_init_q15 ( tpt_lms_q15_t aFilter,
uint16_t  aTaps,
q15_t aCoeffs,
q15_t aState,
q15_t  aMu,
uint32_t  aCount,
uint8_t  aPostShift 
)

Initialization function for the Q15 LMS filter.

Parameters
[in]aFilterpoints to an instance of the Q15 LMS filter structure.
[in]aTapsnumber of filter coefficients.
[in]aCoeffspoints to coefficient buffer.
[in]aStatepoints to state buffer.
[in]aMustep size that controls filter coefficient updates.
[in]aCountnumber of samples to process.
[in]aPostShiftbit shift applied to coefficients.
Returns
none
Details
aCoeffs points to the array of filter coefficients stored in time reversed order:
    { b[aTaps-1], b[aTaps-2], b[N-2], ..., b[1], b[0] }
 
The initial filter coefficients serve as a starting point for the adaptive filter. aState points to the array of state variables and size of array is aTaps + aCount - 1 samples, where aCount is the number of input samples processed by each call to tpt_lms_q15().

◆ tpt_lms_init_q31()

void tpt_lms_init_q31 ( tpt_lms_q31_t aFilter,
uint16_t  aTaps,
q31_t aCoeffs,
q31_t aState,
q31_t  aMu,
uint32_t  aCount,
uint8_t  aPostShift 
)

Initialization function for Q31 LMS filter.

Parameters
[in]aFilterpoints to an instance of the Q31 LMS filter structure.
[in]aTapsnumber of filter coefficients
[in]aCoeffspoints to coefficient buffer
[in]aStatepoints to state buffer
[in]aMustep size that controls filter coefficient updates
[in]aCountnumber of samples to process
[in]aPostShiftbit shift applied to coefficients
Returns
none
Details
aCoeffs points to the array of filter coefficients stored in time reversed order:
   { b[aTaps - 1], b[aTaps - 2], ..., b[1], b[0] }
  
The initial filter coefficients serve as a starting point for the adaptive filter. aState points to an array of length aTaps + aCount - 1 samples, where aCount is the number of input samples processed by each call to tpt_lms_q31().

◆ tpt_lms_q15()

void tpt_lms_q15 ( q15_t aOutData,
q15_t aErrData,
const tpt_lms_q15_t aFilter,
const q15_t aInData,
const q15_t aRefData,
uint32_t  aCount 
)

Processing function for Q15 LMS filter.

Parameters
[out]aOutDatapoints to the block of output data.
[out]aErrDatapoints to the block of error data.
[in]aFilterpoints to an instance of the Q15 LMS filter structure.
[in]aInDatapoints to the block of input data.
[in]aRefDatapoints to the block of reference data.
[in]aCountnumber of samples to process
Returns
none
Scaling and Overflow Behavior
The function is implemented using an internal 64-bit accumulator. Both coefficients and state variables are represented in 1.15 format and multiplications yield a 2.30 result. The 2.30 intermediate results are accumulated in a 64-bit accumulator in 34.30 format. There is no risk of internal overflow with this approach and the full precision of intermediate multiplications is preserved. After all additions have been performed, the accumulator is truncated to 34.15 format by discarding low 15 bits. Lastly, the accumulator is saturated to yield a result in 1.15 format.
In this filter, filter coefficients are updated for each sample and the updation of filter cofficients are saturted.

◆ tpt_lms_q31()

void tpt_lms_q31 ( q31_t aOutData,
q31_t aErrData,
const tpt_lms_q31_t aFilter,
const q31_t aInData,
const q31_t aRefData,
uint32_t  aCount 
)

Processing function for Q31 LMS filter.

Parameters
[out]aOutDatapoints to the block of output data.
[out]aErrDatapoints to the block of error data.
[in]aFilterpoints to an instance of the Q31 LMS filter structure.
[in]aInDatapoints to the block of input data.
[in]aRefDatapoints to the block of reference data.
[in]aCountnumber of samples to process.
Returns
none
Scaling and Overflow Behavior
The function is implemented using an internal 64-bit accumulator. The accumulator has a 2.62 format and maintains full precision of the intermediate multiplication results but provides only a single guard bit. Thus, if the accumulator result overflows it wraps around rather than clips. In order to avoid overflows completely the input signal must be scaled down by log2(uTaps) bits. The reference signal should not be scaled down. After all multiply-accumulates are performed, the 2.62 accumulator is shifted and saturated to 1.31 format to yield the final result. The output signal and error signal are in 1.31 format.
In this filter, filter coefficients are updated for each sample and the updation of filter cofficients are saturted.