- Main Page
- Modules
- Data Structures
- Files
- Examples

- File List
- Globals

# arm_fir_lattice_q31.c

Go to the documentation of this file.

    00001 /* ----------------------------------------------------------------------
    00002 * Copyright (C) 2010 ARM Limited. All rights reserved.
    00003 *
    00004 * $Date:        15. July 2011
    00005 * $Revision:    V1.0.10
    00006 *
    00007 * Project:      CMSIS DSP Library
    00008 * Title:        arm_fir_lattice_q31.c
    00009 *
    00010 * Description:  Q31 FIR lattice filter processing function.
    00011 *
    00012 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
    00013 *
    00014 * Version 1.0.10 2011/7/15
    00015 *    Big Endian support added and Merged M0 and M3/M4 Source code.
    00016 *
    00017 * Version 1.0.3 2010/11/29
    00018 *    Re-organized the CMSIS folders and updated documentation.
    00019 *
    00020 * Version 1.0.2 2010/11/11
    00021 *    Documentation updated.
    00022 *
    00023 * Version 1.0.1 2010/10/05
    00024 *    Production release and review comments incorporated.
    00025 *
    00026 * Version 1.0.0 2010/09/20
    00027 *    Production release and review comments incorporated
    00028 *
    00029 * Version 0.0.7  2010/06/10
    00030 *    Misra-C changes done
    00031 * -------------------------------------------------------------------- */
    00032
    00033 #include "arm_math.h"
    00034
    00058 void arm_fir_lattice_q31(
    00059   const arm_fir_lattice_instance_q31 * S,
    00060   q31_t * pSrc,
    00061   q31_t * pDst,
    00062   uint32_t blockSize)
    00063 {
    00064   q31_t *pState;                                 /* State pointer */
    00065   q31_t *pCoeffs = S->pCoeffs;                   /* Coefficient pointer */
    00066   q31_t *px;                                     /* temporary state pointer */
    00067   q31_t *pk;                                     /* temporary coefficient pointer */
    00068
    00069
    00070 #ifndef ARM_MATH_CM0
    00071
    00072   /* Run the below code for Cortex-M4 and Cortex-M3 */
    00073
    00074   q31_t fcurr1, fnext1, gcurr1 = 0, gnext1;      /* temporary variables for first sample in loop unrolling */
    00075   q63_t fcurr2, fnext2, gnext2;                  /* temporary variables for second sample in loop unrolling */
    00076   q63_t fcurr3, fnext3, gnext3;                  /* temporary variables for third sample in loop unrolling */
    00077   q63_t fcurr4, fnext4, gnext4;                  /* temporary variables for fourth sample in loop unrolling */
    00078   uint32_t numStages = S->numStages;             /* Length of the filter */
    00079   uint32_t blkCnt, stageCnt;                     /* temporary variables for counts */
    00080
    00081   pState = &S->pState[0];
    00082
    00083   blkCnt = blockSize >> 2u;
    00084
    00085   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
    00086    ** a second loop below computes the remaining 1 to 3 samples. */
    00087   while(blkCnt > 0u)
    00088   {
    00089
    00090     /* Read two samples from input buffer */
    00091     /* f0(n) = x(n) */
    00092     fcurr1 = *pSrc++;
    00093     /* f0(n) = x(n) */
    00094     fcurr2 = *pSrc++;
    00095
    00096     /* Initialize coeff pointer */
    00097     pk = (pCoeffs);
    00098
    00099     /* Initialize state pointer */
    00100     px = pState;
    00101
    00102     /* Read g0(n-1) from state */
    00103     gcurr1 = *px;
    00104
    00105     /* Process first sample for first tap */
    00106     /* f1(n) = f0(n) +  K1 * g0(n-1) */
    00107     fnext1 = (q31_t) (((q63_t) gcurr1 * (*pk)) >> 31) + fcurr1;
    00108     /* g1(n) = f0(n) * K1  +  g0(n-1) */
    00109     gnext1 = (q31_t) (((q63_t) fcurr1 * (*pk)) >> 31) + gcurr1;
    00110
    00111     /* Process second sample for first tap */
    00112     /* for sample 2 processing */
    00113     fnext2 = (q31_t) (((q63_t) fcurr1 * (*pk)) >> 31) + fcurr2;
    00114     gnext2 = (q31_t) (((q63_t) fcurr2 * (*pk)) >> 31) + fcurr1;
    00115
    00116
    00117     /* Read next two samples from input buffer */
    00118     /* f0(n+2) = x(n+2) */
    00119     fcurr3 = *pSrc++;
    00120     fcurr4 = *pSrc++;
    00121
    00122     /* Copy only last input samples into the state buffer
    00123        which will be used for next four samples processing */
    00124     *px++ = (q31_t) fcurr4;
    00125
    00126     /* Process third sample for first tap */
    00127     fnext3 = (q31_t) (((q63_t) fcurr2 * (*pk)) >> 31) + fcurr3;
    00128     gnext3 = (q31_t) (((q63_t) fcurr3 * (*pk)) >> 31) + fcurr2;
    00129
    00130     /* Process fourth sample for first tap */
    00131     fnext4 = (q31_t) (((q63_t) fcurr3 * (*pk)) >> 31) + fcurr4;
    00132     gnext4 = (q31_t) (((q63_t) fcurr4 * (*pk++)) >> 31) + fcurr3;
    00133
    00134     /* save g1(n) in state buffer for next sample processing */
    00135     /* *px++ = gnext4;       */
    00136
    00137     /* Update of f values for next coefficient set processing */
    00138     fcurr1 = fnext1;
    00139     fcurr2 = fnext2;
    00140     fcurr3 = fnext3;
    00141     fcurr4 = fnext4;
    00142
    00143
    00144     /* Loop unrolling.  Process 4 taps at a time . */
    00145     stageCnt = (numStages - 1u) >> 2u;
    00146
    00147
    00148     /* Loop over the number of taps.  Unroll by a factor of 4.
    00149      ** Repeat until we've computed numStages-3 coefficients. */
    00150
    00151     /* Process 2nd, 3rd, 4th and 5th taps ... here */
    00152     while(stageCnt > 0u)
    00153     {
    00154       /* Read g1(n-1), g3(n-1) .... from state */
    00155       gcurr1 = *px;
    00156
    00157       /* save g1(n) in state buffer */
    00158       *px++ = (q31_t) gnext4;
    00159
    00160       /* Process first sample for 2nd, 6th .. tap */
    00161       /* Sample processing for K2, K6.... */
    00162       /* f2(n) = f1(n) +  K2 * g1(n-1) */
    00163       fnext1 = (q31_t) (((q63_t) gcurr1 * (*pk)) >> 31) + fcurr1;
    00164       /* Process second sample for 2nd, 6th .. tap */
    00165       /* for sample 2 processing */
    00166       fnext2 = (q31_t) (((q63_t) gnext1 * (*pk)) >> 31) + fcurr2;
    00167       /* Process third sample for 2nd, 6th .. tap */
    00168       fnext3 = (q31_t) (((q63_t) gnext2 * (*pk)) >> 31) + fcurr3;
    00169       /* Process fourth sample for 2nd, 6th .. tap */
    00170       fnext4 = (q31_t) (((q63_t) gnext3 * (*pk)) >> 31) + fcurr4;
    00171
    00172       /* g2(n) = f1(n) * K2  +  g1(n-1) */
    00173       /* Calculation of state values for next stage */
    00174       gnext4 = (q31_t) (((q63_t) fcurr4 * (*pk)) >> 31) + gnext3;
    00175       gnext3 = (q31_t) (((q63_t) fcurr3 * (*pk)) >> 31) + gnext2;
    00176       gnext2 = (q31_t) (((q63_t) fcurr2 * (*pk)) >> 31) + gnext1;
    00177       gnext1 = (q31_t) (((q63_t) fcurr1 * (*pk++)) >> 31) + gcurr1;
    00178
    00179
    00180       /* Read g2(n-1), g4(n-1) .... from state */
    00181       gcurr1 = *px;
    00182
    00183       /* save g2(n) in state buffer */
    00184       *px++ = (q31_t) gnext4;
    00185
    00186       /* Sample processing for K3, K7.... */
    00187       /* Process first sample for 3rd, 7th .. tap */
    00188       /* f3(n) = f2(n) +  K3 * g2(n-1) */
    00189       fcurr1 = (q31_t) (((q63_t) gcurr1 * (*pk)) >> 31) + fnext1;
    00190       /* Process second sample for 3rd, 7th .. tap */
    00191       fcurr2 = (q31_t) (((q63_t) gnext1 * (*pk)) >> 31) + fnext2;
    00192       /* Process third sample for 3rd, 7th .. tap */
    00193       fcurr3 = (q31_t) (((q63_t) gnext2 * (*pk)) >> 31) + fnext3;
    00194       /* Process fourth sample for 3rd, 7th .. tap */
    00195       fcurr4 = (q31_t) (((q63_t) gnext3 * (*pk)) >> 31) + fnext4;
    00196
    00197       /* Calculation of state values for next stage */
    00198       /*  gnext4 = fnext4 * (*pk) +  gnext3; */
    00199       gnext4 = (q31_t) (((q63_t) fnext4 * (*pk)) >> 31) + gnext3;
    00200       gnext3 = (q31_t) (((q63_t) fnext3 * (*pk)) >> 31) + gnext2;
    00201       /*  gnext2 = fnext2 * (*pk) +  gnext1; */
    00202       gnext2 = (q31_t) (((q63_t) fnext2 * (*pk)) >> 31) + gnext1;
    00203
    00204       /* g1(n) = f0(n) * K1  +  g0(n-1) */
    00205       /*  gnext1 = fnext1 * (*pk++) +  gcurr1; */
    00206       gnext1 = (q31_t) (((q63_t) fnext1 * (*pk++)) >> 31) + gcurr1;
    00207
    00208       /* Read g1(n-1), g3(n-1) .... from state */
    00209       gcurr1 = *px;
    00210
    00211       /* save g1(n) in state buffer */
    00212       *px++ = (q31_t) gnext4;
    00213
    00214       /* Sample processing for K4, K8.... */
    00215       /* Process first sample for 4th, 8th .. tap */
    00216       /* f4(n) = f3(n) +  K4 * g3(n-1) */
    00217       fnext1 = (q31_t) (((q63_t) gcurr1 * (*pk)) >> 31) + fcurr1;
    00218       /* Process second sample for 4th, 8th .. tap */
    00219       /* for sample 2 processing */
    00220       fnext2 = (q31_t) (((q63_t) gnext1 * (*pk)) >> 31) + fcurr2;
    00221       /* Process third sample for 4th, 8th .. tap */
    00222       fnext3 = (q31_t) (((q63_t) gnext2 * (*pk)) >> 31) + fcurr3;
    00223       /* Process fourth sample for 4th, 8th .. tap */
    00224       fnext4 = (q31_t) (((q63_t) gnext3 * (*pk)) >> 31) + fcurr4;
    00225
    00226       /* g4(n) = f3(n) * K4  +  g3(n-1) */
    00227       /* Calculation of state values for next stage */
    00228       gnext4 = (q31_t) (((q63_t) fcurr4 * (*pk)) >> 31) + gnext3;
    00229       gnext3 = (q31_t) (((q63_t) fcurr3 * (*pk)) >> 31) + gnext2;
    00230       gnext2 = (q31_t) (((q63_t) fcurr2 * (*pk)) >> 31) + gnext1;
    00231       gnext1 = (q31_t) (((q63_t) fcurr1 * (*pk++)) >> 31) + gcurr1;
    00232
    00233       /* Read g2(n-1), g4(n-1) .... from state */
    00234       gcurr1 = *px;
    00235
    00236       /* save g4(n) in state buffer */
    00237       *px++ = (q31_t) gnext4;
    00238
    00239       /* Sample processing for K5, K9.... */
    00240       /* Process first sample for 5th, 9th .. tap */
    00241       /* f5(n) = f4(n) +  K5 * g4(n-1) */
    00242       fcurr1 = (q31_t) (((q63_t) gcurr1 * (*pk)) >> 31) + fnext1;
    00243       /* Process second sample for 5th, 9th .. tap */
    00244       fcurr2 = (q31_t) (((q63_t) gnext1 * (*pk)) >> 31) + fnext2;
    00245       /* Process third sample for 5th, 9th .. tap */
    00246       fcurr3 = (q31_t) (((q63_t) gnext2 * (*pk)) >> 31) + fnext3;
    00247       /* Process fourth sample for 5th, 9th .. tap */
    00248       fcurr4 = (q31_t) (((q63_t) gnext3 * (*pk)) >> 31) + fnext4;
    00249
    00250       /* Calculation of state values for next stage */
    00251       /* g5(n) = f4(n) * K5  +  g4(n-1) */
    00252       gnext4 = (q31_t) (((q63_t) fnext4 * (*pk)) >> 31) + gnext3;
    00253       gnext3 = (q31_t) (((q63_t) fnext3 * (*pk)) >> 31) + gnext2;
    00254       gnext2 = (q31_t) (((q63_t) fnext2 * (*pk)) >> 31) + gnext1;
    00255       gnext1 = (q31_t) (((q63_t) fnext1 * (*pk++)) >> 31) + gcurr1;
    00256
    00257       stageCnt--;
    00258     }
    00259
    00260     /* If the (filter length -1) is not a multiple of 4, compute the remaining filter taps */
    00261     stageCnt = (numStages - 1u) % 0x4u;
    00262
    00263     while(stageCnt > 0u)
    00264     {
    00265       gcurr1 = *px;
    00266
    00267       /* save g value in state buffer */
    00268       *px++ = (q31_t) gnext4;
    00269
    00270       /* Process four samples for last three taps here */
    00271       fnext1 = (q31_t) (((q63_t) gcurr1 * (*pk)) >> 31) + fcurr1;
    00272       fnext2 = (q31_t) (((q63_t) gnext1 * (*pk)) >> 31) + fcurr2;
    00273       fnext3 = (q31_t) (((q63_t) gnext2 * (*pk)) >> 31) + fcurr3;
    00274       fnext4 = (q31_t) (((q63_t) gnext3 * (*pk)) >> 31) + fcurr4;
    00275
    00276       /* g1(n) = f0(n) * K1  +  g0(n-1) */
    00277       gnext4 = (q31_t) (((q63_t) fcurr4 * (*pk)) >> 31) + gnext3;
    00278       gnext3 = (q31_t) (((q63_t) fcurr3 * (*pk)) >> 31) + gnext2;
    00279       gnext2 = (q31_t) (((q63_t) fcurr2 * (*pk)) >> 31) + gnext1;
    00280       gnext1 = (q31_t) (((q63_t) fcurr1 * (*pk++)) >> 31) + gcurr1;
    00281
    00282       /* Update of f values for next coefficient set processing */
    00283       fcurr1 = fnext1;
    00284       fcurr2 = fnext2;
    00285       fcurr3 = fnext3;
    00286       fcurr4 = fnext4;
    00287
    00288       stageCnt--;
    00289
    00290     }
    00291
    00292     /* The results in the 4 accumulators, store in the destination buffer. */
    00293     /* y(n) = fN(n) */
    00294     *pDst++ = fcurr1;
    00295     *pDst++ = (q31_t) fcurr2;
    00296     *pDst++ = (q31_t) fcurr3;
    00297     *pDst++ = (q31_t) fcurr4;
    00298
    00299     blkCnt--;
    00300   }
    00301
    00302   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
    00303    ** No loop unrolling is used. */
    00304   blkCnt = blockSize % 0x4u;
    00305
    00306   while(blkCnt > 0u)
    00307   {
    00308     /* f0(n) = x(n) */
    00309     fcurr1 = *pSrc++;
    00310
    00311     /* Initialize coeff pointer */
    00312     pk = (pCoeffs);
    00313
    00314     /* Initialize state pointer */
    00315     px = pState;
    00316
    00317     /* read g2(n) from state buffer */
    00318     gcurr1 = *px;
    00319
    00320     /* for sample 1 processing */
    00321     /* f1(n) = f0(n) +  K1 * g0(n-1) */
    00322     fnext1 = (q31_t) (((q63_t) gcurr1 * (*pk)) >> 31) + fcurr1;
    00323     /* g1(n) = f0(n) * K1  +  g0(n-1) */
    00324     gnext1 = (q31_t) (((q63_t) fcurr1 * (*pk++)) >> 31) + gcurr1;
    00325     /* save g1(n) in state buffer */
    00326     *px++ = fcurr1;
    00327
    00328     /* f1(n) is saved in fcurr1
    00329        for next stage processing */
    00330     fcurr1 = fnext1;
    00331
    00332     stageCnt = (numStages - 1u);
    00333
    00334     /* stage loop */
    00335     while(stageCnt > 0u)
    00336     {
    00337       /* read g2(n) from state buffer */
    00338       gcurr1 = *px;
    00339
    00340       /* save g1(n) in state buffer */
    00341       *px++ = gnext1;
    00342
    00343       /* Sample processing for K2, K3.... */
    00344       /* f2(n) = f1(n) +  K2 * g1(n-1) */
    00345       fnext1 = (q31_t) (((q63_t) gcurr1 * (*pk)) >> 31) + fcurr1;
    00346       /* g2(n) = f1(n) * K2  +  g1(n-1) */
    00347       gnext1 = (q31_t) (((q63_t) fcurr1 * (*pk++)) >> 31) + gcurr1;
    00348
    00349       /* f1(n) is saved in fcurr1
    00350          for next stage processing */
    00351       fcurr1 = fnext1;
    00352
    00353       stageCnt--;
    00354
    00355     }
    00356
    00357     /* y(n) = fN(n) */
    00358     *pDst++ = fcurr1;
    00359
    00360     blkCnt--;
    00361
    00362   }
    00363
    00364 #else
    00365
    00366 /* Run the below code for Cortex-M0 */
    00367
    00368   q31_t fcurr, fnext, gcurr, gnext;              /* temporary variables */
    00369   uint32_t numStages = S->numStages;             /* Length of the filter */
    00370   uint32_t blkCnt, stageCnt;                     /* temporary variables for counts */
    00371
    00372   pState = &S->pState[0];
    00373
    00374   blkCnt = blockSize;
    00375
    00376   while(blkCnt > 0u)
    00377   {
    00378     /* f0(n) = x(n) */
    00379     fcurr = *pSrc++;
    00380
    00381     /* Initialize coeff pointer */
    00382     pk = (pCoeffs);
    00383
    00384     /* Initialize state pointer */
    00385     px = pState;
    00386
    00387     /* read g0(n-1) from state buffer */
    00388     gcurr = *px;
    00389
    00390     /* for sample 1 processing */
    00391     /* f1(n) = f0(n) +  K1 * g0(n-1) */
    00392     fnext = (q31_t) (((q63_t) gcurr * (*pk)) >> 31) + fcurr;
    00393     /* g1(n) = f0(n) * K1  +  g0(n-1) */
    00394     gnext = (q31_t) (((q63_t) fcurr * (*pk++)) >> 31) + gcurr;
    00395     /* save g1(n) in state buffer */
    00396     *px++ = fcurr;
    00397
    00398     /* f1(n) is saved in fcurr1
    00399        for next stage processing */
    00400     fcurr = fnext;
    00401
    00402     stageCnt = (numStages - 1u);
    00403
    00404     /* stage loop */
    00405     while(stageCnt > 0u)
    00406     {
    00407       /* read g2(n) from state buffer */
    00408       gcurr = *px;
    00409
    00410       /* save g1(n) in state buffer */
    00411       *px++ = gnext;
    00412
    00413       /* Sample processing for K2, K3.... */
    00414       /* f2(n) = f1(n) +  K2 * g1(n-1) */
    00415       fnext = (q31_t) (((q63_t) gcurr * (*pk)) >> 31) + fcurr;
    00416       /* g2(n) = f1(n) * K2  +  g1(n-1) */
    00417       gnext = (q31_t) (((q63_t) fcurr * (*pk++)) >> 31) + gcurr;
    00418
    00419       /* f1(n) is saved in fcurr1
    00420          for next stage processing */
    00421       fcurr = fnext;
    00422
    00423       stageCnt--;
    00424
    00425     }
    00426
    00427     /* y(n) = fN(n) */
    00428     *pDst++ = fcurr;
    00429
    00430     blkCnt--;
    00431
    00432   }
    00433
    00434 #endif /*   #ifndef ARM_MATH_CM0 */
    00435
    00436 }
    00437

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines