1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010 ARM Limited. All rights reserved.
7 * Project: CMSIS DSP Library
8 * Title: arm_iir_lattice_f32.c
10 * Description: Floating-point IIR Lattice filter processing function.
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 * @defgroup IIR_Lattice Infinite Impulse Response (IIR) Lattice Filters
42 * This set of functions implements lattice filters
43 * for Q15, Q31 and floating-point data types. Lattice filters are used in a
44 * variety of adaptive filter applications. The filter structure has feedforward and
45 * feedback components and the net impulse response is infinite length.
46 * The functions operate on blocks
47 * of input and output data and each call to the function processes
48 * <code>blockSize</code> samples through the filter. <code>pSrc</code> and
49 * <code>pDst</code> point to input and output arrays containing <code>blockSize</code> values.
52 * \image html IIRLattice.gif "Infinite Impulse Response Lattice filter"
55 * fm-1(n) = fm(n) - km * gm-1(n-1) for m = N, N-1, ...1
56 * gm(n) = km * fm-1(n) + gm-1(n-1) for m = N, N-1, ...1
57 * y(n) = vN * gN(n) + vN-1 * gN-1(n) + ...+ v0 * g0(n)
60 * <code>pkCoeffs</code> points to array of reflection coefficients of size <code>numStages</code>.
61 * Reflection coefficients are stored in time-reversed order.
66 * <code>pvCoeffs</code> points to the array of ladder coefficients of size <code>(numStages+1)</code>.
67 * Ladder coefficients are stored in time-reversed order.
72 * <code>pState</code> points to a state array of size <code>numStages + blockSize</code>.
73 * The state variables shown in the figure above (the g values) are stored in the <code>pState</code> array.
74 * The state variables are updated after each block of data is processed; the coefficients are untouched.
75 * \par Instance Structure
76 * The coefficients and state variables for a filter are stored together in an instance data structure.
77 * A separate instance structure must be defined for each filter.
78 * Coefficient arrays may be shared among several instances while state variable arrays cannot be shared.
79 * There are separate instance structure declarations for each of the 3 supported data types.
81 * \par Initialization Functions
82 * There is also an associated initialization function for each data type.
83 * The initialization function performs the following operations:
84 * - Sets the values of the internal structure fields.
85 * - Zeros out the values in the state buffer.
88 * Use of the initialization function is optional.
89 * However, if the initialization function is used, then the instance structure cannot be placed into a const data section.
90 * To place an instance structure into a const data section, the instance structure must be manually initialized.
91 * Set the values in the state buffer to zeros and then manually initialize the instance structure as follows:
93 *arm_iir_lattice_instance_f32 S = {numStages, pState, pkCoeffs, pvCoeffs};
94 *arm_iir_lattice_instance_q31 S = {numStages, pState, pkCoeffs, pvCoeffs};
95 *arm_iir_lattice_instance_q15 S = {numStages, pState, pkCoeffs, pvCoeffs};
98 * where <code>numStages</code> is the number of stages in the filter; <code>pState</code> points to the state buffer array;
99 * <code>pkCoeffs</code> points to array of the reflection coefficients; <code>pvCoeffs</code> points to the array of ladder coefficients.
100 * \par Fixed-Point Behavior
101 * Care must be taken when using the fixed-point versions of the IIR lattice filter functions.
102 * In particular, the overflow and saturation behavior of the accumulator used in each function must be considered.
103 * Refer to the function specific documentation below for usage guidelines.
107 * @addtogroup IIR_Lattice
112 * @brief Processing function for the floating-point IIR lattice filter.
113 * @param[in] *S points to an instance of the floating-point IIR lattice structure.
114 * @param[in] *pSrc points to the block of input data.
115 * @param[out] *pDst points to the block of output data.
116 * @param[in] blockSize number of samples to process.
120 void arm_iir_lattice_f32(
121 const arm_iir_lattice_instance_f32 * S,
126 float32_t fcurr, fnext = 0, gcurr, gnext; /* Temporary variables for lattice stages */
127 float32_t acc; /* Accumlator */
128 uint32_t blkCnt, tapCnt; /* temporary variables for counts */
129 float32_t *px1, *px2, *pk, *pv; /* temporary pointers for state and coef */
130 uint32_t numStages = S->numStages; /* number of stages */
131 float32_t *pState; /* State pointer */
132 float32_t *pStateCurnt; /* State current pointer */
137 /* Run the below code for Cortex-M4 and Cortex-M3 */
142 pState = &S->pState[0];
144 /* Sample processing */
147 /* Read Sample from input buffer */
151 /* Initialize state read pointer */
153 /* Initialize state write pointer */
155 /* Set accumulator to zero */
157 /* Initialize Ladder coeff pointer */
158 pv = &S->pvCoeffs[0];
159 /* Initialize Reflection coeff pointer */
160 pk = &S->pkCoeffs[0];
163 /* Process sample for first tap */
165 /* fN-1(n) = fN(n) - kN * gN-1(n-1) */
166 fnext = fcurr - ((*pk) * gcurr);
167 /* gN(n) = kN * fN-1(n) + gN-1(n-1) */
168 gnext = (fnext * (*pk++)) + gcurr;
169 /* write gN(n) into state for next sample processing */
171 /* y(n) += gN(n) * vN */
172 acc += (gnext * (*pv++));
174 /* Update f values for next coefficient processing */
177 /* Loop unrolling. Process 4 taps at a time. */
178 tapCnt = (numStages - 1u) >> 2;
182 /* Process sample for 2nd, 6th ...taps */
183 /* Read gN-2(n-1) from state buffer */
185 /* Process sample for 2nd, 6th .. taps */
186 /* fN-2(n) = fN-1(n) - kN-1 * gN-2(n-1) */
187 fnext = fcurr - ((*pk) * gcurr);
188 /* gN-1(n) = kN-1 * fN-2(n) + gN-2(n-1) */
189 gnext = (fnext * (*pk++)) + gcurr;
190 /* y(n) += gN-1(n) * vN-1 */
191 /* process for gN-5(n) * vN-5, gN-9(n) * vN-9 ... */
192 acc += (gnext * (*pv++));
193 /* write gN-1(n) into state for next sample processing */
197 /* Process sample for 3nd, 7th ...taps */
198 /* Read gN-3(n-1) from state buffer */
200 /* Process sample for 3rd, 7th .. taps */
201 /* fN-3(n) = fN-2(n) - kN-2 * gN-3(n-1) */
202 fcurr = fnext - ((*pk) * gcurr);
203 /* gN-2(n) = kN-2 * fN-3(n) + gN-3(n-1) */
204 gnext = (fcurr * (*pk++)) + gcurr;
205 /* y(n) += gN-2(n) * vN-2 */
206 /* process for gN-6(n) * vN-6, gN-10(n) * vN-10 ... */
207 acc += (gnext * (*pv++));
208 /* write gN-2(n) into state for next sample processing */
212 /* Process sample for 4th, 8th ...taps */
213 /* Read gN-4(n-1) from state buffer */
215 /* Process sample for 4th, 8th .. taps */
216 /* fN-4(n) = fN-3(n) - kN-3 * gN-4(n-1) */
217 fnext = fcurr - ((*pk) * gcurr);
218 /* gN-3(n) = kN-3 * fN-4(n) + gN-4(n-1) */
219 gnext = (fnext * (*pk++)) + gcurr;
220 /* y(n) += gN-3(n) * vN-3 */
221 /* process for gN-7(n) * vN-7, gN-11(n) * vN-11 ... */
222 acc += (gnext * (*pv++));
223 /* write gN-3(n) into state for next sample processing */
227 /* Process sample for 5th, 9th ...taps */
228 /* Read gN-5(n-1) from state buffer */
230 /* Process sample for 5th, 9th .. taps */
231 /* fN-5(n) = fN-4(n) - kN-4 * gN-1(n-1) */
232 fcurr = fnext - ((*pk) * gcurr);
233 /* gN-4(n) = kN-4 * fN-5(n) + gN-5(n-1) */
234 gnext = (fcurr * (*pk++)) + gcurr;
235 /* y(n) += gN-4(n) * vN-4 */
236 /* process for gN-8(n) * vN-8, gN-12(n) * vN-12 ... */
237 acc += (gnext * (*pv++));
238 /* write gN-4(n) into state for next sample processing */
247 /* If the filter length is not a multiple of 4, compute the remaining filter taps */
248 tapCnt = (numStages - 1u) % 0x4u;
253 /* Process sample for last taps */
254 fnext = fcurr - ((*pk) * gcurr);
255 gnext = (fnext * (*pk++)) + gcurr;
256 /* Output samples for last taps */
257 acc += (gnext * (*pv++));
266 /* y(n) += g0(n) * v0 */
267 acc += (fnext * (*pv));
271 /* write out into pDst */
274 /* Advance the state pointer by 4 to process the next group of 4 samples */
275 pState = pState + 1u;
280 /* Processing is complete. Now copy last S->numStages samples to start of the buffer
281 for the preperation of next frame process */
283 /* Points to the start of the state buffer */
284 pStateCurnt = &S->pState[0];
285 pState = &S->pState[blockSize];
287 tapCnt = numStages >> 2u;
292 *pStateCurnt++ = *pState++;
293 *pStateCurnt++ = *pState++;
294 *pStateCurnt++ = *pState++;
295 *pStateCurnt++ = *pState++;
297 /* Decrement the loop counter */
302 /* Calculate remaining number of copies */
303 tapCnt = (numStages) % 0x4u;
305 /* Copy the remaining q31_t data */
308 *pStateCurnt++ = *pState++;
310 /* Decrement the loop counter */
316 /* Run the below code for Cortex-M0 */
320 pState = &S->pState[0];
322 /* Sample processing */
325 /* Read Sample from input buffer */
329 /* Initialize state read pointer */
331 /* Initialize state write pointer */
333 /* Set accumulator to zero */
335 /* Initialize Ladder coeff pointer */
336 pv = &S->pvCoeffs[0];
337 /* Initialize Reflection coeff pointer */
338 pk = &S->pkCoeffs[0];
341 /* Process sample for numStages */
347 /* Process sample for last taps */
348 fnext = fcurr - ((*pk) * gcurr);
349 gnext = (fnext * (*pk++)) + gcurr;
351 /* Output samples for last taps */
352 acc += (gnext * (*pv++));
356 /* Decrementing loop counter */
361 /* y(n) += g0(n) * v0 */
362 acc += (fnext * (*pv));
366 /* write out into pDst */
369 /* Advance the state pointer by 1 to process the next group of samples */
370 pState = pState + 1u;
375 /* Processing is complete. Now copy last S->numStages samples to start of the buffer
376 for the preperation of next frame process */
378 /* Points to the start of the state buffer */
379 pStateCurnt = &S->pState[0];
380 pState = &S->pState[blockSize];
387 *pStateCurnt++ = *pState++;
389 /* Decrement the loop counter */
393 #endif /* #ifndef ARM_MATH_CM0 */
401 * @} end of IIR_Lattice group