1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010 ARM Limited. All rights reserved.
7 * Project: CMSIS DSP Library
8 * Title: arm_fir_decimate_q15.c
10 * Description: Q15 FIR Decimator.
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
14 * Version 1.0.10 2011/7/15
15 * Big Endian support added and Merged M0 and M3/M4 Source code.
17 * Version 1.0.3 2010/11/29
18 * Re-organized the CMSIS folders and updated documentation.
20 * Version 1.0.2 2010/11/11
21 * Documentation updated.
23 * Version 1.0.1 2010/10/05
24 * Production release and review comments incorporated.
26 * Version 1.0.0 2010/09/20
27 * Production release and review comments incorporated
29 * Version 0.0.7 2010/06/10
30 * Misra-C changes done
31 * -------------------------------------------------------------------- */
36 * @ingroup groupFilters
40 * @addtogroup FIR_decimate
45 * @brief Processing function for the Q15 FIR decimator.
46 * @param[in] *S points to an instance of the Q15 FIR decimator structure.
47 * @param[in] *pSrc points to the block of input data.
48 * @param[out] *pDst points to the location where the output result is written.
49 * @param[in] blockSize number of input samples to process per call.
52 * <b>Scaling and Overflow Behavior:</b>
54 * The function is implemented using a 64-bit internal accumulator.
55 * Both coefficients and state variables are represented in 1.15 format and multiplications yield a 2.30 result.
56 * The 2.30 intermediate results are accumulated in a 64-bit accumulator in 34.30 format.
57 * There is no risk of internal overflow with this approach and the full precision of intermediate multiplications is preserved.
58 * After all additions have been performed, the accumulator is truncated to 34.15 format by discarding low 15 bits.
59 * Lastly, the accumulator is saturated to yield a result in 1.15 format.
62 * Refer to the function <code>arm_fir_decimate_fast_q15()</code> for a faster but less precise implementation of this function for Cortex-M3 and Cortex-M4.
65 void arm_fir_decimate_q15(
66 const arm_fir_decimate_instance_q15 * S,
71 q15_t *pState = S->pState; /* State pointer */
72 q15_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */
73 q15_t *pStateCurnt; /* Points to the current sample of the state */
74 q15_t *px; /* Temporary pointer for state buffer */
75 q15_t *pb; /* Temporary pointer coefficient buffer */
76 q31_t x0, c0; /* Temporary variables to hold state and coefficient values */
77 q63_t sum0; /* Accumulators */
78 uint32_t numTaps = S->numTaps; /* Number of taps */
79 uint32_t i, blkCnt, tapCnt, outBlockSize = blockSize / S->M; /* Loop counters */
83 /* Run the below code for Cortex-M4 and Cortex-M3 */
85 /* S->pState buffer contains previous frame (numTaps - 1) samples */
86 /* pStateCurnt points to the location where the new input data should be written */
87 pStateCurnt = S->pState + (numTaps - 1u);
89 /* Total number of output samples to be computed */
90 blkCnt = outBlockSize;
94 /* Copy decimation factor number of new input samples into the state buffer */
99 *pStateCurnt++ = *pSrc++;
106 /* Initialize state pointer */
109 /* Initialize coeff pointer */
112 /* Loop unrolling. Process 4 taps at a time. */
113 tapCnt = numTaps >> 2;
115 /* Loop over the number of taps. Unroll by a factor of 4.
116 ** Repeat until we've computed numTaps-4 coefficients. */
119 /* Read the Read b[numTaps-1] and b[numTaps-2] coefficients */
120 c0 = *__SIMD32(pb)++;
122 /* Read x[n-numTaps-1] and x[n-numTaps-2]sample */
123 x0 = *__SIMD32(px)++;
125 /* Perform the multiply-accumulate */
126 sum0 = __SMLALD(x0, c0, sum0);
128 /* Read the b[numTaps-3] and b[numTaps-4] coefficient */
129 c0 = *__SIMD32(pb)++;
131 /* Read x[n-numTaps-2] and x[n-numTaps-3] sample */
132 x0 = *__SIMD32(px)++;
134 /* Perform the multiply-accumulate */
135 sum0 = __SMLALD(x0, c0, sum0);
137 /* Decrement the loop counter */
141 /* If the filter length is not a multiple of 4, compute the remaining filter taps */
142 tapCnt = numTaps % 0x4u;
146 /* Read coefficients */
149 /* Fetch 1 state variable */
152 /* Perform the multiply-accumulate */
153 sum0 = __SMLALD(x0, c0, sum0);
155 /* Decrement the loop counter */
159 /* Advance the state pointer by the decimation factor
160 * to process the next group of decimation factor number samples */
161 pState = pState + S->M;
163 /* Store filter output, smlad returns the values in 2.14 format */
164 /* so downsacle by 15 to get output in 1.15 */
165 *pDst++ = (q15_t) (__SSAT((sum0 >> 15), 16));
167 /* Decrement the loop counter */
171 /* Processing is complete.
172 ** Now copy the last numTaps - 1 samples to the satrt of the state buffer.
173 ** This prepares the state buffer for the next function call. */
175 /* Points to the start of the state buffer */
176 pStateCurnt = S->pState;
178 i = (numTaps - 1u) >> 2u;
183 *__SIMD32(pStateCurnt)++ = *__SIMD32(pState)++;
184 *__SIMD32(pStateCurnt)++ = *__SIMD32(pState)++;
186 /* Decrement the loop counter */
190 i = (numTaps - 1u) % 0x04u;
195 *pStateCurnt++ = *pState++;
197 /* Decrement the loop counter */
203 /* Run the below code for Cortex-M0 */
205 /* S->pState buffer contains previous frame (numTaps - 1) samples */
206 /* pStateCurnt points to the location where the new input data should be written */
207 pStateCurnt = S->pState + (numTaps - 1u);
209 /* Total number of output samples to be computed */
210 blkCnt = outBlockSize;
214 /* Copy decimation factor number of new input samples into the state buffer */
219 *pStateCurnt++ = *pSrc++;
226 /* Initialize state pointer */
229 /* Initialize coeff pointer */
236 /* Read coefficients */
239 /* Fetch 1 state variable */
242 /* Perform the multiply-accumulate */
243 sum0 += (q31_t) x0 *c0;
245 /* Decrement the loop counter */
249 /* Advance the state pointer by the decimation factor
250 * to process the next group of decimation factor number samples */
251 pState = pState + S->M;
253 /*Store filter output , smlad will return the values in 2.14 format */
254 /* so downsacle by 15 to get output in 1.15 */
255 *pDst++ = (q15_t) (__SSAT((sum0 >> 15), 16));
257 /* Decrement the loop counter */
261 /* Processing is complete.
262 ** Now copy the last numTaps - 1 samples to the start of the state buffer.
263 ** This prepares the state buffer for the next function call. */
265 /* Points to the start of the state buffer */
266 pStateCurnt = S->pState;
273 *pStateCurnt++ = *pState++;
275 /* Decrement the loop counter */
279 #endif /* #ifndef ARM_MATH_CM0 */
284 * @} end of FIR_decimate group