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

- File List
- Globals

# arm_fir_fast_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_fast_q31.c
    00009 *
    00010 * Description:  Processing function for the Q31 Fast FIR filter.
    00011 *
    00012 * Target Processor: Cortex-M4/Cortex-M3
    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.9  2010/08/27
    00030 *    Initial version
    00031 * -------------------------------------------------------------------- */
    00032
    00033 #include "arm_math.h"
    00034
    00066 void arm_fir_fast_q31(
    00067   const arm_fir_instance_q31 * S,
    00068   q31_t * pSrc,
    00069   q31_t * pDst,
    00070   uint32_t blockSize)
    00071 {
    00072   q31_t *pState = S->pState;                     /* State pointer */
    00073   q31_t *pCoeffs = S->pCoeffs;                   /* Coefficient pointer */
    00074   q31_t *pStateCurnt;                            /* Points to the current sample of the state */
    00075   q31_t x0, x1, x2, x3;                          /* Temporary variables to hold state */
    00076   q31_t c0;                                      /* Temporary variable to hold coefficient value */
    00077   q31_t *px;                                     /* Temporary pointer for state */
    00078   q31_t *pb;                                     /* Temporary pointer for coefficient buffer */
    00079   q63_t acc0, acc1, acc2, acc3;                  /* Accumulators */
    00080   uint32_t numTaps = S->numTaps;                 /* Number of filter coefficients in the filter */
    00081   uint32_t i, tapCnt, blkCnt;                    /* Loop counters */
    00082
    00083   /* S->pState points to buffer which contains previous frame (numTaps - 1) samples */
    00084   /* pStateCurnt points to the location where the new input data should be written */
    00085   pStateCurnt = &(S->pState[(numTaps - 1u)]);
    00086
    00087   /* Apply loop unrolling and compute 4 output values simultaneously.
    00088    * The variables acc0 ... acc3 hold output values that are being computed:
    00089    *
    00090    *    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]
    00091    *    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]
    00092    *    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]
    00093    *    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]
    00094    */
    00095   blkCnt = blockSize >> 2;
    00096
    00097   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
    00098    ** a second loop below computes the remaining 1 to 3 samples. */
    00099   while(blkCnt > 0u)
    00100   {
    00101     /* Copy four new input samples into the state buffer */
    00102     *pStateCurnt++ = *pSrc++;
    00103     *pStateCurnt++ = *pSrc++;
    00104     *pStateCurnt++ = *pSrc++;
    00105     *pStateCurnt++ = *pSrc++;
    00106
    00107     /* Set all accumulators to zero */
    00108     acc0 = 0;
    00109     acc1 = 0;
    00110     acc2 = 0;
    00111     acc3 = 0;
    00112
    00113     /* Initialize state pointer */
    00114     px = pState;
    00115
    00116     /* Initialize coefficient pointer */
    00117     pb = pCoeffs;
    00118
    00119     /* Read the first three samples from the state buffer:
    00120      *  x[n-numTaps], x[n-numTaps-1], x[n-numTaps-2] */
    00121     x0 = *(px++);
    00122     x1 = *(px++);
    00123     x2 = *(px++);
    00124
    00125     /* Loop unrolling.  Process 4 taps at a time. */
    00126     tapCnt = numTaps >> 2;
    00127     i = tapCnt;
    00128
    00129     while(i > 0u)
    00130     {
    00131       /* Read the b[numTaps] coefficient */
    00132       c0 = *(pb++);
    00133
    00134       /* Read x[n-numTaps-3] sample */
    00135       x3 = *(px++);
    00136
    00137       /* acc0 +=  b[numTaps] * x[n-numTaps] */
    00138       acc0 = (q31_t) ((((q63_t) x0 * c0) + (acc0 << 32)) >> 32);
    00139
    00140       /* acc1 +=  b[numTaps] * x[n-numTaps-1] */
    00141       acc1 = (q31_t) ((((q63_t) x1 * c0) + (acc1 << 32)) >> 32);
    00142
    00143       /* acc2 +=  b[numTaps] * x[n-numTaps-2] */
    00144       acc2 = (q31_t) ((((q63_t) x2 * c0) + (acc2 << 32)) >> 32);
    00145
    00146       /* acc3 +=  b[numTaps] * x[n-numTaps-3] */
    00147       acc3 = (q31_t) ((((q63_t) x3 * c0) + (acc3 << 32)) >> 32);
    00148
    00149       /* Read the b[numTaps-1] coefficient */
    00150       c0 = *(pb++);
    00151
    00152       /* Read x[n-numTaps-4] sample */
    00153       x0 = *(px++);
    00154
    00155       /* Perform the multiply-accumulates */
    00156       acc0 = (q31_t) ((((q63_t) x1 * c0) + (acc0 << 32)) >> 32);
    00157       acc1 = (q31_t) ((((q63_t) x2 * c0) + (acc1 << 32)) >> 32);
    00158       acc2 = (q31_t) ((((q63_t) x3 * c0) + (acc2 << 32)) >> 32);
    00159       acc3 = (q31_t) ((((q63_t) x0 * c0) + (acc3 << 32)) >> 32);
    00160
    00161       /* Read the b[numTaps-2] coefficient */
    00162       c0 = *(pb++);
    00163
    00164       /* Read x[n-numTaps-5] sample */
    00165       x1 = *(px++);
    00166
    00167       /* Perform the multiply-accumulates */
    00168       acc0 = (q31_t) ((((q63_t) x2 * c0) + (acc0 << 32)) >> 32);
    00169       acc1 = (q31_t) ((((q63_t) x3 * c0) + (acc1 << 32)) >> 32);
    00170       acc2 = (q31_t) ((((q63_t) x0 * c0) + (acc2 << 32)) >> 32);
    00171       acc3 = (q31_t) ((((q63_t) x1 * c0) + (acc3 << 32)) >> 32);
    00172
    00173       /* Read the b[numTaps-3] coefficients */
    00174       c0 = *(pb++);
    00175
    00176       /* Read x[n-numTaps-6] sample */
    00177       x2 = *(px++);
    00178
    00179       /* Perform the multiply-accumulates */
    00180       acc0 = (q31_t) ((((q63_t) x3 * c0) + (acc0 << 32)) >> 32);
    00181       acc1 = (q31_t) ((((q63_t) x0 * c0) + (acc1 << 32)) >> 32);
    00182       acc2 = (q31_t) ((((q63_t) x1 * c0) + (acc2 << 32)) >> 32);
    00183       acc3 = (q31_t) ((((q63_t) x2 * c0) + (acc3 << 32)) >> 32);
    00184       i--;
    00185     }
    00186
    00187     /* If the filter length is not a multiple of 4, compute the remaining filter taps */
    00188
    00189     i = numTaps - (tapCnt * 4u);
    00190     while(i > 0u)
    00191     {
    00192       /* Read coefficients */
    00193       c0 = *(pb++);
    00194
    00195       /* Fetch 1 state variable */
    00196       x3 = *(px++);
    00197
    00198       /* Perform the multiply-accumulates */
    00199       acc0 = (q31_t) ((((q63_t) x0 * c0) + (acc0 << 32)) >> 32);
    00200       acc1 = (q31_t) ((((q63_t) x1 * c0) + (acc1 << 32)) >> 32);
    00201       acc2 = (q31_t) ((((q63_t) x2 * c0) + (acc2 << 32)) >> 32);
    00202       acc3 = (q31_t) ((((q63_t) x3 * c0) + (acc3 << 32)) >> 32);
    00203
    00204       /* Reuse the present sample states for next sample */
    00205       x0 = x1;
    00206       x1 = x2;
    00207       x2 = x3;
    00208
    00209       /* Decrement the loop counter */
    00210       i--;
    00211     }
    00212
    00213     /* Advance the state pointer by 4 to process the next group of 4 samples */
    00214     pState = pState + 4;
    00215
    00216     /* The results in the 4 accumulators are in 2.30 format.  Convert to 1.31
    00217      ** Then store the 4 outputs in the destination buffer. */
    00218     *pDst++ = (q31_t) (acc0 << 1);
    00219     *pDst++ = (q31_t) (acc1 << 1);
    00220     *pDst++ = (q31_t) (acc2 << 1);
    00221     *pDst++ = (q31_t) (acc3 << 1);
    00222
    00223     /* Decrement the samples loop counter */
    00224     blkCnt--;
    00225   }
    00226
    00227
    00228   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
    00229    ** No loop unrolling is used. */
    00230   blkCnt = blockSize % 4u;
    00231
    00232   while(blkCnt > 0u)
    00233   {
    00234     /* Copy one sample at a time into state buffer */
    00235     *pStateCurnt++ = *pSrc++;
    00236
    00237     /* Set the accumulator to zero */
    00238     acc0 = 0;
    00239
    00240     /* Initialize state pointer */
    00241     px = pState;
    00242
    00243     /* Initialize Coefficient pointer */
    00244     pb = (pCoeffs);
    00245
    00246     i = numTaps;
    00247
    00248     /* Perform the multiply-accumulates */
    00249     do
    00250     {
    00251       acc0 = (q31_t) ((((q63_t) * (px++) * (*(pb++))) + (acc0 << 32)) >> 32);
    00252       i--;
    00253     } while(i > 0u);
    00254
    00255     /* The result is in 2.30 format.  Convert to 1.31
    00256      ** Then store the output in the destination buffer. */
    00257     *pDst++ = (q31_t) (acc0 << 1);
    00258
    00259     /* Advance state pointer by 1 for the next sample */
    00260     pState = pState + 1;
    00261
    00262     /* Decrement the samples loop counter */
    00263     blkCnt--;
    00264   }
    00265
    00266   /* Processing is complete.
    00267    ** Now copy the last numTaps - 1 samples to the satrt of the state buffer.
    00268    ** This prepares the state buffer for the next function call. */
    00269
    00270   /* Points to the start of the state buffer */
    00271   pStateCurnt = S->pState;
    00272
    00273   tapCnt = (numTaps - 1u) >> 2u;
    00274
    00275   /* copy data */
    00276   while(tapCnt > 0u)
    00277   {
    00278     *pStateCurnt++ = *pState++;
    00279     *pStateCurnt++ = *pState++;
    00280     *pStateCurnt++ = *pState++;
    00281     *pStateCurnt++ = *pState++;
    00282
    00283     /* Decrement the loop counter */
    00284     tapCnt--;
    00285   }
    00286
    00287   /* Calculate remaining number of copies */
    00288   tapCnt = (numTaps - 1u) % 0x4u;
    00289
    00290   /* Copy the remaining q31_t data */
    00291   while(tapCnt > 0u)
    00292   {
    00293     *pStateCurnt++ = *pState++;
    00294
    00295     /* Decrement the loop counter */
    00296     tapCnt--;
    00297   }
    00298
    00299 }
    00300

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines