1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010 ARM Limited. All rights reserved.
7 * Project: CMSIS DSP Library
8 * Title: arm_fir_interpolate_f32.c
10 * Description: FIR interpolation for floating-point sequences.
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 * @defgroup FIR_Interpolate Finite Impulse Response (FIR) Interpolator
38 * These functions combine an upsampler (zero stuffer) and an FIR filter.
39 * They are used in multirate systems for increasing the sample rate of a signal without introducing high frequency images.
40 * Conceptually, the functions are equivalent to the block diagram below:
41 * \image html FIRInterpolator.gif "Components included in the FIR Interpolator functions"
42 * After upsampling by a factor of <code>L</code>, the signal should be filtered by a lowpass filter with a normalized
43 * cutoff frequency of <code>1/L</code> in order to eliminate high frequency copies of the spectrum.
44 * The user of the function is responsible for providing the filter coefficients.
46 * The FIR interpolator functions provided in the CMSIS DSP Library combine the upsampler and FIR filter in an efficient manner.
47 * The upsampler inserts <code>L-1</code> zeros between each sample.
48 * Instead of multiplying by these zero values, the FIR filter is designed to skip them.
49 * This leads to an efficient implementation without any wasted effort.
50 * The functions operate on blocks of input and output data.
51 * <code>pSrc</code> points to an array of <code>blockSize</code> input values and
52 * <code>pDst</code> points to an array of <code>blockSize*L</code> output values.
54 * The library provides separate functions for Q15, Q31, and floating-point data types.
57 * The functions use a polyphase filter structure:
59 * y[n] = b[0] * x[n] + b[L] * x[n-1] + ... + b[L*(phaseLength-1)] * x[n-phaseLength+1]
60 * y[n+1] = b[1] * x[n] + b[L+1] * x[n-1] + ... + b[L*(phaseLength-1)+1] * x[n-phaseLength+1]
62 * y[n+(L-1)] = b[L-1] * x[n] + b[2*L-1] * x[n-1] + ....+ b[L*(phaseLength-1)+(L-1)] * x[n-phaseLength+1]
64 * This approach is more efficient than straightforward upsample-then-filter algorithms.
65 * With this method the computation is reduced by a factor of <code>1/L</code> when compared to using a standard FIR filter.
67 * <code>pCoeffs</code> points to a coefficient array of size <code>numTaps</code>.
68 * <code>numTaps</code> must be a multiple of the interpolation factor <code>L</code> and this is checked by the
69 * initialization functions.
70 * Internally, the function divides the FIR filter's impulse response into shorter filters of length
71 * <code>phaseLength=numTaps/L</code>.
72 * Coefficients are stored in time reversed order.
75 * {b[numTaps-1], b[numTaps-2], b[N-2], ..., b[1], b[0]}
78 * <code>pState</code> points to a state array of size <code>blockSize + phaseLength - 1</code>.
79 * Samples in the state buffer are stored in the order:
82 * {x[n-phaseLength+1], x[n-phaseLength], x[n-phaseLength-1], x[n-phaseLength-2]....x[0], x[1], ..., x[blockSize-1]}
84 * The state variables are updated after each block of data is processed, the coefficients are untouched.
86 * \par Instance Structure
87 * The coefficients and state variables for a filter are stored together in an instance data structure.
88 * A separate instance structure must be defined for each filter.
89 * Coefficient arrays may be shared among several instances while state variable array should be allocated separately.
90 * There are separate instance structure declarations for each of the 3 supported data types.
92 * \par Initialization Functions
93 * There is also an associated initialization function for each data type.
94 * The initialization function performs the following operations:
95 * - Sets the values of the internal structure fields.
96 * - Zeros out the values in the state buffer.
97 * - Checks to make sure that the length of the filter is a multiple of the interpolation factor.
100 * Use of the initialization function is optional.
101 * However, if the initialization function is used, then the instance structure cannot be placed into a const data section.
102 * To place an instance structure into a const data section, the instance structure must be manually initialized.
103 * The code below statically initializes each of the 3 different data type filter instance structures
105 * arm_fir_interpolate_instance_f32 S = {L, phaseLength, pCoeffs, pState};
106 * arm_fir_interpolate_instance_q31 S = {L, phaseLength, pCoeffs, pState};
107 * arm_fir_interpolate_instance_q15 S = {L, phaseLength, pCoeffs, pState};
109 * where <code>L</code> is the interpolation factor; <code>phaseLength=numTaps/L</code> is the
110 * length of each of the shorter FIR filters used internally,
111 * <code>pCoeffs</code> is the address of the coefficient buffer;
112 * <code>pState</code> is the address of the state buffer.
113 * Be sure to set the values in the state buffer to zeros when doing static initialization.
115 * \par Fixed-Point Behavior
116 * Care must be taken when using the fixed-point versions of the FIR interpolate filter functions.
117 * In particular, the overflow and saturation behavior of the accumulator used in each function must be considered.
118 * Refer to the function specific documentation below for usage guidelines.
122 * @addtogroup FIR_Interpolate
127 * @brief Processing function for the floating-point FIR interpolator.
128 * @param[in] *S points to an instance of the floating-point FIR interpolator structure.
129 * @param[in] *pSrc points to the block of input data.
130 * @param[out] *pDst points to the block of output data.
131 * @param[in] blockSize number of input samples to process per call.
135 void arm_fir_interpolate_f32(
136 const arm_fir_interpolate_instance_f32 * S,
141 float32_t *pState = S->pState; /* State pointer */
142 float32_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */
143 float32_t *pStateCurnt; /* Points to the current sample of the state */
144 float32_t *ptr1, *ptr2; /* Temporary pointers for state and coefficient buffers */
149 /* Run the below code for Cortex-M4 and Cortex-M3 */
151 float32_t sum0; /* Accumulators */
152 float32_t x0, c0; /* Temporary variables to hold state and coefficient values */
153 uint32_t i, blkCnt, j; /* Loop counters */
154 uint16_t phaseLen = S->phaseLength, tapCnt; /* Length of each polyphase filter component */
157 /* S->pState buffer contains previous frame (phaseLen - 1) samples */
158 /* pStateCurnt points to the location where the new input data should be written */
159 pStateCurnt = S->pState + (phaseLen - 1u);
161 /* Total number of intput samples */
164 /* Loop over the blockSize. */
167 /* Copy new input sample into the state buffer */
168 *pStateCurnt++ = *pSrc++;
170 /* Address modifier index of coefficient buffer */
173 /* Loop over the Interpolation factor. */
177 /* Set accumulator to zero */
180 /* Initialize state pointer */
183 /* Initialize coefficient pointer */
184 ptr2 = pCoeffs + (S->L - j);
186 /* Loop over the polyPhase length. Unroll by a factor of 4.
187 ** Repeat until we've computed numTaps-(4*S->L) coefficients. */
188 tapCnt = phaseLen >> 2u;
192 /* Read the coefficient */
195 /* Upsampling is done by stuffing L-1 zeros between each sample.
196 * So instead of multiplying zeros with coefficients,
197 * Increment the coefficient pointer by interpolation factor times. */
200 /* Read the input sample */
203 /* Perform the multiply-accumulate */
206 /* Read the coefficient */
209 /* Increment the coefficient pointer by interpolation factor times. */
212 /* Read the input sample */
215 /* Perform the multiply-accumulate */
218 /* Read the coefficient */
221 /* Increment the coefficient pointer by interpolation factor times. */
224 /* Read the input sample */
227 /* Perform the multiply-accumulate */
230 /* Read the coefficient */
233 /* Increment the coefficient pointer by interpolation factor times. */
236 /* Read the input sample */
239 /* Perform the multiply-accumulate */
242 /* Decrement the loop counter */
246 /* If the polyPhase length is not a multiple of 4, compute the remaining filter taps */
247 tapCnt = phaseLen % 0x4u;
251 /* Perform the multiply-accumulate */
252 sum0 += *(ptr1++) * (*ptr2);
254 /* Increment the coefficient pointer by interpolation factor times. */
257 /* Decrement the loop counter */
261 /* The result is in the accumulator, store in the destination buffer. */
264 /* Increment the address modifier index of coefficient buffer */
267 /* Decrement the loop counter */
271 /* Advance the state pointer by 1
272 * to process the next group of interpolation factor number samples */
275 /* Decrement the loop counter */
279 /* Processing is complete.
280 ** Now copy the last phaseLen - 1 samples to the satrt of the state buffer.
281 ** This prepares the state buffer for the next function call. */
283 /* Points to the start of the state buffer */
284 pStateCurnt = S->pState;
286 tapCnt = (phaseLen - 1u) >> 2u;
291 *pStateCurnt++ = *pState++;
292 *pStateCurnt++ = *pState++;
293 *pStateCurnt++ = *pState++;
294 *pStateCurnt++ = *pState++;
296 /* Decrement the loop counter */
300 tapCnt = (phaseLen - 1u) % 0x04u;
304 *pStateCurnt++ = *pState++;
306 /* Decrement the loop counter */
312 /* Run the below code for Cortex-M0 */
314 float32_t sum; /* Accumulator */
315 uint32_t i, blkCnt; /* Loop counters */
316 uint16_t phaseLen = S->phaseLength, tapCnt; /* Length of each polyphase filter component */
319 /* S->pState buffer contains previous frame (phaseLen - 1) samples */
320 /* pStateCurnt points to the location where the new input data should be written */
321 pStateCurnt = S->pState + (phaseLen - 1u);
323 /* Total number of intput samples */
326 /* Loop over the blockSize. */
329 /* Copy new input sample into the state buffer */
330 *pStateCurnt++ = *pSrc++;
332 /* Loop over the Interpolation factor. */
337 /* Set accumulator to zero */
340 /* Initialize state pointer */
343 /* Initialize coefficient pointer */
344 ptr2 = pCoeffs + (i - 1u);
346 /* Loop over the polyPhase length */
351 /* Perform the multiply-accumulate */
352 sum += *ptr1++ * *ptr2;
354 /* Increment the coefficient pointer by interpolation factor times. */
357 /* Decrement the loop counter */
361 /* The result is in the accumulator, store in the destination buffer. */
364 /* Decrement the loop counter */
368 /* Advance the state pointer by 1
369 * to process the next group of interpolation factor number samples */
372 /* Decrement the loop counter */
376 /* Processing is complete.
377 ** Now copy the last phaseLen - 1 samples to the start of the state buffer.
378 ** This prepares the state buffer for the next function call. */
380 /* Points to the start of the state buffer */
381 pStateCurnt = S->pState;
383 tapCnt = phaseLen - 1u;
387 *pStateCurnt++ = *pState++;
389 /* Decrement the loop counter */
393 #endif /* #ifndef ARM_MATH_CM0 */
398 * @} end of FIR_Interpolate group