1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010-2013 ARM Limited. All rights reserved.
4 * $Date: 17. January 2013
7 * Project: CMSIS DSP Library
8 * Title: arm_biquad_cascade_df1_32x64_q31.c
10 * Description: High precision Q31 Biquad cascade filter processing function
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * - Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * - Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in
21 * the documentation and/or other materials provided with the
23 * - Neither the name of ARM LIMITED nor the names of its contributors
24 * may be used to endorse or promote products derived from this
25 * software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 * -------------------------------------------------------------------- */
44 * @ingroup groupFilters
48 * @defgroup BiquadCascadeDF1_32x64 High Precision Q31 Biquad Cascade Filter
50 * This function implements a high precision Biquad cascade filter which operates on
51 * Q31 data values. The filter coefficients are in 1.31 format and the state variables
52 * are in 1.63 format. The double precision state variables reduce quantization noise
53 * in the filter and provide a cleaner output.
54 * These filters are particularly useful when implementing filters in which the
55 * singularities are close to the unit circle. This is common for low pass or high
56 * pass filters with very low cutoff frequencies.
58 * The function operates on blocks of input and output data
59 * and each call to the function processes <code>blockSize</code> samples through
60 * the filter. <code>pSrc</code> and <code>pDst</code> points to input and output arrays
61 * containing <code>blockSize</code> Q31 values.
64 * Each Biquad stage implements a second order filter using the difference equation:
66 * y[n] = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2]
68 * A Direct Form I algorithm is used with 5 coefficients and 4 state variables per stage.
69 * \image html Biquad.gif "Single Biquad filter stage"
70 * Coefficients <code>b0, b1, and b2 </code> multiply the input signal <code>x[n]</code> and are referred to as the feedforward coefficients.
71 * Coefficients <code>a1</code> and <code>a2</code> multiply the output signal <code>y[n]</code> and are referred to as the feedback coefficients.
72 * Pay careful attention to the sign of the feedback coefficients.
73 * Some design tools use the difference equation
75 * y[n] = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] - a1 * y[n-1] - a2 * y[n-2]
77 * In this case the feedback coefficients <code>a1</code> and <code>a2</code> must be negated when used with the CMSIS DSP Library.
80 * Higher order filters are realized as a cascade of second order sections.
81 * <code>numStages</code> refers to the number of second order stages used.
82 * For example, an 8th order filter would be realized with <code>numStages=4</code> second order stages.
83 * \image html BiquadCascade.gif "8th order filter using a cascade of Biquad stages"
84 * 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>).
87 * The <code>pState</code> points to state variables array .
88 * Each Biquad stage has 4 state variables <code>x[n-1], x[n-2], y[n-1],</code> and <code>y[n-2]</code> and each state variable in 1.63 format to improve precision.
89 * The state variables are arranged in the array as:
91 * {x[n-1], x[n-2], y[n-1], y[n-2]}
95 * The 4 state variables for stage 1 are first, then the 4 state variables for stage 2, and so on.
96 * The state array has a total length of <code>4*numStages</code> values of data in 1.63 format.
97 * The state variables are updated after each block of data is processed; the coefficients are untouched.
99 * \par Instance Structure
100 * The coefficients and state variables for a filter are stored together in an instance data structure.
101 * A separate instance structure must be defined for each filter.
102 * Coefficient arrays may be shared among several instances while state variable arrays cannot be shared.
105 * There is also an associated initialization function which performs the following operations:
106 * - Sets the values of the internal structure fields.
107 * - Zeros out the values in the state buffer.
108 * To do this manually without calling the init function, assign the follow subfields of the instance structure:
109 * numStages, pCoeffs, postShift, pState. Also set all of the values in pState to zero.
112 * Use of the initialization function is optional.
113 * However, if the initialization function is used, then the instance structure cannot be placed into a const data section.
114 * To place an instance structure into a const data section, the instance structure must be manually initialized.
115 * Set the values in the state buffer to zeros before static initialization.
116 * For example, to statically initialize the filter instance structure use
118 * arm_biquad_cas_df1_32x64_ins_q31 S1 = {numStages, pState, pCoeffs, postShift};
120 * where <code>numStages</code> is the number of Biquad stages in the filter; <code>pState</code> is the address of the state buffer;
121 * <code>pCoeffs</code> is the address of the coefficient buffer; <code>postShift</code> shift to be applied which is described in detail below.
122 * \par Fixed-Point Behavior
123 * Care must be taken while using Biquad Cascade 32x64 filter function.
124 * Following issues must be considered:
125 * - Scaling of coefficients
127 * - Overflow and saturation
130 * Filter coefficients are represented as fractional values and
131 * restricted to lie in the range <code>[-1 +1)</code>.
132 * The processing function has an additional scaling parameter <code>postShift</code>
133 * which allows the filter coefficients to exceed the range <code>[+1 -1)</code>.
134 * At the output of the filter's accumulator is a shift register which shifts the result by <code>postShift</code> bits.
135 * \image html BiquadPostshift.gif "Fixed-point Biquad with shift by postShift bits after accumulator"
136 * This essentially scales the filter coefficients by <code>2^postShift</code>.
137 * For example, to realize the coefficients
139 * {1.5, -0.8, 1.2, 1.6, -0.9}
141 * set the Coefficient array to:
143 * {0.75, -0.4, 0.6, 0.8, -0.45}
145 * and set <code>postShift=1</code>
148 * The second thing to keep in mind is the gain through the filter.
149 * The frequency response of a Biquad filter is a function of its coefficients.
150 * It is possible for the gain through the filter to exceed 1.0 meaning that the filter increases the amplitude of certain frequencies.
151 * 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.
152 * 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.
155 * The third item to consider is the overflow and saturation behavior of the fixed-point Q31 version.
156 * This is described in the function specific documentation below.
160 * @addtogroup BiquadCascadeDF1_32x64
167 * @param[in] *S points to an instance of the high precision Q31 Biquad cascade filter.
168 * @param[in] *pSrc points to the block of input data.
169 * @param[out] *pDst points to the block of output data.
170 * @param[in] blockSize number of samples to process.
174 * The function is implemented using an internal 64-bit accumulator.
175 * The accumulator has a 2.62 format and maintains full precision of the intermediate multiplication results but provides only a single guard bit.
176 * Thus, if the accumulator result overflows it wraps around rather than clip.
177 * In order to avoid overflows completely the input signal must be scaled down by 2 bits and lie in the range [-0.25 +0.25).
178 * After all 5 multiply-accumulates are performed, the 2.62 accumulator is shifted by <code>postShift</code> bits and the result truncated to
179 * 1.31 format by discarding the low 32 bits.
182 * Two related functions are provided in the CMSIS DSP library.
183 * <code>arm_biquad_cascade_df1_q31()</code> implements a Biquad cascade with 32-bit coefficients and state variables with a Q63 accumulator.
184 * <code>arm_biquad_cascade_df1_fast_q31()</code> implements a Biquad cascade with 32-bit coefficients and state variables with a Q31 accumulator.
187 void arm_biquad_cas_df1_32x64_q31(
188 const arm_biquad_cas_df1_32x64_ins_q31
* S
,
193 q31_t
*pIn
= pSrc
; /* input pointer initialization */
194 q31_t
*pOut
= pDst
; /* output pointer initialization */
195 q63_t
*pState
= S
->pState
; /* state pointer initialization */
196 q31_t
*pCoeffs
= S
->pCoeffs
; /* coeff pointer initialization */
197 q63_t acc
; /* accumulator */
198 q31_t Xn1
, Xn2
; /* Input Filter state variables */
199 q63_t Yn1
, Yn2
; /* Output Filter state variables */
200 q31_t b0
, b1
, b2
, a1
, a2
; /* Filter coefficients */
201 q31_t Xn
; /* temporary input */
202 int32_t shift
= (int32_t) S
->postShift
+ 1; /* Shift to be applied to the output */
203 uint32_t sample
, stage
= S
->numStages
; /* loop counters */
204 q31_t acc_l
, acc_h
; /* temporary output */
205 uint32_t uShift
= ((uint32_t) S
->postShift
+ 1u);
206 uint32_t lShift
= 32u - uShift
; /* Shift to be applied to the output */
209 #ifndef ARM_MATH_CM0_FAMILY
211 /* Run the below code for Cortex-M4 and Cortex-M3 */
215 /* Reading the coefficients */
222 /* Reading the state values */
223 Xn1
= (q31_t
) (pState
[0]);
224 Xn2
= (q31_t
) (pState
[1]);
228 /* Apply loop unrolling and compute 4 output values simultaneously. */
229 /* The variable acc hold output value that is being computed and
230 * stored in the destination buffer
231 * acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2]
234 sample
= blockSize
>> 2u;
236 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
237 ** a second loop below computes the remaining 1 to 3 samples. */
243 /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */
245 /* acc = b0 * x[n] */
246 acc
= (q63_t
) Xn
*b0
;
248 /* acc += b1 * x[n-1] */
249 acc
+= (q63_t
) Xn1
*b1
;
251 /* acc += b[2] * x[n-2] */
252 acc
+= (q63_t
) Xn2
*b2
;
254 /* acc += a1 * y[n-1] */
255 acc
+= mult32x64(Yn1
, a1
);
257 /* acc += a2 * y[n-2] */
258 acc
+= mult32x64(Yn2
, a2
);
260 /* The result is converted to 1.63 , Yn2 variable is reused */
263 /* Calc lower part of acc */
264 acc_l
= acc
& 0xffffffff;
266 /* Calc upper part of acc */
267 acc_h
= (acc
>> 32) & 0xffffffff;
269 /* Apply shift for lower part of acc and upper part of acc */
270 acc_h
= (uint32_t) acc_l
>> lShift
| acc_h
<< uShift
;
272 /* Store the output in the destination buffer in 1.31 format. */
275 /* Read the second input into Xn2, to reuse the value */
278 /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */
280 /* acc += b1 * x[n-1] */
281 acc
= (q63_t
) Xn
*b1
;
283 /* acc = b0 * x[n] */
284 acc
+= (q63_t
) Xn2
*b0
;
286 /* acc += b[2] * x[n-2] */
287 acc
+= (q63_t
) Xn1
*b2
;
289 /* acc += a1 * y[n-1] */
290 acc
+= mult32x64(Yn2
, a1
);
292 /* acc += a2 * y[n-2] */
293 acc
+= mult32x64(Yn1
, a2
);
295 /* The result is converted to 1.63, Yn1 variable is reused */
298 /* Calc lower part of acc */
299 acc_l
= acc
& 0xffffffff;
301 /* Calc upper part of acc */
302 acc_h
= (acc
>> 32) & 0xffffffff;
304 /* Apply shift for lower part of acc and upper part of acc */
305 acc_h
= (uint32_t) acc_l
>> lShift
| acc_h
<< uShift
;
307 /* Read the third input into Xn1, to reuse the value */
310 /* The result is converted to 1.31 */
311 /* Store the output in the destination buffer. */
312 *(pOut
+ 1u) = acc_h
;
314 /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */
316 /* acc = b0 * x[n] */
317 acc
= (q63_t
) Xn1
*b0
;
319 /* acc += b1 * x[n-1] */
320 acc
+= (q63_t
) Xn2
*b1
;
322 /* acc += b[2] * x[n-2] */
323 acc
+= (q63_t
) Xn
*b2
;
325 /* acc += a1 * y[n-1] */
326 acc
+= mult32x64(Yn1
, a1
);
328 /* acc += a2 * y[n-2] */
329 acc
+= mult32x64(Yn2
, a2
);
331 /* The result is converted to 1.63, Yn2 variable is reused */
334 /* Calc lower part of acc */
335 acc_l
= acc
& 0xffffffff;
337 /* Calc upper part of acc */
338 acc_h
= (acc
>> 32) & 0xffffffff;
340 /* Apply shift for lower part of acc and upper part of acc */
341 acc_h
= (uint32_t) acc_l
>> lShift
| acc_h
<< uShift
;
343 /* Store the output in the destination buffer in 1.31 format. */
344 *(pOut
+ 2u) = acc_h
;
346 /* Read the fourth input into Xn, to reuse the value */
349 /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */
350 /* acc = b0 * x[n] */
351 acc
= (q63_t
) Xn
*b0
;
353 /* acc += b1 * x[n-1] */
354 acc
+= (q63_t
) Xn1
*b1
;
356 /* acc += b[2] * x[n-2] */
357 acc
+= (q63_t
) Xn2
*b2
;
359 /* acc += a1 * y[n-1] */
360 acc
+= mult32x64(Yn2
, a1
);
362 /* acc += a2 * y[n-2] */
363 acc
+= mult32x64(Yn1
, a2
);
365 /* The result is converted to 1.63, Yn1 variable is reused */
368 /* Calc lower part of acc */
369 acc_l
= acc
& 0xffffffff;
371 /* Calc upper part of acc */
372 acc_h
= (acc
>> 32) & 0xffffffff;
374 /* Apply shift for lower part of acc and upper part of acc */
375 acc_h
= (uint32_t) acc_l
>> lShift
| acc_h
<< uShift
;
377 /* Store the output in the destination buffer in 1.31 format. */
378 *(pOut
+ 3u) = acc_h
;
380 /* Every time after the output is computed state should be updated. */
381 /* The states should be updated as: */
389 /* update output pointer */
392 /* decrement the loop counter */
396 /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
397 ** No loop unrolling is used. */
398 sample
= (blockSize
& 0x3u
);
405 /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */
407 /* acc = b0 * x[n] */
408 acc
= (q63_t
) Xn
*b0
;
409 /* acc += b1 * x[n-1] */
410 acc
+= (q63_t
) Xn1
*b1
;
411 /* acc += b[2] * x[n-2] */
412 acc
+= (q63_t
) Xn2
*b2
;
413 /* acc += a1 * y[n-1] */
414 acc
+= mult32x64(Yn1
, a1
);
415 /* acc += a2 * y[n-2] */
416 acc
+= mult32x64(Yn2
, a2
);
418 /* Every time after the output is computed state should be updated. */
419 /* The states should be updated as: */
427 /* The result is converted to 1.63, Yn1 variable is reused */
430 /* Calc lower part of acc */
431 acc_l
= acc
& 0xffffffff;
433 /* Calc upper part of acc */
434 acc_h
= (acc
>> 32) & 0xffffffff;
436 /* Apply shift for lower part of acc and upper part of acc */
437 acc_h
= (uint32_t) acc_l
>> lShift
| acc_h
<< uShift
;
439 /* Store the output in the destination buffer in 1.31 format. */
441 //Yn1 = acc << shift;
443 /* Store the output in the destination buffer in 1.31 format. */
444 // *pOut++ = (q31_t) (acc >> (32 - shift));
446 /* decrement the loop counter */
450 /* The first stage output is given as input to the second stage. */
453 /* Reset to destination buffer working pointer */
456 /* Store the updated state variables back into the pState array */
457 /* Store the updated state variables back into the pState array */
458 *pState
++ = (q63_t
) Xn1
;
459 *pState
++ = (q63_t
) Xn2
;
467 /* Run the below code for Cortex-M0 */
471 /* Reading the coefficients */
478 /* Reading the state values */
484 /* The variable acc hold output value that is being computed and
485 * stored in the destination buffer
486 * acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2]
496 /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */
497 /* acc = b0 * x[n] */
498 acc
= (q63_t
) Xn
*b0
;
499 /* acc += b1 * x[n-1] */
500 acc
+= (q63_t
) Xn1
*b1
;
501 /* acc += b[2] * x[n-2] */
502 acc
+= (q63_t
) Xn2
*b2
;
503 /* acc += a1 * y[n-1] */
504 acc
+= mult32x64(Yn1
, a1
);
505 /* acc += a2 * y[n-2] */
506 acc
+= mult32x64(Yn2
, a2
);
508 /* Every time after the output is computed state should be updated. */
509 /* The states should be updated as: */
518 /* The result is converted to 1.63, Yn1 variable is reused */
521 /* Calc lower part of acc */
522 acc_l
= acc
& 0xffffffff;
524 /* Calc upper part of acc */
525 acc_h
= (acc
>> 32) & 0xffffffff;
527 /* Apply shift for lower part of acc and upper part of acc */
528 acc_h
= (uint32_t) acc_l
>> lShift
| acc_h
<< uShift
;
530 /* Store the output in the destination buffer in 1.31 format. */
533 //Yn1 = acc << shift;
535 /* Store the output in the destination buffer in 1.31 format. */
536 //*pOut++ = (q31_t) (acc >> (32 - shift));
538 /* decrement the loop counter */
542 /* The first stage output is given as input to the second stage. */
545 /* Reset to destination buffer working pointer */
548 /* Store the updated state variables back into the pState array */
549 *pState
++ = (q63_t
) Xn1
;
550 *pState
++ = (q63_t
) Xn2
;
556 #endif /* #ifndef ARM_MATH_CM0_FAMILY */
560 * @} end of BiquadCascadeDF1_32x64 group