1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010-2013 ARM Limited. All rights reserved.
4 * $Date: 17. January 2013
7 * Project: CMSIS DSP Library
8 * Title: arm_iir_lattice_q31.c
10 * Description: Q31 IIR lattice filter processing function.
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 IIR_Lattice
53 * @brief Processing function for the Q31 IIR lattice filter.
54 * @param[in] *S points to an instance of the Q31 IIR lattice 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 samples to process.
61 * <b>Scaling and Overflow Behavior:</b>
63 * The function is implemented using an internal 64-bit accumulator.
64 * The accumulator has a 2.62 format and maintains full precision of the intermediate multiplication results but provides only a single guard bit.
65 * Thus, if the accumulator result overflows it wraps around rather than clip.
66 * In order to avoid overflows completely the input signal must be scaled down by 2*log2(numStages) bits.
67 * After all multiply-accumulates are performed, the 2.62 accumulator is saturated to 1.32 format and then truncated to 1.31 format.
70 void arm_iir_lattice_q31(
71 const arm_iir_lattice_instance_q31
* S
,
76 q31_t fcurr
, fnext
= 0, gcurr
= 0, gnext
; /* Temporary variables for lattice stages */
77 q63_t acc
; /* Accumlator */
78 uint32_t blkCnt
, tapCnt
; /* Temporary variables for counts */
79 q31_t
*px1
, *px2
, *pk
, *pv
; /* Temporary pointers for state and coef */
80 uint32_t numStages
= S
->numStages
; /* number of stages */
81 q31_t
*pState
; /* State pointer */
82 q31_t
*pStateCurnt
; /* State current pointer */
86 pState
= &S
->pState
[0];
89 #ifndef ARM_MATH_CM0_FAMILY
91 /* Run the below code for Cortex-M4 and Cortex-M3 */
93 /* Sample processing */
96 /* Read Sample from input buffer */
100 /* Initialize state read pointer */
102 /* Initialize state write pointer */
104 /* Set accumulator to zero */
106 /* Initialize Ladder coeff pointer */
107 pv
= &S
->pvCoeffs
[0];
108 /* Initialize Reflection coeff pointer */
109 pk
= &S
->pkCoeffs
[0];
112 /* Process sample for first tap */
114 /* fN-1(n) = fN(n) - kN * gN-1(n-1) */
115 fnext
= __QSUB(fcurr
, (q31_t
) (((q63_t
) gcurr
* (*pk
)) >> 31));
116 /* gN(n) = kN * fN-1(n) + gN-1(n-1) */
117 gnext
= __QADD(gcurr
, (q31_t
) (((q63_t
) fnext
* (*pk
++)) >> 31));
118 /* write gN-1(n-1) into state for next sample processing */
120 /* y(n) += gN(n) * vN */
121 acc
+= ((q63_t
) gnext
* *pv
++);
123 /* Update f values for next coefficient processing */
126 /* Loop unrolling. Process 4 taps at a time. */
127 tapCnt
= (numStages
- 1u) >> 2;
132 /* Process sample for 2nd, 6th .. taps */
133 /* Read gN-2(n-1) from state buffer */
135 /* fN-2(n) = fN-1(n) - kN-1 * gN-2(n-1) */
136 fnext
= __QSUB(fcurr
, (q31_t
) (((q63_t
) gcurr
* (*pk
)) >> 31));
137 /* gN-1(n) = kN-1 * fN-2(n) + gN-2(n-1) */
138 gnext
= __QADD(gcurr
, (q31_t
) (((q63_t
) fnext
* (*pk
++)) >> 31));
139 /* y(n) += gN-1(n) * vN-1 */
140 /* process for gN-5(n) * vN-5, gN-9(n) * vN-9 ... */
141 acc
+= ((q63_t
) gnext
* *pv
++);
142 /* write gN-1(n) into state for next sample processing */
145 /* Process sample for 3nd, 7th ...taps */
146 /* Read gN-3(n-1) from state buffer */
148 /* Process sample for 3rd, 7th .. taps */
149 /* fN-3(n) = fN-2(n) - kN-2 * gN-3(n-1) */
150 fcurr
= __QSUB(fnext
, (q31_t
) (((q63_t
) gcurr
* (*pk
)) >> 31));
151 /* gN-2(n) = kN-2 * fN-3(n) + gN-3(n-1) */
152 gnext
= __QADD(gcurr
, (q31_t
) (((q63_t
) fcurr
* (*pk
++)) >> 31));
153 /* y(n) += gN-2(n) * vN-2 */
154 /* process for gN-6(n) * vN-6, gN-10(n) * vN-10 ... */
155 acc
+= ((q63_t
) gnext
* *pv
++);
156 /* write gN-2(n) into state for next sample processing */
160 /* Process sample for 4th, 8th ...taps */
161 /* Read gN-4(n-1) from state buffer */
163 /* Process sample for 4th, 8th .. taps */
164 /* fN-4(n) = fN-3(n) - kN-3 * gN-4(n-1) */
165 fnext
= __QSUB(fcurr
, (q31_t
) (((q63_t
) gcurr
* (*pk
)) >> 31));
166 /* gN-3(n) = kN-3 * fN-4(n) + gN-4(n-1) */
167 gnext
= __QADD(gcurr
, (q31_t
) (((q63_t
) fnext
* (*pk
++)) >> 31));
168 /* y(n) += gN-3(n) * vN-3 */
169 /* process for gN-7(n) * vN-7, gN-11(n) * vN-11 ... */
170 acc
+= ((q63_t
) gnext
* *pv
++);
171 /* write gN-3(n) into state for next sample processing */
175 /* Process sample for 5th, 9th ...taps */
176 /* Read gN-5(n-1) from state buffer */
178 /* Process sample for 5th, 9th .. taps */
179 /* fN-5(n) = fN-4(n) - kN-4 * gN-1(n-1) */
180 fcurr
= __QSUB(fnext
, (q31_t
) (((q63_t
) gcurr
* (*pk
)) >> 31));
181 /* gN-4(n) = kN-4 * fN-5(n) + gN-5(n-1) */
182 gnext
= __QADD(gcurr
, (q31_t
) (((q63_t
) fcurr
* (*pk
++)) >> 31));
183 /* y(n) += gN-4(n) * vN-4 */
184 /* process for gN-8(n) * vN-8, gN-12(n) * vN-12 ... */
185 acc
+= ((q63_t
) gnext
* *pv
++);
186 /* write gN-4(n) into state for next sample processing */
195 /* If the filter length is not a multiple of 4, compute the remaining filter taps */
196 tapCnt
= (numStages
- 1u) % 0x4u
;
201 /* Process sample for last taps */
202 fnext
= __QSUB(fcurr
, (q31_t
) (((q63_t
) gcurr
* (*pk
)) >> 31));
203 gnext
= __QADD(gcurr
, (q31_t
) (((q63_t
) fnext
* (*pk
++)) >> 31));
204 /* Output samples for last taps */
205 acc
+= ((q63_t
) gnext
* *pv
++);
213 /* y(n) += g0(n) * v0 */
214 acc
+= (q63_t
) fnext
*(
219 /* write out into pDst */
220 *pDst
++ = (q31_t
) (acc
>> 31u);
222 /* Advance the state pointer by 4 to process the next group of 4 samples */
223 pState
= pState
+ 1u;
228 /* Processing is complete. Now copy last S->numStages samples to start of the buffer
229 for the preperation of next frame process */
231 /* Points to the start of the state buffer */
232 pStateCurnt
= &S
->pState
[0];
233 pState
= &S
->pState
[blockSize
];
235 tapCnt
= numStages
>> 2u;
240 *pStateCurnt
++ = *pState
++;
241 *pStateCurnt
++ = *pState
++;
242 *pStateCurnt
++ = *pState
++;
243 *pStateCurnt
++ = *pState
++;
245 /* Decrement the loop counter */
250 /* Calculate remaining number of copies */
251 tapCnt
= (numStages
) % 0x4u
;
253 /* Copy the remaining q31_t data */
256 *pStateCurnt
++ = *pState
++;
258 /* Decrement the loop counter */
264 /* Run the below code for Cortex-M0 */
265 /* Sample processing */
268 /* Read Sample from input buffer */
272 /* Initialize state read pointer */
274 /* Initialize state write pointer */
276 /* Set accumulator to zero */
278 /* Initialize Ladder coeff pointer */
279 pv
= &S
->pvCoeffs
[0];
280 /* Initialize Reflection coeff pointer */
281 pk
= &S
->pkCoeffs
[0];
289 /* fN-1(n) = fN(n) - kN * gN-1(n-1) */
291 clip_q63_to_q31(((q63_t
) fcurr
-
292 ((q31_t
) (((q63_t
) gcurr
* (*pk
)) >> 31))));
293 /* gN(n) = kN * fN-1(n) + gN-1(n-1) */
295 clip_q63_to_q31(((q63_t
) gcurr
+
296 ((q31_t
) (((q63_t
) fnext
* (*pk
++)) >> 31))));
298 /* y(n) += gN(n) * vN */
299 acc
+= ((q63_t
) gnext
* *pv
++);
300 /* write gN-1(n-1) into state for next sample processing */
302 /* Update f values for next coefficient processing */
308 /* y(n) += g0(n) * v0 */
309 acc
+= (q63_t
) fnext
*(
314 /* write out into pDst */
315 *pDst
++ = (q31_t
) (acc
>> 31u);
317 /* Advance the state pointer by 1 to process the next group of samples */
318 pState
= pState
+ 1u;
323 /* Processing is complete. Now copy last S->numStages samples to start of the buffer
324 for the preperation of next frame process */
326 /* Points to the start of the state buffer */
327 pStateCurnt
= &S
->pState
[0];
328 pState
= &S
->pState
[blockSize
];
332 /* Copy the remaining q31_t data */
335 *pStateCurnt
++ = *pState
++;
337 /* Decrement the loop counter */
341 #endif /* #ifndef ARM_MATH_CM0_FAMILY */
349 * @} end of IIR_Lattice group