FIR library
This library implements the Finite Impulse Response (FIR) filters for Q15 data type. Fast versions of Q15 is provided 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. Pointers pSrc
and pDst
point to input and output arrays containing blockSize values.
Algorithm
The FIR filter algorithm is based upon a sequence of multiply-accumulate (MAC) operations. Each filter coefficient b[n]
is multiplied by a state variable which equals a previous input sample x[n]
:
y[n] = b[0]*x[n] + b[1]*x[n-1] + b[2]*x[n-2] + ...+ b[numTaps-1]*x[n-numTaps+1]
pCoeffs points to a coefficient array of size numTaps
. Coefficients are stored in time reversed order :
{b[numTaps-1], b[numTaps-2], b[N-2], ..., b[1], b[0]}
pState
points to a state array of size numTaps + blockSize - 1
. Samples in the state buffer are stored in the following order :
x[n-numTaps+1], x[n-numTaps], x[n-numTaps-1], x[n-numTaps-2]....x[0], x[1], ..., x[blockSize-1]
Note that the length of the state buffer exceeds the length of the coefficient array by blockSize-1
. The increased state buffer length allows circular addressing, which is traditionally used in the FIR filters,
to be avoided and yields a significant speed improvement. The state variables are updated after each block of data is processed; the coefficients are untouched.
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
There is also an associated initialization function for Q15 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.
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 :
TFIR_Instance firInstance = {numTaps, pState, pCoeffs};
where :
numTaps
is the number of filter coefficients in the filter;pState
is the address of the state buffer;pCoeffs
is the address of the coefficient buffer.
Fixed-Point Behavior
Care must be taken when using Q15 fixed-point FIR filter functions. In particular, the overflow and saturation behavior of the accumulator must be considered. Refer to the function descriptions below for usage guidelines.
Scaling and Overflow Behavior
This fast Q15 library 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 log2(numTaps)
bits.
The 2.30 accumulator is then truncated to 2.15 format and saturated to yield the 1.15 result.
Library Routines
FIR_Init
Prototype |
function FIR_Init(firInstance : ^TFIR_Instance; numTaps : uint16_t; pCoeffs : ^q15_t; pState : ^q15_t; blockSize : uint32_t) : byte; |
---|---|
Description |
This function initializes FIR filter. |
Parameters |
pCoeffs points to the array of filter coefficients stored in time reversed order : {b[numTaps-1], b[numTaps-2], b[N-2], ..., b[1], b[0]} Note that {0.3, -0.8, 0.3} set {0.3, -0.8, 0.3, 0}. Similarly, to implement a two point filter : {0.3, -0.3} set {0.3, -0.3, 0, 0}.
|
Returns |
|
Requires |
Nothing. |
Example |
const NUM_TAPS as word = 30 const TEST_LENGTH_SAMPLES as word = 320 const BLOCK_SIZE as word = 32 const firCoeffsQ15 as integer[NUM_TAPS] = ( integer(0xFFC4), integer(0xFFCC), 0x0000, 0x0079, 0x0109, 0x0118, 0x0000, integer(0xFDC6), integer(0xFBA1), integer(0xFBBB), 0x0000, 0x08A8, 0x137B, 0x1C89, 0x2010, 0x1C89, 0x137B, 0x08A8, 0x0000, integer(0xFBBB), integer(0xFBA1), integer(0xFDC6), 0x0000, 0x0118, 0x0109, 0x0079, 0x0000, integer(0xFFCC), integer(0xFFC4), 0x0000 ) dim S as TFIR_Instance numTaps as word firStateQ15 as integer[BLOCK_SIZE + NUM_TAPS - 1] blockSize as longword numTaps = NUM_TAPS blockSize = BLOCK_SIZE ' Call FIR init function to initialize the instance structure FIR_Init(@S, numTaps, @firCoeffsQ15, @firStateQ15, blockSize) |
Notes |
None. |
FIR_Fast
Prototype |
procedure FIR_Fast(const firInstance : ^TFIR_Instance; pSrc : ^q15_t; pDst : ^q15_t; blockSize : uint32_t); |
---|---|
Description |
This function applies FIR filter. |
Parameters |
|
Returns |
Nothing. |
Requires |
Nothing. |
Example |
const NUM_TAPS as word = 30 const TEST_LENGTH_SAMPLES as word = 320 const BLOCK_SIZE as word = 32 const firCoeffsQ15 as integer[NUM_TAPS] = ( integer(0xFFC4), integer(0xFFCC), 0x0000, 0x0079, 0x0109, 0x0118, 0x0000, integer(0xFDC6), integer(0xFBA1), integer(0xFBBB), 0x0000, 0x08A8, 0x137B, 0x1C89, 0x2010, 0x1C89, 0x137B, 0x08A8, 0x0000, integer(0xFBBB), integer(0xFBA1), integer(0xFDC6), 0x0000, 0x0118, 0x0109, 0x0079, 0x0000, integer(0xFFCC), integer(0xFFC4), 0x0000 ) dim cnt as word numBlocks as longword S as TFIR_Instance inputQ15 as integer[TEST_LENGTH_SAMPLES] outputQ15 as integer[TEST_LENGTH_SAMPLES] blockSize as longword numTaps as word firStateQ15 as integer[BLOCK_SIZE + NUM_TAPS - 1] numBlocks = TEST_LENGTH_SAMPLES/BLOCK_SIZE numTaps = NUM_TAPS blockSize = BLOCK_SIZE ' Call the FIR process function for every blockSize samples for cnt = 0 to (numBlocks - 1) FIR_Fast(@S, @inputQ15 + (cnt * blockSize), @outputQ15 + (cnt * blockSize), blockSize) next cnt |
Notes |
None. |