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_q31.c
10 * Description: Q31 FIR Decimator.
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 * @addtogroup FIR_decimate
53 * @brief Processing function for the Q31 FIR decimator.
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>
62 * The function is implemented using an internal 64-bit accumulator.
63 * The accumulator has a 2.62 format and maintains full precision of the intermediate multiplication results but provides only a single guard bit.
64 * Thus, if the accumulator result overflows it wraps around rather than clip.
65 * 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).
66 * After all multiply-accumulates are performed, the 2.62 accumulator is truncated to 1.32 format and then saturated to 1.31 format.
69 * Refer to the function <code>arm_fir_decimate_fast_q31()</code> for a faster but less precise implementation of this function for Cortex-M3 and Cortex-M4.
72 void arm_fir_decimate_q31(
73 const arm_fir_decimate_instance_q31
* S
,
78 q31_t
*pState
= S
->pState
; /* State pointer */
79 q31_t
*pCoeffs
= S
->pCoeffs
; /* Coefficient pointer */
80 q31_t
*pStateCurnt
; /* Points to the current sample of the state */
81 q31_t x0
, c0
; /* Temporary variables to hold state and coefficient values */
82 q31_t
*px
; /* Temporary pointers for state buffer */
83 q31_t
*pb
; /* Temporary pointers for coefficient buffer */
84 q63_t sum0
; /* Accumulator */
85 uint32_t numTaps
= S
->numTaps
; /* Number of taps */
86 uint32_t i
, tapCnt
, blkCnt
, outBlockSize
= blockSize
/ S
->M
; /* Loop counters */
89 #ifndef ARM_MATH_CM0_FAMILY
91 /* Run the below code for Cortex-M4 and Cortex-M3 */
93 /* S->pState buffer contains previous frame (numTaps - 1) samples */
94 /* pStateCurnt points to the location where the new input data should be written */
95 pStateCurnt
= S
->pState
+ (numTaps
- 1u);
97 /* Total number of output samples to be computed */
98 blkCnt
= outBlockSize
;
102 /* Copy decimation factor number of new input samples into the state buffer */
107 *pStateCurnt
++ = *pSrc
++;
111 /* Set accumulator to zero */
114 /* Initialize state pointer */
117 /* Initialize coeff pointer */
120 /* Loop unrolling. Process 4 taps at a time. */
121 tapCnt
= numTaps
>> 2;
123 /* Loop over the number of taps. Unroll by a factor of 4.
124 ** Repeat until we've computed numTaps-4 coefficients. */
127 /* Read the b[numTaps-1] coefficient */
130 /* Read x[n-numTaps-1] sample */
133 /* Perform the multiply-accumulate */
134 sum0
+= (q63_t
) x0
*c0
;
136 /* Read the b[numTaps-2] coefficient */
139 /* Read x[n-numTaps-2] sample */
142 /* Perform the multiply-accumulate */
143 sum0
+= (q63_t
) x0
*c0
;
145 /* Read the b[numTaps-3] coefficient */
148 /* Read x[n-numTaps-3] sample */
151 /* Perform the multiply-accumulate */
152 sum0
+= (q63_t
) x0
*c0
;
154 /* Read the b[numTaps-4] coefficient */
157 /* Read x[n-numTaps-4] sample */
160 /* Perform the multiply-accumulate */
161 sum0
+= (q63_t
) x0
*c0
;
163 /* Decrement the loop counter */
167 /* If the filter length is not a multiple of 4, compute the remaining filter taps */
168 tapCnt
= numTaps
% 0x4u
;
172 /* Read coefficients */
175 /* Fetch 1 state variable */
178 /* Perform the multiply-accumulate */
179 sum0
+= (q63_t
) x0
*c0
;
181 /* Decrement the loop counter */
185 /* Advance the state pointer by the decimation factor
186 * to process the next group of decimation factor number samples */
187 pState
= pState
+ S
->M
;
189 /* The result is in the accumulator, store in the destination buffer. */
190 *pDst
++ = (q31_t
) (sum0
>> 31);
192 /* Decrement the loop counter */
196 /* Processing is complete.
197 ** Now copy the last numTaps - 1 samples to the satrt of the state buffer.
198 ** This prepares the state buffer for the next function call. */
200 /* Points to the start of the state buffer */
201 pStateCurnt
= S
->pState
;
203 i
= (numTaps
- 1u) >> 2u;
208 *pStateCurnt
++ = *pState
++;
209 *pStateCurnt
++ = *pState
++;
210 *pStateCurnt
++ = *pState
++;
211 *pStateCurnt
++ = *pState
++;
213 /* Decrement the loop counter */
217 i
= (numTaps
- 1u) % 0x04u
;
222 *pStateCurnt
++ = *pState
++;
224 /* Decrement the loop counter */
230 /* Run the below code for Cortex-M0 */
232 /* S->pState buffer contains previous frame (numTaps - 1) samples */
233 /* pStateCurnt points to the location where the new input data should be written */
234 pStateCurnt
= S
->pState
+ (numTaps
- 1u);
236 /* Total number of output samples to be computed */
237 blkCnt
= outBlockSize
;
241 /* Copy decimation factor number of new input samples into the state buffer */
246 *pStateCurnt
++ = *pSrc
++;
250 /* Set accumulator to zero */
253 /* Initialize state pointer */
256 /* Initialize coeff pointer */
263 /* Read coefficients */
266 /* Fetch 1 state variable */
269 /* Perform the multiply-accumulate */
270 sum0
+= (q63_t
) x0
*c0
;
272 /* Decrement the loop counter */
276 /* Advance the state pointer by the decimation factor
277 * to process the next group of decimation factor number samples */
278 pState
= pState
+ S
->M
;
280 /* The result is in the accumulator, store in the destination buffer. */
281 *pDst
++ = (q31_t
) (sum0
>> 31);
283 /* Decrement the loop counter */
287 /* Processing is complete.
288 ** Now copy the last numTaps - 1 samples to the start of the state buffer.
289 ** This prepares the state buffer for the next function call. */
291 /* Points to the start of the state buffer */
292 pStateCurnt
= S
->pState
;
299 *pStateCurnt
++ = *pState
++;
301 /* Decrement the loop counter */
305 #endif /* #ifndef ARM_MATH_CM0_FAMILY */
310 * @} end of FIR_decimate group