]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/dsp/cmsis_dsp/FilteringFunctions/arm_fir_f32.c
Merge commit '1fe4406f374291ab2e86e95a97341fd9c475fcb8'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / dsp / cmsis_dsp / FilteringFunctions / arm_fir_f32.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_fir_f32.c
9 *
10 * Description: Floating-point FIR 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 FIR Finite Impulse Response (FIR) Filters
49 *
50 * This set of functions implements Finite Impulse Response (FIR) filters
51 * for Q7, Q15, Q31, and floating-point data types. Fast versions of Q15 and Q31 are also provided.
52 * The functions operate on blocks of input and output data and each call to the function processes
53 * <code>blockSize</code> samples through the filter. <code>pSrc</code> and
54 * <code>pDst</code> points to input and output arrays containing <code>blockSize</code> values.
55 *
56 * \par Algorithm:
57 * The FIR filter algorithm is based upon a sequence of multiply-accumulate (MAC) operations.
58 * Each filter coefficient <code>b[n]</code> is multiplied by a state variable which equals a previous input sample <code>x[n]</code>.
59 * <pre>
60 * y[n] = b[0] * x[n] + b[1] * x[n-1] + b[2] * x[n-2] + ...+ b[numTaps-1] * x[n-numTaps+1]
61 * </pre>
62 * \par
63 * \image html FIR.gif "Finite Impulse Response filter"
64 * \par
65 * <code>pCoeffs</code> points to a coefficient array of size <code>numTaps</code>.
66 * Coefficients are stored in time reversed order.
67 * \par
68 * <pre>
69 * {b[numTaps-1], b[numTaps-2], b[N-2], ..., b[1], b[0]}
70 * </pre>
71 * \par
72 * <code>pState</code> points to a state array of size <code>numTaps + blockSize - 1</code>.
73 * Samples in the state buffer are stored in the following order.
74 * \par
75 * <pre>
76 * {x[n-numTaps+1], x[n-numTaps], x[n-numTaps-1], x[n-numTaps-2]....x[0], x[1], ..., x[blockSize-1]}
77 * </pre>
78 * \par
79 * Note that the length of the state buffer exceeds the length of the coefficient array by <code>blockSize-1</code>.
80 * The increased state buffer length allows circular addressing, which is traditionally used in the FIR filters,
81 * to be avoided and yields a significant speed improvement.
82 * The state variables are updated after each block of data is processed; the coefficients are untouched.
83 * \par Instance Structure
84 * The coefficients and state variables for a filter are stored together in an instance data structure.
85 * A separate instance structure must be defined for each filter.
86 * Coefficient arrays may be shared among several instances while state variable arrays cannot be shared.
87 * There are separate instance structure declarations for each of the 4 supported data types.
88 *
89 * \par Initialization Functions
90 * There is also an associated initialization function for each data type.
91 * The initialization function performs the following operations:
92 * - Sets the values of the internal structure fields.
93 * - Zeros out the values in the state buffer.
94 * To do this manually without calling the init function, assign the follow subfields of the instance structure:
95 * numTaps, pCoeffs, pState. Also set all of the values in pState to zero.
96 *
97 * \par
98 * Use of the initialization function is optional.
99 * However, if the initialization function is used, then the instance structure cannot be placed into a const data section.
100 * To place an instance structure into a const data section, the instance structure must be manually initialized.
101 * Set the values in the state buffer to zeros before static initialization.
102 * The code below statically initializes each of the 4 different data type filter instance structures
103 * <pre>
104 *arm_fir_instance_f32 S = {numTaps, pState, pCoeffs};
105 *arm_fir_instance_q31 S = {numTaps, pState, pCoeffs};
106 *arm_fir_instance_q15 S = {numTaps, pState, pCoeffs};
107 *arm_fir_instance_q7 S = {numTaps, pState, pCoeffs};
108 * </pre>
109 *
110 * where <code>numTaps</code> is the number of filter coefficients in the filter; <code>pState</code> is the address of the state buffer;
111 * <code>pCoeffs</code> is the address of the coefficient buffer.
112 *
113 * \par Fixed-Point Behavior
114 * Care must be taken when using the fixed-point versions of the FIR filter functions.
115 * In particular, the overflow and saturation behavior of the accumulator used in each function must be considered.
116 * Refer to the function specific documentation below for usage guidelines.
117 */
118
119 /**
120 * @addtogroup FIR
121 * @{
122 */
123
124 /**
125 *
126 * @param[in] *S points to an instance of the floating-point FIR filter structure.
127 * @param[in] *pSrc points to the block of input data.
128 * @param[out] *pDst points to the block of output data.
129 * @param[in] blockSize number of samples to process per call.
130 * @return none.
131 *
132 */
133
134 #ifndef ARM_MATH_CM0_FAMILY
135
136 /* Run the below code for Cortex-M4 and Cortex-M3 */
137
138 void arm_fir_f32(
139 const arm_fir_instance_f32 * S,
140 float32_t * pSrc,
141 float32_t * pDst,
142 uint32_t blockSize)
143 {
144 float32_t *pState = S->pState; /* State pointer */
145 float32_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */
146 float32_t *pStateCurnt; /* Points to the current sample of the state */
147 float32_t *px, *pb; /* Temporary pointers for state and coefficient buffers */
148 float32_t acc0, acc1, acc2, acc3, acc4, acc5, acc6, acc7; /* Accumulators */
149 float32_t x0, x1, x2, x3, x4, x5, x6, x7, c0; /* Temporary variables to hold state and coefficient values */
150 uint32_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */
151 uint32_t i, tapCnt, blkCnt; /* Loop counters */
152 float32_t p0,p1,p2,p3,p4,p5,p6,p7; /* Temporary product values */
153
154 /* S->pState points to state array which contains previous frame (numTaps - 1) samples */
155 /* pStateCurnt points to the location where the new input data should be written */
156 pStateCurnt = &(S->pState[(numTaps - 1u)]);
157
158 /* Apply loop unrolling and compute 8 output values simultaneously.
159 * The variables acc0 ... acc7 hold output values that are being computed:
160 *
161 * acc0 = b[numTaps-1] * x[n-numTaps-1] + b[numTaps-2] * x[n-numTaps-2] + b[numTaps-3] * x[n-numTaps-3] +...+ b[0] * x[0]
162 * acc1 = b[numTaps-1] * x[n-numTaps] + b[numTaps-2] * x[n-numTaps-1] + b[numTaps-3] * x[n-numTaps-2] +...+ b[0] * x[1]
163 * acc2 = b[numTaps-1] * x[n-numTaps+1] + b[numTaps-2] * x[n-numTaps] + b[numTaps-3] * x[n-numTaps-1] +...+ b[0] * x[2]
164 * acc3 = b[numTaps-1] * x[n-numTaps+2] + b[numTaps-2] * x[n-numTaps+1] + b[numTaps-3] * x[n-numTaps] +...+ b[0] * x[3]
165 */
166 blkCnt = blockSize >> 3;
167
168 /* First part of the processing with loop unrolling. Compute 8 outputs at a time.
169 ** a second loop below computes the remaining 1 to 7 samples. */
170 while(blkCnt > 0u)
171 {
172 /* Copy four new input samples into the state buffer */
173 *pStateCurnt++ = *pSrc++;
174 *pStateCurnt++ = *pSrc++;
175 *pStateCurnt++ = *pSrc++;
176 *pStateCurnt++ = *pSrc++;
177
178 /* Set all accumulators to zero */
179 acc0 = 0.0f;
180 acc1 = 0.0f;
181 acc2 = 0.0f;
182 acc3 = 0.0f;
183 acc4 = 0.0f;
184 acc5 = 0.0f;
185 acc6 = 0.0f;
186 acc7 = 0.0f;
187
188 /* Initialize state pointer */
189 px = pState;
190
191 /* Initialize coeff pointer */
192 pb = (pCoeffs);
193
194 /* This is separated from the others to avoid
195 * a call to __aeabi_memmove which would be slower
196 */
197 *pStateCurnt++ = *pSrc++;
198 *pStateCurnt++ = *pSrc++;
199 *pStateCurnt++ = *pSrc++;
200 *pStateCurnt++ = *pSrc++;
201
202 /* Read the first seven samples from the state buffer: x[n-numTaps], x[n-numTaps-1], x[n-numTaps-2] */
203 x0 = *px++;
204 x1 = *px++;
205 x2 = *px++;
206 x3 = *px++;
207 x4 = *px++;
208 x5 = *px++;
209 x6 = *px++;
210
211 /* Loop unrolling. Process 8 taps at a time. */
212 tapCnt = numTaps >> 3u;
213
214 /* Loop over the number of taps. Unroll by a factor of 8.
215 ** Repeat until we've computed numTaps-8 coefficients. */
216 while(tapCnt > 0u)
217 {
218 /* Read the b[numTaps-1] coefficient */
219 c0 = *(pb++);
220
221 /* Read x[n-numTaps-3] sample */
222 x7 = *(px++);
223
224 /* acc0 += b[numTaps-1] * x[n-numTaps] */
225 p0 = x0 * c0;
226
227 /* acc1 += b[numTaps-1] * x[n-numTaps-1] */
228 p1 = x1 * c0;
229
230 /* acc2 += b[numTaps-1] * x[n-numTaps-2] */
231 p2 = x2 * c0;
232
233 /* acc3 += b[numTaps-1] * x[n-numTaps-3] */
234 p3 = x3 * c0;
235
236 /* acc4 += b[numTaps-1] * x[n-numTaps-4] */
237 p4 = x4 * c0;
238
239 /* acc1 += b[numTaps-1] * x[n-numTaps-5] */
240 p5 = x5 * c0;
241
242 /* acc2 += b[numTaps-1] * x[n-numTaps-6] */
243 p6 = x6 * c0;
244
245 /* acc3 += b[numTaps-1] * x[n-numTaps-7] */
246 p7 = x7 * c0;
247
248 /* Read the b[numTaps-2] coefficient */
249 c0 = *(pb++);
250
251 /* Read x[n-numTaps-4] sample */
252 x0 = *(px++);
253
254 acc0 += p0;
255 acc1 += p1;
256 acc2 += p2;
257 acc3 += p3;
258 acc4 += p4;
259 acc5 += p5;
260 acc6 += p6;
261 acc7 += p7;
262
263
264 /* Perform the multiply-accumulate */
265 p0 = x1 * c0;
266 p1 = x2 * c0;
267 p2 = x3 * c0;
268 p3 = x4 * c0;
269 p4 = x5 * c0;
270 p5 = x6 * c0;
271 p6 = x7 * c0;
272 p7 = x0 * c0;
273
274 /* Read the b[numTaps-3] coefficient */
275 c0 = *(pb++);
276
277 /* Read x[n-numTaps-5] sample */
278 x1 = *(px++);
279
280 acc0 += p0;
281 acc1 += p1;
282 acc2 += p2;
283 acc3 += p3;
284 acc4 += p4;
285 acc5 += p5;
286 acc6 += p6;
287 acc7 += p7;
288
289 /* Perform the multiply-accumulates */
290 p0 = x2 * c0;
291 p1 = x3 * c0;
292 p2 = x4 * c0;
293 p3 = x5 * c0;
294 p4 = x6 * c0;
295 p5 = x7 * c0;
296 p6 = x0 * c0;
297 p7 = x1 * c0;
298
299 /* Read the b[numTaps-4] coefficient */
300 c0 = *(pb++);
301
302 /* Read x[n-numTaps-6] sample */
303 x2 = *(px++);
304
305 acc0 += p0;
306 acc1 += p1;
307 acc2 += p2;
308 acc3 += p3;
309 acc4 += p4;
310 acc5 += p5;
311 acc6 += p6;
312 acc7 += p7;
313
314 /* Perform the multiply-accumulates */
315 p0 = x3 * c0;
316 p1 = x4 * c0;
317 p2 = x5 * c0;
318 p3 = x6 * c0;
319 p4 = x7 * c0;
320 p5 = x0 * c0;
321 p6 = x1 * c0;
322 p7 = x2 * c0;
323
324 /* Read the b[numTaps-4] coefficient */
325 c0 = *(pb++);
326
327 /* Read x[n-numTaps-6] sample */
328 x3 = *(px++);
329
330 acc0 += p0;
331 acc1 += p1;
332 acc2 += p2;
333 acc3 += p3;
334 acc4 += p4;
335 acc5 += p5;
336 acc6 += p6;
337 acc7 += p7;
338
339 /* Perform the multiply-accumulates */
340 p0 = x4 * c0;
341 p1 = x5 * c0;
342 p2 = x6 * c0;
343 p3 = x7 * c0;
344 p4 = x0 * c0;
345 p5 = x1 * c0;
346 p6 = x2 * c0;
347 p7 = x3 * c0;
348
349 /* Read the b[numTaps-4] coefficient */
350 c0 = *(pb++);
351
352 /* Read x[n-numTaps-6] sample */
353 x4 = *(px++);
354
355 acc0 += p0;
356 acc1 += p1;
357 acc2 += p2;
358 acc3 += p3;
359 acc4 += p4;
360 acc5 += p5;
361 acc6 += p6;
362 acc7 += p7;
363
364 /* Perform the multiply-accumulates */
365 p0 = x5 * c0;
366 p1 = x6 * c0;
367 p2 = x7 * c0;
368 p3 = x0 * c0;
369 p4 = x1 * c0;
370 p5 = x2 * c0;
371 p6 = x3 * c0;
372 p7 = x4 * c0;
373
374 /* Read the b[numTaps-4] coefficient */
375 c0 = *(pb++);
376
377 /* Read x[n-numTaps-6] sample */
378 x5 = *(px++);
379
380 acc0 += p0;
381 acc1 += p1;
382 acc2 += p2;
383 acc3 += p3;
384 acc4 += p4;
385 acc5 += p5;
386 acc6 += p6;
387 acc7 += p7;
388
389 /* Perform the multiply-accumulates */
390 p0 = x6 * c0;
391 p1 = x7 * c0;
392 p2 = x0 * c0;
393 p3 = x1 * c0;
394 p4 = x2 * c0;
395 p5 = x3 * c0;
396 p6 = x4 * c0;
397 p7 = x5 * c0;
398
399 /* Read the b[numTaps-4] coefficient */
400 c0 = *(pb++);
401
402 /* Read x[n-numTaps-6] sample */
403 x6 = *(px++);
404
405 acc0 += p0;
406 acc1 += p1;
407 acc2 += p2;
408 acc3 += p3;
409 acc4 += p4;
410 acc5 += p5;
411 acc6 += p6;
412 acc7 += p7;
413
414 /* Perform the multiply-accumulates */
415 p0 = x7 * c0;
416 p1 = x0 * c0;
417 p2 = x1 * c0;
418 p3 = x2 * c0;
419 p4 = x3 * c0;
420 p5 = x4 * c0;
421 p6 = x5 * c0;
422 p7 = x6 * c0;
423
424 tapCnt--;
425
426 acc0 += p0;
427 acc1 += p1;
428 acc2 += p2;
429 acc3 += p3;
430 acc4 += p4;
431 acc5 += p5;
432 acc6 += p6;
433 acc7 += p7;
434 }
435
436 /* If the filter length is not a multiple of 8, compute the remaining filter taps */
437 tapCnt = numTaps % 0x8u;
438
439 while(tapCnt > 0u)
440 {
441 /* Read coefficients */
442 c0 = *(pb++);
443
444 /* Fetch 1 state variable */
445 x7 = *(px++);
446
447 /* Perform the multiply-accumulates */
448 p0 = x0 * c0;
449 p1 = x1 * c0;
450 p2 = x2 * c0;
451 p3 = x3 * c0;
452 p4 = x4 * c0;
453 p5 = x5 * c0;
454 p6 = x6 * c0;
455 p7 = x7 * c0;
456
457 /* Reuse the present sample states for next sample */
458 x0 = x1;
459 x1 = x2;
460 x2 = x3;
461 x3 = x4;
462 x4 = x5;
463 x5 = x6;
464 x6 = x7;
465
466 acc0 += p0;
467 acc1 += p1;
468 acc2 += p2;
469 acc3 += p3;
470 acc4 += p4;
471 acc5 += p5;
472 acc6 += p6;
473 acc7 += p7;
474
475 /* Decrement the loop counter */
476 tapCnt--;
477 }
478
479 /* Advance the state pointer by 8 to process the next group of 8 samples */
480 pState = pState + 8;
481
482 /* The results in the 8 accumulators, store in the destination buffer. */
483 *pDst++ = acc0;
484 *pDst++ = acc1;
485 *pDst++ = acc2;
486 *pDst++ = acc3;
487 *pDst++ = acc4;
488 *pDst++ = acc5;
489 *pDst++ = acc6;
490 *pDst++ = acc7;
491
492 blkCnt--;
493 }
494
495 /* If the blockSize is not a multiple of 8, compute any remaining output samples here.
496 ** No loop unrolling is used. */
497 blkCnt = blockSize % 0x8u;
498
499 while(blkCnt > 0u)
500 {
501 /* Copy one sample at a time into state buffer */
502 *pStateCurnt++ = *pSrc++;
503
504 /* Set the accumulator to zero */
505 acc0 = 0.0f;
506
507 /* Initialize state pointer */
508 px = pState;
509
510 /* Initialize Coefficient pointer */
511 pb = (pCoeffs);
512
513 i = numTaps;
514
515 /* Perform the multiply-accumulates */
516 do
517 {
518 acc0 += *px++ * *pb++;
519 i--;
520
521 } while(i > 0u);
522
523 /* The result is store in the destination buffer. */
524 *pDst++ = acc0;
525
526 /* Advance state pointer by 1 for the next sample */
527 pState = pState + 1;
528
529 blkCnt--;
530 }
531
532 /* Processing is complete.
533 ** Now copy the last numTaps - 1 samples to the start of the state buffer.
534 ** This prepares the state buffer for the next function call. */
535
536 /* Points to the start of the state buffer */
537 pStateCurnt = S->pState;
538
539 tapCnt = (numTaps - 1u) >> 2u;
540
541 /* copy data */
542 while(tapCnt > 0u)
543 {
544 *pStateCurnt++ = *pState++;
545 *pStateCurnt++ = *pState++;
546 *pStateCurnt++ = *pState++;
547 *pStateCurnt++ = *pState++;
548
549 /* Decrement the loop counter */
550 tapCnt--;
551 }
552
553 /* Calculate remaining number of copies */
554 tapCnt = (numTaps - 1u) % 0x4u;
555
556 /* Copy the remaining q31_t data */
557 while(tapCnt > 0u)
558 {
559 *pStateCurnt++ = *pState++;
560
561 /* Decrement the loop counter */
562 tapCnt--;
563 }
564 }
565
566 #else
567
568 void arm_fir_f32(
569 const arm_fir_instance_f32 * S,
570 float32_t * pSrc,
571 float32_t * pDst,
572 uint32_t blockSize)
573 {
574 float32_t *pState = S->pState; /* State pointer */
575 float32_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */
576 float32_t *pStateCurnt; /* Points to the current sample of the state */
577 float32_t *px, *pb; /* Temporary pointers for state and coefficient buffers */
578 uint32_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */
579 uint32_t i, tapCnt, blkCnt; /* Loop counters */
580
581 /* Run the below code for Cortex-M0 */
582
583 float32_t acc;
584
585 /* S->pState points to state array which contains previous frame (numTaps - 1) samples */
586 /* pStateCurnt points to the location where the new input data should be written */
587 pStateCurnt = &(S->pState[(numTaps - 1u)]);
588
589 /* Initialize blkCnt with blockSize */
590 blkCnt = blockSize;
591
592 while(blkCnt > 0u)
593 {
594 /* Copy one sample at a time into state buffer */
595 *pStateCurnt++ = *pSrc++;
596
597 /* Set the accumulator to zero */
598 acc = 0.0f;
599
600 /* Initialize state pointer */
601 px = pState;
602
603 /* Initialize Coefficient pointer */
604 pb = pCoeffs;
605
606 i = numTaps;
607
608 /* Perform the multiply-accumulates */
609 do
610 {
611 /* acc = b[numTaps-1] * x[n-numTaps-1] + b[numTaps-2] * x[n-numTaps-2] + b[numTaps-3] * x[n-numTaps-3] +...+ b[0] * x[0] */
612 acc += *px++ * *pb++;
613 i--;
614
615 } while(i > 0u);
616
617 /* The result is store in the destination buffer. */
618 *pDst++ = acc;
619
620 /* Advance state pointer by 1 for the next sample */
621 pState = pState + 1;
622
623 blkCnt--;
624 }
625
626 /* Processing is complete.
627 ** Now copy the last numTaps - 1 samples to the starting of the state buffer.
628 ** This prepares the state buffer for the next function call. */
629
630 /* Points to the start of the state buffer */
631 pStateCurnt = S->pState;
632
633 /* Copy numTaps number of values */
634 tapCnt = numTaps - 1u;
635
636 /* Copy data */
637 while(tapCnt > 0u)
638 {
639 *pStateCurnt++ = *pState++;
640
641 /* Decrement the loop counter */
642 tapCnt--;
643 }
644
645 }
646
647 #endif /* #ifndef ARM_MATH_CM0_FAMILY */
648
649 /**
650 * @} end of FIR group
651 */
Imprint / Impressum