1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010-2013 ARM Limited. All rights reserved.
4 * $Date: 17. January 2013
7 * Project: CMSIS DSP Library
8 * Title: arm_fir_decimate_f32.c
10 * Description: FIR decimation for floating-point sequences.
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_decimate Finite Impulse Response (FIR) Decimator
50 * These functions combine an FIR filter together with a decimator.
51 * They are used in multirate systems for reducing the sample rate of a signal without introducing aliasing distortion.
52 * Conceptually, the functions are equivalent to the block diagram below:
53 * \image html FIRDecimator.gif "Components included in the FIR Decimator functions"
54 * When decimating by a factor of <code>M</code>, the signal should be prefiltered by a lowpass filter with a normalized
55 * cutoff frequency of <code>1/M</code> in order to prevent aliasing distortion.
56 * The user of the function is responsible for providing the filter coefficients.
58 * The FIR decimator functions provided in the CMSIS DSP Library combine the FIR filter and the decimator in an efficient manner.
59 * Instead of calculating all of the FIR filter outputs and discarding <code>M-1</code> out of every <code>M</code>, only the
60 * samples output by the decimator are computed.
61 * The functions operate on blocks of input and output data.
62 * <code>pSrc</code> points to an array of <code>blockSize</code> input values and
63 * <code>pDst</code> points to an array of <code>blockSize/M</code> output values.
64 * In order to have an integer number of output samples <code>blockSize</code>
65 * must always be a multiple of the decimation factor <code>M</code>.
67 * The library provides separate functions for Q15, Q31 and floating-point data types.
70 * The FIR portion of the algorithm uses the standard form filter:
72 * y[n] = b[0] * x[n] + b[1] * x[n-1] + b[2] * x[n-2] + ...+ b[numTaps-1] * x[n-numTaps+1]
74 * where, <code>b[n]</code> are the filter coefficients.
76 * The <code>pCoeffs</code> points to a coefficient array of size <code>numTaps</code>.
77 * Coefficients are stored in time reversed order.
80 * {b[numTaps-1], b[numTaps-2], b[N-2], ..., b[1], b[0]}
83 * <code>pState</code> points to a state array of size <code>numTaps + blockSize - 1</code>.
84 * Samples in the state buffer are stored in the order:
87 * {x[n-numTaps+1], x[n-numTaps], x[n-numTaps-1], x[n-numTaps-2]....x[0], x[1], ..., x[blockSize-1]}
89 * The state variables are updated after each block of data is processed, the coefficients are untouched.
91 * \par Instance Structure
92 * The coefficients and state variables for a filter are stored together in an instance data structure.
93 * A separate instance structure must be defined for each filter.
94 * Coefficient arrays may be shared among several instances while state variable array should be allocated separately.
95 * There are separate instance structure declarations for each of the 3 supported data types.
97 * \par Initialization Functions
98 * There is also an associated initialization function for each data type.
99 * The initialization function performs the following operations:
100 * - Sets the values of the internal structure fields.
101 * - Zeros out the values in the state buffer.
102 * - Checks to make sure that the size of the input is a multiple of the decimation factor.
103 * To do this manually without calling the init function, assign the follow subfields of the instance structure:
104 * numTaps, pCoeffs, M (decimation factor), pState. Also set all of the values in pState to zero.
107 * Use of the initialization function is optional.
108 * However, if the initialization function is used, then the instance structure cannot be placed into a const data section.
109 * To place an instance structure into a const data section, the instance structure must be manually initialized.
110 * The code below statically initializes each of the 3 different data type filter instance structures
112 *arm_fir_decimate_instance_f32 S = {M, numTaps, pCoeffs, pState};
113 *arm_fir_decimate_instance_q31 S = {M, numTaps, pCoeffs, pState};
114 *arm_fir_decimate_instance_q15 S = {M, numTaps, pCoeffs, pState};
116 * where <code>M</code> is the decimation factor; <code>numTaps</code> is the number of filter coefficients in the filter;
117 * <code>pCoeffs</code> is the address of the coefficient buffer;
118 * <code>pState</code> is the address of the state buffer.
119 * Be sure to set the values in the state buffer to zeros when doing static initialization.
121 * \par Fixed-Point Behavior
122 * Care must be taken when using the fixed-point versions of the FIR decimate filter functions.
123 * In particular, the overflow and saturation behavior of the accumulator used in each function must be considered.
124 * Refer to the function specific documentation below for usage guidelines.
128 * @addtogroup FIR_decimate
133 * @brief Processing function for the floating-point FIR decimator.
134 * @param[in] *S points to an instance of the floating-point FIR decimator structure.
135 * @param[in] *pSrc points to the block of input data.
136 * @param[out] *pDst points to the block of output data.
137 * @param[in] blockSize number of input samples to process per call.
141 void arm_fir_decimate_f32(
142 const arm_fir_decimate_instance_f32
* S
,
147 float32_t
*pState
= S
->pState
; /* State pointer */
148 float32_t
*pCoeffs
= S
->pCoeffs
; /* Coefficient pointer */
149 float32_t
*pStateCurnt
; /* Points to the current sample of the state */
150 float32_t
*px
, *pb
; /* Temporary pointers for state and coefficient buffers */
151 float32_t sum0
; /* Accumulator */
152 float32_t x0
, c0
; /* Temporary variables to hold state and coefficient values */
153 uint32_t numTaps
= S
->numTaps
; /* Number of filter coefficients in the filter */
154 uint32_t i
, tapCnt
, blkCnt
, outBlockSize
= blockSize
/ S
->M
; /* Loop counters */
156 #ifndef ARM_MATH_CM0_FAMILY
159 float32_t
*px0
, *px1
, *px2
, *px3
;
160 float32_t acc0
, acc1
, acc2
, acc3
;
161 float32_t x1
, x2
, x3
;
163 /* Run the below code for Cortex-M4 and Cortex-M3 */
165 /* S->pState buffer contains previous frame (numTaps - 1) samples */
166 /* pStateCurnt points to the location where the new input data should be written */
167 pStateCurnt
= S
->pState
+ (numTaps
- 1u);
169 /* Total number of output samples to be computed */
170 blkCnt
= outBlockSize
/ 4;
171 blkCntN4
= outBlockSize
- (4 * blkCnt
);
175 /* Copy 4 * decimation factor number of new input samples into the state buffer */
180 *pStateCurnt
++ = *pSrc
++;
184 /* Set accumulators to zero */
190 /* Initialize state pointer for all the samples */
193 px2
= pState
+ 2 * S
->M
;
194 px3
= pState
+ 3 * S
->M
;
196 /* Initialize coeff pointer */
199 /* Loop unrolling. Process 4 taps at a time. */
200 tapCnt
= numTaps
>> 2;
202 /* Loop over the number of taps. Unroll by a factor of 4.
203 ** Repeat until we've computed numTaps-4 coefficients. */
207 /* Read the b[numTaps-1] coefficient */
210 /* Read x[n-numTaps-1] sample for acc0 */
212 /* Read x[n-numTaps-1] sample for acc1 */
214 /* Read x[n-numTaps-1] sample for acc2 */
216 /* Read x[n-numTaps-1] sample for acc3 */
219 /* Perform the multiply-accumulate */
225 /* Read the b[numTaps-2] coefficient */
228 /* Read x[n-numTaps-2] sample for acc0, acc1, acc2, acc3 */
234 /* Perform the multiply-accumulate */
240 /* Read the b[numTaps-3] coefficient */
243 /* Read x[n-numTaps-3] sample acc0, acc1, acc2, acc3 */
249 /* Perform the multiply-accumulate */
255 /* Read the b[numTaps-4] coefficient */
258 /* Read x[n-numTaps-4] sample acc0, acc1, acc2, acc3 */
264 /* Perform the multiply-accumulate */
270 /* Decrement the loop counter */
274 /* If the filter length is not a multiple of 4, compute the remaining filter taps */
275 tapCnt
= numTaps
% 0x4u
;
279 /* Read coefficients */
282 /* Fetch state variables for acc0, acc1, acc2, acc3 */
288 /* Perform the multiply-accumulate */
294 /* Decrement the loop counter */
298 /* Advance the state pointer by the decimation factor
299 * to process the next group of decimation factor number samples */
300 pState
= pState
+ 4 * S
->M
;
302 /* The result is in the accumulator, store in the destination buffer. */
308 /* Decrement the loop counter */
314 /* Copy decimation factor number of new input samples into the state buffer */
319 *pStateCurnt
++ = *pSrc
++;
323 /* Set accumulator to zero */
326 /* Initialize state pointer */
329 /* Initialize coeff pointer */
332 /* Loop unrolling. Process 4 taps at a time. */
333 tapCnt
= numTaps
>> 2;
335 /* Loop over the number of taps. Unroll by a factor of 4.
336 ** Repeat until we've computed numTaps-4 coefficients. */
339 /* Read the b[numTaps-1] coefficient */
342 /* Read x[n-numTaps-1] sample */
345 /* Perform the multiply-accumulate */
348 /* Read the b[numTaps-2] coefficient */
351 /* Read x[n-numTaps-2] sample */
354 /* Perform the multiply-accumulate */
357 /* Read the b[numTaps-3] coefficient */
360 /* Read x[n-numTaps-3] sample */
363 /* Perform the multiply-accumulate */
366 /* Read the b[numTaps-4] coefficient */
369 /* Read x[n-numTaps-4] sample */
372 /* Perform the multiply-accumulate */
375 /* Decrement the loop counter */
379 /* If the filter length is not a multiple of 4, compute the remaining filter taps */
380 tapCnt
= numTaps
% 0x4u
;
384 /* Read coefficients */
387 /* Fetch 1 state variable */
390 /* Perform the multiply-accumulate */
393 /* Decrement the loop counter */
397 /* Advance the state pointer by the decimation factor
398 * to process the next group of decimation factor number samples */
399 pState
= pState
+ S
->M
;
401 /* The result is in the accumulator, store in the destination buffer. */
404 /* Decrement the loop counter */
408 /* Processing is complete.
409 ** Now copy the last numTaps - 1 samples to the satrt of the state buffer.
410 ** This prepares the state buffer for the next function call. */
412 /* Points to the start of the state buffer */
413 pStateCurnt
= S
->pState
;
415 i
= (numTaps
- 1u) >> 2;
420 *pStateCurnt
++ = *pState
++;
421 *pStateCurnt
++ = *pState
++;
422 *pStateCurnt
++ = *pState
++;
423 *pStateCurnt
++ = *pState
++;
425 /* Decrement the loop counter */
429 i
= (numTaps
- 1u) % 0x04u
;
434 *pStateCurnt
++ = *pState
++;
436 /* Decrement the loop counter */
442 /* Run the below code for Cortex-M0 */
444 /* S->pState buffer contains previous frame (numTaps - 1) samples */
445 /* pStateCurnt points to the location where the new input data should be written */
446 pStateCurnt
= S
->pState
+ (numTaps
- 1u);
448 /* Total number of output samples to be computed */
449 blkCnt
= outBlockSize
;
453 /* Copy decimation factor number of new input samples into the state buffer */
458 *pStateCurnt
++ = *pSrc
++;
462 /* Set accumulator to zero */
465 /* Initialize state pointer */
468 /* Initialize coeff pointer */
475 /* Read coefficients */
478 /* Fetch 1 state variable */
481 /* Perform the multiply-accumulate */
484 /* Decrement the loop counter */
488 /* Advance the state pointer by the decimation factor
489 * to process the next group of decimation factor number samples */
490 pState
= pState
+ S
->M
;
492 /* The result is in the accumulator, store in the destination buffer. */
495 /* Decrement the loop counter */
499 /* Processing is complete.
500 ** Now copy the last numTaps - 1 samples to the start of the state buffer.
501 ** This prepares the state buffer for the next function call. */
503 /* Points to the start of the state buffer */
504 pStateCurnt
= S
->pState
;
506 /* Copy numTaps number of values */
512 *pStateCurnt
++ = *pState
++;
514 /* Decrement the loop counter */
518 #endif /* #ifndef ARM_MATH_CM0_FAMILY */
523 * @} end of FIR_decimate group