]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/dsp/cmsis_dsp/FilteringFunctions/arm_biquad_cascade_df1_32x64_q31.c
Merge commit '1fe4406f374291ab2e86e95a97341fd9c475fcb8'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / dsp / cmsis_dsp / FilteringFunctions / arm_biquad_cascade_df1_32x64_q31.c
1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010-2013 ARM Limited. All rights reserved.
3 *
4 * $Date: 17. January 2013
5 * $Revision: V1.4.1
6 *
7 * Project: CMSIS DSP Library
8 * Title: arm_biquad_cascade_df1_32x64_q31.c
9 *
10 * Description: High precision Q31 Biquad cascade filter processing function
11 *
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
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
22 * distribution.
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.
26 *
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 * -------------------------------------------------------------------- */
40
41 #include "arm_math.h"
42
43 /**
44 * @ingroup groupFilters
45 */
46
47 /**
48 * @defgroup BiquadCascadeDF1_32x64 High Precision Q31 Biquad Cascade Filter
49 *
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.
57 *
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.
62 *
63 * \par Algorithm
64 * Each Biquad stage implements a second order filter using the difference equation:
65 * <pre>
66 * y[n] = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2]
67 * </pre>
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
74 * <pre>
75 * y[n] = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] - a1 * y[n-1] - a2 * y[n-2]
76 * </pre>
77 * In this case the feedback coefficients <code>a1</code> and <code>a2</code> must be negated when used with the CMSIS DSP Library.
78 *
79 * \par
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>).
85 *
86 * \par
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:
90 * <pre>
91 * {x[n-1], x[n-2], y[n-1], y[n-2]}
92 * </pre>
93 *
94 * \par
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.
98 *
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.
103 *
104 * \par Init Function
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.
110 *
111 * \par
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
117 * <pre>
118 * arm_biquad_cas_df1_32x64_ins_q31 S1 = {numStages, pState, pCoeffs, postShift};
119 * </pre>
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
126 * - Filter gain
127 * - Overflow and saturation
128 *
129 * \par
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
138 * <pre>
139 * {1.5, -0.8, 1.2, 1.6, -0.9}
140 * </pre>
141 * set the Coefficient array to:
142 * <pre>
143 * {0.75, -0.4, 0.6, 0.8, -0.45}
144 * </pre>
145 * and set <code>postShift=1</code>
146 *
147 * \par
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.
153 *
154 * \par
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.
157 */
158
159 /**
160 * @addtogroup BiquadCascadeDF1_32x64
161 * @{
162 */
163
164 /**
165 * @details
166
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.
171 * @return none.
172 *
173 * \par
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.
180 *
181 * \par
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.
185 */
186
187 void arm_biquad_cas_df1_32x64_q31(
188 const arm_biquad_cas_df1_32x64_ins_q31 * S,
189 q31_t * pSrc,
190 q31_t * pDst,
191 uint32_t blockSize)
192 {
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 */
207
208
209 #ifndef ARM_MATH_CM0_FAMILY
210
211 /* Run the below code for Cortex-M4 and Cortex-M3 */
212
213 do
214 {
215 /* Reading the coefficients */
216 b0 = *pCoeffs++;
217 b1 = *pCoeffs++;
218 b2 = *pCoeffs++;
219 a1 = *pCoeffs++;
220 a2 = *pCoeffs++;
221
222 /* Reading the state values */
223 Xn1 = (q31_t) (pState[0]);
224 Xn2 = (q31_t) (pState[1]);
225 Yn1 = pState[2];
226 Yn2 = pState[3];
227
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]
232 */
233
234 sample = blockSize >> 2u;
235
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. */
238 while(sample > 0u)
239 {
240 /* Read the input */
241 Xn = *pIn++;
242
243 /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */
244
245 /* acc = b0 * x[n] */
246 acc = (q63_t) Xn *b0;
247
248 /* acc += b1 * x[n-1] */
249 acc += (q63_t) Xn1 *b1;
250
251 /* acc += b[2] * x[n-2] */
252 acc += (q63_t) Xn2 *b2;
253
254 /* acc += a1 * y[n-1] */
255 acc += mult32x64(Yn1, a1);
256
257 /* acc += a2 * y[n-2] */
258 acc += mult32x64(Yn2, a2);
259
260 /* The result is converted to 1.63 , Yn2 variable is reused */
261 Yn2 = acc << shift;
262
263 /* Calc lower part of acc */
264 acc_l = acc & 0xffffffff;
265
266 /* Calc upper part of acc */
267 acc_h = (acc >> 32) & 0xffffffff;
268
269 /* Apply shift for lower part of acc and upper part of acc */
270 acc_h = (uint32_t) acc_l >> lShift | acc_h << uShift;
271
272 /* Store the output in the destination buffer in 1.31 format. */
273 *pOut = acc_h;
274
275 /* Read the second input into Xn2, to reuse the value */
276 Xn2 = *pIn++;
277
278 /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */
279
280 /* acc += b1 * x[n-1] */
281 acc = (q63_t) Xn *b1;
282
283 /* acc = b0 * x[n] */
284 acc += (q63_t) Xn2 *b0;
285
286 /* acc += b[2] * x[n-2] */
287 acc += (q63_t) Xn1 *b2;
288
289 /* acc += a1 * y[n-1] */
290 acc += mult32x64(Yn2, a1);
291
292 /* acc += a2 * y[n-2] */
293 acc += mult32x64(Yn1, a2);
294
295 /* The result is converted to 1.63, Yn1 variable is reused */
296 Yn1 = acc << shift;
297
298 /* Calc lower part of acc */
299 acc_l = acc & 0xffffffff;
300
301 /* Calc upper part of acc */
302 acc_h = (acc >> 32) & 0xffffffff;
303
304 /* Apply shift for lower part of acc and upper part of acc */
305 acc_h = (uint32_t) acc_l >> lShift | acc_h << uShift;
306
307 /* Read the third input into Xn1, to reuse the value */
308 Xn1 = *pIn++;
309
310 /* The result is converted to 1.31 */
311 /* Store the output in the destination buffer. */
312 *(pOut + 1u) = acc_h;
313
314 /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */
315
316 /* acc = b0 * x[n] */
317 acc = (q63_t) Xn1 *b0;
318
319 /* acc += b1 * x[n-1] */
320 acc += (q63_t) Xn2 *b1;
321
322 /* acc += b[2] * x[n-2] */
323 acc += (q63_t) Xn *b2;
324
325 /* acc += a1 * y[n-1] */
326 acc += mult32x64(Yn1, a1);
327
328 /* acc += a2 * y[n-2] */
329 acc += mult32x64(Yn2, a2);
330
331 /* The result is converted to 1.63, Yn2 variable is reused */
332 Yn2 = acc << shift;
333
334 /* Calc lower part of acc */
335 acc_l = acc & 0xffffffff;
336
337 /* Calc upper part of acc */
338 acc_h = (acc >> 32) & 0xffffffff;
339
340 /* Apply shift for lower part of acc and upper part of acc */
341 acc_h = (uint32_t) acc_l >> lShift | acc_h << uShift;
342
343 /* Store the output in the destination buffer in 1.31 format. */
344 *(pOut + 2u) = acc_h;
345
346 /* Read the fourth input into Xn, to reuse the value */
347 Xn = *pIn++;
348
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;
352
353 /* acc += b1 * x[n-1] */
354 acc += (q63_t) Xn1 *b1;
355
356 /* acc += b[2] * x[n-2] */
357 acc += (q63_t) Xn2 *b2;
358
359 /* acc += a1 * y[n-1] */
360 acc += mult32x64(Yn2, a1);
361
362 /* acc += a2 * y[n-2] */
363 acc += mult32x64(Yn1, a2);
364
365 /* The result is converted to 1.63, Yn1 variable is reused */
366 Yn1 = acc << shift;
367
368 /* Calc lower part of acc */
369 acc_l = acc & 0xffffffff;
370
371 /* Calc upper part of acc */
372 acc_h = (acc >> 32) & 0xffffffff;
373
374 /* Apply shift for lower part of acc and upper part of acc */
375 acc_h = (uint32_t) acc_l >> lShift | acc_h << uShift;
376
377 /* Store the output in the destination buffer in 1.31 format. */
378 *(pOut + 3u) = acc_h;
379
380 /* Every time after the output is computed state should be updated. */
381 /* The states should be updated as: */
382 /* Xn2 = Xn1 */
383 /* Xn1 = Xn */
384 /* Yn2 = Yn1 */
385 /* Yn1 = acc */
386 Xn2 = Xn1;
387 Xn1 = Xn;
388
389 /* update output pointer */
390 pOut += 4u;
391
392 /* decrement the loop counter */
393 sample--;
394 }
395
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);
399
400 while(sample > 0u)
401 {
402 /* Read the input */
403 Xn = *pIn++;
404
405 /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */
406
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);
417
418 /* Every time after the output is computed state should be updated. */
419 /* The states should be updated as: */
420 /* Xn2 = Xn1 */
421 /* Xn1 = Xn */
422 /* Yn2 = Yn1 */
423 /* Yn1 = acc */
424 Xn2 = Xn1;
425 Xn1 = Xn;
426 Yn2 = Yn1;
427 /* The result is converted to 1.63, Yn1 variable is reused */
428 Yn1 = acc << shift;
429
430 /* Calc lower part of acc */
431 acc_l = acc & 0xffffffff;
432
433 /* Calc upper part of acc */
434 acc_h = (acc >> 32) & 0xffffffff;
435
436 /* Apply shift for lower part of acc and upper part of acc */
437 acc_h = (uint32_t) acc_l >> lShift | acc_h << uShift;
438
439 /* Store the output in the destination buffer in 1.31 format. */
440 *pOut++ = acc_h;
441 //Yn1 = acc << shift;
442
443 /* Store the output in the destination buffer in 1.31 format. */
444 // *pOut++ = (q31_t) (acc >> (32 - shift));
445
446 /* decrement the loop counter */
447 sample--;
448 }
449
450 /* The first stage output is given as input to the second stage. */
451 pIn = pDst;
452
453 /* Reset to destination buffer working pointer */
454 pOut = pDst;
455
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;
460 *pState++ = Yn1;
461 *pState++ = Yn2;
462
463 } while(--stage);
464
465 #else
466
467 /* Run the below code for Cortex-M0 */
468
469 do
470 {
471 /* Reading the coefficients */
472 b0 = *pCoeffs++;
473 b1 = *pCoeffs++;
474 b2 = *pCoeffs++;
475 a1 = *pCoeffs++;
476 a2 = *pCoeffs++;
477
478 /* Reading the state values */
479 Xn1 = pState[0];
480 Xn2 = pState[1];
481 Yn1 = pState[2];
482 Yn2 = pState[3];
483
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]
487 */
488
489 sample = blockSize;
490
491 while(sample > 0u)
492 {
493 /* Read the input */
494 Xn = *pIn++;
495
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);
507
508 /* Every time after the output is computed state should be updated. */
509 /* The states should be updated as: */
510 /* Xn2 = Xn1 */
511 /* Xn1 = Xn */
512 /* Yn2 = Yn1 */
513 /* Yn1 = acc */
514 Xn2 = Xn1;
515 Xn1 = Xn;
516 Yn2 = Yn1;
517
518 /* The result is converted to 1.63, Yn1 variable is reused */
519 Yn1 = acc << shift;
520
521 /* Calc lower part of acc */
522 acc_l = acc & 0xffffffff;
523
524 /* Calc upper part of acc */
525 acc_h = (acc >> 32) & 0xffffffff;
526
527 /* Apply shift for lower part of acc and upper part of acc */
528 acc_h = (uint32_t) acc_l >> lShift | acc_h << uShift;
529
530 /* Store the output in the destination buffer in 1.31 format. */
531 *pOut++ = acc_h;
532
533 //Yn1 = acc << shift;
534
535 /* Store the output in the destination buffer in 1.31 format. */
536 //*pOut++ = (q31_t) (acc >> (32 - shift));
537
538 /* decrement the loop counter */
539 sample--;
540 }
541
542 /* The first stage output is given as input to the second stage. */
543 pIn = pDst;
544
545 /* Reset to destination buffer working pointer */
546 pOut = pDst;
547
548 /* Store the updated state variables back into the pState array */
549 *pState++ = (q63_t) Xn1;
550 *pState++ = (q63_t) Xn2;
551 *pState++ = Yn1;
552 *pState++ = Yn2;
553
554 } while(--stage);
555
556 #endif /* #ifndef ARM_MATH_CM0_FAMILY */
557 }
558
559 /**
560 * @} end of BiquadCascadeDF1_32x64 group
561 */
Imprint / Impressum