The CMSIS DSP 5 library

Dependents:   Nucleo-Heart-Rate ejercicioVrms2 PROYECTOFINAL ejercicioVrms ... more

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?

UserRevisionLine numberNew contents of line
xorjoep 1:24714b45cd1b 1 /* ----------------------------------------------------------------------
xorjoep 1:24714b45cd1b 2 * Project: CMSIS DSP Library
xorjoep 1:24714b45cd1b 3 * Title: arm_biquad_cascade_df1_q15.c
xorjoep 1:24714b45cd1b 4 * Description: Processing function for the Q15 Biquad cascade DirectFormI(DF1) 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 Q15 Biquad cascade filter.
xorjoep 1:24714b45cd1b 42 * @param[in] *S points to an instance of the Q15 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 location where the output result is written.
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 *
xorjoep 1:24714b45cd1b 49 * <b>Scaling and Overflow Behavior:</b>
xorjoep 1:24714b45cd1b 50 * \par
xorjoep 1:24714b45cd1b 51 * The function is implemented using a 64-bit internal accumulator.
xorjoep 1:24714b45cd1b 52 * Both coefficients and state variables are represented in 1.15 format and multiplications yield a 2.30 result.
xorjoep 1:24714b45cd1b 53 * The 2.30 intermediate results are accumulated in a 64-bit accumulator in 34.30 format.
xorjoep 1:24714b45cd1b 54 * There is no risk of internal overflow with this approach and the full precision of intermediate multiplications is preserved.
xorjoep 1:24714b45cd1b 55 * The accumulator is then shifted by <code>postShift</code> bits to truncate the result to 1.15 format by discarding the low 16 bits.
xorjoep 1:24714b45cd1b 56 * Finally, the result is saturated to 1.15 format.
xorjoep 1:24714b45cd1b 57 *
xorjoep 1:24714b45cd1b 58 * \par
xorjoep 1:24714b45cd1b 59 * Refer to the function <code>arm_biquad_cascade_df1_fast_q15()</code> for a faster but less precise implementation of this filter for Cortex-M3 and Cortex-M4.
xorjoep 1:24714b45cd1b 60 */
xorjoep 1:24714b45cd1b 61
xorjoep 1:24714b45cd1b 62 void arm_biquad_cascade_df1_q15(
xorjoep 1:24714b45cd1b 63 const arm_biquad_casd_df1_inst_q15 * S,
xorjoep 1:24714b45cd1b 64 q15_t * pSrc,
xorjoep 1:24714b45cd1b 65 q15_t * pDst,
xorjoep 1:24714b45cd1b 66 uint32_t blockSize)
xorjoep 1:24714b45cd1b 67 {
xorjoep 1:24714b45cd1b 68
xorjoep 1:24714b45cd1b 69
xorjoep 1:24714b45cd1b 70 #if defined (ARM_MATH_DSP)
xorjoep 1:24714b45cd1b 71
xorjoep 1:24714b45cd1b 72 /* Run the below code for Cortex-M4 and Cortex-M3 */
xorjoep 1:24714b45cd1b 73
xorjoep 1:24714b45cd1b 74 q15_t *pIn = pSrc; /* Source pointer */
xorjoep 1:24714b45cd1b 75 q15_t *pOut = pDst; /* Destination pointer */
xorjoep 1:24714b45cd1b 76 q31_t in; /* Temporary variable to hold input value */
xorjoep 1:24714b45cd1b 77 q31_t out; /* Temporary variable to hold output value */
xorjoep 1:24714b45cd1b 78 q31_t b0; /* Temporary variable to hold bo value */
xorjoep 1:24714b45cd1b 79 q31_t b1, a1; /* Filter coefficients */
xorjoep 1:24714b45cd1b 80 q31_t state_in, state_out; /* Filter state variables */
xorjoep 1:24714b45cd1b 81 q31_t acc_l, acc_h;
xorjoep 1:24714b45cd1b 82 q63_t acc; /* Accumulator */
xorjoep 1:24714b45cd1b 83 int32_t lShift = (15 - (int32_t) S->postShift); /* Post shift */
xorjoep 1:24714b45cd1b 84 q15_t *pState = S->pState; /* State pointer */
xorjoep 1:24714b45cd1b 85 q15_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */
xorjoep 1:24714b45cd1b 86 uint32_t sample, stage = (uint32_t) S->numStages; /* Stage loop counter */
xorjoep 1:24714b45cd1b 87 int32_t uShift = (32 - lShift);
xorjoep 1:24714b45cd1b 88
xorjoep 1:24714b45cd1b 89 do
xorjoep 1:24714b45cd1b 90 {
xorjoep 1:24714b45cd1b 91 /* Read the b0 and 0 coefficients using SIMD */
xorjoep 1:24714b45cd1b 92 b0 = *__SIMD32(pCoeffs)++;
xorjoep 1:24714b45cd1b 93
xorjoep 1:24714b45cd1b 94 /* Read the b1 and b2 coefficients using SIMD */
xorjoep 1:24714b45cd1b 95 b1 = *__SIMD32(pCoeffs)++;
xorjoep 1:24714b45cd1b 96
xorjoep 1:24714b45cd1b 97 /* Read the a1 and a2 coefficients using SIMD */
xorjoep 1:24714b45cd1b 98 a1 = *__SIMD32(pCoeffs)++;
xorjoep 1:24714b45cd1b 99
xorjoep 1:24714b45cd1b 100 /* Read the input state values from the state buffer: x[n-1], x[n-2] */
xorjoep 1:24714b45cd1b 101 state_in = *__SIMD32(pState)++;
xorjoep 1:24714b45cd1b 102
xorjoep 1:24714b45cd1b 103 /* Read the output state values from the state buffer: y[n-1], y[n-2] */
xorjoep 1:24714b45cd1b 104 state_out = *__SIMD32(pState)--;
xorjoep 1:24714b45cd1b 105
xorjoep 1:24714b45cd1b 106 /* Apply loop unrolling and compute 2 output values simultaneously. */
xorjoep 1:24714b45cd1b 107 /* The variable acc hold output values that are being computed:
xorjoep 1:24714b45cd1b 108 *
xorjoep 1:24714b45cd1b 109 * acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2]
xorjoep 1:24714b45cd1b 110 * acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2]
xorjoep 1:24714b45cd1b 111 */
xorjoep 1:24714b45cd1b 112 sample = blockSize >> 1U;
xorjoep 1:24714b45cd1b 113
xorjoep 1:24714b45cd1b 114 /* First part of the processing with loop unrolling. Compute 2 outputs at a time.
xorjoep 1:24714b45cd1b 115 ** a second loop below computes the remaining 1 sample. */
xorjoep 1:24714b45cd1b 116 while (sample > 0U)
xorjoep 1:24714b45cd1b 117 {
xorjoep 1:24714b45cd1b 118
xorjoep 1:24714b45cd1b 119 /* Read the input */
xorjoep 1:24714b45cd1b 120 in = *__SIMD32(pIn)++;
xorjoep 1:24714b45cd1b 121
xorjoep 1:24714b45cd1b 122 /* out = b0 * x[n] + 0 * 0 */
xorjoep 1:24714b45cd1b 123 out = __SMUAD(b0, in);
xorjoep 1:24714b45cd1b 124
xorjoep 1:24714b45cd1b 125 /* acc += b1 * x[n-1] + b2 * x[n-2] + out */
xorjoep 1:24714b45cd1b 126 acc = __SMLALD(b1, state_in, out);
xorjoep 1:24714b45cd1b 127 /* acc += a1 * y[n-1] + a2 * y[n-2] */
xorjoep 1:24714b45cd1b 128 acc = __SMLALD(a1, state_out, acc);
xorjoep 1:24714b45cd1b 129
xorjoep 1:24714b45cd1b 130 /* The result is converted from 3.29 to 1.31 if postShift = 1, and then saturation is applied */
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 out = (uint32_t) acc_l >> lShift | acc_h << uShift;
xorjoep 1:24714b45cd1b 139
xorjoep 1:24714b45cd1b 140 out = __SSAT(out, 16);
xorjoep 1:24714b45cd1b 141
xorjoep 1:24714b45cd1b 142 /* Every time after the output is computed state should be updated. */
xorjoep 1:24714b45cd1b 143 /* The states should be updated as: */
xorjoep 1:24714b45cd1b 144 /* Xn2 = Xn1 */
xorjoep 1:24714b45cd1b 145 /* Xn1 = Xn */
xorjoep 1:24714b45cd1b 146 /* Yn2 = Yn1 */
xorjoep 1:24714b45cd1b 147 /* Yn1 = acc */
xorjoep 1:24714b45cd1b 148 /* x[n-N], x[n-N-1] are packed together to make state_in of type q31 */
xorjoep 1:24714b45cd1b 149 /* y[n-N], y[n-N-1] are packed together to make state_out of type q31 */
xorjoep 1:24714b45cd1b 150
xorjoep 1:24714b45cd1b 151 #ifndef ARM_MATH_BIG_ENDIAN
xorjoep 1:24714b45cd1b 152
xorjoep 1:24714b45cd1b 153 state_in = __PKHBT(in, state_in, 16);
xorjoep 1:24714b45cd1b 154 state_out = __PKHBT(out, state_out, 16);
xorjoep 1:24714b45cd1b 155
xorjoep 1:24714b45cd1b 156 #else
xorjoep 1:24714b45cd1b 157
xorjoep 1:24714b45cd1b 158 state_in = __PKHBT(state_in >> 16, (in >> 16), 16);
xorjoep 1:24714b45cd1b 159 state_out = __PKHBT(state_out >> 16, (out), 16);
xorjoep 1:24714b45cd1b 160
xorjoep 1:24714b45cd1b 161 #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
xorjoep 1:24714b45cd1b 162
xorjoep 1:24714b45cd1b 163 /* out = b0 * x[n] + 0 * 0 */
xorjoep 1:24714b45cd1b 164 out = __SMUADX(b0, in);
xorjoep 1:24714b45cd1b 165 /* acc += b1 * x[n-1] + b2 * x[n-2] + out */
xorjoep 1:24714b45cd1b 166 acc = __SMLALD(b1, state_in, out);
xorjoep 1:24714b45cd1b 167 /* acc += a1 * y[n-1] + a2 * y[n-2] */
xorjoep 1:24714b45cd1b 168 acc = __SMLALD(a1, state_out, acc);
xorjoep 1:24714b45cd1b 169
xorjoep 1:24714b45cd1b 170 /* The result is converted from 3.29 to 1.31 if postShift = 1, and then saturation is applied */
xorjoep 1:24714b45cd1b 171 /* Calc lower part of acc */
xorjoep 1:24714b45cd1b 172 acc_l = acc & 0xffffffff;
xorjoep 1:24714b45cd1b 173
xorjoep 1:24714b45cd1b 174 /* Calc upper part of acc */
xorjoep 1:24714b45cd1b 175 acc_h = (acc >> 32) & 0xffffffff;
xorjoep 1:24714b45cd1b 176
xorjoep 1:24714b45cd1b 177 /* Apply shift for lower part of acc and upper part of acc */
xorjoep 1:24714b45cd1b 178 out = (uint32_t) acc_l >> lShift | acc_h << uShift;
xorjoep 1:24714b45cd1b 179
xorjoep 1:24714b45cd1b 180 out = __SSAT(out, 16);
xorjoep 1:24714b45cd1b 181
xorjoep 1:24714b45cd1b 182 /* Store the output in the destination buffer. */
xorjoep 1:24714b45cd1b 183
xorjoep 1:24714b45cd1b 184 #ifndef ARM_MATH_BIG_ENDIAN
xorjoep 1:24714b45cd1b 185
xorjoep 1:24714b45cd1b 186 *__SIMD32(pOut)++ = __PKHBT(state_out, out, 16);
xorjoep 1:24714b45cd1b 187
xorjoep 1:24714b45cd1b 188 #else
xorjoep 1:24714b45cd1b 189
xorjoep 1:24714b45cd1b 190 *__SIMD32(pOut)++ = __PKHBT(out, state_out >> 16, 16);
xorjoep 1:24714b45cd1b 191
xorjoep 1:24714b45cd1b 192 #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
xorjoep 1:24714b45cd1b 193
xorjoep 1:24714b45cd1b 194 /* Every time after the output is computed state should be updated. */
xorjoep 1:24714b45cd1b 195 /* The states should be updated as: */
xorjoep 1:24714b45cd1b 196 /* Xn2 = Xn1 */
xorjoep 1:24714b45cd1b 197 /* Xn1 = Xn */
xorjoep 1:24714b45cd1b 198 /* Yn2 = Yn1 */
xorjoep 1:24714b45cd1b 199 /* Yn1 = acc */
xorjoep 1:24714b45cd1b 200 /* x[n-N], x[n-N-1] are packed together to make state_in of type q31 */
xorjoep 1:24714b45cd1b 201 /* y[n-N], y[n-N-1] are packed together to make state_out of type q31 */
xorjoep 1:24714b45cd1b 202 #ifndef ARM_MATH_BIG_ENDIAN
xorjoep 1:24714b45cd1b 203
xorjoep 1:24714b45cd1b 204 state_in = __PKHBT(in >> 16, state_in, 16);
xorjoep 1:24714b45cd1b 205 state_out = __PKHBT(out, state_out, 16);
xorjoep 1:24714b45cd1b 206
xorjoep 1:24714b45cd1b 207 #else
xorjoep 1:24714b45cd1b 208
xorjoep 1:24714b45cd1b 209 state_in = __PKHBT(state_in >> 16, in, 16);
xorjoep 1:24714b45cd1b 210 state_out = __PKHBT(state_out >> 16, out, 16);
xorjoep 1:24714b45cd1b 211
xorjoep 1:24714b45cd1b 212 #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
xorjoep 1:24714b45cd1b 213
xorjoep 1:24714b45cd1b 214
xorjoep 1:24714b45cd1b 215 /* Decrement the loop counter */
xorjoep 1:24714b45cd1b 216 sample--;
xorjoep 1:24714b45cd1b 217
xorjoep 1:24714b45cd1b 218 }
xorjoep 1:24714b45cd1b 219
xorjoep 1:24714b45cd1b 220 /* If the blockSize is not a multiple of 2, compute any remaining output samples here.
xorjoep 1:24714b45cd1b 221 ** No loop unrolling is used. */
xorjoep 1:24714b45cd1b 222
xorjoep 1:24714b45cd1b 223 if ((blockSize & 0x1U) != 0U)
xorjoep 1:24714b45cd1b 224 {
xorjoep 1:24714b45cd1b 225 /* Read the input */
xorjoep 1:24714b45cd1b 226 in = *pIn++;
xorjoep 1:24714b45cd1b 227
xorjoep 1:24714b45cd1b 228 /* out = b0 * x[n] + 0 * 0 */
xorjoep 1:24714b45cd1b 229
xorjoep 1:24714b45cd1b 230 #ifndef ARM_MATH_BIG_ENDIAN
xorjoep 1:24714b45cd1b 231
xorjoep 1:24714b45cd1b 232 out = __SMUAD(b0, in);
xorjoep 1:24714b45cd1b 233
xorjoep 1:24714b45cd1b 234 #else
xorjoep 1:24714b45cd1b 235
xorjoep 1:24714b45cd1b 236 out = __SMUADX(b0, in);
xorjoep 1:24714b45cd1b 237
xorjoep 1:24714b45cd1b 238 #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
xorjoep 1:24714b45cd1b 239
xorjoep 1:24714b45cd1b 240 /* acc = b1 * x[n-1] + b2 * x[n-2] + out */
xorjoep 1:24714b45cd1b 241 acc = __SMLALD(b1, state_in, out);
xorjoep 1:24714b45cd1b 242 /* acc += a1 * y[n-1] + a2 * y[n-2] */
xorjoep 1:24714b45cd1b 243 acc = __SMLALD(a1, state_out, acc);
xorjoep 1:24714b45cd1b 244
xorjoep 1:24714b45cd1b 245 /* The result is converted from 3.29 to 1.31 if postShift = 1, and then saturation is applied */
xorjoep 1:24714b45cd1b 246 /* Calc lower part of acc */
xorjoep 1:24714b45cd1b 247 acc_l = acc & 0xffffffff;
xorjoep 1:24714b45cd1b 248
xorjoep 1:24714b45cd1b 249 /* Calc upper part of acc */
xorjoep 1:24714b45cd1b 250 acc_h = (acc >> 32) & 0xffffffff;
xorjoep 1:24714b45cd1b 251
xorjoep 1:24714b45cd1b 252 /* Apply shift for lower part of acc and upper part of acc */
xorjoep 1:24714b45cd1b 253 out = (uint32_t) acc_l >> lShift | acc_h << uShift;
xorjoep 1:24714b45cd1b 254
xorjoep 1:24714b45cd1b 255 out = __SSAT(out, 16);
xorjoep 1:24714b45cd1b 256
xorjoep 1:24714b45cd1b 257 /* Store the output in the destination buffer. */
xorjoep 1:24714b45cd1b 258 *pOut++ = (q15_t) out;
xorjoep 1:24714b45cd1b 259
xorjoep 1:24714b45cd1b 260 /* Every time after the output is computed state should be updated. */
xorjoep 1:24714b45cd1b 261 /* The states should be updated as: */
xorjoep 1:24714b45cd1b 262 /* Xn2 = Xn1 */
xorjoep 1:24714b45cd1b 263 /* Xn1 = Xn */
xorjoep 1:24714b45cd1b 264 /* Yn2 = Yn1 */
xorjoep 1:24714b45cd1b 265 /* Yn1 = acc */
xorjoep 1:24714b45cd1b 266 /* x[n-N], x[n-N-1] are packed together to make state_in of type q31 */
xorjoep 1:24714b45cd1b 267 /* y[n-N], y[n-N-1] are packed together to make state_out of type q31 */
xorjoep 1:24714b45cd1b 268
xorjoep 1:24714b45cd1b 269 #ifndef ARM_MATH_BIG_ENDIAN
xorjoep 1:24714b45cd1b 270
xorjoep 1:24714b45cd1b 271 state_in = __PKHBT(in, state_in, 16);
xorjoep 1:24714b45cd1b 272 state_out = __PKHBT(out, state_out, 16);
xorjoep 1:24714b45cd1b 273
xorjoep 1:24714b45cd1b 274 #else
xorjoep 1:24714b45cd1b 275
xorjoep 1:24714b45cd1b 276 state_in = __PKHBT(state_in >> 16, in, 16);
xorjoep 1:24714b45cd1b 277 state_out = __PKHBT(state_out >> 16, out, 16);
xorjoep 1:24714b45cd1b 278
xorjoep 1:24714b45cd1b 279 #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
xorjoep 1:24714b45cd1b 280
xorjoep 1:24714b45cd1b 281 }
xorjoep 1:24714b45cd1b 282
xorjoep 1:24714b45cd1b 283 /* The first stage goes from the input wire to the output wire. */
xorjoep 1:24714b45cd1b 284 /* Subsequent numStages occur in-place in the output wire */
xorjoep 1:24714b45cd1b 285 pIn = pDst;
xorjoep 1:24714b45cd1b 286
xorjoep 1:24714b45cd1b 287 /* Reset the output pointer */
xorjoep 1:24714b45cd1b 288 pOut = pDst;
xorjoep 1:24714b45cd1b 289
xorjoep 1:24714b45cd1b 290 /* Store the updated state variables back into the state array */
xorjoep 1:24714b45cd1b 291 *__SIMD32(pState)++ = state_in;
xorjoep 1:24714b45cd1b 292 *__SIMD32(pState)++ = state_out;
xorjoep 1:24714b45cd1b 293
xorjoep 1:24714b45cd1b 294
xorjoep 1:24714b45cd1b 295 /* Decrement the loop counter */
xorjoep 1:24714b45cd1b 296 stage--;
xorjoep 1:24714b45cd1b 297
xorjoep 1:24714b45cd1b 298 } while (stage > 0U);
xorjoep 1:24714b45cd1b 299
xorjoep 1:24714b45cd1b 300 #else
xorjoep 1:24714b45cd1b 301
xorjoep 1:24714b45cd1b 302 /* Run the below code for Cortex-M0 */
xorjoep 1:24714b45cd1b 303
xorjoep 1:24714b45cd1b 304 q15_t *pIn = pSrc; /* Source pointer */
xorjoep 1:24714b45cd1b 305 q15_t *pOut = pDst; /* Destination pointer */
xorjoep 1:24714b45cd1b 306 q15_t b0, b1, b2, a1, a2; /* Filter coefficients */
xorjoep 1:24714b45cd1b 307 q15_t Xn1, Xn2, Yn1, Yn2; /* Filter state variables */
xorjoep 1:24714b45cd1b 308 q15_t Xn; /* temporary input */
xorjoep 1:24714b45cd1b 309 q63_t acc; /* Accumulator */
xorjoep 1:24714b45cd1b 310 int32_t shift = (15 - (int32_t) S->postShift); /* Post shift */
xorjoep 1:24714b45cd1b 311 q15_t *pState = S->pState; /* State pointer */
xorjoep 1:24714b45cd1b 312 q15_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */
xorjoep 1:24714b45cd1b 313 uint32_t sample, stage = (uint32_t) S->numStages; /* Stage loop counter */
xorjoep 1:24714b45cd1b 314
xorjoep 1:24714b45cd1b 315 do
xorjoep 1:24714b45cd1b 316 {
xorjoep 1:24714b45cd1b 317 /* Reading the coefficients */
xorjoep 1:24714b45cd1b 318 b0 = *pCoeffs++;
xorjoep 1:24714b45cd1b 319 pCoeffs++; // skip the 0 coefficient
xorjoep 1:24714b45cd1b 320 b1 = *pCoeffs++;
xorjoep 1:24714b45cd1b 321 b2 = *pCoeffs++;
xorjoep 1:24714b45cd1b 322 a1 = *pCoeffs++;
xorjoep 1:24714b45cd1b 323 a2 = *pCoeffs++;
xorjoep 1:24714b45cd1b 324
xorjoep 1:24714b45cd1b 325 /* Reading the state values */
xorjoep 1:24714b45cd1b 326 Xn1 = pState[0];
xorjoep 1:24714b45cd1b 327 Xn2 = pState[1];
xorjoep 1:24714b45cd1b 328 Yn1 = pState[2];
xorjoep 1:24714b45cd1b 329 Yn2 = pState[3];
xorjoep 1:24714b45cd1b 330
xorjoep 1:24714b45cd1b 331 /* The variables acc holds the output value that is computed:
xorjoep 1:24714b45cd1b 332 * acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2]
xorjoep 1:24714b45cd1b 333 */
xorjoep 1:24714b45cd1b 334
xorjoep 1:24714b45cd1b 335 sample = blockSize;
xorjoep 1:24714b45cd1b 336
xorjoep 1:24714b45cd1b 337 while (sample > 0U)
xorjoep 1:24714b45cd1b 338 {
xorjoep 1:24714b45cd1b 339 /* Read the input */
xorjoep 1:24714b45cd1b 340 Xn = *pIn++;
xorjoep 1:24714b45cd1b 341
xorjoep 1:24714b45cd1b 342 /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */
xorjoep 1:24714b45cd1b 343 /* acc = b0 * x[n] */
xorjoep 1:24714b45cd1b 344 acc = (q31_t) b0 *Xn;
xorjoep 1:24714b45cd1b 345
xorjoep 1:24714b45cd1b 346 /* acc += b1 * x[n-1] */
xorjoep 1:24714b45cd1b 347 acc += (q31_t) b1 *Xn1;
xorjoep 1:24714b45cd1b 348 /* acc += b[2] * x[n-2] */
xorjoep 1:24714b45cd1b 349 acc += (q31_t) b2 *Xn2;
xorjoep 1:24714b45cd1b 350 /* acc += a1 * y[n-1] */
xorjoep 1:24714b45cd1b 351 acc += (q31_t) a1 *Yn1;
xorjoep 1:24714b45cd1b 352 /* acc += a2 * y[n-2] */
xorjoep 1:24714b45cd1b 353 acc += (q31_t) a2 *Yn2;
xorjoep 1:24714b45cd1b 354
xorjoep 1:24714b45cd1b 355 /* The result is converted to 1.31 */
xorjoep 1:24714b45cd1b 356 acc = __SSAT((acc >> shift), 16);
xorjoep 1:24714b45cd1b 357
xorjoep 1:24714b45cd1b 358 /* Every time after the output is computed state should be updated. */
xorjoep 1:24714b45cd1b 359 /* The states should be updated as: */
xorjoep 1:24714b45cd1b 360 /* Xn2 = Xn1 */
xorjoep 1:24714b45cd1b 361 /* Xn1 = Xn */
xorjoep 1:24714b45cd1b 362 /* Yn2 = Yn1 */
xorjoep 1:24714b45cd1b 363 /* Yn1 = acc */
xorjoep 1:24714b45cd1b 364 Xn2 = Xn1;
xorjoep 1:24714b45cd1b 365 Xn1 = Xn;
xorjoep 1:24714b45cd1b 366 Yn2 = Yn1;
xorjoep 1:24714b45cd1b 367 Yn1 = (q15_t) acc;
xorjoep 1:24714b45cd1b 368
xorjoep 1:24714b45cd1b 369 /* Store the output in the destination buffer. */
xorjoep 1:24714b45cd1b 370 *pOut++ = (q15_t) acc;
xorjoep 1:24714b45cd1b 371
xorjoep 1:24714b45cd1b 372 /* decrement the loop counter */
xorjoep 1:24714b45cd1b 373 sample--;
xorjoep 1:24714b45cd1b 374 }
xorjoep 1:24714b45cd1b 375
xorjoep 1:24714b45cd1b 376 /* The first stage goes from the input buffer to the output buffer. */
xorjoep 1:24714b45cd1b 377 /* Subsequent stages occur in-place in the output buffer */
xorjoep 1:24714b45cd1b 378 pIn = pDst;
xorjoep 1:24714b45cd1b 379
xorjoep 1:24714b45cd1b 380 /* Reset to destination pointer */
xorjoep 1:24714b45cd1b 381 pOut = pDst;
xorjoep 1:24714b45cd1b 382
xorjoep 1:24714b45cd1b 383 /* Store the updated state variables back into the pState array */
xorjoep 1:24714b45cd1b 384 *pState++ = Xn1;
xorjoep 1:24714b45cd1b 385 *pState++ = Xn2;
xorjoep 1:24714b45cd1b 386 *pState++ = Yn1;
xorjoep 1:24714b45cd1b 387 *pState++ = Yn2;
xorjoep 1:24714b45cd1b 388
xorjoep 1:24714b45cd1b 389 } while (--stage);
xorjoep 1:24714b45cd1b 390
xorjoep 1:24714b45cd1b 391 #endif /* #if defined (ARM_MATH_DSP) */
xorjoep 1:24714b45cd1b 392
xorjoep 1:24714b45cd1b 393 }
xorjoep 1:24714b45cd1b 394
xorjoep 1:24714b45cd1b 395
xorjoep 1:24714b45cd1b 396 /**
xorjoep 1:24714b45cd1b 397 * @} end of BiquadCascadeDF1 group
xorjoep 1:24714b45cd1b 398 */