1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010 ARM Limited. All rights reserved.
7 * Project: CMSIS DSP Library
10 * Description: Floating-point FIR filter processing function.
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
14 * Version 1.0.10 2011/7/15
15 * Big Endian support added and Merged M0 and M3/M4 Source code.
17 * Version 1.0.3 2010/11/29
18 * Re-organized the CMSIS folders and updated documentation.
20 * Version 1.0.2 2010/11/11
21 * Documentation updated.
23 * Version 1.0.1 2010/10/05
24 * Production release and review comments incorporated.
26 * Version 1.0.0 2010/09/20
27 * Production release and review comments incorporated.
29 * Version 0.0.5 2010/04/26
30 * incorporated review comments and updated with latest CMSIS layer
32 * Version 0.0.3 2010/03/10
34 * -------------------------------------------------------------------- */
39 * @ingroup groupFilters
43 * @defgroup FIR Finite Impulse Response (FIR) Filters
45 * This set of functions implements Finite Impulse Response (FIR) filters
46 * for Q7, Q15, Q31, and floating-point data types.
47 * Fast versions of Q15 and Q31 are also provided on Cortex-M4 and Cortex-M3.
48 * The functions operate on blocks of input and output data and each call to the function processes
49 * <code>blockSize</code> samples through the filter. <code>pSrc</code> and
50 * <code>pDst</code> points to input and output arrays containing <code>blockSize</code> values.
53 * The FIR filter algorithm is based upon a sequence of multiply-accumulate (MAC) operations.
54 * Each filter coefficient <code>b[n]</code> is multiplied by a state variable which equals a previous input sample <code>x[n]</code>.
56 * y[n] = b[0] * x[n] + b[1] * x[n-1] + b[2] * x[n-2] + ...+ b[numTaps-1] * x[n-numTaps+1]
59 * \image html FIR.gif "Finite Impulse Response filter"
61 * <code>pCoeffs</code> points to a coefficient array of size <code>numTaps</code>.
62 * Coefficients are stored in time reversed order.
65 * {b[numTaps-1], b[numTaps-2], b[N-2], ..., b[1], b[0]}
68 * <code>pState</code> points to a state array of size <code>numTaps + blockSize - 1</code>.
69 * Samples in the state buffer are stored in the following order.
72 * {x[n-numTaps+1], x[n-numTaps], x[n-numTaps-1], x[n-numTaps-2]....x[0], x[1], ..., x[blockSize-1]}
75 * Note that the length of the state buffer exceeds the length of the coefficient array by <code>blockSize-1</code>.
76 * The increased state buffer length allows circular addressing, which is traditionally used in the FIR filters,
77 * to be avoided and yields a significant speed improvement.
78 * The state variables are updated after each block of data is processed; the coefficients are untouched.
79 * \par Instance Structure
80 * The coefficients and state variables for a filter are stored together in an instance data structure.
81 * A separate instance structure must be defined for each filter.
82 * Coefficient arrays may be shared among several instances while state variable arrays cannot be shared.
83 * There are separate instance structure declarations for each of the 4 supported data types.
85 * \par Initialization Functions
86 * There is also an associated initialization function for each data type.
87 * The initialization function performs the following operations:
88 * - Sets the values of the internal structure fields.
89 * - Zeros out the values in the state buffer.
92 * Use of the initialization function is optional.
93 * However, if the initialization function is used, then the instance structure cannot be placed into a const data section.
94 * To place an instance structure into a const data section, the instance structure must be manually initialized.
95 * Set the values in the state buffer to zeros before static initialization.
96 * The code below statically initializes each of the 4 different data type filter instance structures
98 *arm_fir_instance_f32 S = {numTaps, pState, pCoeffs};
99 *arm_fir_instance_q31 S = {numTaps, pState, pCoeffs};
100 *arm_fir_instance_q15 S = {numTaps, pState, pCoeffs};
101 *arm_fir_instance_q7 S = {numTaps, pState, pCoeffs};
104 * where <code>numTaps</code> is the number of filter coefficients in the filter; <code>pState</code> is the address of the state buffer;
105 * <code>pCoeffs</code> is the address of the coefficient buffer.
107 * \par Fixed-Point Behavior
108 * Care must be taken when using the fixed-point versions of the FIR filter functions.
109 * In particular, the overflow and saturation behavior of the accumulator used in each function must be considered.
110 * Refer to the function specific documentation below for usage guidelines.
120 * @param[in] *S points to an instance of the floating-point FIR filter structure.
121 * @param[in] *pSrc points to the block of input data.
122 * @param[out] *pDst points to the block of output data.
123 * @param[in] blockSize number of samples to process per call.
129 const arm_fir_instance_f32 * S,
135 float32_t *pState = S->pState; /* State pointer */
136 float32_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */
137 float32_t *pStateCurnt; /* Points to the current sample of the state */
138 float32_t *px, *pb; /* Temporary pointers for state and coefficient buffers */
139 uint32_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */
140 uint32_t i, tapCnt, blkCnt; /* Loop counters */
145 /* Run the below code for Cortex-M4 and Cortex-M3 */
147 float32_t acc0, acc1, acc2, acc3; /* Accumulators */
148 float32_t x0, x1, x2, x3, c0; /* Temporary variables to hold state and coefficient values */
151 /* S->pState points to state array which contains previous frame (numTaps - 1) samples */
152 /* pStateCurnt points to the location where the new input data should be written */
153 pStateCurnt = &(S->pState[(numTaps - 1u)]);
155 /* Apply loop unrolling and compute 4 output values simultaneously.
156 * The variables acc0 ... acc3 hold output values that are being computed:
158 * acc0 = b[numTaps-1] * x[n-numTaps-1] + b[numTaps-2] * x[n-numTaps-2] + b[numTaps-3] * x[n-numTaps-3] +...+ b[0] * x[0]
159 * acc1 = b[numTaps-1] * x[n-numTaps] + b[numTaps-2] * x[n-numTaps-1] + b[numTaps-3] * x[n-numTaps-2] +...+ b[0] * x[1]
160 * acc2 = b[numTaps-1] * x[n-numTaps+1] + b[numTaps-2] * x[n-numTaps] + b[numTaps-3] * x[n-numTaps-1] +...+ b[0] * x[2]
161 * acc3 = b[numTaps-1] * x[n-numTaps+2] + b[numTaps-2] * x[n-numTaps+1] + b[numTaps-3] * x[n-numTaps] +...+ b[0] * x[3]
163 blkCnt = blockSize >> 2;
165 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
166 ** a second loop below computes the remaining 1 to 3 samples. */
169 /* Copy four new input samples into the state buffer */
170 *pStateCurnt++ = *pSrc++;
171 *pStateCurnt++ = *pSrc++;
172 *pStateCurnt++ = *pSrc++;
173 *pStateCurnt++ = *pSrc++;
175 /* Set all accumulators to zero */
181 /* Initialize state pointer */
184 /* Initialize coeff pointer */
187 /* Read the first three samples from the state buffer: x[n-numTaps], x[n-numTaps-1], x[n-numTaps-2] */
192 /* Loop unrolling. Process 4 taps at a time. */
193 tapCnt = numTaps >> 2u;
195 /* Loop over the number of taps. Unroll by a factor of 4.
196 ** Repeat until we've computed numTaps-4 coefficients. */
199 /* Read the b[numTaps-1] coefficient */
202 /* Read x[n-numTaps-3] sample */
205 /* acc0 += b[numTaps-1] * x[n-numTaps] */
208 /* acc1 += b[numTaps-1] * x[n-numTaps-1] */
211 /* acc2 += b[numTaps-1] * x[n-numTaps-2] */
214 /* acc3 += b[numTaps-1] * x[n-numTaps-3] */
217 /* Read the b[numTaps-2] coefficient */
220 /* Read x[n-numTaps-4] sample */
223 /* Perform the multiply-accumulate */
229 /* Read the b[numTaps-3] coefficient */
232 /* Read x[n-numTaps-5] sample */
235 /* Perform the multiply-accumulates */
241 /* Read the b[numTaps-4] coefficient */
244 /* Read x[n-numTaps-6] sample */
247 /* Perform the multiply-accumulates */
256 /* If the filter length is not a multiple of 4, compute the remaining filter taps */
257 tapCnt = numTaps % 0x4u;
261 /* Read coefficients */
264 /* Fetch 1 state variable */
267 /* Perform the multiply-accumulates */
273 /* Reuse the present sample states for next sample */
278 /* Decrement the loop counter */
282 /* Advance the state pointer by 4 to process the next group of 4 samples */
285 /* The results in the 4 accumulators, store in the destination buffer. */
294 /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
295 ** No loop unrolling is used. */
296 blkCnt = blockSize % 0x4u;
300 /* Copy one sample at a time into state buffer */
301 *pStateCurnt++ = *pSrc++;
303 /* Set the accumulator to zero */
306 /* Initialize state pointer */
309 /* Initialize Coefficient pointer */
314 /* Perform the multiply-accumulates */
317 acc0 += *px++ * *pb++;
322 /* The result is store in the destination buffer. */
325 /* Advance state pointer by 1 for the next sample */
331 /* Processing is complete.
332 ** Now copy the last numTaps - 1 samples to the satrt of the state buffer.
333 ** This prepares the state buffer for the next function call. */
335 /* Points to the start of the state buffer */
336 pStateCurnt = S->pState;
338 tapCnt = (numTaps - 1u) >> 2u;
343 *pStateCurnt++ = *pState++;
344 *pStateCurnt++ = *pState++;
345 *pStateCurnt++ = *pState++;
346 *pStateCurnt++ = *pState++;
348 /* Decrement the loop counter */
352 /* Calculate remaining number of copies */
353 tapCnt = (numTaps - 1u) % 0x4u;
355 /* Copy the remaining q31_t data */
358 *pStateCurnt++ = *pState++;
360 /* Decrement the loop counter */
366 /* Run the below code for Cortex-M0 */
370 /* S->pState points to state array which contains previous frame (numTaps - 1) samples */
371 /* pStateCurnt points to the location where the new input data should be written */
372 pStateCurnt = &(S->pState[(numTaps - 1u)]);
374 /* Initialize blkCnt with blockSize */
379 /* Copy one sample at a time into state buffer */
380 *pStateCurnt++ = *pSrc++;
382 /* Set the accumulator to zero */
385 /* Initialize state pointer */
388 /* Initialize Coefficient pointer */
393 /* Perform the multiply-accumulates */
396 /* acc = b[numTaps-1] * x[n-numTaps-1] + b[numTaps-2] * x[n-numTaps-2] + b[numTaps-3] * x[n-numTaps-3] +...+ b[0] * x[0] */
397 acc += *px++ * *pb++;
402 /* The result is store in the destination buffer. */
405 /* Advance state pointer by 1 for the next sample */
411 /* Processing is complete.
412 ** Now copy the last numTaps - 1 samples to the starting of the state buffer.
413 ** This prepares the state buffer for the next function call. */
415 /* Points to the start of the state buffer */
416 pStateCurnt = S->pState;
418 /* Copy numTaps number of values */
419 tapCnt = numTaps - 1u;
424 *pStateCurnt++ = *pState++;
426 /* Decrement the loop counter */
430 #endif /* #ifndef ARM_MATH_CM0 */
435 * @} end of FIR group