1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010 ARM Limited. All rights reserved.
7 * Project: CMSIS DSP Library
8 * Title: arm_iir_lattice_q15.c
10 * Description: Q15 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 * @addtogroup IIR_Lattice
45 * @brief Processing function for the Q15 IIR lattice filter.
46 * @param[in] *S points to an instance of the Q15 IIR lattice structure.
47 * @param[in] *pSrc points to the block of input data.
48 * @param[out] *pDst points to the block of output data.
49 * @param[in] blockSize number of samples to process.
53 * <b>Scaling and Overflow Behavior:</b>
55 * The function is implemented using a 64-bit internal accumulator.
56 * Both coefficients and state variables are represented in 1.15 format and multiplications yield a 2.30 result.
57 * The 2.30 intermediate results are accumulated in a 64-bit accumulator in 34.30 format.
58 * There is no risk of internal overflow with this approach and the full precision of intermediate multiplications is preserved.
59 * After all additions have been performed, the accumulator is truncated to 34.15 format by discarding low 15 bits.
60 * Lastly, the accumulator is saturated to yield a result in 1.15 format.
63 void arm_iir_lattice_q15(
64 const arm_iir_lattice_instance_q15 * S,
73 /* Run the below code for Cortex-M4 and Cortex-M3 */
75 q31_t fcurr, fnext, gcurr = 0, gnext; /* Temporary variables for lattice stages */
76 q15_t gnext1, gnext2; /* Temporary variables for lattice stages */
77 uint32_t stgCnt; /* Temporary variables for counts */
78 q63_t acc; /* Accumlator */
79 uint32_t blkCnt, tapCnt; /* Temporary variables for counts */
80 q15_t *px1, *px2, *pk, *pv; /* temporary pointers for state and coef */
81 uint32_t numStages = S->numStages; /* number of stages */
82 q15_t *pState; /* State pointer */
83 q15_t *pStateCurnt; /* State current pointer */
84 q15_t out; /* Temporary variable for output */
85 q31_t v; /* Temporary variable for ladder coefficient */
90 pState = &S->pState[0];
92 /* Sample processing */
95 /* Read Sample from input buffer */
99 /* Initialize state read pointer */
101 /* Initialize state write pointer */
103 /* Set accumulator to zero */
105 /* Initialize Ladder coeff pointer */
106 pv = &S->pvCoeffs[0];
107 /* Initialize Reflection coeff pointer */
108 pk = &S->pkCoeffs[0];
111 /* Process sample for first tap */
113 /* fN-1(n) = fN(n) - kN * gN-1(n-1) */
114 fnext = fcurr - (((q31_t) gcurr * (*pk)) >> 15);
115 fnext = __SSAT(fnext, 16);
116 /* gN(n) = kN * fN-1(n) + gN-1(n-1) */
117 gnext = (((q31_t) fnext * (*pk++)) >> 15) + gcurr;
118 gnext = __SSAT(gnext, 16);
119 /* write gN(n) into state for next sample processing */
120 *px2++ = (q15_t) gnext;
121 /* y(n) += gN(n) * vN */
122 acc += (q31_t) ((gnext * (*pv++)));
125 /* Update f values for next coefficient processing */
128 /* Loop unrolling. Process 4 taps at a time. */
129 tapCnt = (numStages - 1u) >> 2;
134 /* Process sample for 2nd, 6th ...taps */
135 /* Read gN-2(n-1) from state buffer */
137 /* Process sample for 2nd, 6th .. taps */
138 /* fN-2(n) = fN-1(n) - kN-1 * gN-2(n-1) */
139 fnext = fcurr - (((q31_t) gcurr * (*pk)) >> 15);
140 fnext = __SSAT(fnext, 16);
141 /* gN-1(n) = kN-1 * fN-2(n) + gN-2(n-1) */
142 gnext = (((q31_t) fnext * (*pk++)) >> 15) + gcurr;
143 gnext1 = (q15_t) __SSAT(gnext, 16);
144 /* write gN-1(n) into state */
145 *px2++ = (q15_t) gnext1;
148 /* Process sample for 3nd, 7th ...taps */
149 /* Read gN-3(n-1) from state */
151 /* Process sample for 3rd, 7th .. taps */
152 /* fN-3(n) = fN-2(n) - kN-2 * gN-3(n-1) */
153 fcurr = fnext - (((q31_t) gcurr * (*pk)) >> 15);
154 fcurr = __SSAT(fcurr, 16);
155 /* gN-2(n) = kN-2 * fN-3(n) + gN-3(n-1) */
156 gnext = (((q31_t) fcurr * (*pk++)) >> 15) + gcurr;
157 gnext2 = (q15_t) __SSAT(gnext, 16);
158 /* write gN-2(n) into state */
159 *px2++ = (q15_t) gnext2;
161 /* Read vN-1 and vN-2 at a time */
165 /* Pack gN-1(n) and gN-2(n) */
167 #ifndef ARM_MATH_BIG_ENDIAN
169 gnext = __PKHBT(gnext1, gnext2, 16);
173 gnext = __PKHBT(gnext2, gnext1, 16);
175 #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
177 /* y(n) += gN-1(n) * vN-1 */
178 /* process for gN-5(n) * vN-5, gN-9(n) * vN-9 ... */
179 /* y(n) += gN-2(n) * vN-2 */
180 /* process for gN-6(n) * vN-6, gN-10(n) * vN-10 ... */
181 acc = __SMLALD(gnext, v, acc);
184 /* Process sample for 4th, 8th ...taps */
185 /* Read gN-4(n-1) from state */
187 /* Process sample for 4th, 8th .. taps */
188 /* fN-4(n) = fN-3(n) - kN-3 * gN-4(n-1) */
189 fnext = fcurr - (((q31_t) gcurr * (*pk)) >> 15);
190 fnext = __SSAT(fnext, 16);
191 /* gN-3(n) = kN-3 * fN-1(n) + gN-1(n-1) */
192 gnext = (((q31_t) fnext * (*pk++)) >> 15) + gcurr;
193 gnext1 = (q15_t) __SSAT(gnext, 16);
194 /* write gN-3(n) for the next sample process */
195 *px2++ = (q15_t) gnext1;
198 /* Process sample for 5th, 9th ...taps */
199 /* Read gN-5(n-1) from state */
201 /* Process sample for 5th, 9th .. taps */
202 /* fN-5(n) = fN-4(n) - kN-4 * gN-5(n-1) */
203 fcurr = fnext - (((q31_t) gcurr * (*pk)) >> 15);
204 fcurr = __SSAT(fcurr, 16);
205 /* gN-4(n) = kN-4 * fN-5(n) + gN-5(n-1) */
206 gnext = (((q31_t) fcurr * (*pk++)) >> 15) + gcurr;
207 gnext2 = (q15_t) __SSAT(gnext, 16);
208 /* write gN-4(n) for the next sample process */
209 *px2++ = (q15_t) gnext2;
211 /* Read vN-3 and vN-4 at a time */
214 /* Pack gN-3(n) and gN-4(n) */
215 #ifndef ARM_MATH_BIG_ENDIAN
217 gnext = __PKHBT(gnext1, gnext2, 16);
221 gnext = __PKHBT(gnext2, gnext1, 16);
223 #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
225 /* y(n) += gN-4(n) * vN-4 */
226 /* process for gN-8(n) * vN-8, gN-12(n) * vN-12 ... */
227 /* y(n) += gN-3(n) * vN-3 */
228 /* process for gN-7(n) * vN-7, gN-11(n) * vN-11 ... */
229 acc = __SMLALD(gnext, v, acc);
237 /* If the filter length is not a multiple of 4, compute the remaining filter taps */
238 tapCnt = (numStages - 1u) % 0x4u;
243 /* Process sample for last taps */
244 fnext = fcurr - (((q31_t) gcurr * (*pk)) >> 15);
245 fnext = __SSAT(fnext, 16);
246 gnext = (((q31_t) fnext * (*pk++)) >> 15) + gcurr;
247 gnext = __SSAT(gnext, 16);
248 /* Output samples for last taps */
249 acc += (q31_t) (((q31_t) gnext * (*pv++)));
250 *px2++ = (q15_t) gnext;
256 /* y(n) += g0(n) * v0 */
257 acc += (q31_t) (((q31_t) fnext * (*pv++)));
259 out = (q15_t) __SSAT(acc >> 15, 16);
260 *px2++ = (q15_t) fnext;
262 /* write out into pDst */
265 /* Advance the state pointer by 4 to process the next group of 4 samples */
266 pState = pState + 1u;
271 /* Processing is complete. Now copy last S->numStages samples to start of the buffer
272 for the preperation of next frame process */
273 /* Points to the start of the state buffer */
274 pStateCurnt = &S->pState[0];
275 pState = &S->pState[blockSize];
277 stgCnt = (numStages >> 2u);
282 *__SIMD32(pStateCurnt)++ = *__SIMD32(pState)++;
283 *__SIMD32(pStateCurnt)++ = *__SIMD32(pState)++;
285 /* Decrement the loop counter */
290 /* Calculation of count for remaining q15_t data */
291 stgCnt = (numStages) % 0x4u;
296 *pStateCurnt++ = *pState++;
298 /* Decrement the loop counter */
304 /* Run the below code for Cortex-M0 */
306 q31_t fcurr, fnext = 0, gcurr = 0, gnext; /* Temporary variables for lattice stages */
307 uint32_t stgCnt; /* Temporary variables for counts */
308 q63_t acc; /* Accumlator */
309 uint32_t blkCnt, tapCnt; /* Temporary variables for counts */
310 q15_t *px1, *px2, *pk, *pv; /* temporary pointers for state and coef */
311 uint32_t numStages = S->numStages; /* number of stages */
312 q15_t *pState; /* State pointer */
313 q15_t *pStateCurnt; /* State current pointer */
314 q15_t out; /* Temporary variable for output */
319 pState = &S->pState[0];
321 /* Sample processing */
324 /* Read Sample from input buffer */
328 /* Initialize state read pointer */
330 /* Initialize state write pointer */
332 /* Set accumulator to zero */
334 /* Initialize Ladder coeff pointer */
335 pv = &S->pvCoeffs[0];
336 /* Initialize Reflection coeff pointer */
337 pk = &S->pkCoeffs[0];
345 /* fN-1(n) = fN(n) - kN * gN-1(n-1) */
346 fnext = fcurr - ((gcurr * (*pk)) >> 15);
347 fnext = __SSAT(fnext, 16);
348 /* gN(n) = kN * fN-1(n) + gN-1(n-1) */
349 gnext = ((fnext * (*pk++)) >> 15) + gcurr;
350 gnext = __SSAT(gnext, 16);
352 /* y(n) += gN(n) * vN */
353 acc += (q31_t) ((gnext * (*pv++)));
354 /* write gN(n) into state for next sample processing */
355 *px2++ = (q15_t) gnext;
356 /* Update f values for next coefficient processing */
362 /* y(n) += g0(n) * v0 */
363 acc += (q31_t) ((fnext * (*pv++)));
365 out = (q15_t) __SSAT(acc >> 15, 16);
366 *px2++ = (q15_t) fnext;
368 /* write out into pDst */
371 /* Advance the state pointer by 1 to process the next group of samples */
372 pState = pState + 1u;
377 /* Processing is complete. Now copy last S->numStages samples to start of the buffer
378 for the preperation of next frame process */
379 /* Points to the start of the state buffer */
380 pStateCurnt = &S->pState[0];
381 pState = &S->pState[blockSize];
388 *pStateCurnt++ = *pState++;
390 /* Decrement the loop counter */
394 #endif /* #ifndef ARM_MATH_CM0 */
402 * @} end of IIR_Lattice group