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_q15.c
10 * Description: Q15 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 Q15 FIR interpolator.
54 * @param[in] *S points to an instance of the Q15 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 a 64-bit internal accumulator.
63 * Both coefficients and state variables are represented in 1.15 format and multiplications yield a 2.30 result.
64 * The 2.30 intermediate results are accumulated in a 64-bit accumulator in 34.30 format.
65 * There is no risk of internal overflow with this approach and the full precision of intermediate multiplications is preserved.
66 * After all additions have been performed, the accumulator is truncated to 34.15 format by discarding low 15 bits.
67 * Lastly, the accumulator is saturated to yield a result in 1.15 format.
70 #ifndef ARM_MATH_CM0_FAMILY
72 /* Run the below code for Cortex-M4 and Cortex-M3 */
74 void arm_fir_interpolate_q15(
75 const arm_fir_interpolate_instance_q15
* S
,
80 q15_t
*pState
= S
->pState
; /* State pointer */
81 q15_t
*pCoeffs
= S
->pCoeffs
; /* Coefficient pointer */
82 q15_t
*pStateCurnt
; /* Points to the current sample of the state */
83 q15_t
*ptr1
, *ptr2
; /* Temporary pointers for state and coefficient buffers */
84 q63_t sum0
; /* Accumulators */
85 q15_t x0
, c0
; /* Temporary variables to hold state and coefficient values */
86 uint32_t i
, blkCnt
, j
, tapCnt
; /* Loop counters */
87 uint16_t phaseLen
= S
->phaseLength
; /* Length of each polyphase filter component */
92 /* S->pState buffer contains previous frame (phaseLen - 1) samples */
93 /* pStateCurnt points to the location where the new input data should be written */
94 pStateCurnt
= S
->pState
+ ((q31_t
) phaseLen
- 1);
96 /* Initialise blkCnt */
97 blkCnt
= blockSize
/ 2;
98 blkCntN2
= blockSize
- (2 * blkCnt
);
100 /* Samples loop unrolled by 2 */
103 /* Copy new input sample into the state buffer */
104 *pStateCurnt
++ = *pSrc
++;
105 *pStateCurnt
++ = *pSrc
++;
107 /* Address modifier index of coefficient buffer */
110 /* Loop over the Interpolation factor. */
115 /* Set accumulator to zero */
119 /* Initialize state pointer */
122 /* Initialize coefficient pointer */
123 ptr2
= pCoeffs
+ (S
->L
- j
);
125 /* Loop over the polyPhase length. Unroll by a factor of 4.
126 ** Repeat until we've computed numTaps-(4*S->L) coefficients. */
127 tapCnt
= phaseLen
>> 2u;
134 /* Read the input sample */
137 /* Read the coefficient */
140 /* Perform the multiply-accumulate */
141 acc0
+= (q63_t
) x0
*c0
;
142 acc1
+= (q63_t
) x1
*c0
;
145 /* Read the coefficient */
148 /* Read the input sample */
151 /* Perform the multiply-accumulate */
152 acc0
+= (q63_t
) x1
*c0
;
153 acc1
+= (q63_t
) x0
*c0
;
156 /* Read the coefficient */
157 c0
= *(ptr2
+ S
->L
* 2);
159 /* Read the input sample */
162 /* Perform the multiply-accumulate */
163 acc0
+= (q63_t
) x0
*c0
;
164 acc1
+= (q63_t
) x1
*c0
;
166 /* Read the coefficient */
167 c0
= *(ptr2
+ S
->L
* 3);
169 /* Read the input sample */
172 /* Perform the multiply-accumulate */
173 acc0
+= (q63_t
) x1
*c0
;
174 acc1
+= (q63_t
) x0
*c0
;
177 /* Upsampling is done by stuffing L-1 zeros between each sample.
178 * So instead of multiplying zeros with coefficients,
179 * Increment the coefficient pointer by interpolation factor times. */
182 /* Decrement the loop counter */
186 /* If the polyPhase length is not a multiple of 4, compute the remaining filter taps */
187 tapCnt
= phaseLen
% 0x4u
;
192 /* Read the input sample */
195 /* Read the coefficient */
198 /* Perform the multiply-accumulate */
199 acc0
+= (q63_t
) x0
*c0
;
200 acc1
+= (q63_t
) x1
*c0
;
202 /* Increment the coefficient pointer by interpolation factor times. */
205 /* update states for next sample processing */
208 /* Decrement the loop counter */
212 /* The result is in the accumulator, store in the destination buffer. */
213 *pDst
= (q15_t
) (__SSAT((acc0
>> 15), 16));
214 *(pDst
+ S
->L
) = (q15_t
) (__SSAT((acc1
>> 15), 16));
218 /* Increment the address modifier index of coefficient buffer */
221 /* Decrement the loop counter */
225 /* Advance the state pointer by 1
226 * to process the next group of interpolation factor number samples */
231 /* Decrement the loop counter */
235 /* If the blockSize is not a multiple of 2, compute any remaining output samples here.
236 ** No loop unrolling is used. */
239 /* Loop over the blockSize. */
242 /* Copy new input sample into the state buffer */
243 *pStateCurnt
++ = *pSrc
++;
245 /* Address modifier index of coefficient buffer */
248 /* Loop over the Interpolation factor. */
252 /* Set accumulator to zero */
255 /* Initialize state pointer */
258 /* Initialize coefficient pointer */
259 ptr2
= pCoeffs
+ (S
->L
- j
);
261 /* Loop over the polyPhase length. Unroll by a factor of 4.
262 ** Repeat until we've computed numTaps-(4*S->L) coefficients. */
263 tapCnt
= phaseLen
>> 2;
267 /* Read the coefficient */
270 /* Upsampling is done by stuffing L-1 zeros between each sample.
271 * So instead of multiplying zeros with coefficients,
272 * Increment the coefficient pointer by interpolation factor times. */
275 /* Read the input sample */
278 /* Perform the multiply-accumulate */
279 sum0
+= (q63_t
) x0
*c0
;
281 /* Read the coefficient */
284 /* Increment the coefficient pointer by interpolation factor times. */
287 /* Read the input sample */
290 /* Perform the multiply-accumulate */
291 sum0
+= (q63_t
) x0
*c0
;
293 /* Read the coefficient */
296 /* Increment the coefficient pointer by interpolation factor times. */
299 /* Read the input sample */
302 /* Perform the multiply-accumulate */
303 sum0
+= (q63_t
) x0
*c0
;
305 /* Read the coefficient */
308 /* Increment the coefficient pointer by interpolation factor times. */
311 /* Read the input sample */
314 /* Perform the multiply-accumulate */
315 sum0
+= (q63_t
) x0
*c0
;
317 /* Decrement the loop counter */
321 /* If the polyPhase length is not a multiple of 4, compute the remaining filter taps */
322 tapCnt
= phaseLen
& 0x3u
;
326 /* Read the coefficient */
329 /* Increment the coefficient pointer by interpolation factor times. */
332 /* Read the input sample */
335 /* Perform the multiply-accumulate */
336 sum0
+= (q63_t
) x0
*c0
;
338 /* Decrement the loop counter */
342 /* The result is in the accumulator, store in the destination buffer. */
343 *pDst
++ = (q15_t
) (__SSAT((sum0
>> 15), 16));
347 /* Decrement the loop counter */
351 /* Advance the state pointer by 1
352 * to process the next group of interpolation factor number samples */
355 /* Decrement the loop counter */
360 /* Processing is complete.
361 ** Now copy the last phaseLen - 1 samples to the satrt of the state buffer.
362 ** This prepares the state buffer for the next function call. */
364 /* Points to the start of the state buffer */
365 pStateCurnt
= S
->pState
;
367 i
= ((uint32_t) phaseLen
- 1u) >> 2u;
372 #ifndef UNALIGNED_SUPPORT_DISABLE
374 *__SIMD32(pStateCurnt
)++ = *__SIMD32(pState
)++;
375 *__SIMD32(pStateCurnt
)++ = *__SIMD32(pState
)++;
379 *pStateCurnt
++ = *pState
++;
380 *pStateCurnt
++ = *pState
++;
381 *pStateCurnt
++ = *pState
++;
382 *pStateCurnt
++ = *pState
++;
384 #endif /* #ifndef UNALIGNED_SUPPORT_DISABLE */
386 /* Decrement the loop counter */
390 i
= ((uint32_t) phaseLen
- 1u) % 0x04u
;
394 *pStateCurnt
++ = *pState
++;
396 /* Decrement the loop counter */
403 /* Run the below code for Cortex-M0 */
405 void arm_fir_interpolate_q15(
406 const arm_fir_interpolate_instance_q15
* S
,
411 q15_t
*pState
= S
->pState
; /* State pointer */
412 q15_t
*pCoeffs
= S
->pCoeffs
; /* Coefficient pointer */
413 q15_t
*pStateCurnt
; /* Points to the current sample of the state */
414 q15_t
*ptr1
, *ptr2
; /* Temporary pointers for state and coefficient buffers */
415 q63_t sum
; /* Accumulator */
416 q15_t x0
, c0
; /* Temporary variables to hold state and coefficient values */
417 uint32_t i
, blkCnt
, tapCnt
; /* Loop counters */
418 uint16_t phaseLen
= S
->phaseLength
; /* Length of each polyphase filter component */
421 /* S->pState buffer contains previous frame (phaseLen - 1) samples */
422 /* pStateCurnt points to the location where the new input data should be written */
423 pStateCurnt
= S
->pState
+ (phaseLen
- 1u);
425 /* Total number of intput samples */
428 /* Loop over the blockSize. */
431 /* Copy new input sample into the state buffer */
432 *pStateCurnt
++ = *pSrc
++;
434 /* Loop over the Interpolation factor. */
439 /* Set accumulator to zero */
442 /* Initialize state pointer */
445 /* Initialize coefficient pointer */
446 ptr2
= pCoeffs
+ (i
- 1u);
448 /* Loop over the polyPhase length */
449 tapCnt
= (uint32_t) phaseLen
;
453 /* Read the coefficient */
456 /* Increment the coefficient pointer by interpolation factor times. */
459 /* Read the input sample */
462 /* Perform the multiply-accumulate */
463 sum
+= ((q31_t
) x0
* c0
);
465 /* Decrement the loop counter */
469 /* Store the result after converting to 1.15 format in the destination buffer */
470 *pDst
++ = (q15_t
) (__SSAT((sum
>> 15), 16));
472 /* Decrement the loop counter */
476 /* Advance the state pointer by 1
477 * to process the next group of interpolation factor number samples */
480 /* Decrement the loop counter */
484 /* Processing is complete.
485 ** Now copy the last phaseLen - 1 samples to the start of the state buffer.
486 ** This prepares the state buffer for the next function call. */
488 /* Points to the start of the state buffer */
489 pStateCurnt
= S
->pState
;
491 i
= (uint32_t) phaseLen
- 1u;
495 *pStateCurnt
++ = *pState
++;
497 /* Decrement the loop counter */
503 #endif /* #ifndef ARM_MATH_CM0_FAMILY */
507 * @} end of FIR_Interpolate group