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