# Microchip® Advanced Software Framework

DSPLIB - Filters

All the filters functions currently supported by the DSP library.

# Coefficient Generation

The following functions are using coefficients to operate. These coefficients define the properties of the filter used and must be generated first.
There are different ways to do it:

• By using a dedicated software, many of them can be easily found on the web.
• By using a digital signal software which provides the capability to do it such as Matlab (http://www.mathworks.com/) or Scilab (http://www.scilab.org/) (Scilab is a free tool similar to Matlab).
• By using a dedicated DSP Library function such as Lowpass FIR design.

The following shows how to generate 4th order low-pass filter coefficients using Scilab.

wfir('lp', 4, [fc/fs, 0], 're', [0 0])

# Finite Impulse Response Filter

This function computes a real FIR filter using the impulse response of the desire filter onto a fixed-length signal. It returns a signal of a length equals to (size - h_size + 1) elements.
Here is the formula of the FIR filter:

Note
The impulse response of the filter has to be scaled to avoid overflowing values.
All the vectors have to be 32-bit aligned.

Relative functions:

# Infinite Impulse Response Filter

This function computes a real IIR filter using the coefficients of the desire filter onto a fixed-length signal. It returns a signal of a length equals to size elements.
Here is the formula of the IIR filter:

Note
The data have to be scaled to avoid overflowing values.
All the vectors have to be 32-bit aligned.

Relative functions:

# Partial Infinite Impulse Response Filter

This function computes a real IIR filter using the coefficients of the desire filter onto a fixed-length signal. It returns a signal of a length equals to (size - num_size + 1) elements.
Here is the formula of the IIR filter:

Note
The data have to be scaled to avoid overflowing values.
All the vectors have to be 32-bit aligned.

Relative functions:

# Least Mean Square Filter

This function computes an adaptive LMS filter. It returns a (size)-length signal.
Here is the formula of the LMS filter:

Note
The data have to be scaled to avoid overflowing values.
All the vectors have to be 32-bit aligned.
You can change the value of the mu coefficient of the LMS filter by defying DSP_LMS_MU, at the compilation, with the desired value. This value defines mu as follow:

Relative functions:

# Normalized Least Mean Square Filter

This function computes an adaptive NLMS filter. It returns a (size)-length signal.
Here is the formula of the NLMS filter:

Note
The data have to be scaled to avoid overflowing values.
All the vectors have to be 32-bit aligned. You can change the value of the mu coefficient of the NLMS filter by defying DSP_NLMS_MU, at the compilation, with the desired value. This value defines mu as follow:

Relative functions:

# Interpolation filter

This function performs an interpolation over the input signal. It returns a (vect2_size * interpolation_ratio)-length signal.
Here is the principle of the interpolation:

Note
The data have to be scaled to avoid overflowing values.
All the vectors have to be 32-bit aligned.

Relative functions:

# Lowpass FIR design

These functions calculate lowpass FIR filter's coefficients.

Note
It does not take care of overflowing values.

Relative functions:

## Functions

static void dsp16_filt_fir (dsp16_t *vect1, dsp16_t *vect2, int size, dsp16_t *h, int h_size)
16 bits fixed point version of the FIR . More...

void dsp16_filt_iir (dsp16_t *y, dsp16_t *x, int size, dsp16_t *num, int num_size, dsp16_t *den, int den_size, int num_prediv, int den_prediv)
16-bit fixed point version of the IIR . More...

void dsp16_filt_iirpart (dsp16_t *vect1, dsp16_t *vect2, int size, dsp16_t *num, int num_size, dsp16_t *den, int den_size, int num_prediv, int den_prediv)
16 bits fixed point version of the IIR . More...

void dsp16_filt_interpolation (dsp16_t *vect1, dsp16_t *vect2, int vect2_size, dsp16_t *h, int h_size, int interpolation_ratio)
This function interpolates a vector. More...

void dsp16_filt_interpolation_coefsort (dsp16_t *fir_coefs, int n_tap, int interpolation_ratio)
This function resort the coefficients of a FIR filter to be used with the function dsp16_filt_interpolation. More...

void dsp16_filt_lms (dsp16_t *x, dsp16_t *w, int size, dsp16_t new_x, dsp16_t d, dsp16_t *y, dsp16_t *e)
16-bit fixed point version of the LMS filter. More...

void dsp16_filt_lpfirdesign (dsp16_t *c, int fc, int fs, int order)
16-bit fixed point version of the low-pass FIR filter design. More...

void dsp16_filt_lpfirdesign_windowed_sinc (dsp16_t *c, int fc_hz, int fs_hz, int order)
16-bit fixed point version of the windowed sinc low-pass FIR filter design. More...

void dsp16_filt_nlms (dsp16_t *x, dsp16_t *w, int size, dsp16_t new_x, dsp16_t d, dsp16_t *y, dsp16_t *e)
16-bit fixed point version of the NLMS filter. More...

static void dsp32_filt_fir (dsp32_t *vect1, dsp32_t *vect2, int size, dsp32_t *h, int h_size)
32 bits fixed point version of the FIR . More...

void dsp32_filt_iir (dsp32_t *y, dsp32_t *x, int size, dsp32_t *num, int num_size, dsp32_t *den, int den_size, int num_prediv, int den_prediv)
32-bit fixed point version of the IIR . More...

void dsp32_filt_iirpart (dsp32_t *vect1, dsp32_t *vect2, int size, dsp32_t *num, int num_size, dsp32_t *den, int den_size, int num_prediv, int den_prediv)
32 bits fixed point version of the IIR . More...

void dsp32_filt_lms (dsp32_t *x, dsp32_t *w, int size, dsp32_t new_x, dsp32_t d, dsp32_t *y, dsp32_t *e)
32-bit fixed point version of the LMS filter. More...

void dsp32_filt_nlms (dsp32_t *x, dsp32_t *w, int size, dsp32_t new_x, dsp32_t d, dsp32_t *y, dsp32_t *e)
32-bit fixed point version of the NLMS filter. More...

 static void dsp16_filt_fir ( dsp16_t * vect1, dsp16_t * vect2, int size, dsp16_t * h, int h_size )
inlinestatic

16 bits fixed point version of the FIR .

Parameters
 vect1 A pointer on a 16-bit fixed-point vector of (size - h_size + 1) elements corresponding to the output buffer. vect2 A pointer on a 16-bit fixed-point vector of size elements corresponding to the input buffer. size The length of the input buffer (must be greater or equals to 4). h A pointer on a 16-bit fixed-point vector of h_size elements corresponding to the buffer containing the impulse response coefficients. h_size The length of the impulse response of the filter (must be greater than 7)
Warning
Due to its implementation, for the avr32-uc3 optimized version of the FIR, the output buffer (vect1) have to have a length of 4*n elements to avoid overflows.
Note
You need the "Partial Convolution" module

References dsp16_vect_convpart().

 void dsp16_filt_iir ( dsp16_t * y, dsp16_t * x, int size, dsp16_t * num, int num_size, dsp16_t * den, int den_size, int num_prediv, int den_prediv )

16-bit fixed point version of the IIR .

Parameters
 y A pointer on a 16-bit fixed-point vector of size elements corresponding to the output buffer. The den_size previous elements correspond to values of old output samples and have to be set to 0 at the initialization (y-1, y-2, ...). x A pointer on a 16-bit fixed-point vector of size elements corresponding to the input buffer. The num_size previous elements correspond to old samples input samples have to be set to 0 at the initialization (x-1, x-2, ...). size The length of the input buffer. num A pointer on a 16-bit fixed-point vector of num_size elements corresponding to the buffer containing the numerator's coefficients of the filter. num_size The length of the numerator's coefficients of the filter (must be a multiple of 2). den A pointer on a 16-bit fixed-point vector of den_size elements corresponding to the buffer containing the denominator's coefficients of the filter. den_size The length of the denominator's coefficients of the filter (must be a multiple of 2). num_prediv The predivisor used to scale down the numerator's coefficients of the filter in order to avoid overflow values. So when you use this feature, you have to prescale manually the numerator's coefficients by 2^num_prediv else leave this field to 0. den_prediv The predivisor used to scale down the denominator's coefficients of the filter in order to avoid overflow values. So when you use this feature, you have to prescale manually the denominator's coefficients by 2^den_prediv else leave this field to 0.
Warning
Due to its implementation, for the avr32-uc3 optimized version of the IIR, the length of the output buffer (vect1) have to be multiple of 6 in order to avoid overflows.

References DSP16_QB, and n.

 void dsp16_filt_iirpart ( dsp16_t * vect1, dsp16_t * vect2, int size, dsp16_t * num, int num_size, dsp16_t * den, int den_size, int num_prediv, int den_prediv )

16 bits fixed point version of the IIR .

Parameters
 vect1 A pointer on a 16-bit fixed-point vector of (size - num_size + 1) elements corresponding to the output buffer. vect2 A pointer on a 16-bit fixed-point vector of size elements corresponding to the input buffer. size The length of the input buffer. num A pointer on a 16-bit fixed-point vector of num_size elements corresponding to the buffer containing the numerator's coefficients of the filter. num_size The length of the numerator's coefficients of the filter. den A pointer on a 16-bit fixed-point vector of den_size elements corresponding to the buffer containing the denominator's coefficients of the filter. den_size The length of the denominator's coefficients of the filter. num_prediv The predivisor used to scale down the numerator's coefficients of the filter in order to avoid overflow values. So when you use this feature, you have to prescale manually the numerator's coefficients by 2^num_prediv else leave this field to 0. den_prediv The predivisor used to scale down the denominator's coefficients of the filter in order to avoid overflow values. So when you use this feature, you have to prescale manually the denominator's coefficients by 2^den_prediv else leave this field to 0.
Warning
Due to its implementation, for the avr32-uc3 optimized version of the IIR, the length of the output buffer (vect1) have to be multiple of 6 in order to avoid overflows.

References DSP16_QB, and n.

 void dsp16_filt_interpolation ( dsp16_t * vect1, dsp16_t * vect2, int vect2_size, dsp16_t * h, int h_size, int interpolation_ratio )

This function interpolates a vector.

Parameters
 vect1 A pointer on a 16-bit fixed-point vector where to store the result. It must be of a size (in sample) equals to the size of the input buffer multiplied by the interpolation factor. vect2 A pointer on a 16-bit fixed-point vector containig the input samples. vect2_size The size of the input buffer. h A pointer on a 16-bit fixed-point vector which contains the coefficients of the filter. These coefficients must be reorder with the function dsp16_filt_interpolation_coefsort before being used. h_size The size of this buffer. interpolation_ratio The interpolation factor desired for this interpolation.
 void dsp16_filt_interpolation_coefsort ( dsp16_t * fir_coefs, int n_tap, int interpolation_ratio )

This function resort the coefficients of a FIR filter to be used with the function dsp16_filt_interpolation.

Parameters
 fir_coefs A pointer on a 16-bit fixed-point vector where the coefficients are stored. n_tap The number of tap of this filter. interpolation_ratio The interpolation factor of the interpolation to perform with this filter.

References get_new_index().

Referenced by dsp16_resampling_setup().

 void dsp16_filt_lms ( dsp16_t * x, dsp16_t * w, int size, dsp16_t new_x, dsp16_t d, dsp16_t * y, dsp16_t * e )

16-bit fixed point version of the LMS filter.

Parameters
 x A pointer on a 16-bit fixed-point vector of (size) elements that acts as a circular buffer, filled with the input samples. Its elements have to be initialized to zero and then you just need to reinject this vector each time you call this functions without filling any of its values. w A pointer on a 16-bit fixed-point vector of size elements corresponding to the coefficients of the filter. Just initialize its elements to zero and after several iterations, this buffer will be filled with the actual coefficients of the filter. size The length of the circular buffer (x) and of the coefficient's buffer (w). It must be a multiple of 4. new_x A 16-bit fixed-point value which contains a new input sample signal. d A 16-bit fixed-point value which contains the current sample of the reference's signal. y A pointer on a 16-bit fixed-point value corresponding to the current sample of the output signal. e A pointer on a 16-bit fixed-point value corresponding to the current sample of the output error signal.

References DSP16_QB, DSP_LMS_MU, i, and temp.

 void dsp16_filt_lpfirdesign ( dsp16_t * c, int fc, int fs, int order )

16-bit fixed point version of the low-pass FIR filter design.

Parameters
 c A pointer on a 16-bit fixed-point vector of "order" size, used to store the coefficients of the filter designed. fc Cutoff frequency of the low-pass filter. fs Sample rate of the signal to filter. order Order of the filter to design.

References CST_PI, DSP16_MUL_TO_DSP32, dsp16_op_mul(), DSP16_Q, DSP16_QB, DSP32_DIV_TO_DSP16, dsp32_op_sin(), DSP_Q, and i.

 void dsp16_filt_lpfirdesign_windowed_sinc ( dsp16_t * c, int fc_hz, int fs_hz, int order )

16-bit fixed point version of the windowed sinc low-pass FIR filter design.

Parameters
 c A pointer on a 16-bit fixed-point vector of "order" size, used to store the coefficients of the filter designed. fc Cutoff frequency of the low-pass filter. fs Sample rate of the signal to filter. order Order of the filter to design.

16-bit fixed point version of the windowed sinc low-pass FIR filter design.

k is chosen so that coef(order/2+1) equals 1.

fc = f_hz / fs_hz; for i=0:(order-1), if (i - order/2) <> 0, cst = sin(2 * pi * fc * (i - order/2)) / (i - order/2); coef(i+1) = k * cst * (0.42 - 0.5 * cos(2 * pi * i / order) + 0.08 * cos(4 * pi * i / order)); end end coef(order/2+1) = k * 2 * pi * fc;

References CST_PI, dsp16_op_cos(), dsp16_op_mul(), dsp16_op_sin(), DSP16_Q, DSP16_QB, DSP_Q, and i.

 void dsp16_filt_nlms ( dsp16_t * x, dsp16_t * w, int size, dsp16_t new_x, dsp16_t d, dsp16_t * y, dsp16_t * e )

16-bit fixed point version of the NLMS filter.

Parameters
 x A pointer on a 16-bit fixed-point vector of (size) elements that acts as a circular buffer, filled with the input samples. Its elements have to be initialized to zero and then you just need to reinject this vector each time you call this functions without filling any of its values. w A pointer on a 16-bit fixed-point vector of size elements corresponding to the coefficients of the filter. Just initialize its elements to zero and after several iterations, this buffer will be filled with the actual coefficients of the filter. size The length of the circular buffer (x) and of the coefficient's buffer (w). It must be a multiple of 4. new_x A 16-bit fixed-point value which contains a new input sample signal. d A 16-bit fixed-point value which contains the current sample of the reference's signal. y A pointer on a 16-bit fixed-point value corresponding to the current sample of the output signal. e A pointer on a 16-bit fixed-point value corresponding to the current sample of the output error signal.

References DSP16_QB, DSP_NLMS_MU, i, and temp.

 static void dsp32_filt_fir ( dsp32_t * vect1, dsp32_t * vect2, int size, dsp32_t * h, int h_size )
inlinestatic

32 bits fixed point version of the FIR .

Parameters
 vect1 A pointer on a 32-bit fixed-point vector of (size - h_size + 1) elements corresponding to the output buffer. vect2 A pointer on a 32-bit fixed-point vector of size elements corresponding to the input buffer. size The length of the input buffer (must be greater or equals to 4). h A pointer on a 32-bit fixed-point vector of h_size elements corresponding to the buffer containing the impulse response coefficients. h_size The length of the impulse response of the filter (must be greater than 7)
Note
You need the "Partial Convolution" module

References dsp32_vect_convpart().

 void dsp32_filt_iir ( dsp32_t * y, dsp32_t * x, int size, dsp32_t * num, int num_size, dsp32_t * den, int den_size, int num_prediv, int den_prediv )

32-bit fixed point version of the IIR .

Parameters
 y A pointer on a 32-bit fixed-point vector of size elements corresponding to the output buffer. The den_size previous elements correspond to values of old output samples and have to be set to 0 at the initialization (y-1, y-2, ...). x A pointer on a 32-bit fixed-point vector of size elements corresponding to the input buffer. The num_size previous elements correspond to old samples input samples have to be set to 0 at the initialization (x-1, x-2, ...). size The length of the input buffer. num A pointer on a 32-bit fixed-point vector of num_size elements corresponding to the buffer containing the numerator's coefficients of the filter. num_size The length of the numerator's coefficients of the filter (must be a multiple of 2). den A pointer on a 32-bit fixed-point vector of den_size elements corresponding to the buffer containing the denominator's coefficients of the filter. den_size The length of the denominator's coefficients of the filter (must be a multiple of 2). num_prediv The predivisor used to scale down the numerator's coefficients of the filter in order to avoid overflow values. So when you use this feature, you have to prescale manually the numerator's coefficients by 2^num_prediv else leave this field to 0. den_prediv The predivisor used to scale down the denominator's coefficients of the filter in order to avoid overflow values. So when you use this feature, you have to prescale manually the denominator's coefficients by 2^den_prediv else leave this field to 0.
Warning
Due to its implementation, for the avr32-uc3 optimized version of the IIR, the length of the output buffer (vect1) have to be multiple of 6 in order to avoid overflows.

References DSP32_QB, and n.

 void dsp32_filt_iirpart ( dsp32_t * vect1, dsp32_t * vect2, int size, dsp32_t * num, int num_size, dsp32_t * den, int den_size, int num_prediv, int den_prediv )

32 bits fixed point version of the IIR .

Parameters
 vect1 A pointer on a 32-bit fixed-point vector of (size - num_size + 1) elements corresponding to the output buffer. vect2 A pointer on a 32-bit fixed-point vector of size elements corresponding to the input buffer. size The length of the input buffer. num A pointer on a 32-bit fixed-point vector of num_size elements corresponding to the buffer containing the numerator's coefficients of the filter. num_size The length of the numerator's coefficients of the filter. den A pointer on a 32-bit fixed-point vector of den_size elements corresponding to the buffer containing the denominator's coefficients of the filter. den_size The length of the denominator's coefficients of the filter. num_prediv The predivisor used to scale down the numerator's coefficients of the filter in order to avoid overflow values. So when you use this feature, you have to prescale manually the numerator's coefficients by 2^num_prediv else leave this field to 0. den_prediv The predivisor used to scale down the denominator's coefficients of the filter in order to avoid overflow values. So when you use this feature, you have to prescale manually the denominator's coefficients by 2^den_prediv else leave this field to 0.

References DSP32_QB, and n.

 void dsp32_filt_lms ( dsp32_t * x, dsp32_t * w, int size, dsp32_t new_x, dsp32_t d, dsp32_t * y, dsp32_t * e )

32-bit fixed point version of the LMS filter.

Parameters
 x A pointer on a 32-bit fixed-point vector of (size) elements that acts as a circular buffer, filled with the input samples. Its elements have to be initialized to zero and then you just need to reinject this vector each time you call this functions without filling any of its values. w A pointer on a 32-bit fixed-point vector of size elements corresponding to the coefficients of the filter. Just initialize its elements to zero and after several iterations, this buffer will be filled with the actual coefficients of the filter. size The length of the circular buffer (x) and of the coefficient's buffer (w). new_x A 32-bit fixed-point value which contains a new input sample signal. d A 32-bit fixed-point value which contains the current sample of the reference's signal. y A pointer on a 32-bit fixed-point value corresponding to the current sample of the output signal. e A pointer on a 32-bit fixed-point value corresponding to the current sample of the output error signal.

References DSP32_QB, DSP_LMS_MU, i, and temp.

 void dsp32_filt_nlms ( dsp32_t * x, dsp32_t * w, int size, dsp32_t new_x, dsp32_t d, dsp32_t * y, dsp32_t * e )

32-bit fixed point version of the NLMS filter.

Parameters
 x A pointer on a 32-bit fixed-point vector of (size) elements that acts as a circular buffer, filled with the input samples. Its elements have to be initialized to zero and then you just need to reinject this vector each time you call this functions without filling any of its values. w A pointer on a 32-bit fixed-point vector of size elements corresponding to the coefficients of the filter. Just initialize its elements to zero and after several iterations, this buffer will be filled with the actual coefficients of the filter. size The length of the circular buffer (x) and of the coefficient's buffer (w). new_x A 32-bit fixed-point value which contains a new input sample signal. d A 32-bit fixed-point value which contains the current sample of the reference's signal. y A pointer on a 32-bit fixed-point value corresponding to the current sample of the output signal. e A pointer on a 32-bit fixed-point value corresponding to the current sample of the output error signal.

References DSP32_QB, DSP_NLMS_MU, i, and temp.