The CMSIS DSP 5 library
Dependents: Nucleo-Heart-Rate ejercicioVrms2 PROYECTOFINAL ejercicioVrms ... more
functions/FilteringFunctions/arm_biquad_cascade_df1_q31.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_biquad_cascade_df1_q31.c |
xorjoep | 1:24714b45cd1b | 4 | * Description: Processing function for the Q31 Biquad cascade filter |
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 BiquadCascadeDF1 |
xorjoep | 1:24714b45cd1b | 37 | * @{ |
xorjoep | 1:24714b45cd1b | 38 | */ |
xorjoep | 1:24714b45cd1b | 39 | |
xorjoep | 1:24714b45cd1b | 40 | /** |
xorjoep | 1:24714b45cd1b | 41 | * @brief Processing function for the Q31 Biquad cascade filter. |
xorjoep | 1:24714b45cd1b | 42 | * @param[in] *S points to an instance of the Q31 Biquad cascade structure. |
xorjoep | 1:24714b45cd1b | 43 | * @param[in] *pSrc points to the block of input data. |
xorjoep | 1:24714b45cd1b | 44 | * @param[out] *pDst points to the block of output data. |
xorjoep | 1:24714b45cd1b | 45 | * @param[in] blockSize number of samples to process per call. |
xorjoep | 1:24714b45cd1b | 46 | * @return none. |
xorjoep | 1:24714b45cd1b | 47 | * |
xorjoep | 1:24714b45cd1b | 48 | * <b>Scaling and Overflow Behavior:</b> |
xorjoep | 1:24714b45cd1b | 49 | * \par |
xorjoep | 1:24714b45cd1b | 50 | * The function is implemented using an internal 64-bit accumulator. |
xorjoep | 1:24714b45cd1b | 51 | * The accumulator has a 2.62 format and maintains full precision of the intermediate multiplication results but provides only a single guard bit. |
xorjoep | 1:24714b45cd1b | 52 | * Thus, if the accumulator result overflows it wraps around rather than clip. |
xorjoep | 1:24714b45cd1b | 53 | * In order to avoid overflows completely the input signal must be scaled down by 2 bits and lie in the range [-0.25 +0.25). |
xorjoep | 1:24714b45cd1b | 54 | * After all 5 multiply-accumulates are performed, the 2.62 accumulator is shifted by <code>postShift</code> bits and the result truncated to |
xorjoep | 1:24714b45cd1b | 55 | * 1.31 format by discarding the low 32 bits. |
xorjoep | 1:24714b45cd1b | 56 | * |
xorjoep | 1:24714b45cd1b | 57 | * \par |
xorjoep | 1:24714b45cd1b | 58 | * Refer to the function <code>arm_biquad_cascade_df1_fast_q31()</code> for a faster but less precise implementation of this filter for Cortex-M3 and Cortex-M4. |
xorjoep | 1:24714b45cd1b | 59 | */ |
xorjoep | 1:24714b45cd1b | 60 | |
xorjoep | 1:24714b45cd1b | 61 | void arm_biquad_cascade_df1_q31( |
xorjoep | 1:24714b45cd1b | 62 | const arm_biquad_casd_df1_inst_q31 * S, |
xorjoep | 1:24714b45cd1b | 63 | q31_t * pSrc, |
xorjoep | 1:24714b45cd1b | 64 | q31_t * pDst, |
xorjoep | 1:24714b45cd1b | 65 | uint32_t blockSize) |
xorjoep | 1:24714b45cd1b | 66 | { |
xorjoep | 1:24714b45cd1b | 67 | q63_t acc; /* accumulator */ |
xorjoep | 1:24714b45cd1b | 68 | uint32_t uShift = ((uint32_t) S->postShift + 1U); |
xorjoep | 1:24714b45cd1b | 69 | uint32_t lShift = 32U - uShift; /* Shift to be applied to the output */ |
xorjoep | 1:24714b45cd1b | 70 | q31_t *pIn = pSrc; /* input pointer initialization */ |
xorjoep | 1:24714b45cd1b | 71 | q31_t *pOut = pDst; /* output pointer initialization */ |
xorjoep | 1:24714b45cd1b | 72 | q31_t *pState = S->pState; /* pState pointer initialization */ |
xorjoep | 1:24714b45cd1b | 73 | q31_t *pCoeffs = S->pCoeffs; /* coeff pointer initialization */ |
xorjoep | 1:24714b45cd1b | 74 | q31_t Xn1, Xn2, Yn1, Yn2; /* Filter state variables */ |
xorjoep | 1:24714b45cd1b | 75 | q31_t b0, b1, b2, a1, a2; /* Filter coefficients */ |
xorjoep | 1:24714b45cd1b | 76 | q31_t Xn; /* temporary input */ |
xorjoep | 1:24714b45cd1b | 77 | uint32_t sample, stage = S->numStages; /* loop counters */ |
xorjoep | 1:24714b45cd1b | 78 | |
xorjoep | 1:24714b45cd1b | 79 | |
xorjoep | 1:24714b45cd1b | 80 | #if defined (ARM_MATH_DSP) |
xorjoep | 1:24714b45cd1b | 81 | |
xorjoep | 1:24714b45cd1b | 82 | q31_t acc_l, acc_h; /* temporary output variables */ |
xorjoep | 1:24714b45cd1b | 83 | |
xorjoep | 1:24714b45cd1b | 84 | /* Run the below code for Cortex-M4 and Cortex-M3 */ |
xorjoep | 1:24714b45cd1b | 85 | |
xorjoep | 1:24714b45cd1b | 86 | do |
xorjoep | 1:24714b45cd1b | 87 | { |
xorjoep | 1:24714b45cd1b | 88 | /* Reading the coefficients */ |
xorjoep | 1:24714b45cd1b | 89 | b0 = *pCoeffs++; |
xorjoep | 1:24714b45cd1b | 90 | b1 = *pCoeffs++; |
xorjoep | 1:24714b45cd1b | 91 | b2 = *pCoeffs++; |
xorjoep | 1:24714b45cd1b | 92 | a1 = *pCoeffs++; |
xorjoep | 1:24714b45cd1b | 93 | a2 = *pCoeffs++; |
xorjoep | 1:24714b45cd1b | 94 | |
xorjoep | 1:24714b45cd1b | 95 | /* Reading the state values */ |
xorjoep | 1:24714b45cd1b | 96 | Xn1 = pState[0]; |
xorjoep | 1:24714b45cd1b | 97 | Xn2 = pState[1]; |
xorjoep | 1:24714b45cd1b | 98 | Yn1 = pState[2]; |
xorjoep | 1:24714b45cd1b | 99 | Yn2 = pState[3]; |
xorjoep | 1:24714b45cd1b | 100 | |
xorjoep | 1:24714b45cd1b | 101 | /* Apply loop unrolling and compute 4 output values simultaneously. */ |
xorjoep | 1:24714b45cd1b | 102 | /* The variable acc hold output values that are being computed: |
xorjoep | 1:24714b45cd1b | 103 | * |
xorjoep | 1:24714b45cd1b | 104 | * acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] |
xorjoep | 1:24714b45cd1b | 105 | */ |
xorjoep | 1:24714b45cd1b | 106 | |
xorjoep | 1:24714b45cd1b | 107 | sample = blockSize >> 2U; |
xorjoep | 1:24714b45cd1b | 108 | |
xorjoep | 1:24714b45cd1b | 109 | /* First part of the processing with loop unrolling. Compute 4 outputs at a time. |
xorjoep | 1:24714b45cd1b | 110 | ** a second loop below computes the remaining 1 to 3 samples. */ |
xorjoep | 1:24714b45cd1b | 111 | while (sample > 0U) |
xorjoep | 1:24714b45cd1b | 112 | { |
xorjoep | 1:24714b45cd1b | 113 | /* Read the input */ |
xorjoep | 1:24714b45cd1b | 114 | Xn = *pIn++; |
xorjoep | 1:24714b45cd1b | 115 | |
xorjoep | 1:24714b45cd1b | 116 | /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */ |
xorjoep | 1:24714b45cd1b | 117 | |
xorjoep | 1:24714b45cd1b | 118 | /* acc = b0 * x[n] */ |
xorjoep | 1:24714b45cd1b | 119 | acc = (q63_t) b0 *Xn; |
xorjoep | 1:24714b45cd1b | 120 | /* acc += b1 * x[n-1] */ |
xorjoep | 1:24714b45cd1b | 121 | acc += (q63_t) b1 *Xn1; |
xorjoep | 1:24714b45cd1b | 122 | /* acc += b[2] * x[n-2] */ |
xorjoep | 1:24714b45cd1b | 123 | acc += (q63_t) b2 *Xn2; |
xorjoep | 1:24714b45cd1b | 124 | /* acc += a1 * y[n-1] */ |
xorjoep | 1:24714b45cd1b | 125 | acc += (q63_t) a1 *Yn1; |
xorjoep | 1:24714b45cd1b | 126 | /* acc += a2 * y[n-2] */ |
xorjoep | 1:24714b45cd1b | 127 | acc += (q63_t) a2 *Yn2; |
xorjoep | 1:24714b45cd1b | 128 | |
xorjoep | 1:24714b45cd1b | 129 | /* The result is converted to 1.31 , Yn2 variable is reused */ |
xorjoep | 1:24714b45cd1b | 130 | |
xorjoep | 1:24714b45cd1b | 131 | /* Calc lower part of acc */ |
xorjoep | 1:24714b45cd1b | 132 | acc_l = acc & 0xffffffff; |
xorjoep | 1:24714b45cd1b | 133 | |
xorjoep | 1:24714b45cd1b | 134 | /* Calc upper part of acc */ |
xorjoep | 1:24714b45cd1b | 135 | acc_h = (acc >> 32) & 0xffffffff; |
xorjoep | 1:24714b45cd1b | 136 | |
xorjoep | 1:24714b45cd1b | 137 | /* Apply shift for lower part of acc and upper part of acc */ |
xorjoep | 1:24714b45cd1b | 138 | Yn2 = (uint32_t) acc_l >> lShift | acc_h << uShift; |
xorjoep | 1:24714b45cd1b | 139 | |
xorjoep | 1:24714b45cd1b | 140 | /* Store the output in the destination buffer. */ |
xorjoep | 1:24714b45cd1b | 141 | *pOut++ = Yn2; |
xorjoep | 1:24714b45cd1b | 142 | |
xorjoep | 1:24714b45cd1b | 143 | /* Read the second input */ |
xorjoep | 1:24714b45cd1b | 144 | Xn2 = *pIn++; |
xorjoep | 1:24714b45cd1b | 145 | |
xorjoep | 1:24714b45cd1b | 146 | /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */ |
xorjoep | 1:24714b45cd1b | 147 | |
xorjoep | 1:24714b45cd1b | 148 | /* acc = b0 * x[n] */ |
xorjoep | 1:24714b45cd1b | 149 | acc = (q63_t) b0 *Xn2; |
xorjoep | 1:24714b45cd1b | 150 | /* acc += b1 * x[n-1] */ |
xorjoep | 1:24714b45cd1b | 151 | acc += (q63_t) b1 *Xn; |
xorjoep | 1:24714b45cd1b | 152 | /* acc += b[2] * x[n-2] */ |
xorjoep | 1:24714b45cd1b | 153 | acc += (q63_t) b2 *Xn1; |
xorjoep | 1:24714b45cd1b | 154 | /* acc += a1 * y[n-1] */ |
xorjoep | 1:24714b45cd1b | 155 | acc += (q63_t) a1 *Yn2; |
xorjoep | 1:24714b45cd1b | 156 | /* acc += a2 * y[n-2] */ |
xorjoep | 1:24714b45cd1b | 157 | acc += (q63_t) a2 *Yn1; |
xorjoep | 1:24714b45cd1b | 158 | |
xorjoep | 1:24714b45cd1b | 159 | |
xorjoep | 1:24714b45cd1b | 160 | /* The result is converted to 1.31, Yn1 variable is reused */ |
xorjoep | 1:24714b45cd1b | 161 | |
xorjoep | 1:24714b45cd1b | 162 | /* Calc lower part of acc */ |
xorjoep | 1:24714b45cd1b | 163 | acc_l = acc & 0xffffffff; |
xorjoep | 1:24714b45cd1b | 164 | |
xorjoep | 1:24714b45cd1b | 165 | /* Calc upper part of acc */ |
xorjoep | 1:24714b45cd1b | 166 | acc_h = (acc >> 32) & 0xffffffff; |
xorjoep | 1:24714b45cd1b | 167 | |
xorjoep | 1:24714b45cd1b | 168 | |
xorjoep | 1:24714b45cd1b | 169 | /* Apply shift for lower part of acc and upper part of acc */ |
xorjoep | 1:24714b45cd1b | 170 | Yn1 = (uint32_t) acc_l >> lShift | acc_h << uShift; |
xorjoep | 1:24714b45cd1b | 171 | |
xorjoep | 1:24714b45cd1b | 172 | /* Store the output in the destination buffer. */ |
xorjoep | 1:24714b45cd1b | 173 | *pOut++ = Yn1; |
xorjoep | 1:24714b45cd1b | 174 | |
xorjoep | 1:24714b45cd1b | 175 | /* Read the third input */ |
xorjoep | 1:24714b45cd1b | 176 | Xn1 = *pIn++; |
xorjoep | 1:24714b45cd1b | 177 | |
xorjoep | 1:24714b45cd1b | 178 | /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */ |
xorjoep | 1:24714b45cd1b | 179 | |
xorjoep | 1:24714b45cd1b | 180 | /* acc = b0 * x[n] */ |
xorjoep | 1:24714b45cd1b | 181 | acc = (q63_t) b0 *Xn1; |
xorjoep | 1:24714b45cd1b | 182 | /* acc += b1 * x[n-1] */ |
xorjoep | 1:24714b45cd1b | 183 | acc += (q63_t) b1 *Xn2; |
xorjoep | 1:24714b45cd1b | 184 | /* acc += b[2] * x[n-2] */ |
xorjoep | 1:24714b45cd1b | 185 | acc += (q63_t) b2 *Xn; |
xorjoep | 1:24714b45cd1b | 186 | /* acc += a1 * y[n-1] */ |
xorjoep | 1:24714b45cd1b | 187 | acc += (q63_t) a1 *Yn1; |
xorjoep | 1:24714b45cd1b | 188 | /* acc += a2 * y[n-2] */ |
xorjoep | 1:24714b45cd1b | 189 | acc += (q63_t) a2 *Yn2; |
xorjoep | 1:24714b45cd1b | 190 | |
xorjoep | 1:24714b45cd1b | 191 | /* The result is converted to 1.31, Yn2 variable is reused */ |
xorjoep | 1:24714b45cd1b | 192 | /* Calc lower part of acc */ |
xorjoep | 1:24714b45cd1b | 193 | acc_l = acc & 0xffffffff; |
xorjoep | 1:24714b45cd1b | 194 | |
xorjoep | 1:24714b45cd1b | 195 | /* Calc upper part of acc */ |
xorjoep | 1:24714b45cd1b | 196 | acc_h = (acc >> 32) & 0xffffffff; |
xorjoep | 1:24714b45cd1b | 197 | |
xorjoep | 1:24714b45cd1b | 198 | |
xorjoep | 1:24714b45cd1b | 199 | /* Apply shift for lower part of acc and upper part of acc */ |
xorjoep | 1:24714b45cd1b | 200 | Yn2 = (uint32_t) acc_l >> lShift | acc_h << uShift; |
xorjoep | 1:24714b45cd1b | 201 | |
xorjoep | 1:24714b45cd1b | 202 | /* Store the output in the destination buffer. */ |
xorjoep | 1:24714b45cd1b | 203 | *pOut++ = Yn2; |
xorjoep | 1:24714b45cd1b | 204 | |
xorjoep | 1:24714b45cd1b | 205 | /* Read the forth input */ |
xorjoep | 1:24714b45cd1b | 206 | Xn = *pIn++; |
xorjoep | 1:24714b45cd1b | 207 | |
xorjoep | 1:24714b45cd1b | 208 | /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */ |
xorjoep | 1:24714b45cd1b | 209 | |
xorjoep | 1:24714b45cd1b | 210 | /* acc = b0 * x[n] */ |
xorjoep | 1:24714b45cd1b | 211 | acc = (q63_t) b0 *Xn; |
xorjoep | 1:24714b45cd1b | 212 | /* acc += b1 * x[n-1] */ |
xorjoep | 1:24714b45cd1b | 213 | acc += (q63_t) b1 *Xn1; |
xorjoep | 1:24714b45cd1b | 214 | /* acc += b[2] * x[n-2] */ |
xorjoep | 1:24714b45cd1b | 215 | acc += (q63_t) b2 *Xn2; |
xorjoep | 1:24714b45cd1b | 216 | /* acc += a1 * y[n-1] */ |
xorjoep | 1:24714b45cd1b | 217 | acc += (q63_t) a1 *Yn2; |
xorjoep | 1:24714b45cd1b | 218 | /* acc += a2 * y[n-2] */ |
xorjoep | 1:24714b45cd1b | 219 | acc += (q63_t) a2 *Yn1; |
xorjoep | 1:24714b45cd1b | 220 | |
xorjoep | 1:24714b45cd1b | 221 | /* The result is converted to 1.31, Yn1 variable is reused */ |
xorjoep | 1:24714b45cd1b | 222 | /* Calc lower part of acc */ |
xorjoep | 1:24714b45cd1b | 223 | acc_l = acc & 0xffffffff; |
xorjoep | 1:24714b45cd1b | 224 | |
xorjoep | 1:24714b45cd1b | 225 | /* Calc upper part of acc */ |
xorjoep | 1:24714b45cd1b | 226 | acc_h = (acc >> 32) & 0xffffffff; |
xorjoep | 1:24714b45cd1b | 227 | |
xorjoep | 1:24714b45cd1b | 228 | /* Apply shift for lower part of acc and upper part of acc */ |
xorjoep | 1:24714b45cd1b | 229 | Yn1 = (uint32_t) acc_l >> lShift | acc_h << uShift; |
xorjoep | 1:24714b45cd1b | 230 | |
xorjoep | 1:24714b45cd1b | 231 | /* Every time after the output is computed state should be updated. */ |
xorjoep | 1:24714b45cd1b | 232 | /* The states should be updated as: */ |
xorjoep | 1:24714b45cd1b | 233 | /* Xn2 = Xn1 */ |
xorjoep | 1:24714b45cd1b | 234 | /* Xn1 = Xn */ |
xorjoep | 1:24714b45cd1b | 235 | /* Yn2 = Yn1 */ |
xorjoep | 1:24714b45cd1b | 236 | /* Yn1 = acc */ |
xorjoep | 1:24714b45cd1b | 237 | Xn2 = Xn1; |
xorjoep | 1:24714b45cd1b | 238 | Xn1 = Xn; |
xorjoep | 1:24714b45cd1b | 239 | |
xorjoep | 1:24714b45cd1b | 240 | /* Store the output in the destination buffer. */ |
xorjoep | 1:24714b45cd1b | 241 | *pOut++ = Yn1; |
xorjoep | 1:24714b45cd1b | 242 | |
xorjoep | 1:24714b45cd1b | 243 | /* decrement the loop counter */ |
xorjoep | 1:24714b45cd1b | 244 | sample--; |
xorjoep | 1:24714b45cd1b | 245 | } |
xorjoep | 1:24714b45cd1b | 246 | |
xorjoep | 1:24714b45cd1b | 247 | /* If the blockSize is not a multiple of 4, compute any remaining output samples here. |
xorjoep | 1:24714b45cd1b | 248 | ** No loop unrolling is used. */ |
xorjoep | 1:24714b45cd1b | 249 | sample = (blockSize & 0x3U); |
xorjoep | 1:24714b45cd1b | 250 | |
xorjoep | 1:24714b45cd1b | 251 | while (sample > 0U) |
xorjoep | 1:24714b45cd1b | 252 | { |
xorjoep | 1:24714b45cd1b | 253 | /* Read the input */ |
xorjoep | 1:24714b45cd1b | 254 | Xn = *pIn++; |
xorjoep | 1:24714b45cd1b | 255 | |
xorjoep | 1:24714b45cd1b | 256 | /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */ |
xorjoep | 1:24714b45cd1b | 257 | |
xorjoep | 1:24714b45cd1b | 258 | /* acc = b0 * x[n] */ |
xorjoep | 1:24714b45cd1b | 259 | acc = (q63_t) b0 *Xn; |
xorjoep | 1:24714b45cd1b | 260 | /* acc += b1 * x[n-1] */ |
xorjoep | 1:24714b45cd1b | 261 | acc += (q63_t) b1 *Xn1; |
xorjoep | 1:24714b45cd1b | 262 | /* acc += b[2] * x[n-2] */ |
xorjoep | 1:24714b45cd1b | 263 | acc += (q63_t) b2 *Xn2; |
xorjoep | 1:24714b45cd1b | 264 | /* acc += a1 * y[n-1] */ |
xorjoep | 1:24714b45cd1b | 265 | acc += (q63_t) a1 *Yn1; |
xorjoep | 1:24714b45cd1b | 266 | /* acc += a2 * y[n-2] */ |
xorjoep | 1:24714b45cd1b | 267 | acc += (q63_t) a2 *Yn2; |
xorjoep | 1:24714b45cd1b | 268 | |
xorjoep | 1:24714b45cd1b | 269 | /* The result is converted to 1.31 */ |
xorjoep | 1:24714b45cd1b | 270 | acc = acc >> lShift; |
xorjoep | 1:24714b45cd1b | 271 | |
xorjoep | 1:24714b45cd1b | 272 | /* Every time after the output is computed state should be updated. */ |
xorjoep | 1:24714b45cd1b | 273 | /* The states should be updated as: */ |
xorjoep | 1:24714b45cd1b | 274 | /* Xn2 = Xn1 */ |
xorjoep | 1:24714b45cd1b | 275 | /* Xn1 = Xn */ |
xorjoep | 1:24714b45cd1b | 276 | /* Yn2 = Yn1 */ |
xorjoep | 1:24714b45cd1b | 277 | /* Yn1 = acc */ |
xorjoep | 1:24714b45cd1b | 278 | Xn2 = Xn1; |
xorjoep | 1:24714b45cd1b | 279 | Xn1 = Xn; |
xorjoep | 1:24714b45cd1b | 280 | Yn2 = Yn1; |
xorjoep | 1:24714b45cd1b | 281 | Yn1 = (q31_t) acc; |
xorjoep | 1:24714b45cd1b | 282 | |
xorjoep | 1:24714b45cd1b | 283 | /* Store the output in the destination buffer. */ |
xorjoep | 1:24714b45cd1b | 284 | *pOut++ = (q31_t) acc; |
xorjoep | 1:24714b45cd1b | 285 | |
xorjoep | 1:24714b45cd1b | 286 | /* decrement the loop counter */ |
xorjoep | 1:24714b45cd1b | 287 | sample--; |
xorjoep | 1:24714b45cd1b | 288 | } |
xorjoep | 1:24714b45cd1b | 289 | |
xorjoep | 1:24714b45cd1b | 290 | /* The first stage goes from the input buffer to the output buffer. */ |
xorjoep | 1:24714b45cd1b | 291 | /* Subsequent stages occur in-place in the output buffer */ |
xorjoep | 1:24714b45cd1b | 292 | pIn = pDst; |
xorjoep | 1:24714b45cd1b | 293 | |
xorjoep | 1:24714b45cd1b | 294 | /* Reset to destination pointer */ |
xorjoep | 1:24714b45cd1b | 295 | pOut = pDst; |
xorjoep | 1:24714b45cd1b | 296 | |
xorjoep | 1:24714b45cd1b | 297 | /* Store the updated state variables back into the pState array */ |
xorjoep | 1:24714b45cd1b | 298 | *pState++ = Xn1; |
xorjoep | 1:24714b45cd1b | 299 | *pState++ = Xn2; |
xorjoep | 1:24714b45cd1b | 300 | *pState++ = Yn1; |
xorjoep | 1:24714b45cd1b | 301 | *pState++ = Yn2; |
xorjoep | 1:24714b45cd1b | 302 | |
xorjoep | 1:24714b45cd1b | 303 | } while (--stage); |
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 | do |
xorjoep | 1:24714b45cd1b | 310 | { |
xorjoep | 1:24714b45cd1b | 311 | /* Reading the coefficients */ |
xorjoep | 1:24714b45cd1b | 312 | b0 = *pCoeffs++; |
xorjoep | 1:24714b45cd1b | 313 | b1 = *pCoeffs++; |
xorjoep | 1:24714b45cd1b | 314 | b2 = *pCoeffs++; |
xorjoep | 1:24714b45cd1b | 315 | a1 = *pCoeffs++; |
xorjoep | 1:24714b45cd1b | 316 | a2 = *pCoeffs++; |
xorjoep | 1:24714b45cd1b | 317 | |
xorjoep | 1:24714b45cd1b | 318 | /* Reading the state values */ |
xorjoep | 1:24714b45cd1b | 319 | Xn1 = pState[0]; |
xorjoep | 1:24714b45cd1b | 320 | Xn2 = pState[1]; |
xorjoep | 1:24714b45cd1b | 321 | Yn1 = pState[2]; |
xorjoep | 1:24714b45cd1b | 322 | Yn2 = pState[3]; |
xorjoep | 1:24714b45cd1b | 323 | |
xorjoep | 1:24714b45cd1b | 324 | /* The variables acc holds the output value that is computed: |
xorjoep | 1:24714b45cd1b | 325 | * acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] |
xorjoep | 1:24714b45cd1b | 326 | */ |
xorjoep | 1:24714b45cd1b | 327 | |
xorjoep | 1:24714b45cd1b | 328 | sample = blockSize; |
xorjoep | 1:24714b45cd1b | 329 | |
xorjoep | 1:24714b45cd1b | 330 | while (sample > 0U) |
xorjoep | 1:24714b45cd1b | 331 | { |
xorjoep | 1:24714b45cd1b | 332 | /* Read the input */ |
xorjoep | 1:24714b45cd1b | 333 | Xn = *pIn++; |
xorjoep | 1:24714b45cd1b | 334 | |
xorjoep | 1:24714b45cd1b | 335 | /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */ |
xorjoep | 1:24714b45cd1b | 336 | /* acc = b0 * x[n] */ |
xorjoep | 1:24714b45cd1b | 337 | acc = (q63_t) b0 *Xn; |
xorjoep | 1:24714b45cd1b | 338 | |
xorjoep | 1:24714b45cd1b | 339 | /* acc += b1 * x[n-1] */ |
xorjoep | 1:24714b45cd1b | 340 | acc += (q63_t) b1 *Xn1; |
xorjoep | 1:24714b45cd1b | 341 | /* acc += b[2] * x[n-2] */ |
xorjoep | 1:24714b45cd1b | 342 | acc += (q63_t) b2 *Xn2; |
xorjoep | 1:24714b45cd1b | 343 | /* acc += a1 * y[n-1] */ |
xorjoep | 1:24714b45cd1b | 344 | acc += (q63_t) a1 *Yn1; |
xorjoep | 1:24714b45cd1b | 345 | /* acc += a2 * y[n-2] */ |
xorjoep | 1:24714b45cd1b | 346 | acc += (q63_t) a2 *Yn2; |
xorjoep | 1:24714b45cd1b | 347 | |
xorjoep | 1:24714b45cd1b | 348 | /* The result is converted to 1.31 */ |
xorjoep | 1:24714b45cd1b | 349 | acc = acc >> lShift; |
xorjoep | 1:24714b45cd1b | 350 | |
xorjoep | 1:24714b45cd1b | 351 | /* Every time after the output is computed state should be updated. */ |
xorjoep | 1:24714b45cd1b | 352 | /* The states should be updated as: */ |
xorjoep | 1:24714b45cd1b | 353 | /* Xn2 = Xn1 */ |
xorjoep | 1:24714b45cd1b | 354 | /* Xn1 = Xn */ |
xorjoep | 1:24714b45cd1b | 355 | /* Yn2 = Yn1 */ |
xorjoep | 1:24714b45cd1b | 356 | /* Yn1 = acc */ |
xorjoep | 1:24714b45cd1b | 357 | Xn2 = Xn1; |
xorjoep | 1:24714b45cd1b | 358 | Xn1 = Xn; |
xorjoep | 1:24714b45cd1b | 359 | Yn2 = Yn1; |
xorjoep | 1:24714b45cd1b | 360 | Yn1 = (q31_t) acc; |
xorjoep | 1:24714b45cd1b | 361 | |
xorjoep | 1:24714b45cd1b | 362 | /* Store the output in the destination buffer. */ |
xorjoep | 1:24714b45cd1b | 363 | *pOut++ = (q31_t) acc; |
xorjoep | 1:24714b45cd1b | 364 | |
xorjoep | 1:24714b45cd1b | 365 | /* decrement the loop counter */ |
xorjoep | 1:24714b45cd1b | 366 | sample--; |
xorjoep | 1:24714b45cd1b | 367 | } |
xorjoep | 1:24714b45cd1b | 368 | |
xorjoep | 1:24714b45cd1b | 369 | /* The first stage goes from the input buffer to the output buffer. */ |
xorjoep | 1:24714b45cd1b | 370 | /* Subsequent stages occur in-place in the output buffer */ |
xorjoep | 1:24714b45cd1b | 371 | pIn = pDst; |
xorjoep | 1:24714b45cd1b | 372 | |
xorjoep | 1:24714b45cd1b | 373 | /* Reset to destination pointer */ |
xorjoep | 1:24714b45cd1b | 374 | pOut = pDst; |
xorjoep | 1:24714b45cd1b | 375 | |
xorjoep | 1:24714b45cd1b | 376 | /* Store the updated state variables back into the pState array */ |
xorjoep | 1:24714b45cd1b | 377 | *pState++ = Xn1; |
xorjoep | 1:24714b45cd1b | 378 | *pState++ = Xn2; |
xorjoep | 1:24714b45cd1b | 379 | *pState++ = Yn1; |
xorjoep | 1:24714b45cd1b | 380 | *pState++ = Yn2; |
xorjoep | 1:24714b45cd1b | 381 | |
xorjoep | 1:24714b45cd1b | 382 | } while (--stage); |
xorjoep | 1:24714b45cd1b | 383 | |
xorjoep | 1:24714b45cd1b | 384 | #endif /* #if defined (ARM_MATH_DSP) */ |
xorjoep | 1:24714b45cd1b | 385 | } |
xorjoep | 1:24714b45cd1b | 386 | |
xorjoep | 1:24714b45cd1b | 387 | |
xorjoep | 1:24714b45cd1b | 388 | |
xorjoep | 1:24714b45cd1b | 389 | |
xorjoep | 1:24714b45cd1b | 390 | /** |
xorjoep | 1:24714b45cd1b | 391 | * @} end of BiquadCascadeDF1 group |
xorjoep | 1:24714b45cd1b | 392 | */ |