STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
arm_iir_lattice_q31.c
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010-2014 ARM Limited. All rights reserved.
3 *
4 * $Date: 19. March 2015
5 * $Revision: V.1.4.5
6 *
7 * Project: CMSIS DSP Library
8 * Title: arm_iir_lattice_q31.c
9 *
10 * Description: Q31 IIR lattice filter processing function.
11 *
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
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
22 * distribution.
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.
26 *
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 * -------------------------------------------------------------------- */
40 
41 #include "arm_math.h"
42 
72  q31_t * pSrc,
73  q31_t * pDst,
74  uint32_t blockSize)
75 {
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 */
83 
84  blkCnt = blockSize;
85 
86  pState = &S->pState[0];
87 
88 
89 #ifndef ARM_MATH_CM0_FAMILY
90 
91  /* Run the below code for Cortex-M4 and Cortex-M3 */
92 
93  /* Sample processing */
94  while(blkCnt > 0u)
95  {
96  /* Read Sample from input buffer */
97  /* fN(n) = x(n) */
98  fcurr = *pSrc++;
99 
100  /* Initialize state read pointer */
101  px1 = pState;
102  /* Initialize state write pointer */
103  px2 = pState;
104  /* Set accumulator to zero */
105  acc = 0;
106  /* Initialize Ladder coeff pointer */
107  pv = &S->pvCoeffs[0];
108  /* Initialize Reflection coeff pointer */
109  pk = &S->pkCoeffs[0];
110 
111 
112  /* Process sample for first tap */
113  gcurr = *px1++;
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 */
119  *px2++ = gnext;
120  /* y(n) += gN(n) * vN */
121  acc += ((q63_t) gnext * *pv++);
122 
123  /* Update f values for next coefficient processing */
124  fcurr = fnext;
125 
126  /* Loop unrolling. Process 4 taps at a time. */
127  tapCnt = (numStages - 1u) >> 2;
128 
129  while(tapCnt > 0u)
130  {
131 
132  /* Process sample for 2nd, 6th .. taps */
133  /* Read gN-2(n-1) from state buffer */
134  gcurr = *px1++;
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 */
143  *px2++ = gnext;
144 
145  /* Process sample for 3nd, 7th ...taps */
146  /* Read gN-3(n-1) from state buffer */
147  gcurr = *px1++;
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 */
157  *px2++ = gnext;
158 
159 
160  /* Process sample for 4th, 8th ...taps */
161  /* Read gN-4(n-1) from state buffer */
162  gcurr = *px1++;
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 */
172  *px2++ = gnext;
173 
174 
175  /* Process sample for 5th, 9th ...taps */
176  /* Read gN-5(n-1) from state buffer */
177  gcurr = *px1++;
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 */
187  *px2++ = gnext;
188 
189  tapCnt--;
190 
191  }
192 
193  fnext = fcurr;
194 
195  /* If the filter length is not a multiple of 4, compute the remaining filter taps */
196  tapCnt = (numStages - 1u) % 0x4u;
197 
198  while(tapCnt > 0u)
199  {
200  gcurr = *px1++;
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++);
206  *px2++ = gnext;
207  fcurr = fnext;
208 
209  tapCnt--;
210 
211  }
212 
213  /* y(n) += g0(n) * v0 */
214  acc += (q63_t) fnext *(
215  *pv++);
216 
217  *px2++ = fnext;
218 
219  /* write out into pDst */
220  *pDst++ = (q31_t) (acc >> 31u);
221 
222  /* Advance the state pointer by 4 to process the next group of 4 samples */
223  pState = pState + 1u;
224  blkCnt--;
225 
226  }
227 
228  /* Processing is complete. Now copy last S->numStages samples to start of the buffer
229  for the preperation of next frame process */
230 
231  /* Points to the start of the state buffer */
232  pStateCurnt = &S->pState[0];
233  pState = &S->pState[blockSize];
234 
235  tapCnt = numStages >> 2u;
236 
237  /* copy data */
238  while(tapCnt > 0u)
239  {
240  *pStateCurnt++ = *pState++;
241  *pStateCurnt++ = *pState++;
242  *pStateCurnt++ = *pState++;
243  *pStateCurnt++ = *pState++;
244 
245  /* Decrement the loop counter */
246  tapCnt--;
247 
248  }
249 
250  /* Calculate remaining number of copies */
251  tapCnt = (numStages) % 0x4u;
252 
253  /* Copy the remaining q31_t data */
254  while(tapCnt > 0u)
255  {
256  *pStateCurnt++ = *pState++;
257 
258  /* Decrement the loop counter */
259  tapCnt--;
260  };
261 
262 #else
263 
264  /* Run the below code for Cortex-M0 */
265  /* Sample processing */
266  while(blkCnt > 0u)
267  {
268  /* Read Sample from input buffer */
269  /* fN(n) = x(n) */
270  fcurr = *pSrc++;
271 
272  /* Initialize state read pointer */
273  px1 = pState;
274  /* Initialize state write pointer */
275  px2 = pState;
276  /* Set accumulator to zero */
277  acc = 0;
278  /* Initialize Ladder coeff pointer */
279  pv = &S->pvCoeffs[0];
280  /* Initialize Reflection coeff pointer */
281  pk = &S->pkCoeffs[0];
282 
283  tapCnt = numStages;
284 
285  while(tapCnt > 0u)
286  {
287  gcurr = *px1++;
288  /* Process sample */
289  /* fN-1(n) = fN(n) - kN * gN-1(n-1) */
290  fnext =
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) */
294  gnext =
295  clip_q63_to_q31(((q63_t) gcurr +
296  ((q31_t) (((q63_t) fnext * (*pk++)) >> 31))));
297  /* Output samples */
298  /* y(n) += gN(n) * vN */
299  acc += ((q63_t) gnext * *pv++);
300  /* write gN-1(n-1) into state for next sample processing */
301  *px2++ = gnext;
302  /* Update f values for next coefficient processing */
303  fcurr = fnext;
304 
305  tapCnt--;
306  }
307 
308  /* y(n) += g0(n) * v0 */
309  acc += (q63_t) fnext *(
310  *pv++);
311 
312  *px2++ = fnext;
313 
314  /* write out into pDst */
315  *pDst++ = (q31_t) (acc >> 31u);
316 
317  /* Advance the state pointer by 1 to process the next group of samples */
318  pState = pState + 1u;
319  blkCnt--;
320 
321  }
322 
323  /* Processing is complete. Now copy last S->numStages samples to start of the buffer
324  for the preperation of next frame process */
325 
326  /* Points to the start of the state buffer */
327  pStateCurnt = &S->pState[0];
328  pState = &S->pState[blockSize];
329 
330  tapCnt = numStages;
331 
332  /* Copy the remaining q31_t data */
333  while(tapCnt > 0u)
334  {
335  *pStateCurnt++ = *pState++;
336 
337  /* Decrement the loop counter */
338  tapCnt--;
339  }
340 
341 #endif /* #ifndef ARM_MATH_CM0_FAMILY */
342 
343 }
344 
345 
346 
347 
void arm_iir_lattice_q31(const arm_iir_lattice_instance_q31 *S, q31_t *pSrc, q31_t *pDst, uint32_t blockSize)
Processing function for the Q31 IIR lattice filter.
Instance structure for the Q31 IIR lattice filter.
Definition: arm_math.h:3849
int64_t q63_t
64-bit fractional data type in 1.63 format.
Definition: arm_math.h:402
int32_t q31_t
32-bit fractional data type in 1.31 format.
Definition: arm_math.h:397