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_interpolate_q31.c
10 * Description: Q31 FIR interpolation.
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_Interpolate
53 * @brief Processing function for the Q31 FIR interpolator.
54 * @param[in] *S points to an instance of the Q31 FIR interpolator 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 <code>1/(numTaps/L)</code>.
66 * since <code>numTaps/L</code> additions occur per output sample.
67 * After all multiply-accumulates are performed, the 2.62 accumulator is truncated to 1.32 format and then saturated to 1.31 format.
70 #ifndef ARM_MATH_CM0_FAMILY
72 /* Run the below code for Cortex-M4 and Cortex-M3 */
74 void arm_fir_interpolate_q31(
75 const arm_fir_interpolate_instance_q31
* S
,
80 q31_t
*pState
= S
->pState
; /* State pointer */
81 q31_t
*pCoeffs
= S
->pCoeffs
; /* Coefficient pointer */
82 q31_t
*pStateCurnt
; /* Points to the current sample of the state */
83 q31_t
*ptr1
, *ptr2
; /* Temporary pointers for state and coefficient buffers */
84 q63_t sum0
; /* Accumulators */
85 q31_t x0
, c0
; /* Temporary variables to hold state and coefficient values */
86 uint32_t i
, blkCnt
, j
; /* Loop counters */
87 uint16_t phaseLen
= S
->phaseLength
, tapCnt
; /* Length of each polyphase filter component */
93 /* S->pState buffer contains previous frame (phaseLen - 1) samples */
94 /* pStateCurnt points to the location where the new input data should be written */
95 pStateCurnt
= S
->pState
+ ((q31_t
) phaseLen
- 1);
97 /* Initialise blkCnt */
98 blkCnt
= blockSize
/ 2;
99 blkCntN2
= blockSize
- (2 * blkCnt
);
101 /* Samples loop unrolled by 2 */
104 /* Copy new input sample into the state buffer */
105 *pStateCurnt
++ = *pSrc
++;
106 *pStateCurnt
++ = *pSrc
++;
108 /* Address modifier index of coefficient buffer */
111 /* Loop over the Interpolation factor. */
116 /* Set accumulator to zero */
120 /* Initialize state pointer */
123 /* Initialize coefficient pointer */
124 ptr2
= pCoeffs
+ (S
->L
- j
);
126 /* Loop over the polyPhase length. Unroll by a factor of 4.
127 ** Repeat until we've computed numTaps-(4*S->L) coefficients. */
128 tapCnt
= phaseLen
>> 2u;
135 /* Read the input sample */
138 /* Read the coefficient */
141 /* Perform the multiply-accumulate */
142 acc0
+= (q63_t
) x0
*c0
;
143 acc1
+= (q63_t
) x1
*c0
;
146 /* Read the coefficient */
149 /* Read the input sample */
152 /* Perform the multiply-accumulate */
153 acc0
+= (q63_t
) x1
*c0
;
154 acc1
+= (q63_t
) x0
*c0
;
157 /* Read the coefficient */
158 c0
= *(ptr2
+ S
->L
* 2);
160 /* Read the input sample */
163 /* Perform the multiply-accumulate */
164 acc0
+= (q63_t
) x0
*c0
;
165 acc1
+= (q63_t
) x1
*c0
;
167 /* Read the coefficient */
168 c0
= *(ptr2
+ S
->L
* 3);
170 /* Read the input sample */
173 /* Perform the multiply-accumulate */
174 acc0
+= (q63_t
) x1
*c0
;
175 acc1
+= (q63_t
) x0
*c0
;
178 /* Upsampling is done by stuffing L-1 zeros between each sample.
179 * So instead of multiplying zeros with coefficients,
180 * Increment the coefficient pointer by interpolation factor times. */
183 /* Decrement the loop counter */
187 /* If the polyPhase length is not a multiple of 4, compute the remaining filter taps */
188 tapCnt
= phaseLen
% 0x4u
;
193 /* Read the input sample */
196 /* Read the coefficient */
199 /* Perform the multiply-accumulate */
200 acc0
+= (q63_t
) x0
*c0
;
201 acc1
+= (q63_t
) x1
*c0
;
203 /* Increment the coefficient pointer by interpolation factor times. */
206 /* update states for next sample processing */
209 /* Decrement the loop counter */
213 /* The result is in the accumulator, store in the destination buffer. */
214 *pDst
= (q31_t
) (acc0
>> 31);
215 *(pDst
+ S
->L
) = (q31_t
) (acc1
>> 31);
220 /* Increment the address modifier index of coefficient buffer */
223 /* Decrement the loop counter */
227 /* Advance the state pointer by 1
228 * to process the next group of interpolation factor number samples */
233 /* Decrement the loop counter */
237 /* If the blockSize is not a multiple of 2, compute any remaining output samples here.
238 ** No loop unrolling is used. */
241 /* Loop over the blockSize. */
244 /* Copy new input sample into the state buffer */
245 *pStateCurnt
++ = *pSrc
++;
247 /* Address modifier index of coefficient buffer */
250 /* Loop over the Interpolation factor. */
254 /* Set accumulator to zero */
257 /* Initialize state pointer */
260 /* Initialize coefficient pointer */
261 ptr2
= pCoeffs
+ (S
->L
- j
);
263 /* Loop over the polyPhase length. Unroll by a factor of 4.
264 ** Repeat until we've computed numTaps-(4*S->L) coefficients. */
265 tapCnt
= phaseLen
>> 2;
269 /* Read the coefficient */
272 /* Upsampling is done by stuffing L-1 zeros between each sample.
273 * So instead of multiplying zeros with coefficients,
274 * Increment the coefficient pointer by interpolation factor times. */
277 /* Read the input sample */
280 /* Perform the multiply-accumulate */
281 sum0
+= (q63_t
) x0
*c0
;
283 /* Read the coefficient */
286 /* Increment the coefficient pointer by interpolation factor times. */
289 /* Read the input sample */
292 /* Perform the multiply-accumulate */
293 sum0
+= (q63_t
) x0
*c0
;
295 /* Read the coefficient */
298 /* Increment the coefficient pointer by interpolation factor times. */
301 /* Read the input sample */
304 /* Perform the multiply-accumulate */
305 sum0
+= (q63_t
) x0
*c0
;
307 /* Read the coefficient */
310 /* Increment the coefficient pointer by interpolation factor times. */
313 /* Read the input sample */
316 /* Perform the multiply-accumulate */
317 sum0
+= (q63_t
) x0
*c0
;
319 /* Decrement the loop counter */
323 /* If the polyPhase length is not a multiple of 4, compute the remaining filter taps */
324 tapCnt
= phaseLen
& 0x3u
;
328 /* Read the coefficient */
331 /* Increment the coefficient pointer by interpolation factor times. */
334 /* Read the input sample */
337 /* Perform the multiply-accumulate */
338 sum0
+= (q63_t
) x0
*c0
;
340 /* Decrement the loop counter */
344 /* The result is in the accumulator, store in the destination buffer. */
345 *pDst
++ = (q31_t
) (sum0
>> 31);
347 /* Increment the address modifier index of coefficient buffer */
350 /* Decrement the loop counter */
354 /* Advance the state pointer by 1
355 * to process the next group of interpolation factor number samples */
358 /* Decrement the loop counter */
362 /* Processing is complete.
363 ** Now copy the last phaseLen - 1 samples to the satrt of the state buffer.
364 ** This prepares the state buffer for the next function call. */
366 /* Points to the start of the state buffer */
367 pStateCurnt
= S
->pState
;
369 tapCnt
= (phaseLen
- 1u) >> 2u;
374 *pStateCurnt
++ = *pState
++;
375 *pStateCurnt
++ = *pState
++;
376 *pStateCurnt
++ = *pState
++;
377 *pStateCurnt
++ = *pState
++;
379 /* Decrement the loop counter */
383 tapCnt
= (phaseLen
- 1u) % 0x04u
;
388 *pStateCurnt
++ = *pState
++;
390 /* Decrement the loop counter */
399 void arm_fir_interpolate_q31(
400 const arm_fir_interpolate_instance_q31
* S
,
405 q31_t
*pState
= S
->pState
; /* State pointer */
406 q31_t
*pCoeffs
= S
->pCoeffs
; /* Coefficient pointer */
407 q31_t
*pStateCurnt
; /* Points to the current sample of the state */
408 q31_t
*ptr1
, *ptr2
; /* Temporary pointers for state and coefficient buffers */
410 /* Run the below code for Cortex-M0 */
412 q63_t sum
; /* Accumulator */
413 q31_t x0
, c0
; /* Temporary variables to hold state and coefficient values */
414 uint32_t i
, blkCnt
; /* Loop counters */
415 uint16_t phaseLen
= S
->phaseLength
, tapCnt
; /* Length of each polyphase filter component */
418 /* S->pState buffer contains previous frame (phaseLen - 1) samples */
419 /* pStateCurnt points to the location where the new input data should be written */
420 pStateCurnt
= S
->pState
+ ((q31_t
) phaseLen
- 1);
422 /* Total number of intput samples */
425 /* Loop over the blockSize. */
428 /* Copy new input sample into the state buffer */
429 *pStateCurnt
++ = *pSrc
++;
431 /* Loop over the Interpolation factor. */
436 /* Set accumulator to zero */
439 /* Initialize state pointer */
442 /* Initialize coefficient pointer */
443 ptr2
= pCoeffs
+ (i
- 1u);
449 /* Read the coefficient */
452 /* Increment the coefficient pointer by interpolation factor times. */
455 /* Read the input sample */
458 /* Perform the multiply-accumulate */
459 sum
+= (q63_t
) x0
*c0
;
461 /* Decrement the loop counter */
465 /* The result is in the accumulator, store in the destination buffer. */
466 *pDst
++ = (q31_t
) (sum
>> 31);
468 /* Decrement the loop counter */
472 /* Advance the state pointer by 1
473 * to process the next group of interpolation factor number samples */
476 /* Decrement the loop counter */
480 /* Processing is complete.
481 ** Now copy the last phaseLen - 1 samples to the satrt of the state buffer.
482 ** This prepares the state buffer for the next function call. */
484 /* Points to the start of the state buffer */
485 pStateCurnt
= S
->pState
;
487 tapCnt
= phaseLen
- 1u;
492 *pStateCurnt
++ = *pState
++;
494 /* Decrement the loop counter */
500 #endif /* #ifndef ARM_MATH_CM0_FAMILY */
503 * @} end of FIR_Interpolate group