CMSIS DSP library
Dependents: performance_timer Surfboard_ gps2rtty Capstone ... more
arm_fir_q7.c
00001 /* ---------------------------------------------------------------------- 00002 * Copyright (C) 2010-2014 ARM Limited. All rights reserved. 00003 * 00004 * $Date: 19. March 2015 00005 * $Revision: V.1.4.5 00006 * 00007 * Project: CMSIS DSP Library 00008 * Title: arm_fir_q7.c 00009 * 00010 * Description: Q7 FIR filter processing function. 00011 * 00012 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0 00013 * 00014 * Redistribution and use in source and binary forms, with or without 00015 * modification, are permitted provided that the following conditions 00016 * are met: 00017 * - Redistributions of source code must retain the above copyright 00018 * notice, this list of conditions and the following disclaimer. 00019 * - Redistributions in binary form must reproduce the above copyright 00020 * notice, this list of conditions and the following disclaimer in 00021 * the documentation and/or other materials provided with the 00022 * distribution. 00023 * - Neither the name of ARM LIMITED nor the names of its contributors 00024 * may be used to endorse or promote products derived from this 00025 * software without specific prior written permission. 00026 * 00027 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00028 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00029 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00030 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00031 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00032 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00033 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00034 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00035 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00036 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00037 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00038 * POSSIBILITY OF SUCH DAMAGE. 00039 * -------------------------------------------------------------------- */ 00040 00041 #include "arm_math.h" 00042 00043 /** 00044 * @ingroup groupFilters 00045 */ 00046 00047 /** 00048 * @addtogroup FIR 00049 * @{ 00050 */ 00051 00052 /** 00053 * @param[in] *S points to an instance of the Q7 FIR filter structure. 00054 * @param[in] *pSrc points to the block of input data. 00055 * @param[out] *pDst points to the block of output data. 00056 * @param[in] blockSize number of samples to process per call. 00057 * @return none. 00058 * 00059 * <b>Scaling and Overflow Behavior:</b> 00060 * \par 00061 * The function is implemented using a 32-bit internal accumulator. 00062 * Both coefficients and state variables are represented in 1.7 format and multiplications yield a 2.14 result. 00063 * The 2.14 intermediate results are accumulated in a 32-bit accumulator in 18.14 format. 00064 * There is no risk of internal overflow with this approach and the full precision of intermediate multiplications is preserved. 00065 * The accumulator is converted to 18.7 format by discarding the low 7 bits. 00066 * Finally, the result is truncated to 1.7 format. 00067 */ 00068 00069 void arm_fir_q7( 00070 const arm_fir_instance_q7 * S, 00071 q7_t * pSrc, 00072 q7_t * pDst, 00073 uint32_t blockSize) 00074 { 00075 00076 #ifndef ARM_MATH_CM0_FAMILY 00077 00078 /* Run the below code for Cortex-M4 and Cortex-M3 */ 00079 00080 q7_t *pState = S->pState; /* State pointer */ 00081 q7_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ 00082 q7_t *pStateCurnt; /* Points to the current sample of the state */ 00083 q7_t x0, x1, x2, x3; /* Temporary variables to hold state */ 00084 q7_t c0; /* Temporary variable to hold coefficient value */ 00085 q7_t *px; /* Temporary pointer for state */ 00086 q7_t *pb; /* Temporary pointer for coefficient buffer */ 00087 q31_t acc0, acc1, acc2, acc3; /* Accumulators */ 00088 uint32_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */ 00089 uint32_t i, tapCnt, blkCnt; /* Loop counters */ 00090 00091 /* S->pState points to state array which contains previous frame (numTaps - 1) samples */ 00092 /* pStateCurnt points to the location where the new input data should be written */ 00093 pStateCurnt = &(S->pState[(numTaps - 1u)]); 00094 00095 /* Apply loop unrolling and compute 4 output values simultaneously. 00096 * The variables acc0 ... acc3 hold output values that are being computed: 00097 * 00098 * 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] 00099 * 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] 00100 * 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] 00101 * 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] 00102 */ 00103 blkCnt = blockSize >> 2; 00104 00105 /* First part of the processing with loop unrolling. Compute 4 outputs at a time. 00106 ** a second loop below computes the remaining 1 to 3 samples. */ 00107 while(blkCnt > 0u) 00108 { 00109 /* Copy four new input samples into the state buffer */ 00110 *pStateCurnt++ = *pSrc++; 00111 *pStateCurnt++ = *pSrc++; 00112 *pStateCurnt++ = *pSrc++; 00113 *pStateCurnt++ = *pSrc++; 00114 00115 /* Set all accumulators to zero */ 00116 acc0 = 0; 00117 acc1 = 0; 00118 acc2 = 0; 00119 acc3 = 0; 00120 00121 /* Initialize state pointer */ 00122 px = pState; 00123 00124 /* Initialize coefficient pointer */ 00125 pb = pCoeffs; 00126 00127 /* Read the first three samples from the state buffer: 00128 * x[n-numTaps], x[n-numTaps-1], x[n-numTaps-2] */ 00129 x0 = *(px++); 00130 x1 = *(px++); 00131 x2 = *(px++); 00132 00133 /* Loop unrolling. Process 4 taps at a time. */ 00134 tapCnt = numTaps >> 2; 00135 i = tapCnt; 00136 00137 while(i > 0u) 00138 { 00139 /* Read the b[numTaps] coefficient */ 00140 c0 = *pb; 00141 00142 /* Read x[n-numTaps-3] sample */ 00143 x3 = *px; 00144 00145 /* acc0 += b[numTaps] * x[n-numTaps] */ 00146 acc0 += ((q15_t) x0 * c0); 00147 00148 /* acc1 += b[numTaps] * x[n-numTaps-1] */ 00149 acc1 += ((q15_t) x1 * c0); 00150 00151 /* acc2 += b[numTaps] * x[n-numTaps-2] */ 00152 acc2 += ((q15_t) x2 * c0); 00153 00154 /* acc3 += b[numTaps] * x[n-numTaps-3] */ 00155 acc3 += ((q15_t) x3 * c0); 00156 00157 /* Read the b[numTaps-1] coefficient */ 00158 c0 = *(pb + 1u); 00159 00160 /* Read x[n-numTaps-4] sample */ 00161 x0 = *(px + 1u); 00162 00163 /* Perform the multiply-accumulates */ 00164 acc0 += ((q15_t) x1 * c0); 00165 acc1 += ((q15_t) x2 * c0); 00166 acc2 += ((q15_t) x3 * c0); 00167 acc3 += ((q15_t) x0 * c0); 00168 00169 /* Read the b[numTaps-2] coefficient */ 00170 c0 = *(pb + 2u); 00171 00172 /* Read x[n-numTaps-5] sample */ 00173 x1 = *(px + 2u); 00174 00175 /* Perform the multiply-accumulates */ 00176 acc0 += ((q15_t) x2 * c0); 00177 acc1 += ((q15_t) x3 * c0); 00178 acc2 += ((q15_t) x0 * c0); 00179 acc3 += ((q15_t) x1 * c0); 00180 00181 /* Read the b[numTaps-3] coefficients */ 00182 c0 = *(pb + 3u); 00183 00184 /* Read x[n-numTaps-6] sample */ 00185 x2 = *(px + 3u); 00186 00187 /* Perform the multiply-accumulates */ 00188 acc0 += ((q15_t) x3 * c0); 00189 acc1 += ((q15_t) x0 * c0); 00190 acc2 += ((q15_t) x1 * c0); 00191 acc3 += ((q15_t) x2 * c0); 00192 00193 /* update coefficient pointer */ 00194 pb += 4u; 00195 px += 4u; 00196 00197 /* Decrement the loop counter */ 00198 i--; 00199 } 00200 00201 /* If the filter length is not a multiple of 4, compute the remaining filter taps */ 00202 00203 i = numTaps - (tapCnt * 4u); 00204 while(i > 0u) 00205 { 00206 /* Read coefficients */ 00207 c0 = *(pb++); 00208 00209 /* Fetch 1 state variable */ 00210 x3 = *(px++); 00211 00212 /* Perform the multiply-accumulates */ 00213 acc0 += ((q15_t) x0 * c0); 00214 acc1 += ((q15_t) x1 * c0); 00215 acc2 += ((q15_t) x2 * c0); 00216 acc3 += ((q15_t) x3 * c0); 00217 00218 /* Reuse the present sample states for next sample */ 00219 x0 = x1; 00220 x1 = x2; 00221 x2 = x3; 00222 00223 /* Decrement the loop counter */ 00224 i--; 00225 } 00226 00227 /* Advance the state pointer by 4 to process the next group of 4 samples */ 00228 pState = pState + 4; 00229 00230 /* The results in the 4 accumulators are in 2.62 format. Convert to 1.31 00231 ** Then store the 4 outputs in the destination buffer. */ 00232 acc0 = __SSAT((acc0 >> 7u), 8); 00233 *pDst++ = acc0; 00234 acc1 = __SSAT((acc1 >> 7u), 8); 00235 *pDst++ = acc1; 00236 acc2 = __SSAT((acc2 >> 7u), 8); 00237 *pDst++ = acc2; 00238 acc3 = __SSAT((acc3 >> 7u), 8); 00239 *pDst++ = acc3; 00240 00241 /* Decrement the samples loop counter */ 00242 blkCnt--; 00243 } 00244 00245 00246 /* If the blockSize is not a multiple of 4, compute any remaining output samples here. 00247 ** No loop unrolling is used. */ 00248 blkCnt = blockSize % 4u; 00249 00250 while(blkCnt > 0u) 00251 { 00252 /* Copy one sample at a time into state buffer */ 00253 *pStateCurnt++ = *pSrc++; 00254 00255 /* Set the accumulator to zero */ 00256 acc0 = 0; 00257 00258 /* Initialize state pointer */ 00259 px = pState; 00260 00261 /* Initialize Coefficient pointer */ 00262 pb = (pCoeffs); 00263 00264 i = numTaps; 00265 00266 /* Perform the multiply-accumulates */ 00267 do 00268 { 00269 acc0 += (q15_t) * (px++) * (*(pb++)); 00270 i--; 00271 } while(i > 0u); 00272 00273 /* The result is in 2.14 format. Convert to 1.7 00274 ** Then store the output in the destination buffer. */ 00275 *pDst++ = __SSAT((acc0 >> 7u), 8); 00276 00277 /* Advance state pointer by 1 for the next sample */ 00278 pState = pState + 1; 00279 00280 /* Decrement the samples loop counter */ 00281 blkCnt--; 00282 } 00283 00284 /* Processing is complete. 00285 ** Now copy the last numTaps - 1 samples to the satrt of the state buffer. 00286 ** This prepares the state buffer for the next function call. */ 00287 00288 /* Points to the start of the state buffer */ 00289 pStateCurnt = S->pState; 00290 00291 tapCnt = (numTaps - 1u) >> 2u; 00292 00293 /* copy data */ 00294 while(tapCnt > 0u) 00295 { 00296 *pStateCurnt++ = *pState++; 00297 *pStateCurnt++ = *pState++; 00298 *pStateCurnt++ = *pState++; 00299 *pStateCurnt++ = *pState++; 00300 00301 /* Decrement the loop counter */ 00302 tapCnt--; 00303 } 00304 00305 /* Calculate remaining number of copies */ 00306 tapCnt = (numTaps - 1u) % 0x4u; 00307 00308 /* Copy the remaining q31_t data */ 00309 while(tapCnt > 0u) 00310 { 00311 *pStateCurnt++ = *pState++; 00312 00313 /* Decrement the loop counter */ 00314 tapCnt--; 00315 } 00316 00317 #else 00318 00319 /* Run the below code for Cortex-M0 */ 00320 00321 uint32_t numTaps = S->numTaps; /* Number of taps in the filter */ 00322 uint32_t i, blkCnt; /* Loop counters */ 00323 q7_t *pState = S->pState; /* State pointer */ 00324 q7_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ 00325 q7_t *px, *pb; /* Temporary pointers to state and coeff */ 00326 q31_t acc = 0; /* Accumlator */ 00327 q7_t *pStateCurnt; /* Points to the current sample of the state */ 00328 00329 00330 /* S->pState points to state array which contains previous frame (numTaps - 1) samples */ 00331 /* pStateCurnt points to the location where the new input data should be written */ 00332 pStateCurnt = S->pState + (numTaps - 1u); 00333 00334 /* Initialize blkCnt with blockSize */ 00335 blkCnt = blockSize; 00336 00337 /* Perform filtering upto BlockSize - BlockSize%4 */ 00338 while(blkCnt > 0u) 00339 { 00340 /* Copy one sample at a time into state buffer */ 00341 *pStateCurnt++ = *pSrc++; 00342 00343 /* Set accumulator to zero */ 00344 acc = 0; 00345 00346 /* Initialize state pointer of type q7 */ 00347 px = pState; 00348 00349 /* Initialize coeff pointer of type q7 */ 00350 pb = pCoeffs; 00351 00352 00353 i = numTaps; 00354 00355 while(i > 0u) 00356 { 00357 /* 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] */ 00358 acc += (q15_t) * px++ * *pb++; 00359 i--; 00360 } 00361 00362 /* Store the 1.7 format filter output in destination buffer */ 00363 *pDst++ = (q7_t) __SSAT((acc >> 7), 8); 00364 00365 /* Advance the state pointer by 1 to process the next sample */ 00366 pState = pState + 1; 00367 00368 /* Decrement the loop counter */ 00369 blkCnt--; 00370 } 00371 00372 /* Processing is complete. 00373 ** Now copy the last numTaps - 1 samples to the satrt of the state buffer. 00374 ** This prepares the state buffer for the next function call. */ 00375 00376 00377 /* Points to the start of the state buffer */ 00378 pStateCurnt = S->pState; 00379 00380 00381 /* Copy numTaps number of values */ 00382 i = (numTaps - 1u); 00383 00384 /* Copy q7_t data */ 00385 while(i > 0u) 00386 { 00387 *pStateCurnt++ = *pState++; 00388 i--; 00389 } 00390 00391 #endif /* #ifndef ARM_MATH_CM0_FAMILY */ 00392 00393 } 00394 00395 /** 00396 * @} end of FIR group 00397 */
Generated on Tue Jul 12 2022 11:59:17 by 1.7.2