1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010 ARM Limited. All rights reserved.
7 * Project: CMSIS DSP Library
8 * Title: arm_biquad_cascade_df1_f32.c
10 * Description: Processing function for the
11 * floating-point Biquad cascade DirectFormI(DF1) filter.
13 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
15 * Version 1.0.10 2011/7/15
16 * Big Endian support added and Merged M0 and M3/M4 Source code.
18 * Version 1.0.3 2010/11/29
19 * Re-organized the CMSIS folders and updated documentation.
21 * Version 1.0.2 2010/11/11
22 * Documentation updated.
24 * Version 1.0.1 2010/10/05
25 * Production release and review comments incorporated.
27 * Version 1.0.0 2010/09/20
28 * Production release and review comments incorporated.
30 * Version 0.0.5 2010/04/26
31 * incorporated review comments and updated with latest CMSIS layer
33 * Version 0.0.3 2010/03/10
35 * -------------------------------------------------------------------- */
40 * @ingroup groupFilters
44 * @defgroup BiquadCascadeDF1 Biquad Cascade IIR Filters Using Direct Form I Structure
46 * This set of functions implements arbitrary order recursive (IIR) filters.
47 * The filters are implemented as a cascade of second order Biquad sections.
48 * The functions support Q15, Q31 and floating-point data types.
49 * Fast version of Q15 and Q31 also supported on CortexM4 and Cortex-M3.
51 * The functions operate on blocks of input and output data and each call to the function
52 * processes <code>blockSize</code> samples through the filter.
53 * <code>pSrc</code> points to the array of input data and
54 * <code>pDst</code> points to the array of output data.
55 * Both arrays contain <code>blockSize</code> values.
58 * Each Biquad stage implements a second order filter using the difference equation:
60 * y[n] = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2]
62 * A Direct Form I algorithm is used with 5 coefficients and 4 state variables per stage.
63 * \image html Biquad.gif "Single Biquad filter stage"
64 * Coefficients <code>b0, b1 and b2 </code> multiply the input signal <code>x[n]</code> and are referred to as the feedforward coefficients.
65 * Coefficients <code>a1</code> and <code>a2</code> multiply the output signal <code>y[n]</code> and are referred to as the feedback coefficients.
66 * Pay careful attention to the sign of the feedback coefficients.
67 * Some design tools use the difference equation
69 * y[n] = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] - a1 * y[n-1] - a2 * y[n-2]
71 * In this case the feedback coefficients <code>a1</code> and <code>a2</code> must be negated when used with the CMSIS DSP Library.
74 * Higher order filters are realized as a cascade of second order sections.
75 * <code>numStages</code> refers to the number of second order stages used.
76 * For example, an 8th order filter would be realized with <code>numStages=4</code> second order stages.
77 * \image html BiquadCascade.gif "8th order filter using a cascade of Biquad stages"
78 * A 9th order filter would be realized with <code>numStages=5</code> second order stages with the coefficients for one of the stages configured as a first order filter (<code>b2=0</code> and <code>a2=0</code>).
81 * The <code>pState</code> points to state variables array.
82 * Each Biquad stage has 4 state variables <code>x[n-1], x[n-2], y[n-1],</code> and <code>y[n-2]</code>.
83 * The state variables are arranged in the <code>pState</code> array as:
85 * {x[n-1], x[n-2], y[n-1], y[n-2]}
89 * The 4 state variables for stage 1 are first, then the 4 state variables for stage 2, and so on.
90 * The state array has a total length of <code>4*numStages</code> values.
91 * The state variables are updated after each block of data is processed, the coefficients are untouched.
93 * \par Instance Structure
94 * The coefficients and state variables for a filter are stored together in an instance data structure.
95 * A separate instance structure must be defined for each filter.
96 * Coefficient arrays may be shared among several instances while state variable arrays cannot be shared.
97 * There are separate instance structure declarations for each of the 3 supported data types.
100 * There is also an associated initialization function for each data type.
101 * The initialization function performs following operations:
102 * - Sets the values of the internal structure fields.
103 * - Zeros out the values in the state buffer.
106 * Use of the initialization function is optional.
107 * However, if the initialization function is used, then the instance structure cannot be placed into a const data section.
108 * To place an instance structure into a const data section, the instance structure must be manually initialized.
109 * Set the values in the state buffer to zeros before static initialization.
110 * The code below statically initializes each of the 3 different data type filter instance structures
112 * arm_biquad_casd_df1_inst_f32 S1 = {numStages, pState, pCoeffs};
113 * arm_biquad_casd_df1_inst_q15 S2 = {numStages, pState, pCoeffs, postShift};
114 * arm_biquad_casd_df1_inst_q31 S3 = {numStages, pState, pCoeffs, postShift};
116 * where <code>numStages</code> is the number of Biquad stages in the filter; <code>pState</code> is the address of the state buffer;
117 * <code>pCoeffs</code> is the address of the coefficient buffer; <code>postShift</code> shift to be applied.
119 * \par Fixed-Point Behavior
120 * Care must be taken when using the Q15 and Q31 versions of the Biquad Cascade filter functions.
121 * Following issues must be considered:
122 * - Scaling of coefficients
124 * - Overflow and saturation
127 * <b>Scaling of coefficients: </b>
128 * Filter coefficients are represented as fractional values and
129 * coefficients are restricted to lie in the range <code>[-1 +1)</code>.
130 * The fixed-point functions have an additional scaling parameter <code>postShift</code>
131 * which allow the filter coefficients to exceed the range <code>[+1 -1)</code>.
132 * At the output of the filter's accumulator is a shift register which shifts the result by <code>postShift</code> bits.
133 * \image html BiquadPostshift.gif "Fixed-point Biquad with shift by postShift bits after accumulator"
134 * This essentially scales the filter coefficients by <code>2^postShift</code>.
135 * For example, to realize the coefficients
137 * {1.5, -0.8, 1.2, 1.6, -0.9}
139 * set the pCoeffs array to:
141 * {0.75, -0.4, 0.6, 0.8, -0.45}
143 * and set <code>postShift=1</code>
146 * <b>Filter gain: </b>
147 * The frequency response of a Biquad filter is a function of its coefficients.
148 * It is possible for the gain through the filter to exceed 1.0 meaning that the filter increases the amplitude of certain frequencies.
149 * 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.
150 * 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.
153 * <b>Overflow and saturation: </b>
154 * For Q15 and Q31 versions, it is described separately as part of the function specific documentation below.
158 * @addtogroup BiquadCascadeDF1
163 * @param[in] *S points to an instance of the floating-point Biquad cascade structure.
164 * @param[in] *pSrc points to the block of input data.
165 * @param[out] *pDst points to the block of output data.
166 * @param[in] blockSize number of samples to process per call.
171 void arm_biquad_cascade_df1_f32(
172 const arm_biquad_casd_df1_inst_f32 * S,
177 float32_t *pIn = pSrc; /* source pointer */
178 float32_t *pOut = pDst; /* destination pointer */
179 float32_t *pState = S->pState; /* pState pointer */
180 float32_t *pCoeffs = S->pCoeffs; /* coefficient pointer */
181 float32_t acc; /* Simulates the accumulator */
182 float32_t b0, b1, b2, a1, a2; /* Filter coefficients */
183 float32_t Xn1, Xn2, Yn1, Yn2; /* Filter pState variables */
184 float32_t Xn; /* temporary input */
185 uint32_t sample, stage = S->numStages; /* loop counters */
190 /* Run the below code for Cortex-M4 and Cortex-M3 */
194 /* Reading the coefficients */
201 /* Reading the pState values */
207 /* Apply loop unrolling and compute 4 output values simultaneously. */
208 /* The variable acc hold output values that are being computed:
210 * acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2]
211 * acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2]
212 * acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2]
213 * acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2]
216 sample = blockSize >> 2u;
218 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
219 ** a second loop below computes the remaining 1 to 3 samples. */
222 /* Read the first input */
225 /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */
226 Yn2 = (b0 * Xn) + (b1 * Xn1) + (b2 * Xn2) + (a1 * Yn1) + (a2 * Yn2);
228 /* Store the result in the accumulator in the destination buffer. */
231 /* Every time after the output is computed state should be updated. */
232 /* The states should be updated as: */
238 /* Read the second input */
241 /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */
242 Yn1 = (b0 * Xn2) + (b1 * Xn) + (b2 * Xn1) + (a1 * Yn2) + (a2 * Yn1);
244 /* Store the result in the accumulator in the destination buffer. */
247 /* Every time after the output is computed state should be updated. */
248 /* The states should be updated as: */
254 /* Read the third input */
257 /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */
258 Yn2 = (b0 * Xn1) + (b1 * Xn2) + (b2 * Xn) + (a1 * Yn1) + (a2 * Yn2);
260 /* Store the result in the accumulator in the destination buffer. */
263 /* Every time after the output is computed state should be updated. */
264 /* The states should be updated as: */
270 /* Read the forth input */
273 /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */
274 Yn1 = (b0 * Xn) + (b1 * Xn1) + (b2 * Xn2) + (a1 * Yn2) + (a2 * Yn1);
276 /* Store the result in the accumulator in the destination buffer. */
279 /* Every time after the output is computed state should be updated. */
280 /* The states should be updated as: */
288 /* decrement the loop counter */
293 /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
294 ** No loop unrolling is used. */
295 sample = blockSize & 0x3u;
302 /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */
303 acc = (b0 * Xn) + (b1 * Xn1) + (b2 * Xn2) + (a1 * Yn1) + (a2 * Yn2);
305 /* Store the result in the accumulator in the destination buffer. */
308 /* Every time after the output is computed state should be updated. */
309 /* The states should be updated as: */
319 /* decrement the loop counter */
324 /* Store the updated state variables back into the pState array */
330 /* The first stage goes from the input buffer to the output buffer. */
331 /* Subsequent numStages occur in-place in the output buffer */
334 /* Reset the output pointer */
337 /* decrement the loop counter */
344 /* Run the below code for Cortex-M0 */
348 /* Reading the coefficients */
355 /* Reading the pState values */
361 /* The variables acc holds the output value that is computed:
362 * acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2]
372 /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */
373 acc = (b0 * Xn) + (b1 * Xn1) + (b2 * Xn2) + (a1 * Yn1) + (a2 * Yn2);
375 /* Store the result in the accumulator in the destination buffer. */
378 /* Every time after the output is computed state should be updated. */
379 /* The states should be updated as: */
389 /* decrement the loop counter */
393 /* Store the updated state variables back into the pState array */
399 /* The first stage goes from the input buffer to the output buffer. */
400 /* Subsequent numStages occur in-place in the output buffer */
403 /* Reset the output pointer */
406 /* decrement the loop counter */
411 #endif /* #ifndef ARM_MATH_CM0 */
417 * @} end of BiquadCascadeDF1 group