]>
git.gir.st - tmk_keyboard.git/blob - 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.
4 * $Date: 17. January 2013
7 * Project: CMSIS DSP Library
10 * Description: Floating-point FIR 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 FIR Finite Impulse Response (FIR) Filters
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.
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>.
60 * y[n] = b[0] * x[n] + b[1] * x[n-1] + b[2] * x[n-2] + ...+ b[numTaps-1] * x[n-numTaps+1]
63 * \image html FIR.gif "Finite Impulse Response filter"
65 * <code>pCoeffs</code> points to a coefficient array of size <code>numTaps</code>.
66 * Coefficients are stored in time reversed order.
69 * {b[numTaps-1], b[numTaps-2], b[N-2], ..., b[1], b[0]}
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.
76 * {x[n-numTaps+1], x[n-numTaps], x[n-numTaps-1], x[n-numTaps-2]....x[0], x[1], ..., x[blockSize-1]}
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.
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.
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
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};
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.
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.
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.
134 #ifndef ARM_MATH_CM0_FAMILY
136 /* Run the below code for Cortex-M4 and Cortex-M3 */
139 const arm_fir_instance_f32
* S
,
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 */
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)]);
158 /* Apply loop unrolling and compute 8 output values simultaneously.
159 * The variables acc0 ... acc7 hold output values that are being computed:
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]
166 blkCnt
= blockSize
>> 3;
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. */
172 /* Copy four new input samples into the state buffer */
173 *pStateCurnt
++ = *pSrc
++;
174 *pStateCurnt
++ = *pSrc
++;
175 *pStateCurnt
++ = *pSrc
++;
176 *pStateCurnt
++ = *pSrc
++;
178 /* Set all accumulators to zero */
188 /* Initialize state pointer */
191 /* Initialize coeff pointer */
194 /* This is separated from the others to avoid
195 * a call to __aeabi_memmove which would be slower
197 *pStateCurnt
++ = *pSrc
++;
198 *pStateCurnt
++ = *pSrc
++;
199 *pStateCurnt
++ = *pSrc
++;
200 *pStateCurnt
++ = *pSrc
++;
202 /* Read the first seven samples from the state buffer: x[n-numTaps], x[n-numTaps-1], x[n-numTaps-2] */
211 /* Loop unrolling. Process 8 taps at a time. */
212 tapCnt
= numTaps
>> 3u;
214 /* Loop over the number of taps. Unroll by a factor of 8.
215 ** Repeat until we've computed numTaps-8 coefficients. */
218 /* Read the b[numTaps-1] coefficient */
221 /* Read x[n-numTaps-3] sample */
224 /* acc0 += b[numTaps-1] * x[n-numTaps] */
227 /* acc1 += b[numTaps-1] * x[n-numTaps-1] */
230 /* acc2 += b[numTaps-1] * x[n-numTaps-2] */
233 /* acc3 += b[numTaps-1] * x[n-numTaps-3] */
236 /* acc4 += b[numTaps-1] * x[n-numTaps-4] */
239 /* acc1 += b[numTaps-1] * x[n-numTaps-5] */
242 /* acc2 += b[numTaps-1] * x[n-numTaps-6] */
245 /* acc3 += b[numTaps-1] * x[n-numTaps-7] */
248 /* Read the b[numTaps-2] coefficient */
251 /* Read x[n-numTaps-4] sample */
264 /* Perform the multiply-accumulate */
274 /* Read the b[numTaps-3] coefficient */
277 /* Read x[n-numTaps-5] sample */
289 /* Perform the multiply-accumulates */
299 /* Read the b[numTaps-4] coefficient */
302 /* Read x[n-numTaps-6] sample */
314 /* Perform the multiply-accumulates */
324 /* Read the b[numTaps-4] coefficient */
327 /* Read x[n-numTaps-6] sample */
339 /* Perform the multiply-accumulates */
349 /* Read the b[numTaps-4] coefficient */
352 /* Read x[n-numTaps-6] sample */
364 /* Perform the multiply-accumulates */
374 /* Read the b[numTaps-4] coefficient */
377 /* Read x[n-numTaps-6] sample */
389 /* Perform the multiply-accumulates */
399 /* Read the b[numTaps-4] coefficient */
402 /* Read x[n-numTaps-6] sample */
414 /* Perform the multiply-accumulates */
436 /* If the filter length is not a multiple of 8, compute the remaining filter taps */
437 tapCnt
= numTaps
% 0x8u
;
441 /* Read coefficients */
444 /* Fetch 1 state variable */
447 /* Perform the multiply-accumulates */
457 /* Reuse the present sample states for next sample */
475 /* Decrement the loop counter */
479 /* Advance the state pointer by 8 to process the next group of 8 samples */
482 /* The results in the 8 accumulators, store in the destination buffer. */
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
;
501 /* Copy one sample at a time into state buffer */
502 *pStateCurnt
++ = *pSrc
++;
504 /* Set the accumulator to zero */
507 /* Initialize state pointer */
510 /* Initialize Coefficient pointer */
515 /* Perform the multiply-accumulates */
518 acc0
+= *px
++ * *pb
++;
523 /* The result is store in the destination buffer. */
526 /* Advance state pointer by 1 for the next sample */
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. */
536 /* Points to the start of the state buffer */
537 pStateCurnt
= S
->pState
;
539 tapCnt
= (numTaps
- 1u) >> 2u;
544 *pStateCurnt
++ = *pState
++;
545 *pStateCurnt
++ = *pState
++;
546 *pStateCurnt
++ = *pState
++;
547 *pStateCurnt
++ = *pState
++;
549 /* Decrement the loop counter */
553 /* Calculate remaining number of copies */
554 tapCnt
= (numTaps
- 1u) % 0x4u
;
556 /* Copy the remaining q31_t data */
559 *pStateCurnt
++ = *pState
++;
561 /* Decrement the loop counter */
569 const arm_fir_instance_f32
* S
,
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 */
581 /* Run the below code for Cortex-M0 */
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)]);
589 /* Initialize blkCnt with blockSize */
594 /* Copy one sample at a time into state buffer */
595 *pStateCurnt
++ = *pSrc
++;
597 /* Set the accumulator to zero */
600 /* Initialize state pointer */
603 /* Initialize Coefficient pointer */
608 /* Perform the multiply-accumulates */
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
++;
617 /* The result is store in the destination buffer. */
620 /* Advance state pointer by 1 for the next sample */
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. */
630 /* Points to the start of the state buffer */
631 pStateCurnt
= S
->pState
;
633 /* Copy numTaps number of values */
634 tapCnt
= numTaps
- 1u;
639 *pStateCurnt
++ = *pState
++;
641 /* Decrement the loop counter */
647 #endif /* #ifndef ARM_MATH_CM0_FAMILY */
650 * @} end of FIR group