IIR library
This library implements arbitrary order recursive (IIR) filters. The filters are implemented as a cascade of second order Biquad sections. The functions support fast version of Q15 data type on Cortex-M4.
The functions operate on blocks of input and output data and each call to the function processes blockSize samples through the filter.
pSrc
points to the array of input data and pDst
points to the array of output data. Both arrays contain blockSize
values.
Algorithm
Each Biquad stage implements a second order filter using the difference equation :
y[n] = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2]
A Direct Form I algorithm is used with 5 coefficients and 4 state variables per stage. Coefficients b0
, b1
and b2
multiply the input signal x[n]
and are referred to as the feedforward coefficients.
Coefficients a1
and a2
multiply the output signal y[n]
and are referred to as the feedback coefficients. numStages
is the number of Biquad stages in the filter;
pState
is the address of the state buffer;
pCoeffs
is the address of the coeffi * y[n-2]
In this case the feedback coefficients a1
and a2
must be negated when used with the library.
Higher order filters are realized as a cascade of second order sections. numStages
refers to the number of second order stages used. For example, an 8th order filter would be realized with numStages = 4
second order stages.
A 9th order filter would be realized with numStages=5 second order stages with the coefficients for one of the stages configured as a first order filter (b2 = 0
and a2 = 0
).
The pState
points to state variables array. Each Biquad stage has 4 state variables x[n-1]
, x[n-2]
, y[n-1]
, and y[n-2]
. The state variables are arranged in the pState
array as :
{x[n-1], x[n-2], y[n-1], y[n-2]}
The 4 state variables for stage 1 are first, then the 4 state variables for stage 2, and so on. The state array has a total length of 4*numStages
values.
The state variables are updated after each block of data is processed, however the coefficients remain unchanged.
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.
Coefficient arrays may be shared among several instances while state variable arrays cannot be shared.
Initialization function
The initialization function performs following operations:
- Sets the values of the internal structure fields.
- Zeros out the values in the state buffer.
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 Q15 data type filter instance structure :
TIIR_Instance iirInstance = {numStages, pState, pCoeffs, postShift};
where :
numStages
ie number of Biquad stages in the filter;pState
is the address of the state buffer;pCoeffs
is the address of the coefficient buffer;postShift
shift to be applied.
Fixed-Point Behavior
Care must be taken when using the Q15 Biquad Cascade filter function. Following issues must be considered :
- Scaling of coefficients,
- Filter gain,
- 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 postShift
which allow the filter coefficients to exceed the range [+1 -1).
At the output of the filter's accumulator is a shift register which shifts the result by postShift
bits.
This essentially scales the filter coefficients by 2^postShift
. For example, to realize the coefficients :
{1.5, -0.8, 1.2, 1.6, -0.9}
set the pCoeffs
array to :
{0.75, -0.4, 0.6, 0.8, -0.45}
and set postShift = 1
.
Filter gain
The frequency response of a Biquad filter is a function of its coefficients. It is possible for the gain through the filter to exceed 1.0 meaning that the filter increases the amplitude of certain frequencies.
This means that an input signal with amplitude < 1.0 may result in an output > 1.0 and these are saturated or overflowed based on the implementation of the filter.
To avoid this behavior the filter needs to be scaled down such that its peak gain < 1.0 or the input signal must be scaled down so that the combination of input and filter are never overflowed.
Overflow and saturation
This fast Q15 version uses a 32-bit accumulator with 2.30 format. The accumulator maintains full precision of the intermediate multiplication results but provides only a single guard bit.
Thus, if the accumulator result overflows it wraps around and distorts the result. In order to avoid overflows completely the input signal must be scaled down by two bits and lie in the range [-0.25 +0.25).
The 2.30 accumulator is then shifted by postShift bits and the result truncated to 1.15 format by discarding the low 16 bits.
Library Routines
IIR_Init
Prototype |
procedure IIR_Init(iirInstance : ^TIIR_Instance; numStages : uint8_t; pCoeffs : ^q15_t; pState : ^q15_t; postShift : int8_t); |
---|---|
Description |
This function initializes IIR filter . |
Parameters |
The coefficients are stored in the array {b10, 0, b11, b12, a11, a12, b20, 0, b21, b22, a21, a22, ...} where The state variables are stored in the array {x[n-1], x[n-2], y[n-1], y[n-2]} The 4 state variables for stage 1 are first, then the 4 state variables for stage 2, and so on. The state array has a total length of |
Returns |
Nothing. |
Requires |
Nothing. |
Example |
const NUMSTAGES : byte = 2; const coeffTable : array[6*NUMSTAGES] of integer = (1053, 0, 2107, 1053, 19100, -6930, 1053, 0, 2107, 1053, 19100, -6930); var biquadState1 : array[4*NUMSTAGES] of integer; S1 : TIIR_Instance; // Initialize the state and coefficient buffers for biquad section IIR_Init(@S1, NUMSTAGES, @coeffTable, @biquadState1, 1); |
Notes |
None. |
IIR_Fast
Prototype |
procedure IIR_Fast(const iirInstance : ^TIIR_Instance; pSrc : ^q15_t; pDst : ^q15_t; blockSize : uint32_t); |
---|---|
Description |
This function applies IIR filter. |
Parameters |
|
Returns |
Nothing. |
Requires |
Nothing. |
Example |
const BLOCKSIZE : word = 32; var S1 : TIIR_Instance; inputQ15 : array[BLOCKSIZE] of integer; outputQ15 : array[BLOCKSIZE] of integer; // Call double cascade of 2nd order section // Total filter order = 12 IIR_Fast(@S1, @inputQ15, @outputQ15, BLOCKSIZE); |
Notes |
None. |