1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010-2014 ARM Limited. All rights reserved.
4 * $Date: 19. March 2015
7 * Project: CMSIS DSP Library
8 * Title: arm_biquad_cascade_df2T_f32.c
10 * Description: Processing function for the floating-point transposed
11 * direct form II Biquad cascade filter.
13 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * - Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * - Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
24 * - Neither the name of ARM LIMITED nor the names of its contributors
25 * may be used to endorse or promote products derived from this
26 * software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
31 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
32 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
33 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
34 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
36 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
38 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
40 * -------------------------------------------------------------------- */
45 * @ingroup groupFilters
49 * @defgroup BiquadCascadeDF2T Biquad Cascade IIR Filters Using a Direct Form II Transposed Structure
51 * This set of functions implements arbitrary order recursive (IIR) filters using a transposed direct form II structure.
52 * The filters are implemented as a cascade of second order Biquad sections.
53 * These functions provide a slight memory savings as compared to the direct form I Biquad filter functions.
54 * Only floating-point data is supported.
56 * This function operate on blocks of input and output data and each call to the function
57 * processes <code>blockSize</code> samples through the filter.
58 * <code>pSrc</code> points to the array of input data and
59 * <code>pDst</code> points to the array of output data.
60 * Both arrays contain <code>blockSize</code> values.
63 * Each Biquad stage implements a second order filter using the difference equation:
65 * y[n] = b0 * x[n] + d1
66 * d1 = b1 * x[n] + a1 * y[n] + d2
67 * d2 = b2 * x[n] + a2 * y[n]
69 * where d1 and d2 represent the two state values.
72 * A Biquad filter using a transposed Direct Form II structure is shown below.
73 * \image html BiquadDF2Transposed.gif "Single transposed Direct Form II Biquad"
74 * Coefficients <code>b0, b1, and b2 </code> multiply the input signal <code>x[n]</code> and are referred to as the feedforward coefficients.
75 * Coefficients <code>a1</code> and <code>a2</code> multiply the output signal <code>y[n]</code> and are referred to as the feedback coefficients.
76 * Pay careful attention to the sign of the feedback coefficients.
77 * Some design tools flip the sign of the feedback coefficients:
79 * y[n] = b0 * x[n] + d1;
80 * d1 = b1 * x[n] - a1 * y[n] + d2;
81 * d2 = b2 * x[n] - a2 * y[n];
83 * In this case the feedback coefficients <code>a1</code> and <code>a2</code> must be negated when used with the CMSIS DSP Library.
86 * Higher order filters are realized as a cascade of second order sections.
87 * <code>numStages</code> refers to the number of second order stages used.
88 * For example, an 8th order filter would be realized with <code>numStages=4</code> second order stages.
89 * A 9th order filter would be realized with <code>numStages=5</code> second order stages with the
90 * coefficients for one of the stages configured as a first order filter (<code>b2=0</code> and <code>a2=0</code>).
93 * <code>pState</code> points to the state variable array.
94 * Each Biquad stage has 2 state variables <code>d1</code> and <code>d2</code>.
95 * The state variables are arranged in the <code>pState</code> array as:
97 * {d11, d12, d21, d22, ...}
99 * where <code>d1x</code> refers to the state variables for the first Biquad and
100 * <code>d2x</code> refers to the state variables for the second Biquad.
101 * The state array has a total length of <code>2*numStages</code> values.
102 * The state variables are updated after each block of data is processed; the coefficients are untouched.
105 * The CMSIS library contains Biquad filters in both Direct Form I and transposed Direct Form II.
106 * The advantage of the Direct Form I structure is that it is numerically more robust for fixed-point data types.
107 * That is why the Direct Form I structure supports Q15 and Q31 data types.
108 * The transposed Direct Form II structure, on the other hand, requires a wide dynamic range for the state variables <code>d1</code> and <code>d2</code>.
109 * Because of this, the CMSIS library only has a floating-point version of the Direct Form II Biquad.
110 * The advantage of the Direct Form II Biquad is that it requires half the number of state variables, 2 rather than 4, per Biquad stage.
112 * \par Instance Structure
113 * The coefficients and state variables for a filter are stored together in an instance data structure.
114 * A separate instance structure must be defined for each filter.
115 * Coefficient arrays may be shared among several instances while state variable arrays cannot be shared.
117 * \par Init Functions
118 * There is also an associated initialization function.
119 * The initialization function performs following operations:
120 * - Sets the values of the internal structure fields.
121 * - Zeros out the values in the state buffer.
122 * To do this manually without calling the init function, assign the follow subfields of the instance structure:
123 * numStages, pCoeffs, pState. Also set all of the values in pState to zero.
126 * Use of the initialization function is optional.
127 * However, if the initialization function is used, then the instance structure cannot be placed into a const data section.
128 * To place an instance structure into a const data section, the instance structure must be manually initialized.
129 * Set the values in the state buffer to zeros before static initialization.
130 * For example, to statically initialize the instance structure use
132 * arm_biquad_cascade_df2T_instance_f32 S1 = {numStages, pState, pCoeffs};
134 * where <code>numStages</code> is the number of Biquad stages in the filter; <code>pState</code> is the address of the state buffer.
135 * <code>pCoeffs</code> is the address of the coefficient buffer;
140 * @addtogroup BiquadCascadeDF2T
145 * @brief Processing function for the floating-point transposed direct form II Biquad cascade filter.
146 * @param[in] *S points to an instance of the filter data structure.
147 * @param[in] *pSrc points to the block of input data.
148 * @param[out] *pDst points to the block of output data
149 * @param[in] blockSize number of samples to process.
154 LOW_OPTIMIZATION_ENTER
155 void arm_biquad_cascade_df2T_f32(
156 const arm_biquad_cascade_df2T_instance_f32 * S,
162 float32_t *pIn = pSrc; /* source pointer */
163 float32_t *pOut = pDst; /* destination pointer */
164 float32_t *pState = S->pState; /* State pointer */
165 float32_t *pCoeffs = S->pCoeffs; /* coefficient pointer */
166 float32_t acc1; /* accumulator */
167 float32_t b0, b1, b2, a1, a2; /* Filter coefficients */
168 float32_t Xn1; /* temporary input */
169 float32_t d1, d2; /* state variables */
170 uint32_t sample, stage = S->numStages; /* loop counters */
172 #if defined(ARM_MATH_CM7)
174 float32_t Xn2, Xn3, Xn4, Xn5, Xn6, Xn7, Xn8; /* Input State variables */
175 float32_t Xn9, Xn10, Xn11, Xn12, Xn13, Xn14, Xn15, Xn16;
176 float32_t acc2, acc3, acc4, acc5, acc6, acc7; /* Simulates the accumulator */
177 float32_t acc8, acc9, acc10, acc11, acc12, acc13, acc14, acc15, acc16;
181 /* Reading the coefficients */
186 /* Apply loop unrolling and compute 16 output values simultaneously. */
187 sample = blockSize >> 4u;
190 /*Reading the state values */
197 /* First part of the processing with loop unrolling. Compute 16 outputs at a time.
198 ** a second loop below computes the remaining 1 to 15 samples. */
201 /* y[n] = b0 * x[n] + d1 */
202 /* d1 = b1 * x[n] + a1 * y[n] + d2 */
203 /* d2 = b2 * x[n] + a2 * y[n] */
205 /* Read the first 2 inputs. 2 cycles */
209 /* Sample 1. 5 cycles */
211 acc1 = b0 * Xn1 + d1;
225 /* Sample 2. 5 cycles */
227 acc2 = b0 * Xn2 + d1;
241 /* Sample 3. 5 cycles */
243 acc3 = b0 * Xn3 + d1;
257 /* Sample 4. 5 cycles */
258 acc4 = b0 * Xn4 + d1;
264 /* Sample 5. 5 cycles */
265 acc5 = b0 * Xn5 + d1;
271 /* Sample 6. 5 cycles */
272 acc6 = b0 * Xn6 + d1;
278 /* Sample 7. 5 cycles */
279 acc7 = b0 * Xn7 + d1;
285 /* Sample 8. 5 cycles */
286 acc8 = b0 * Xn8 + d1;
292 /* Sample 9. 5 cycles */
293 acc9 = b0 * Xn9 + d1;
299 /* Sample 10. 5 cycles */
300 acc10 = b0 * Xn10 + d1;
306 /* Sample 11. 5 cycles */
307 acc11 = b0 * Xn11 + d1;
313 /* Sample 12. 5 cycles */
314 acc12 = b0 * Xn12 + d1;
320 /* Sample 13. 5 cycles */
321 acc13 = b0 * Xn13 + d1;
331 /* Sample 14. 5 cycles */
333 acc14 = b0 * Xn14 + d1;
347 /* Sample 15. 5 cycles */
350 acc15 = b0 * Xn15 + d1;
364 /* Sample 16. 5 cycles */
366 acc16 = b0 * Xn16 + d1;
381 sample = blockSize & 0xFu;
384 acc1 = b0 * Xn1 + d1;
399 /* Store the updated state variables back into the state array */
401 /* The current stage input is given as the output to the next stage */
405 /* decrement the loop counter */
410 /*Reset the output working pointer */
415 #elif defined(ARM_MATH_CM0_FAMILY)
417 /* Run the below code for Cortex-M0 */
421 /* Reading the coefficients */
428 /*Reading the state values */
440 /* y[n] = b0 * x[n] + d1 */
441 acc1 = (b0 * Xn1) + d1;
443 /* Store the result in the accumulator in the destination buffer. */
446 /* Every time after the output is computed state should be updated. */
447 /* d1 = b1 * x[n] + a1 * y[n] + d2 */
448 d1 = ((b1 * Xn1) + (a1 * acc1)) + d2;
450 /* d2 = b2 * x[n] + a2 * y[n] */
451 d2 = (b2 * Xn1) + (a2 * acc1);
453 /* decrement the loop counter */
457 /* Store the updated state variables back into the state array */
461 /* The current stage input is given as the output to the next stage */
464 /*Reset the output working pointer */
467 /* decrement the loop counter */
474 float32_t Xn2, Xn3, Xn4; /* Input State variables */
475 float32_t acc2, acc3, acc4; /* accumulator */
478 float32_t p0, p1, p2, p3, p4, A1;
480 /* Run the below code for Cortex-M4 and Cortex-M3 */
483 /* Reading the coefficients */
491 /*Reading the state values */
495 /* Apply loop unrolling and compute 4 output values simultaneously. */
496 sample = blockSize >> 2u;
498 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
499 ** a second loop below computes the remaining 1 to 3 samples. */
502 /* y[n] = b0 * x[n] + d1 */
503 /* d1 = b1 * x[n] + a1 * y[n] + d2 */
504 /* d2 = b2 * x[n] + a2 * y[n] */
506 /* Read the four inputs */
562 sample = blockSize & 0x3u;
581 /* Store the updated state variables back into the state array */
585 /* The current stage input is given as the output to the next stage */
588 /*Reset the output working pointer */
591 /* decrement the loop counter */
599 LOW_OPTIMIZATION_EXIT
602 * @} end of BiquadCascadeDF2T group