CMSIS DSP Software Library: arm_fir_q31.c Source File

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

- File List
- Globals

# arm_fir_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_q31.c
    00009 *
    00010 * Description:  Q31 FIR 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.5  2010/04/26
    00030 *    incorporated review comments and updated with latest CMSIS layer
    00031 *
    00032 * Version 0.0.3  2010/03/10
    00033 *    Initial version
    00034 * -------------------------------------------------------------------- */
    00035
    00036 #include "arm_math.h"
    00037
    00067 void arm_fir_q31(
    00068   const arm_fir_instance_q31 * S,
    00069   q31_t * pSrc,
    00070   q31_t * pDst,
    00071   uint32_t blockSize)
    00072 {
    00073   q31_t *pState = S->pState;                     /* State pointer */
    00074   q31_t *pCoeffs = S->pCoeffs;                   /* Coefficient pointer */
    00075   q31_t *pStateCurnt;                            /* Points to the current sample of the state */
    00076
    00077
    00078 #ifndef ARM_MATH_CM0
    00079
    00080   /* Run the below code for Cortex-M4 and Cortex-M3 */
    00081
    00082   q31_t x0, x1, x2, x3;                          /* Temporary variables to hold state */
    00083   q31_t c0;                                      /* Temporary variable to hold coefficient value */
    00084   q31_t *px;                                     /* Temporary pointer for state */
    00085   q31_t *pb;                                     /* Temporary pointer for coefficient buffer */
    00086   q63_t acc0, acc1, acc2, acc3;                  /* Accumulators */
    00087   uint32_t numTaps = S->numTaps;                 /* Number of filter coefficients in the filter */
    00088   uint32_t i, tapCnt, blkCnt;                    /* Loop counters */
    00089
    00090   /* S->pState points to state array which contains previous frame (numTaps - 1) samples */
    00091   /* pStateCurnt points to the location where the new input data should be written */
    00092   pStateCurnt = &(S->pState[(numTaps - 1u)]);
    00093
    00094   /* Apply loop unrolling and compute 4 output values simultaneously.
    00095    * The variables acc0 ... acc3 hold output values that are being computed:
    00096    *
    00097    *    acc0 =  b[numTaps-1] * x[n-numTaps-1] + b[numTaps-2] * x[n-numTaps-2] + b[numTaps-3] * x[n-numTaps-3] +...+ b[0] * x[0]
    00098    *    acc1 =  b[numTaps-1] * x[n-numTaps] +   b[numTaps-2] * x[n-numTaps-1] + b[numTaps-3] * x[n-numTaps-2] +...+ b[0] * x[1]
    00099    *    acc2 =  b[numTaps-1] * x[n-numTaps+1] + b[numTaps-2] * x[n-numTaps] +   b[numTaps-3] * x[n-numTaps-1] +...+ b[0] * x[2]
    00100    *    acc3 =  b[numTaps-1] * x[n-numTaps+2] + b[numTaps-2] * x[n-numTaps+1] + b[numTaps-3] * x[n-numTaps]   +...+ b[0] * x[3]
    00101    */
    00102   blkCnt = blockSize >> 2;
    00103
    00104   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
    00105    ** a second loop below computes the remaining 1 to 3 samples. */
    00106   while(blkCnt > 0u)
    00107   {
    00108     /* Copy four new input samples into the state buffer */
    00109     *pStateCurnt++ = *pSrc++;
    00110     *pStateCurnt++ = *pSrc++;
    00111     *pStateCurnt++ = *pSrc++;
    00112     *pStateCurnt++ = *pSrc++;
    00113
    00114     /* Set all accumulators to zero */
    00115     acc0 = 0;
    00116     acc1 = 0;
    00117     acc2 = 0;
    00118     acc3 = 0;
    00119
    00120     /* Initialize state pointer */
    00121     px = pState;
    00122
    00123     /* Initialize coefficient pointer */
    00124     pb = pCoeffs;
    00125
    00126     /* Read the first three samples from the state buffer:
    00127      *  x[n-numTaps], x[n-numTaps-1], x[n-numTaps-2] */
    00128     x0 = *(px++);
    00129     x1 = *(px++);
    00130     x2 = *(px++);
    00131
    00132     /* Loop unrolling.  Process 4 taps at a time. */
    00133     tapCnt = numTaps >> 2;
    00134     i = tapCnt;
    00135
    00136     while(i > 0u)
    00137     {
    00138       /* Read the b[numTaps] coefficient */
    00139       c0 = *(pb++);
    00140
    00141       /* Read x[n-numTaps-3] sample */
    00142       x3 = *(px++);
    00143
    00144       /* acc0 +=  b[numTaps] * x[n-numTaps] */
    00145       acc0 += ((q63_t) x0 * c0);
    00146
    00147       /* acc1 +=  b[numTaps] * x[n-numTaps-1] */
    00148       acc1 += ((q63_t) x1 * c0);
    00149
    00150       /* acc2 +=  b[numTaps] * x[n-numTaps-2] */
    00151       acc2 += ((q63_t) x2 * c0);
    00152
    00153       /* acc3 +=  b[numTaps] * x[n-numTaps-3] */
    00154       acc3 += ((q63_t) x3 * c0);
    00155
    00156       /* Read the b[numTaps-1] coefficient */
    00157       c0 = *(pb++);
    00158
    00159       /* Read x[n-numTaps-4] sample */
    00160       x0 = *(px++);
    00161
    00162       /* Perform the multiply-accumulates */
    00163       acc0 += ((q63_t) x1 * c0);
    00164       acc1 += ((q63_t) x2 * c0);
    00165       acc2 += ((q63_t) x3 * c0);
    00166       acc3 += ((q63_t) x0 * c0);
    00167
    00168       /* Read the b[numTaps-2] coefficient */
    00169       c0 = *(pb++);
    00170
    00171       /* Read x[n-numTaps-5] sample */
    00172       x1 = *(px++);
    00173
    00174       /* Perform the multiply-accumulates */
    00175       acc0 += ((q63_t) x2 * c0);
    00176       acc1 += ((q63_t) x3 * c0);
    00177       acc2 += ((q63_t) x0 * c0);
    00178       acc3 += ((q63_t) x1 * c0);
    00179       /* Read the b[numTaps-3] coefficients */
    00180       c0 = *(pb++);
    00181
    00182       /* Read x[n-numTaps-6] sample */
    00183       x2 = *(px++);
    00184
    00185       /* Perform the multiply-accumulates */
    00186       acc0 += ((q63_t) x3 * c0);
    00187       acc1 += ((q63_t) x0 * c0);
    00188       acc2 += ((q63_t) x1 * c0);
    00189       acc3 += ((q63_t) x2 * c0);
    00190       i--;
    00191     }
    00192
    00193     /* If the filter length is not a multiple of 4, compute the remaining filter taps */
    00194
    00195     i = numTaps - (tapCnt * 4u);
    00196     while(i > 0u)
    00197     {
    00198       /* Read coefficients */
    00199       c0 = *(pb++);
    00200
    00201       /* Fetch 1 state variable */
    00202       x3 = *(px++);
    00203
    00204       /* Perform the multiply-accumulates */
    00205       acc0 += ((q63_t) x0 * c0);
    00206       acc1 += ((q63_t) x1 * c0);
    00207       acc2 += ((q63_t) x2 * c0);
    00208       acc3 += ((q63_t) x3 * c0);
    00209
    00210       /* Reuse the present sample states for next sample */
    00211       x0 = x1;
    00212       x1 = x2;
    00213       x2 = x3;
    00214
    00215       /* Decrement the loop counter */
    00216       i--;
    00217     }
    00218
    00219     /* Advance the state pointer by 4 to process the next group of 4 samples */
    00220     pState = pState + 4;
    00221
    00222     /* The results in the 4 accumulators are in 2.62 format.  Convert to 1.31
    00223      ** Then store the 4 outputs in the destination buffer. */
    00224     *pDst++ = (q31_t) (acc0 >> 31u);
    00225     *pDst++ = (q31_t) (acc1 >> 31u);
    00226     *pDst++ = (q31_t) (acc2 >> 31u);
    00227     *pDst++ = (q31_t) (acc3 >> 31u);
    00228
    00229     /* Decrement the samples loop counter */
    00230     blkCnt--;
    00231   }
    00232
    00233
    00234   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
    00235    ** No loop unrolling is used. */
    00236   blkCnt = blockSize % 4u;
    00237
    00238   while(blkCnt > 0u)
    00239   {
    00240     /* Copy one sample at a time into state buffer */
    00241     *pStateCurnt++ = *pSrc++;
    00242
    00243     /* Set the accumulator to zero */
    00244     acc0 = 0;
    00245
    00246     /* Initialize state pointer */
    00247     px = pState;
    00248
    00249     /* Initialize Coefficient pointer */
    00250     pb = (pCoeffs);
    00251
    00252     i = numTaps;
    00253
    00254     /* Perform the multiply-accumulates */
    00255     do
    00256     {
    00257       acc0 += (q63_t) * (px++) * (*(pb++));
    00258       i--;
    00259     } while(i > 0u);
    00260
    00261     /* The result is in 2.62 format.  Convert to 1.31
    00262      ** Then store the output in the destination buffer. */
    00263     *pDst++ = (q31_t) (acc0 >> 31u);
    00264
    00265     /* Advance state pointer by 1 for the next sample */
    00266     pState = pState + 1;
    00267
    00268     /* Decrement the samples loop counter */
    00269     blkCnt--;
    00270   }
    00271
    00272   /* Processing is complete.
    00273    ** Now copy the last numTaps - 1 samples to the satrt of the state buffer.
    00274    ** This prepares the state buffer for the next function call. */
    00275
    00276   /* Points to the start of the state buffer */
    00277   pStateCurnt = S->pState;
    00278
    00279   tapCnt = (numTaps - 1u) >> 2u;
    00280
    00281   /* copy data */
    00282   while(tapCnt > 0u)
    00283   {
    00284     *pStateCurnt++ = *pState++;
    00285     *pStateCurnt++ = *pState++;
    00286     *pStateCurnt++ = *pState++;
    00287     *pStateCurnt++ = *pState++;
    00288
    00289     /* Decrement the loop counter */
    00290     tapCnt--;
    00291   }
    00292
    00293   /* Calculate remaining number of copies */
    00294   tapCnt = (numTaps - 1u) % 0x4u;
    00295
    00296   /* Copy the remaining q31_t data */
    00297   while(tapCnt > 0u)
    00298   {
    00299     *pStateCurnt++ = *pState++;
    00300
    00301     /* Decrement the loop counter */
    00302     tapCnt--;
    00303   }
    00304
    00305 #else
    00306
    00307 /* Run the below code for Cortex-M0 */
    00308
    00309   q31_t *px;                                     /* Temporary pointer for state */
    00310   q31_t *pb;                                     /* Temporary pointer for coefficient buffer */
    00311   q63_t acc;                                     /* Accumulator */
    00312   uint32_t numTaps = S->numTaps;                 /* Length of the filter */
    00313   uint32_t i, tapCnt, blkCnt;                    /* Loop counters */
    00314
    00315   /* S->pState buffer contains previous frame (numTaps - 1) samples */
    00316   /* pStateCurnt points to the location where the new input data should be written */
    00317   pStateCurnt = &(S->pState[(numTaps - 1u)]);
    00318
    00319   /* Initialize blkCnt with blockSize */
    00320   blkCnt = blockSize;
    00321
    00322   while(blkCnt > 0u)
    00323   {
    00324     /* Copy one sample at a time into state buffer */
    00325     *pStateCurnt++ = *pSrc++;
    00326
    00327     /* Set the accumulator to zero */
    00328     acc = 0;
    00329
    00330     /* Initialize state pointer */
    00331     px = pState;
    00332
    00333     /* Initialize Coefficient pointer */
    00334     pb = pCoeffs;
    00335
    00336     i = numTaps;
    00337
    00338     /* Perform the multiply-accumulates */
    00339     do
    00340     {
    00341       /* acc =  b[numTaps-1] * x[n-numTaps-1] + b[numTaps-2] * x[n-numTaps-2] + b[numTaps-3] * x[n-numTaps-3] +...+ b[0] * x[0] */
    00342       acc += (q63_t) * px++ * *pb++;
    00343       i--;
    00344     } while(i > 0u);
    00345
    00346     /* The result is in 2.62 format.  Convert to 1.31
    00347      ** Then store the output in the destination buffer. */
    00348     *pDst++ = (q31_t) (acc >> 31u);
    00349
    00350     /* Advance state pointer by 1 for the next sample */
    00351     pState = pState + 1;
    00352
    00353     /* Decrement the samples loop counter */
    00354     blkCnt--;
    00355   }
    00356
    00357   /* Processing is complete.
    00358    ** Now copy the last numTaps - 1 samples to the starting of the state buffer.
    00359    ** This prepares the state buffer for the next function call. */
    00360
    00361   /* Points to the start of the state buffer */
    00362   pStateCurnt = S->pState;
    00363
    00364   /* Copy numTaps number of values */
    00365   tapCnt = numTaps - 1u;
    00366
    00367   /* Copy the data */
    00368   while(tapCnt > 0u)
    00369   {
    00370     *pStateCurnt++ = *pState++;
    00371
    00372     /* Decrement the loop counter */
    00373     tapCnt--;
    00374   }
    00375
    00376
    00377 #endif /*  #ifndef ARM_MATH_CM0 */
    00378
    00379 }
    00380

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines