Added all the F4 libraries to the project
[fw/stlink] / exampleF4 / CMSIS / DSP_Lib / Source / FilteringFunctions / arm_iir_lattice_q15.c
1 /* ----------------------------------------------------------------------   
2 * Copyright (C) 2010 ARM Limited. All rights reserved.   
3 *   
4 * $Date:        15. July 2011  
5 * $Revision:    V1.0.10  
6 *   
7 * Project:          CMSIS DSP Library   
8 * Title:            arm_iir_lattice_q15.c   
9 *   
10 * Description:  Q15 IIR lattice filter processing function.   
11 *   
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
13 *  
14 * Version 1.0.10 2011/7/15 
15 *    Big Endian support added and Merged M0 and M3/M4 Source code.  
16 *   
17 * Version 1.0.3 2010/11/29  
18 *    Re-organized the CMSIS folders and updated documentation.   
19 *    
20 * Version 1.0.2 2010/11/11   
21 *    Documentation updated.    
22 *   
23 * Version 1.0.1 2010/10/05    
24 *    Production release and review comments incorporated.   
25 *   
26 * Version 1.0.0 2010/09/20    
27 *    Production release and review comments incorporated   
28 *   
29 * Version 0.0.7  2010/06/10    
30 *    Misra-C changes done   
31 * -------------------------------------------------------------------- */
32
33 #include "arm_math.h"
34
35 /**   
36  * @ingroup groupFilters   
37  */
38
39 /**   
40  * @addtogroup IIR_Lattice   
41  * @{   
42  */
43
44 /**   
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.   
50  * @return none.   
51  *   
52  * @details   
53  * <b>Scaling and Overflow Behavior:</b>   
54  * \par   
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.   
61  */
62
63 void arm_iir_lattice_q15(
64   const arm_iir_lattice_instance_q15 * S,
65   q15_t * pSrc,
66   q15_t * pDst,
67   uint32_t blockSize)
68 {
69
70
71 #ifndef ARM_MATH_CM0
72
73   /* Run the below code for Cortex-M4 and Cortex-M3 */
74
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 */
86
87
88   blkCnt = blockSize;
89
90   pState = &S->pState[0];
91
92   /* Sample processing */
93   while(blkCnt > 0u)
94   {
95     /* Read Sample from input buffer */
96     /* fN(n) = x(n) */
97     fcurr = *pSrc++;
98
99     /* Initialize state read pointer */
100     px1 = pState;
101     /* Initialize state write pointer */
102     px2 = pState;
103     /* Set accumulator to zero */
104     acc = 0;
105     /* Initialize Ladder coeff pointer */
106     pv = &S->pvCoeffs[0];
107     /* Initialize Reflection coeff pointer */
108     pk = &S->pkCoeffs[0];
109
110
111     /* Process sample for first tap */
112     gcurr = *px1++;
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++)));
123
124
125     /* Update f values for next coefficient processing */
126     fcurr = fnext;
127
128     /* Loop unrolling.  Process 4 taps at a time. */
129     tapCnt = (numStages - 1u) >> 2;
130
131     while(tapCnt > 0u)
132     {
133
134       /* Process sample for 2nd, 6th ...taps */
135       /* Read gN-2(n-1) from state buffer */
136       gcurr = *px1++;
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;
146
147
148       /* Process sample for 3nd, 7th ...taps */
149       /* Read gN-3(n-1) from state */
150       gcurr = *px1++;
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;
160
161       /* Read vN-1 and vN-2 at a time */
162       v = *__SIMD32(pv)++;
163
164
165       /* Pack gN-1(n) and gN-2(n) */
166
167 #ifndef  ARM_MATH_BIG_ENDIAN
168
169       gnext = __PKHBT(gnext1, gnext2, 16);
170
171 #else
172
173       gnext = __PKHBT(gnext2, gnext1, 16);
174
175 #endif /*   #ifndef  ARM_MATH_BIG_ENDIAN    */
176
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);
182
183
184       /* Process sample for 4th, 8th ...taps */
185       /* Read gN-4(n-1) from state */
186       gcurr = *px1++;
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;
196
197
198       /* Process sample for 5th, 9th ...taps */
199       /* Read gN-5(n-1) from state */
200       gcurr = *px1++;
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;
210
211       /* Read vN-3 and vN-4 at a time */
212       v = *__SIMD32(pv)++;
213
214       /* Pack gN-3(n) and gN-4(n) */
215 #ifndef  ARM_MATH_BIG_ENDIAN
216
217       gnext = __PKHBT(gnext1, gnext2, 16);
218
219 #else
220
221       gnext = __PKHBT(gnext2, gnext1, 16);
222
223 #endif /*      #ifndef  ARM_MATH_BIG_ENDIAN    */
224
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);
230
231       tapCnt--;
232
233     }
234
235     fnext = fcurr;
236
237     /* If the filter length is not a multiple of 4, compute the remaining filter taps */
238     tapCnt = (numStages - 1u) % 0x4u;
239
240     while(tapCnt > 0u)
241     {
242       gcurr = *px1++;
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;
251       fcurr = fnext;
252
253       tapCnt--;
254     }
255
256     /* y(n) += g0(n) * v0 */
257     acc += (q31_t) (((q31_t) fnext * (*pv++)));
258
259     out = (q15_t) __SSAT(acc >> 15, 16);
260     *px2++ = (q15_t) fnext;
261
262     /* write out into pDst */
263     *pDst++ = out;
264
265     /* Advance the state pointer by 4 to process the next group of 4 samples */
266     pState = pState + 1u;
267     blkCnt--;
268
269   }
270
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];
276
277   stgCnt = (numStages >> 2u);
278
279   /* copy data */
280   while(stgCnt > 0u)
281   {
282     *__SIMD32(pStateCurnt)++ = *__SIMD32(pState)++;
283     *__SIMD32(pStateCurnt)++ = *__SIMD32(pState)++;
284
285     /* Decrement the loop counter */
286     stgCnt--;
287
288   }
289
290   /* Calculation of count for remaining q15_t data */
291   stgCnt = (numStages) % 0x4u;
292
293   /* copy data */
294   while(stgCnt > 0u)
295   {
296     *pStateCurnt++ = *pState++;
297
298     /* Decrement the loop counter */
299     stgCnt--;
300   }
301
302 #else
303
304   /* Run the below code for Cortex-M0 */
305
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 */
315
316
317   blkCnt = blockSize;
318
319   pState = &S->pState[0];
320
321   /* Sample processing */
322   while(blkCnt > 0u)
323   {
324     /* Read Sample from input buffer */
325     /* fN(n) = x(n) */
326     fcurr = *pSrc++;
327
328     /* Initialize state read pointer */
329     px1 = pState;
330     /* Initialize state write pointer */
331     px2 = pState;
332     /* Set accumulator to zero */
333     acc = 0;
334     /* Initialize Ladder coeff pointer */
335     pv = &S->pvCoeffs[0];
336     /* Initialize Reflection coeff pointer */
337     pk = &S->pkCoeffs[0];
338
339     tapCnt = numStages;
340
341     while(tapCnt > 0u)
342     {
343       gcurr = *px1++;
344       /* Process sample */
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);
351       /* Output samples */
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 */
357       fcurr = fnext;
358
359       tapCnt--;
360     }
361
362     /* y(n) += g0(n) * v0 */
363     acc += (q31_t) ((fnext * (*pv++)));
364
365     out = (q15_t) __SSAT(acc >> 15, 16);
366     *px2++ = (q15_t) fnext;
367
368     /* write out into pDst */
369     *pDst++ = out;
370
371     /* Advance the state pointer by 1 to process the next group of samples */
372     pState = pState + 1u;
373     blkCnt--;
374
375   }
376
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];
382
383   stgCnt = numStages;
384
385   /* copy data */
386   while(stgCnt > 0u)
387   {
388     *pStateCurnt++ = *pState++;
389
390     /* Decrement the loop counter */
391     stgCnt--;
392   }
393
394 #endif /*   #ifndef ARM_MATH_CM0 */
395
396 }
397
398
399
400
401 /**   
402  * @} end of IIR_Lattice group   
403  */