Subversion Repositories ScreenTimer

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /* ----------------------------------------------------------------------
  2.  * Project:      CMSIS DSP Library
  3.  * Title:        arm_fir_q7.c
  4.  * Description:  Q7 FIR filter processing function
  5.  *
  6.  * $Date:        27. January 2017
  7.  * $Revision:    V.1.5.1
  8.  *
  9.  * Target Processor: Cortex-M cores
  10.  * -------------------------------------------------------------------- */
  11. /*
  12.  * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved.
  13.  *
  14.  * SPDX-License-Identifier: Apache-2.0
  15.  *
  16.  * Licensed under the Apache License, Version 2.0 (the License); you may
  17.  * not use this file except in compliance with the License.
  18.  * You may obtain a copy of the License at
  19.  *
  20.  * www.apache.org/licenses/LICENSE-2.0
  21.  *
  22.  * Unless required by applicable law or agreed to in writing, software
  23.  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  24.  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25.  * See the License for the specific language governing permissions and
  26.  * limitations under the License.
  27.  */
  28.  
  29. #include "arm_math.h"
  30.  
  31. /**
  32.  * @ingroup groupFilters
  33.  */
  34.  
  35. /**
  36.  * @addtogroup FIR
  37.  * @{
  38.  */
  39.  
  40. /**
  41.  * @param[in]   *S points to an instance of the Q7 FIR filter structure.
  42.  * @param[in]   *pSrc points to the block of input data.
  43.  * @param[out]  *pDst points to the block of output data.
  44.  * @param[in]   blockSize number of samples to process per call.
  45.  * @return      none.
  46.  *
  47.  * <b>Scaling and Overflow Behavior:</b>
  48.  * \par
  49.  * The function is implemented using a 32-bit internal accumulator.
  50.  * Both coefficients and state variables are represented in 1.7 format and multiplications yield a 2.14 result.
  51.  * The 2.14 intermediate results are accumulated in a 32-bit accumulator in 18.14 format.
  52.  * There is no risk of internal overflow with this approach and the full precision of intermediate multiplications is preserved.
  53.  * The accumulator is converted to 18.7 format by discarding the low 7 bits.
  54.  * Finally, the result is truncated to 1.7 format.
  55.  */
  56.  
  57. void arm_fir_q7(
  58.   const arm_fir_instance_q7 * S,
  59.   q7_t * pSrc,
  60.   q7_t * pDst,
  61.   uint32_t blockSize)
  62. {
  63.  
  64. #if defined (ARM_MATH_DSP)
  65.  
  66.   /* Run the below code for Cortex-M4 and Cortex-M3 */
  67.  
  68.   q7_t *pState = S->pState;                      /* State pointer */
  69.   q7_t *pCoeffs = S->pCoeffs;                    /* Coefficient pointer */
  70.   q7_t *pStateCurnt;                             /* Points to the current sample of the state */
  71.   q7_t x0, x1, x2, x3;                           /* Temporary variables to hold state */
  72.   q7_t c0;                                       /* Temporary variable to hold coefficient value */
  73.   q7_t *px;                                      /* Temporary pointer for state */
  74.   q7_t *pb;                                      /* Temporary pointer for coefficient buffer */
  75.   q31_t acc0, acc1, acc2, acc3;                  /* Accumulators */
  76.   uint32_t numTaps = S->numTaps;                 /* Number of filter coefficients in the filter */
  77.   uint32_t i, tapCnt, blkCnt;                    /* Loop counters */
  78.  
  79.   /* S->pState points to state array which contains previous frame (numTaps - 1) samples */
  80.   /* pStateCurnt points to the location where the new input data should be written */
  81.   pStateCurnt = &(S->pState[(numTaps - 1U)]);
  82.  
  83.   /* Apply loop unrolling and compute 4 output values simultaneously.
  84.    * The variables acc0 ... acc3 hold output values that are being computed:
  85.    *
  86.    *    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]
  87.    *    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]
  88.    *    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]
  89.    *    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]
  90.    */
  91.   blkCnt = blockSize >> 2;
  92.  
  93.   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
  94.    ** a second loop below computes the remaining 1 to 3 samples. */
  95.   while (blkCnt > 0U)
  96.   {
  97.     /* Copy four new input samples into the state buffer */
  98.     *pStateCurnt++ = *pSrc++;
  99.     *pStateCurnt++ = *pSrc++;
  100.     *pStateCurnt++ = *pSrc++;
  101.     *pStateCurnt++ = *pSrc++;
  102.  
  103.     /* Set all accumulators to zero */
  104.     acc0 = 0;
  105.     acc1 = 0;
  106.     acc2 = 0;
  107.     acc3 = 0;
  108.  
  109.     /* Initialize state pointer */
  110.     px = pState;
  111.  
  112.     /* Initialize coefficient pointer */
  113.     pb = pCoeffs;
  114.  
  115.     /* Read the first three samples from the state buffer:
  116.      *  x[n-numTaps], x[n-numTaps-1], x[n-numTaps-2] */
  117.     x0 = *(px++);
  118.     x1 = *(px++);
  119.     x2 = *(px++);
  120.  
  121.     /* Loop unrolling.  Process 4 taps at a time. */
  122.     tapCnt = numTaps >> 2;
  123.     i = tapCnt;
  124.  
  125.     while (i > 0U)
  126.     {
  127.       /* Read the b[numTaps] coefficient */
  128.       c0 = *pb;
  129.  
  130.       /* Read x[n-numTaps-3] sample */
  131.       x3 = *px;
  132.  
  133.       /* acc0 +=  b[numTaps] * x[n-numTaps] */
  134.       acc0 += ((q15_t) x0 * c0);
  135.  
  136.       /* acc1 +=  b[numTaps] * x[n-numTaps-1] */
  137.       acc1 += ((q15_t) x1 * c0);
  138.  
  139.       /* acc2 +=  b[numTaps] * x[n-numTaps-2] */
  140.       acc2 += ((q15_t) x2 * c0);
  141.  
  142.       /* acc3 +=  b[numTaps] * x[n-numTaps-3] */
  143.       acc3 += ((q15_t) x3 * c0);
  144.  
  145.       /* Read the b[numTaps-1] coefficient */
  146.       c0 = *(pb + 1U);
  147.  
  148.       /* Read x[n-numTaps-4] sample */
  149.       x0 = *(px + 1U);
  150.  
  151.       /* Perform the multiply-accumulates */
  152.       acc0 += ((q15_t) x1 * c0);
  153.       acc1 += ((q15_t) x2 * c0);
  154.       acc2 += ((q15_t) x3 * c0);
  155.       acc3 += ((q15_t) x0 * c0);
  156.  
  157.       /* Read the b[numTaps-2] coefficient */
  158.       c0 = *(pb + 2U);
  159.  
  160.       /* Read x[n-numTaps-5] sample */
  161.       x1 = *(px + 2U);
  162.  
  163.       /* Perform the multiply-accumulates */
  164.       acc0 += ((q15_t) x2 * c0);
  165.       acc1 += ((q15_t) x3 * c0);
  166.       acc2 += ((q15_t) x0 * c0);
  167.       acc3 += ((q15_t) x1 * c0);
  168.  
  169.       /* Read the b[numTaps-3] coefficients */
  170.       c0 = *(pb + 3U);
  171.  
  172.       /* Read x[n-numTaps-6] sample */
  173.       x2 = *(px + 3U);
  174.  
  175.       /* Perform the multiply-accumulates */
  176.       acc0 += ((q15_t) x3 * c0);
  177.       acc1 += ((q15_t) x0 * c0);
  178.       acc2 += ((q15_t) x1 * c0);
  179.       acc3 += ((q15_t) x2 * c0);
  180.  
  181.       /* update coefficient pointer */
  182.       pb += 4U;
  183.       px += 4U;
  184.  
  185.       /* Decrement the loop counter */
  186.       i--;
  187.     }
  188.  
  189.     /* If the filter length is not a multiple of 4, compute the remaining filter taps */
  190.  
  191.     i = numTaps - (tapCnt * 4U);
  192.     while (i > 0U)
  193.     {
  194.       /* Read coefficients */
  195.       c0 = *(pb++);
  196.  
  197.       /* Fetch 1 state variable */
  198.       x3 = *(px++);
  199.  
  200.       /* Perform the multiply-accumulates */
  201.       acc0 += ((q15_t) x0 * c0);
  202.       acc1 += ((q15_t) x1 * c0);
  203.       acc2 += ((q15_t) x2 * c0);
  204.       acc3 += ((q15_t) x3 * c0);
  205.  
  206.       /* Reuse the present sample states for next sample */
  207.       x0 = x1;
  208.       x1 = x2;
  209.       x2 = x3;
  210.  
  211.       /* Decrement the loop counter */
  212.       i--;
  213.     }
  214.  
  215.     /* Advance the state pointer by 4 to process the next group of 4 samples */
  216.     pState = pState + 4;
  217.  
  218.     /* The results in the 4 accumulators are in 2.62 format.  Convert to 1.31
  219.      ** Then store the 4 outputs in the destination buffer. */
  220.     acc0 = __SSAT((acc0 >> 7U), 8);
  221.     *pDst++ = acc0;
  222.     acc1 = __SSAT((acc1 >> 7U), 8);
  223.     *pDst++ = acc1;
  224.     acc2 = __SSAT((acc2 >> 7U), 8);
  225.     *pDst++ = acc2;
  226.     acc3 = __SSAT((acc3 >> 7U), 8);
  227.     *pDst++ = acc3;
  228.  
  229.     /* Decrement the samples loop counter */
  230.     blkCnt--;
  231.   }
  232.  
  233.  
  234.   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  235.    ** No loop unrolling is used. */
  236.   blkCnt = blockSize % 4U;
  237.  
  238.   while (blkCnt > 0U)
  239.   {
  240.     /* Copy one sample at a time into state buffer */
  241.     *pStateCurnt++ = *pSrc++;
  242.  
  243.     /* Set the accumulator to zero */
  244.     acc0 = 0;
  245.  
  246.     /* Initialize state pointer */
  247.     px = pState;
  248.  
  249.     /* Initialize Coefficient pointer */
  250.     pb = (pCoeffs);
  251.  
  252.     i = numTaps;
  253.  
  254.     /* Perform the multiply-accumulates */
  255.     do
  256.     {
  257.       acc0 += (q15_t) * (px++) * (*(pb++));
  258.       i--;
  259.     } while (i > 0U);
  260.  
  261.     /* The result is in 2.14 format.  Convert to 1.7
  262.      ** Then store the output in the destination buffer. */
  263.     *pDst++ = __SSAT((acc0 >> 7U), 8);
  264.  
  265.     /* Advance state pointer by 1 for the next sample */
  266.     pState = pState + 1;
  267.  
  268.     /* Decrement the samples loop counter */
  269.     blkCnt--;
  270.   }
  271.  
  272.   /* Processing is complete.
  273.    ** Now copy the last numTaps - 1 samples to the satrt of the state buffer.
  274.    ** This prepares the state buffer for the next function call. */
  275.  
  276.   /* Points to the start of the state buffer */
  277.   pStateCurnt = S->pState;
  278.  
  279.   tapCnt = (numTaps - 1U) >> 2U;
  280.  
  281.   /* copy data */
  282.   while (tapCnt > 0U)
  283.   {
  284.     *pStateCurnt++ = *pState++;
  285.     *pStateCurnt++ = *pState++;
  286.     *pStateCurnt++ = *pState++;
  287.     *pStateCurnt++ = *pState++;
  288.  
  289.     /* Decrement the loop counter */
  290.     tapCnt--;
  291.   }
  292.  
  293.   /* Calculate remaining number of copies */
  294.   tapCnt = (numTaps - 1U) % 0x4U;
  295.  
  296.   /* Copy the remaining q31_t data */
  297.   while (tapCnt > 0U)
  298.   {
  299.     *pStateCurnt++ = *pState++;
  300.  
  301.     /* Decrement the loop counter */
  302.     tapCnt--;
  303.   }
  304.  
  305. #else
  306.  
  307. /* Run the below code for Cortex-M0 */
  308.  
  309.   uint32_t numTaps = S->numTaps;                 /* Number of taps in the filter */
  310.   uint32_t i, blkCnt;                            /* Loop counters */
  311.   q7_t *pState = S->pState;                      /* State pointer */
  312.   q7_t *pCoeffs = S->pCoeffs;                    /* Coefficient pointer */
  313.   q7_t *px, *pb;                                 /* Temporary pointers to state and coeff */
  314.   q31_t acc = 0;                                 /* Accumlator */
  315.   q7_t *pStateCurnt;                             /* Points to the current sample of the state */
  316.  
  317.  
  318.   /* S->pState points to state array which contains previous frame (numTaps - 1) samples */
  319.   /* pStateCurnt points to the location where the new input data should be written */
  320.   pStateCurnt = S->pState + (numTaps - 1U);
  321.  
  322.   /* Initialize blkCnt with blockSize */
  323.   blkCnt = blockSize;
  324.  
  325.   /* Perform filtering upto BlockSize - BlockSize%4  */
  326.   while (blkCnt > 0U)
  327.   {
  328.     /* Copy one sample at a time into state buffer */
  329.     *pStateCurnt++ = *pSrc++;
  330.  
  331.     /* Set accumulator to zero */
  332.     acc = 0;
  333.  
  334.     /* Initialize state pointer of type q7 */
  335.     px = pState;
  336.  
  337.     /* Initialize coeff pointer of type q7 */
  338.     pb = pCoeffs;
  339.  
  340.  
  341.     i = numTaps;
  342.  
  343.     while (i > 0U)
  344.     {
  345.       /* 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] */
  346.       acc += (q15_t) * px++ * *pb++;
  347.       i--;
  348.     }
  349.  
  350.     /* Store the 1.7 format filter output in destination buffer */
  351.     *pDst++ = (q7_t) __SSAT((acc >> 7), 8);
  352.  
  353.     /* Advance the state pointer by 1 to process the next sample */
  354.     pState = pState + 1;
  355.  
  356.     /* Decrement the loop counter */
  357.     blkCnt--;
  358.   }
  359.  
  360.   /* Processing is complete.
  361.    ** Now copy the last numTaps - 1 samples to the satrt of the state buffer.
  362.    ** This prepares the state buffer for the next function call. */
  363.  
  364.  
  365.   /* Points to the start of the state buffer */
  366.   pStateCurnt = S->pState;
  367.  
  368.  
  369.   /* Copy numTaps number of values */
  370.   i = (numTaps - 1U);
  371.  
  372.   /* Copy q7_t data */
  373.   while (i > 0U)
  374.   {
  375.     *pStateCurnt++ = *pState++;
  376.     i--;
  377.   }
  378.  
  379. #endif /*   #if defined (ARM_MATH_DSP) */
  380.  
  381. }
  382.  
  383. /**
  384.  * @} end of FIR group
  385.  */
  386.