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_fast_q31.c
10 * Description: Fast Q31 FIR Decimator.
12 * Target Processor: Cortex-M4/Cortex-M3
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 * @addtogroup FIR_decimate
53 * @brief Processing function for the Q31 FIR decimator (fast variant) for Cortex-M3 and Cortex-M4.
54 * @param[in] *S points to an instance of the Q31 FIR decimator structure.
55 * @param[in] *pSrc points to the block of input data.
56 * @param[out] *pDst points to the block of output data
57 * @param[in] blockSize number of input samples to process per call.
60 * <b>Scaling and Overflow Behavior:</b>
63 * This function is optimized for speed at the expense of fixed-point precision and overflow protection.
64 * The result of each 1.31 x 1.31 multiplication is truncated to 2.30 format.
65 * These intermediate results are added to a 2.30 accumulator.
66 * Finally, the accumulator is saturated and converted to a 1.31 result.
67 * The fast version has the same overflow behavior as the standard version and provides less precision since it discards the low 32 bits of each multiplication result.
68 * In order to avoid overflows completely the input signal must be scaled down by log2(numTaps) bits (where log2 is read as log to the base 2).
71 * Refer to the function <code>arm_fir_decimate_q31()</code> for a slower implementation of this function which uses a 64-bit accumulator to provide higher precision.
72 * Both the slow and the fast versions use the same instance structure.
73 * Use the function <code>arm_fir_decimate_init_q31()</code> to initialize the filter structure.
76 void arm_fir_decimate_fast_q31(
77 arm_fir_decimate_instance_q31
* S
,
82 q31_t
*pState
= S
->pState
; /* State pointer */
83 q31_t
*pCoeffs
= S
->pCoeffs
; /* Coefficient pointer */
84 q31_t
*pStateCurnt
; /* Points to the current sample of the state */
85 q31_t x0
, c0
; /* Temporary variables to hold state and coefficient values */
86 q31_t
*px
; /* Temporary pointers for state buffer */
87 q31_t
*pb
; /* Temporary pointers for coefficient buffer */
88 q31_t sum0
; /* Accumulator */
89 uint32_t numTaps
= S
->numTaps
; /* Number of taps */
90 uint32_t i
, tapCnt
, blkCnt
, outBlockSize
= blockSize
/ S
->M
; /* Loop counters */
96 /* S->pState buffer contains previous frame (numTaps - 1) samples */
97 /* pStateCurnt points to the location where the new input data should be written */
98 pStateCurnt
= S
->pState
+ (numTaps
- 1u);
100 /* Total number of output samples to be computed */
102 blkCnt
= outBlockSize
/ 2;
103 blkCntN2
= outBlockSize
- (2 * blkCnt
);
107 /* Copy decimation factor number of new input samples into the state buffer */
112 *pStateCurnt
++ = *pSrc
++;
116 /* Set accumulator to zero */
120 /* Initialize state pointer */
124 /* Initialize coeff pointer */
127 /* Loop unrolling. Process 4 taps at a time. */
128 tapCnt
= numTaps
>> 2;
130 /* Loop over the number of taps. Unroll by a factor of 4.
131 ** Repeat until we've computed numTaps-4 coefficients. */
134 /* Read the b[numTaps-1] coefficient */
137 /* Read x[n-numTaps-1] for sample 0 sample 1 */
141 /* Perform the multiply-accumulate */
142 acc0
= (q31_t
) ((((q63_t
) acc0
<< 32) + ((q63_t
) x0
* c0
)) >> 32);
143 acc1
= (q31_t
) ((((q63_t
) acc1
<< 32) + ((q63_t
) x1
* c0
)) >> 32);
145 /* Read the b[numTaps-2] coefficient */
148 /* Read x[n-numTaps-2] for sample 0 sample 1 */
152 /* Perform the multiply-accumulate */
153 acc0
= (q31_t
) ((((q63_t
) acc0
<< 32) + ((q63_t
) x0
* c0
)) >> 32);
154 acc1
= (q31_t
) ((((q63_t
) acc1
<< 32) + ((q63_t
) x1
* c0
)) >> 32);
156 /* Read the b[numTaps-3] coefficient */
159 /* Read x[n-numTaps-3] for sample 0 sample 1 */
164 /* Perform the multiply-accumulate */
165 acc0
= (q31_t
) ((((q63_t
) acc0
<< 32) + ((q63_t
) x0
* c0
)) >> 32);
166 acc1
= (q31_t
) ((((q63_t
) acc1
<< 32) + ((q63_t
) x1
* c0
)) >> 32);
168 /* Read the b[numTaps-4] coefficient */
171 /* Read x[n-numTaps-4] for sample 0 sample 1 */
176 /* Perform the multiply-accumulate */
177 acc0
= (q31_t
) ((((q63_t
) acc0
<< 32) + ((q63_t
) x0
* c0
)) >> 32);
178 acc1
= (q31_t
) ((((q63_t
) acc1
<< 32) + ((q63_t
) x1
* c0
)) >> 32);
180 /* update state pointers */
184 /* Decrement the loop counter */
188 /* If the filter length is not a multiple of 4, compute the remaining filter taps */
189 tapCnt
= numTaps
% 0x4u
;
193 /* Read coefficients */
196 /* Fetch 1 state variable */
200 /* Perform the multiply-accumulate */
201 acc0
= (q31_t
) ((((q63_t
) acc0
<< 32) + ((q63_t
) x0
* c0
)) >> 32);
202 acc1
= (q31_t
) ((((q63_t
) acc1
<< 32) + ((q63_t
) x1
* c0
)) >> 32);
204 /* Decrement the loop counter */
208 /* Advance the state pointer by the decimation factor
209 * to process the next group of decimation factor number samples */
210 pState
= pState
+ S
->M
* 2;
212 /* The result is in the accumulator, store in the destination buffer. */
213 *pDst
++ = (q31_t
) (acc0
<< 1);
214 *pDst
++ = (q31_t
) (acc1
<< 1);
216 /* Decrement the loop counter */
222 /* Copy decimation factor number of new input samples into the state buffer */
227 *pStateCurnt
++ = *pSrc
++;
231 /* Set accumulator to zero */
234 /* Initialize state pointer */
237 /* Initialize coeff pointer */
240 /* Loop unrolling. Process 4 taps at a time. */
241 tapCnt
= numTaps
>> 2;
243 /* Loop over the number of taps. Unroll by a factor of 4.
244 ** Repeat until we've computed numTaps-4 coefficients. */
247 /* Read the b[numTaps-1] coefficient */
250 /* Read x[n-numTaps-1] sample */
253 /* Perform the multiply-accumulate */
254 sum0
= (q31_t
) ((((q63_t
) sum0
<< 32) + ((q63_t
) x0
* c0
)) >> 32);
256 /* Read the b[numTaps-2] coefficient */
259 /* Read x[n-numTaps-2] sample */
262 /* Perform the multiply-accumulate */
263 sum0
= (q31_t
) ((((q63_t
) sum0
<< 32) + ((q63_t
) x0
* c0
)) >> 32);
265 /* Read the b[numTaps-3] coefficient */
268 /* Read x[n-numTaps-3] sample */
271 /* Perform the multiply-accumulate */
272 sum0
= (q31_t
) ((((q63_t
) sum0
<< 32) + ((q63_t
) x0
* c0
)) >> 32);
274 /* Read the b[numTaps-4] coefficient */
277 /* Read x[n-numTaps-4] sample */
280 /* Perform the multiply-accumulate */
281 sum0
= (q31_t
) ((((q63_t
) sum0
<< 32) + ((q63_t
) x0
* c0
)) >> 32);
283 /* Decrement the loop counter */
287 /* If the filter length is not a multiple of 4, compute the remaining filter taps */
288 tapCnt
= numTaps
% 0x4u
;
292 /* Read coefficients */
295 /* Fetch 1 state variable */
298 /* Perform the multiply-accumulate */
299 sum0
= (q31_t
) ((((q63_t
) sum0
<< 32) + ((q63_t
) x0
* c0
)) >> 32);
301 /* Decrement the loop counter */
305 /* Advance the state pointer by the decimation factor
306 * to process the next group of decimation factor number samples */
307 pState
= pState
+ S
->M
;
309 /* The result is in the accumulator, store in the destination buffer. */
310 *pDst
++ = (q31_t
) (sum0
<< 1);
312 /* Decrement the loop counter */
316 /* Processing is complete.
317 ** Now copy the last numTaps - 1 samples to the satrt of the state buffer.
318 ** This prepares the state buffer for the next function call. */
320 /* Points to the start of the state buffer */
321 pStateCurnt
= S
->pState
;
323 i
= (numTaps
- 1u) >> 2u;
328 *pStateCurnt
++ = *pState
++;
329 *pStateCurnt
++ = *pState
++;
330 *pStateCurnt
++ = *pState
++;
331 *pStateCurnt
++ = *pState
++;
333 /* Decrement the loop counter */
337 i
= (numTaps
- 1u) % 0x04u
;
342 *pStateCurnt
++ = *pState
++;
344 /* Decrement the loop counter */
350 * @} end of FIR_decimate group