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_stereo_df2T_f32.c
10 * Description: Processing function for the floating-point transposed
11 * direct form II Biquad cascade filter. 2 channels
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_stereo_df2T_f32(
156 const arm_biquad_cascade_stereo_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 acc1a, acc1b; /* accumulator */
167 float32_t b0, b1, b2, a1, a2; /* Filter coefficients */
168 float32_t Xn1a, Xn1b; /* temporary input */
169 float32_t d1a, d2a, d1b, d2b; /* state variables */
170 uint32_t sample, stage = S->numStages; /* loop counters */
172 #if defined(ARM_MATH_CM7)
174 float32_t Xn2a, Xn3a, Xn4a, Xn5a, Xn6a, Xn7a, Xn8a; /* Input State variables */
175 float32_t Xn2b, Xn3b, Xn4b, Xn5b, Xn6b, Xn7b, Xn8b; /* Input State variables */
176 float32_t acc2a, acc3a, acc4a, acc5a, acc6a, acc7a, acc8a; /* Simulates the accumulator */
177 float32_t acc2b, acc3b, acc4b, acc5b, acc6b, acc7b, acc8b; /* Simulates the accumulator */
181 /* Reading the coefficients */
186 /* Apply loop unrolling and compute 8 output values simultaneously. */
187 sample = blockSize >> 3u;
190 /*Reading the state values */
198 /* First part of the processing with loop unrolling. Compute 8 outputs at a time.
199 ** a second loop below computes the remaining 1 to 7 samples. */
202 /* y[n] = b0 * x[n] + d1 */
203 /* d1 = b1 * x[n] + a1 * y[n] + d2 */
204 /* d2 = b2 * x[n] + a2 * y[n] */
206 /* Read the first 2 inputs. 2 cycles */
210 /* Sample 1. 5 cycles */
212 acc1a = b0 * Xn1a + d1a;
215 d1a = b1 * Xn1a + d2a;
226 /* Sample 2. 5 cycles */
228 acc1b = b0 * Xn1b + d1b;
231 d1b = b1 * Xn1b + d2b;
242 /* Sample 3. 5 cycles */
244 acc2a = b0 * Xn2a + d1a;
247 d1a = b1 * Xn2a + d2a;
258 /* Sample 4. 5 cycles */
259 acc2b = b0 * Xn2b + d1b;
260 d1b = b1 * Xn2b + d2b;
265 /* Sample 5. 5 cycles */
266 acc3a = b0 * Xn3a + d1a;
267 d1a = b1 * Xn3a + d2a;
272 /* Sample 6. 5 cycles */
273 acc3b = b0 * Xn3b + d1b;
274 d1b = b1 * Xn3b + d2b;
279 /* Sample 7. 5 cycles */
280 acc4a = b0 * Xn4a + d1a;
281 d1a = b1 * Xn4a + d2a;
286 /* Sample 8. 5 cycles */
287 acc4b = b0 * Xn4b + d1b;
288 d1b = b1 * Xn4b + d2b;
293 /* Sample 9. 5 cycles */
294 acc5a = b0 * Xn5a + d1a;
295 d1a = b1 * Xn5a + d2a;
300 /* Sample 10. 5 cycles */
301 acc5b = b0 * Xn5b + d1b;
302 d1b = b1 * Xn5b + d2b;
307 /* Sample 11. 5 cycles */
308 acc6a = b0 * Xn6a + d1a;
309 d1a = b1 * Xn6a + d2a;
314 /* Sample 12. 5 cycles */
315 acc6b = b0 * Xn6b + d1b;
316 d1b = b1 * Xn6b + d2b;
321 /* Sample 13. 5 cycles */
322 acc7a = b0 * Xn7a + d1a;
323 d1a = b1 * Xn7a + d2a;
334 /* Sample 14. 5 cycles */
336 acc7b = b0 * Xn7b + d1b;
339 d1b = b1 * Xn7b + d2b;
350 /* Sample 15. 5 cycles */
352 acc8a = b0 * Xn8a + d1a;
355 d1a = b1 * Xn8a + d2a;
366 /* Sample 16. 5 cycles */
368 acc8b = b0 * Xn8b + d1b;
371 d1b = b1 * Xn8b + d2b;
383 sample = blockSize & 0x7u;
386 Xn1a = *pIn++; //Channel a
387 Xn1b = *pIn++; //Channel b
389 /* y[n] = b0 * x[n] + d1 */
390 acc1a = (b0 * Xn1a) + d1a;
391 acc1b = (b0 * Xn1b) + d1b;
393 /* Store the result in the accumulator in the destination buffer. */
397 /* Every time after the output is computed state should be updated. */
398 /* d1 = b1 * x[n] + a1 * y[n] + d2 */
399 d1a = ((b1 * Xn1a) + (a1 * acc1a)) + d2a;
400 d1b = ((b1 * Xn1b) + (a1 * acc1b)) + d2b;
402 /* d2 = b2 * x[n] + a2 * y[n] */
403 d2a = (b2 * Xn1a) + (a2 * acc1a);
404 d2b = (b2 * Xn1b) + (a2 * acc1b);
409 /* Store the updated state variables back into the state array */
416 /* The current stage input is given as the output to the next stage */
418 /* decrement the loop counter */
422 /*Reset the output working pointer */
427 #elif defined(ARM_MATH_CM0_FAMILY)
429 /* Run the below code for Cortex-M0 */
433 /* Reading the coefficients */
440 /*Reading the state values */
452 Xn1a = *pIn++; //Channel a
453 Xn1b = *pIn++; //Channel b
455 /* y[n] = b0 * x[n] + d1 */
456 acc1a = (b0 * Xn1a) + d1a;
457 acc1b = (b0 * Xn1b) + d1b;
459 /* Store the result in the accumulator in the destination buffer. */
463 /* Every time after the output is computed state should be updated. */
464 /* d1 = b1 * x[n] + a1 * y[n] + d2 */
465 d1a = ((b1 * Xn1a) + (a1 * acc1a)) + d2a;
466 d1b = ((b1 * Xn1b) + (a1 * acc1b)) + d2b;
468 /* d2 = b2 * x[n] + a2 * y[n] */
469 d2a = (b2 * Xn1a) + (a2 * acc1a);
470 d2b = (b2 * Xn1b) + (a2 * acc1b);
472 /* decrement the loop counter */
476 /* Store the updated state variables back into the state array */
482 /* The current stage input is given as the output to the next stage */
485 /*Reset the output working pointer */
488 /* decrement the loop counter */
495 float32_t Xn2a, Xn3a, Xn4a; /* Input State variables */
496 float32_t Xn2b, Xn3b, Xn4b; /* Input State variables */
497 float32_t acc2a, acc3a, acc4a; /* accumulator */
498 float32_t acc2b, acc3b, acc4b; /* accumulator */
499 float32_t p0a, p1a, p2a, p3a, p4a, A1a;
500 float32_t p0b, p1b, p2b, p3b, p4b, A1b;
502 /* Run the below code for Cortex-M4 and Cortex-M3 */
505 /* Reading the coefficients */
512 /*Reading the state values */
518 /* Apply loop unrolling and compute 4 output values simultaneously. */
519 sample = blockSize >> 2u;
521 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
522 ** a second loop below computes the remaining 1 to 3 samples. */
525 /* y[n] = b0 * x[n] + d1 */
526 /* d1 = b1 * x[n] + a1 * y[n] + d2 */
527 /* d2 = b2 * x[n] + a2 * y[n] */
529 /* Read the four inputs */
629 sample = blockSize & 0x3u;
659 /* Store the updated state variables back into the state array */
665 /* The current stage input is given as the output to the next stage */
668 /*Reset the output working pointer */
671 /* decrement the loop counter */
679 LOW_OPTIMIZATION_EXIT
682 * @} end of BiquadCascadeDF2T group