CMSIS DSP Library from CMSIS 2.0. See http://www.onarm.com/cmsis/ for full details
Dependents: K22F_DSP_Matrix_least_square BNO055-ELEC3810 1BNO055 ECE4180Project--Slave2 ... more
src/Cortex-M4-M3/FilteringFunctions/arm_iir_lattice_q31.c@0:1014af42efd9, 2011-03-10 (annotated)
- Committer:
- simon
- Date:
- Thu Mar 10 15:07:50 2011 +0000
- Revision:
- 0:1014af42efd9
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
simon | 0:1014af42efd9 | 1 | /* ---------------------------------------------------------------------- |
simon | 0:1014af42efd9 | 2 | * Copyright (C) 2010 ARM Limited. All rights reserved. |
simon | 0:1014af42efd9 | 3 | * |
simon | 0:1014af42efd9 | 4 | * $Date: 29. November 2010 |
simon | 0:1014af42efd9 | 5 | * $Revision: V1.0.3 |
simon | 0:1014af42efd9 | 6 | * |
simon | 0:1014af42efd9 | 7 | * Project: CMSIS DSP Library |
simon | 0:1014af42efd9 | 8 | * Title: arm_iir_lattice_q31.c |
simon | 0:1014af42efd9 | 9 | * |
simon | 0:1014af42efd9 | 10 | * Description: Q31 IIR lattice filter processing function. |
simon | 0:1014af42efd9 | 11 | * |
simon | 0:1014af42efd9 | 12 | * Target Processor: Cortex-M4/Cortex-M3 |
simon | 0:1014af42efd9 | 13 | * |
simon | 0:1014af42efd9 | 14 | * Version 1.0.3 2010/11/29 |
simon | 0:1014af42efd9 | 15 | * Re-organized the CMSIS folders and updated documentation. |
simon | 0:1014af42efd9 | 16 | * |
simon | 0:1014af42efd9 | 17 | * Version 1.0.2 2010/11/11 |
simon | 0:1014af42efd9 | 18 | * Documentation updated. |
simon | 0:1014af42efd9 | 19 | * |
simon | 0:1014af42efd9 | 20 | * Version 1.0.1 2010/10/05 |
simon | 0:1014af42efd9 | 21 | * Production release and review comments incorporated. |
simon | 0:1014af42efd9 | 22 | * |
simon | 0:1014af42efd9 | 23 | * Version 1.0.0 2010/09/20 |
simon | 0:1014af42efd9 | 24 | * Production release and review comments incorporated |
simon | 0:1014af42efd9 | 25 | * |
simon | 0:1014af42efd9 | 26 | * Version 0.0.7 2010/06/10 |
simon | 0:1014af42efd9 | 27 | * Misra-C changes done |
simon | 0:1014af42efd9 | 28 | * -------------------------------------------------------------------- */ |
simon | 0:1014af42efd9 | 29 | |
simon | 0:1014af42efd9 | 30 | #include "arm_math.h" |
simon | 0:1014af42efd9 | 31 | |
simon | 0:1014af42efd9 | 32 | /** |
simon | 0:1014af42efd9 | 33 | * @ingroup groupFilters |
simon | 0:1014af42efd9 | 34 | */ |
simon | 0:1014af42efd9 | 35 | |
simon | 0:1014af42efd9 | 36 | /** |
simon | 0:1014af42efd9 | 37 | * @addtogroup IIR_Lattice |
simon | 0:1014af42efd9 | 38 | * @{ |
simon | 0:1014af42efd9 | 39 | */ |
simon | 0:1014af42efd9 | 40 | |
simon | 0:1014af42efd9 | 41 | /** |
simon | 0:1014af42efd9 | 42 | * @brief Processing function for the Q31 IIR lattice filter. |
simon | 0:1014af42efd9 | 43 | * @param[in] *S points to an instance of the Q31 IIR lattice structure. |
simon | 0:1014af42efd9 | 44 | * @param[in] *pSrc points to the block of input data. |
simon | 0:1014af42efd9 | 45 | * @param[out] *pDst points to the block of output data. |
simon | 0:1014af42efd9 | 46 | * @param[in] blockSize number of samples to process. |
simon | 0:1014af42efd9 | 47 | * @return none. |
simon | 0:1014af42efd9 | 48 | * |
simon | 0:1014af42efd9 | 49 | * @details |
simon | 0:1014af42efd9 | 50 | * <b>Scaling and Overflow Behavior:</b> |
simon | 0:1014af42efd9 | 51 | * \par |
simon | 0:1014af42efd9 | 52 | * The function is implemented using an internal 64-bit accumulator. |
simon | 0:1014af42efd9 | 53 | * The accumulator has a 2.62 format and maintains full precision of the intermediate multiplication results but provides only a single guard bit. |
simon | 0:1014af42efd9 | 54 | * Thus, if the accumulator result overflows it wraps around rather than clip. |
simon | 0:1014af42efd9 | 55 | * In order to avoid overflows completely the input signal must be scaled down by 2*log2(numStages) bits. |
simon | 0:1014af42efd9 | 56 | * After all multiply-accumulates are performed, the 2.62 accumulator is saturated to 1.32 format and then truncated to 1.31 format. |
simon | 0:1014af42efd9 | 57 | */ |
simon | 0:1014af42efd9 | 58 | |
simon | 0:1014af42efd9 | 59 | void arm_iir_lattice_q31( |
simon | 0:1014af42efd9 | 60 | const arm_iir_lattice_instance_q31 * S, |
simon | 0:1014af42efd9 | 61 | q31_t * pSrc, |
simon | 0:1014af42efd9 | 62 | q31_t * pDst, |
simon | 0:1014af42efd9 | 63 | uint32_t blockSize) |
simon | 0:1014af42efd9 | 64 | { |
simon | 0:1014af42efd9 | 65 | q31_t fcurr, fnext, gcurr = 0, gnext; /* Temporary variables for lattice stages */ |
simon | 0:1014af42efd9 | 66 | q63_t acc; /* Accumlator */ |
simon | 0:1014af42efd9 | 67 | uint32_t blkCnt, tapCnt; /* Temporary variables for counts */ |
simon | 0:1014af42efd9 | 68 | q31_t *px1, *px2, *pk, *pv; /* Temporary pointers for state and coef */ |
simon | 0:1014af42efd9 | 69 | uint32_t numStages = S->numStages; /* number of stages */ |
simon | 0:1014af42efd9 | 70 | q31_t *pState; /* State pointer */ |
simon | 0:1014af42efd9 | 71 | q31_t *pStateCurnt; /* State current pointer */ |
simon | 0:1014af42efd9 | 72 | |
simon | 0:1014af42efd9 | 73 | blkCnt = blockSize; |
simon | 0:1014af42efd9 | 74 | |
simon | 0:1014af42efd9 | 75 | pState = &S->pState[0]; |
simon | 0:1014af42efd9 | 76 | |
simon | 0:1014af42efd9 | 77 | /* Sample processing */ |
simon | 0:1014af42efd9 | 78 | while(blkCnt > 0u) |
simon | 0:1014af42efd9 | 79 | { |
simon | 0:1014af42efd9 | 80 | /* Read Sample from input buffer */ |
simon | 0:1014af42efd9 | 81 | /* fN(n) = x(n) */ |
simon | 0:1014af42efd9 | 82 | fcurr = *pSrc++; |
simon | 0:1014af42efd9 | 83 | |
simon | 0:1014af42efd9 | 84 | /* Initialize state read pointer */ |
simon | 0:1014af42efd9 | 85 | px1 = pState; |
simon | 0:1014af42efd9 | 86 | /* Initialize state write pointer */ |
simon | 0:1014af42efd9 | 87 | px2 = pState; |
simon | 0:1014af42efd9 | 88 | /* Set accumulator to zero */ |
simon | 0:1014af42efd9 | 89 | acc = 0; |
simon | 0:1014af42efd9 | 90 | /* Initialize Ladder coeff pointer */ |
simon | 0:1014af42efd9 | 91 | pv = &S->pvCoeffs[0]; |
simon | 0:1014af42efd9 | 92 | /* Initialize Reflection coeff pointer */ |
simon | 0:1014af42efd9 | 93 | pk = &S->pkCoeffs[0]; |
simon | 0:1014af42efd9 | 94 | |
simon | 0:1014af42efd9 | 95 | |
simon | 0:1014af42efd9 | 96 | /* Process sample for first tap */ |
simon | 0:1014af42efd9 | 97 | gcurr = *px1++; |
simon | 0:1014af42efd9 | 98 | /* fN-1(n) = fN(n) - kN * gN-1(n-1) */ |
simon | 0:1014af42efd9 | 99 | fnext = __QSUB(fcurr, (q31_t) (((q63_t) gcurr * (*pk)) >> 31)); |
simon | 0:1014af42efd9 | 100 | /* gN(n) = kN * fN-1(n) + gN-1(n-1) */ |
simon | 0:1014af42efd9 | 101 | gnext = __QADD(gcurr, (q31_t) (((q63_t) fnext * (*pk++)) >> 31)); |
simon | 0:1014af42efd9 | 102 | /* write gN-1(n-1) into state for next sample processing */ |
simon | 0:1014af42efd9 | 103 | *px2++ = gnext; |
simon | 0:1014af42efd9 | 104 | /* y(n) += gN(n) * vN */ |
simon | 0:1014af42efd9 | 105 | acc += ((q63_t) gnext * *pv++); |
simon | 0:1014af42efd9 | 106 | |
simon | 0:1014af42efd9 | 107 | /* Update f values for next coefficient processing */ |
simon | 0:1014af42efd9 | 108 | fcurr = fnext; |
simon | 0:1014af42efd9 | 109 | |
simon | 0:1014af42efd9 | 110 | /* Loop unrolling. Process 4 taps at a time. */ |
simon | 0:1014af42efd9 | 111 | tapCnt = (numStages - 1u) >> 2; |
simon | 0:1014af42efd9 | 112 | |
simon | 0:1014af42efd9 | 113 | while(tapCnt > 0u) |
simon | 0:1014af42efd9 | 114 | { |
simon | 0:1014af42efd9 | 115 | |
simon | 0:1014af42efd9 | 116 | /* Process sample for 2nd, 6th .. taps */ |
simon | 0:1014af42efd9 | 117 | /* Read gN-2(n-1) from state buffer */ |
simon | 0:1014af42efd9 | 118 | gcurr = *px1++; |
simon | 0:1014af42efd9 | 119 | /* fN-2(n) = fN-1(n) - kN-1 * gN-2(n-1) */ |
simon | 0:1014af42efd9 | 120 | fnext = __QSUB(fcurr, (q31_t) (((q63_t) gcurr * (*pk)) >> 31)); |
simon | 0:1014af42efd9 | 121 | /* gN-1(n) = kN-1 * fN-2(n) + gN-2(n-1) */ |
simon | 0:1014af42efd9 | 122 | gnext = __QADD(gcurr, (q31_t) (((q63_t) fnext * (*pk++)) >> 31)); |
simon | 0:1014af42efd9 | 123 | /* y(n) += gN-1(n) * vN-1 */ |
simon | 0:1014af42efd9 | 124 | /* process for gN-5(n) * vN-5, gN-9(n) * vN-9 ... */ |
simon | 0:1014af42efd9 | 125 | acc += ((q63_t) gnext * *pv++); |
simon | 0:1014af42efd9 | 126 | /* write gN-1(n) into state for next sample processing */ |
simon | 0:1014af42efd9 | 127 | *px2++ = gnext; |
simon | 0:1014af42efd9 | 128 | |
simon | 0:1014af42efd9 | 129 | /* Process sample for 3nd, 7th ...taps */ |
simon | 0:1014af42efd9 | 130 | /* Read gN-3(n-1) from state buffer */ |
simon | 0:1014af42efd9 | 131 | gcurr = *px1++; |
simon | 0:1014af42efd9 | 132 | /* Process sample for 3rd, 7th .. taps */ |
simon | 0:1014af42efd9 | 133 | /* fN-3(n) = fN-2(n) - kN-2 * gN-3(n-1) */ |
simon | 0:1014af42efd9 | 134 | fcurr = __QSUB(fnext, (q31_t) (((q63_t) gcurr * (*pk)) >> 31)); |
simon | 0:1014af42efd9 | 135 | /* gN-2(n) = kN-2 * fN-3(n) + gN-3(n-1) */ |
simon | 0:1014af42efd9 | 136 | gnext = __QADD(gcurr, (q31_t) (((q63_t) fcurr * (*pk++)) >> 31)); |
simon | 0:1014af42efd9 | 137 | /* y(n) += gN-2(n) * vN-2 */ |
simon | 0:1014af42efd9 | 138 | /* process for gN-6(n) * vN-6, gN-10(n) * vN-10 ... */ |
simon | 0:1014af42efd9 | 139 | acc += ((q63_t) gnext * *pv++); |
simon | 0:1014af42efd9 | 140 | /* write gN-2(n) into state for next sample processing */ |
simon | 0:1014af42efd9 | 141 | *px2++ = gnext; |
simon | 0:1014af42efd9 | 142 | |
simon | 0:1014af42efd9 | 143 | |
simon | 0:1014af42efd9 | 144 | /* Process sample for 4th, 8th ...taps */ |
simon | 0:1014af42efd9 | 145 | /* Read gN-4(n-1) from state buffer */ |
simon | 0:1014af42efd9 | 146 | gcurr = *px1++; |
simon | 0:1014af42efd9 | 147 | /* Process sample for 4th, 8th .. taps */ |
simon | 0:1014af42efd9 | 148 | /* fN-4(n) = fN-3(n) - kN-3 * gN-4(n-1) */ |
simon | 0:1014af42efd9 | 149 | fnext = __QSUB(fcurr, (q31_t) (((q63_t) gcurr * (*pk)) >> 31)); |
simon | 0:1014af42efd9 | 150 | /* gN-3(n) = kN-3 * fN-4(n) + gN-4(n-1) */ |
simon | 0:1014af42efd9 | 151 | gnext = __QADD(gcurr, (q31_t) (((q63_t) fnext * (*pk++)) >> 31)); |
simon | 0:1014af42efd9 | 152 | /* y(n) += gN-3(n) * vN-3 */ |
simon | 0:1014af42efd9 | 153 | /* process for gN-7(n) * vN-7, gN-11(n) * vN-11 ... */ |
simon | 0:1014af42efd9 | 154 | acc += ((q63_t) gnext * *pv++); |
simon | 0:1014af42efd9 | 155 | /* write gN-3(n) into state for next sample processing */ |
simon | 0:1014af42efd9 | 156 | *px2++ = gnext; |
simon | 0:1014af42efd9 | 157 | |
simon | 0:1014af42efd9 | 158 | |
simon | 0:1014af42efd9 | 159 | /* Process sample for 5th, 9th ...taps */ |
simon | 0:1014af42efd9 | 160 | /* Read gN-5(n-1) from state buffer */ |
simon | 0:1014af42efd9 | 161 | gcurr = *px1++; |
simon | 0:1014af42efd9 | 162 | /* Process sample for 5th, 9th .. taps */ |
simon | 0:1014af42efd9 | 163 | /* fN-5(n) = fN-4(n) - kN-4 * gN-1(n-1) */ |
simon | 0:1014af42efd9 | 164 | fcurr = __QSUB(fnext, (q31_t) (((q63_t) gcurr * (*pk)) >> 31)); |
simon | 0:1014af42efd9 | 165 | /* gN-4(n) = kN-4 * fN-5(n) + gN-5(n-1) */ |
simon | 0:1014af42efd9 | 166 | gnext = __QADD(gcurr, (q31_t) (((q63_t) fcurr * (*pk++)) >> 31)); |
simon | 0:1014af42efd9 | 167 | /* y(n) += gN-4(n) * vN-4 */ |
simon | 0:1014af42efd9 | 168 | /* process for gN-8(n) * vN-8, gN-12(n) * vN-12 ... */ |
simon | 0:1014af42efd9 | 169 | acc += ((q63_t) gnext * *pv++); |
simon | 0:1014af42efd9 | 170 | /* write gN-4(n) into state for next sample processing */ |
simon | 0:1014af42efd9 | 171 | *px2++ = gnext; |
simon | 0:1014af42efd9 | 172 | |
simon | 0:1014af42efd9 | 173 | tapCnt--; |
simon | 0:1014af42efd9 | 174 | |
simon | 0:1014af42efd9 | 175 | } |
simon | 0:1014af42efd9 | 176 | |
simon | 0:1014af42efd9 | 177 | fnext = fcurr; |
simon | 0:1014af42efd9 | 178 | |
simon | 0:1014af42efd9 | 179 | /* If the filter length is not a multiple of 4, compute the remaining filter taps */ |
simon | 0:1014af42efd9 | 180 | tapCnt = (numStages - 1u) % 0x4u; |
simon | 0:1014af42efd9 | 181 | |
simon | 0:1014af42efd9 | 182 | while(tapCnt > 0u) |
simon | 0:1014af42efd9 | 183 | { |
simon | 0:1014af42efd9 | 184 | gcurr = *px1++; |
simon | 0:1014af42efd9 | 185 | /* Process sample for last taps */ |
simon | 0:1014af42efd9 | 186 | fnext = __QSUB(fcurr, (q31_t) (((q63_t) gcurr * (*pk)) >> 31)); |
simon | 0:1014af42efd9 | 187 | gnext = __QADD(gcurr, (q31_t) (((q63_t) fnext * (*pk++)) >> 31)); |
simon | 0:1014af42efd9 | 188 | /* Output samples for last taps */ |
simon | 0:1014af42efd9 | 189 | acc += ((q63_t) gnext * *pv++); |
simon | 0:1014af42efd9 | 190 | *px2++ = gnext; |
simon | 0:1014af42efd9 | 191 | fcurr = fnext; |
simon | 0:1014af42efd9 | 192 | |
simon | 0:1014af42efd9 | 193 | tapCnt--; |
simon | 0:1014af42efd9 | 194 | |
simon | 0:1014af42efd9 | 195 | } |
simon | 0:1014af42efd9 | 196 | |
simon | 0:1014af42efd9 | 197 | /* y(n) += g0(n) * v0 */ |
simon | 0:1014af42efd9 | 198 | acc += (q63_t) fnext *( |
simon | 0:1014af42efd9 | 199 | *pv++); |
simon | 0:1014af42efd9 | 200 | |
simon | 0:1014af42efd9 | 201 | *px2++ = fnext; |
simon | 0:1014af42efd9 | 202 | |
simon | 0:1014af42efd9 | 203 | /* write out into pDst */ |
simon | 0:1014af42efd9 | 204 | *pDst++ = (q31_t) (acc >> 31u); |
simon | 0:1014af42efd9 | 205 | |
simon | 0:1014af42efd9 | 206 | /* Advance the state pointer by 4 to process the next group of 4 samples */ |
simon | 0:1014af42efd9 | 207 | pState = pState + 1u; |
simon | 0:1014af42efd9 | 208 | blkCnt--; |
simon | 0:1014af42efd9 | 209 | |
simon | 0:1014af42efd9 | 210 | } |
simon | 0:1014af42efd9 | 211 | |
simon | 0:1014af42efd9 | 212 | /* Processing is complete. Now copy last S->numStages samples to start of the buffer |
simon | 0:1014af42efd9 | 213 | for the preperation of next frame process */ |
simon | 0:1014af42efd9 | 214 | |
simon | 0:1014af42efd9 | 215 | /* Points to the start of the state buffer */ |
simon | 0:1014af42efd9 | 216 | pStateCurnt = &S->pState[0]; |
simon | 0:1014af42efd9 | 217 | pState = &S->pState[blockSize]; |
simon | 0:1014af42efd9 | 218 | |
simon | 0:1014af42efd9 | 219 | tapCnt = numStages >> 2u; |
simon | 0:1014af42efd9 | 220 | |
simon | 0:1014af42efd9 | 221 | /* copy data */ |
simon | 0:1014af42efd9 | 222 | while(tapCnt > 0u) |
simon | 0:1014af42efd9 | 223 | { |
simon | 0:1014af42efd9 | 224 | *pStateCurnt++ = *pState++; |
simon | 0:1014af42efd9 | 225 | *pStateCurnt++ = *pState++; |
simon | 0:1014af42efd9 | 226 | *pStateCurnt++ = *pState++; |
simon | 0:1014af42efd9 | 227 | *pStateCurnt++ = *pState++; |
simon | 0:1014af42efd9 | 228 | |
simon | 0:1014af42efd9 | 229 | /* Decrement the loop counter */ |
simon | 0:1014af42efd9 | 230 | tapCnt--; |
simon | 0:1014af42efd9 | 231 | |
simon | 0:1014af42efd9 | 232 | } |
simon | 0:1014af42efd9 | 233 | |
simon | 0:1014af42efd9 | 234 | /* Calculate remaining number of copies */ |
simon | 0:1014af42efd9 | 235 | tapCnt = (numStages) % 0x4u; |
simon | 0:1014af42efd9 | 236 | |
simon | 0:1014af42efd9 | 237 | /* Copy the remaining q31_t data */ |
simon | 0:1014af42efd9 | 238 | while(tapCnt > 0u) |
simon | 0:1014af42efd9 | 239 | { |
simon | 0:1014af42efd9 | 240 | *pStateCurnt++ = *pState++; |
simon | 0:1014af42efd9 | 241 | |
simon | 0:1014af42efd9 | 242 | /* Decrement the loop counter */ |
simon | 0:1014af42efd9 | 243 | tapCnt--; |
simon | 0:1014af42efd9 | 244 | }; |
simon | 0:1014af42efd9 | 245 | |
simon | 0:1014af42efd9 | 246 | } |
simon | 0:1014af42efd9 | 247 | |
simon | 0:1014af42efd9 | 248 | |
simon | 0:1014af42efd9 | 249 | |
simon | 0:1014af42efd9 | 250 | |
simon | 0:1014af42efd9 | 251 | /** |
simon | 0:1014af42efd9 | 252 | * @} end of IIR_Lattice group |
simon | 0:1014af42efd9 | 253 | */ |