FIR Filter 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 |
char FIR_Init(TFIR_Instance *firInstance, uint16_t numTaps, q15_t *pCoeffs, q15_t *pState, uint32_t blockSize); |
---|---|
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 |
#define BLOCK_SIZE 32 #define NUM_TAPS 30 const q15_t firCoeffsQ15[NUM_TAPS] = { 0xFFC4, 0xFFCC, 0x0000, 0x0079, 0x0109, 0x0118, 0x0000, 0xFDC6, 0xFBA1, 0xFBBB, 0x0000, 0x08A8, 0x137B, 0x1C89, 0x2010, 0x1C89, 0x137B, 0x08A8, 0x0000, 0xFBBB, 0xFBA1, 0xFDC6, 0x0000, 0x0118, 0x0109, 0x0079, 0x0000, 0xFFCC, 0xFFC4, 0x0000 }; q15_t firStateQ15[BLOCK_SIZE + NUM_TAPS - 1]; TFIR_Instance S; // Call FIR init function to initialize the instance structure FIR_Init(&S, numTaps, (q15_t *) firCoeffsQ15, firStateQ15, blockSize); |
Notes |
None. |
FIR_Fast
Prototype |
void FIR_Fast(const TFIR_Instance *firInstance, q15_t *pSrc, q15_t *pDst, uint32_t blockSize); |
---|---|
Description |
This function applies FIR filter. |
Parameters |
|
Returns |
Nothing. |
Requires |
Nothing. |
Example |
#define TEST_LENGTH_SAMPLES 320 #define BLOCK_SIZE 32 #define NUM_TAPS 30 q15_t inputQ15[TEST_LENGTH_SAMPLES]; q15_t outputQ15[TEST_LENGTH_SAMPLES]; TFIR_Instance S; uint32_t i; uint32_t numBlocks = TEST_LENGTH_SAMPLES/BLOCK_SIZE; // Call the FIR process function for every blockSize samples for(i=0; i < numBlocks; i++) { FIR_Fast(&S, inputQ15 + (i * blockSize), outputQ15 + (i * blockSize), blockSize); } |
Notes |
None. |
What do you think about this topic ? Send us feedback!