The CMSIS DSP 5 library
Dependents: Nucleo-Heart-Rate ejercicioVrms2 PROYECTOFINAL ejercicioVrms ... more
functions/FilteringFunctions/arm_biquad_cascade_df1_f32.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_f32.c |
xorjoep | 1:24714b45cd1b | 4 | * Description: Processing function for the floating-point 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 | * @defgroup BiquadCascadeDF1 Biquad Cascade IIR Filters Using Direct Form I Structure |
xorjoep | 1:24714b45cd1b | 37 | * |
xorjoep | 1:24714b45cd1b | 38 | * This set of functions implements arbitrary order recursive (IIR) filters. |
xorjoep | 1:24714b45cd1b | 39 | * The filters are implemented as a cascade of second order Biquad sections. |
xorjoep | 1:24714b45cd1b | 40 | * The functions support Q15, Q31 and floating-point data types. |
xorjoep | 1:24714b45cd1b | 41 | * Fast version of Q15 and Q31 also supported on CortexM4 and Cortex-M3. |
xorjoep | 1:24714b45cd1b | 42 | * |
xorjoep | 1:24714b45cd1b | 43 | * The functions operate on blocks of input and output data and each call to the function |
xorjoep | 1:24714b45cd1b | 44 | * processes <code>blockSize</code> samples through the filter. |
xorjoep | 1:24714b45cd1b | 45 | * <code>pSrc</code> points to the array of input data and |
xorjoep | 1:24714b45cd1b | 46 | * <code>pDst</code> points to the array of output data. |
xorjoep | 1:24714b45cd1b | 47 | * Both arrays contain <code>blockSize</code> values. |
xorjoep | 1:24714b45cd1b | 48 | * |
xorjoep | 1:24714b45cd1b | 49 | * \par Algorithm |
xorjoep | 1:24714b45cd1b | 50 | * Each Biquad stage implements a second order filter using the difference equation: |
xorjoep | 1:24714b45cd1b | 51 | * <pre> |
xorjoep | 1:24714b45cd1b | 52 | * y[n] = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] |
xorjoep | 1:24714b45cd1b | 53 | * </pre> |
xorjoep | 1:24714b45cd1b | 54 | * A Direct Form I algorithm is used with 5 coefficients and 4 state variables per stage. |
xorjoep | 1:24714b45cd1b | 55 | * \image html Biquad.gif "Single Biquad filter stage" |
xorjoep | 1:24714b45cd1b | 56 | * Coefficients <code>b0, b1 and b2 </code> multiply the input signal <code>x[n]</code> and are referred to as the feedforward coefficients. |
xorjoep | 1:24714b45cd1b | 57 | * Coefficients <code>a1</code> and <code>a2</code> multiply the output signal <code>y[n]</code> and are referred to as the feedback coefficients. |
xorjoep | 1:24714b45cd1b | 58 | * Pay careful attention to the sign of the feedback coefficients. |
xorjoep | 1:24714b45cd1b | 59 | * Some design tools use the difference equation |
xorjoep | 1:24714b45cd1b | 60 | * <pre> |
xorjoep | 1:24714b45cd1b | 61 | * y[n] = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] - a1 * y[n-1] - a2 * y[n-2] |
xorjoep | 1:24714b45cd1b | 62 | * </pre> |
xorjoep | 1:24714b45cd1b | 63 | * In this case the feedback coefficients <code>a1</code> and <code>a2</code> must be negated when used with the CMSIS DSP Library. |
xorjoep | 1:24714b45cd1b | 64 | * |
xorjoep | 1:24714b45cd1b | 65 | * \par |
xorjoep | 1:24714b45cd1b | 66 | * Higher order filters are realized as a cascade of second order sections. |
xorjoep | 1:24714b45cd1b | 67 | * <code>numStages</code> refers to the number of second order stages used. |
xorjoep | 1:24714b45cd1b | 68 | * For example, an 8th order filter would be realized with <code>numStages=4</code> second order stages. |
xorjoep | 1:24714b45cd1b | 69 | * \image html BiquadCascade.gif "8th order filter using a cascade of Biquad stages" |
xorjoep | 1:24714b45cd1b | 70 | * A 9th order filter would be realized with <code>numStages=5</code> second order stages with the coefficients for one of the stages configured as a first order filter (<code>b2=0</code> and <code>a2=0</code>). |
xorjoep | 1:24714b45cd1b | 71 | * |
xorjoep | 1:24714b45cd1b | 72 | * \par |
xorjoep | 1:24714b45cd1b | 73 | * The <code>pState</code> points to state variables array. |
xorjoep | 1:24714b45cd1b | 74 | * Each Biquad stage has 4 state variables <code>x[n-1], x[n-2], y[n-1],</code> and <code>y[n-2]</code>. |
xorjoep | 1:24714b45cd1b | 75 | * The state variables are arranged in the <code>pState</code> array as: |
xorjoep | 1:24714b45cd1b | 76 | * <pre> |
xorjoep | 1:24714b45cd1b | 77 | * {x[n-1], x[n-2], y[n-1], y[n-2]} |
xorjoep | 1:24714b45cd1b | 78 | * </pre> |
xorjoep | 1:24714b45cd1b | 79 | * |
xorjoep | 1:24714b45cd1b | 80 | * \par |
xorjoep | 1:24714b45cd1b | 81 | * The 4 state variables for stage 1 are first, then the 4 state variables for stage 2, and so on. |
xorjoep | 1:24714b45cd1b | 82 | * The state array has a total length of <code>4*numStages</code> values. |
xorjoep | 1:24714b45cd1b | 83 | * The state variables are updated after each block of data is processed, the coefficients are untouched. |
xorjoep | 1:24714b45cd1b | 84 | * |
xorjoep | 1:24714b45cd1b | 85 | * \par Instance Structure |
xorjoep | 1:24714b45cd1b | 86 | * The coefficients and state variables for a filter are stored together in an instance data structure. |
xorjoep | 1:24714b45cd1b | 87 | * A separate instance structure must be defined for each filter. |
xorjoep | 1:24714b45cd1b | 88 | * Coefficient arrays may be shared among several instances while state variable arrays cannot be shared. |
xorjoep | 1:24714b45cd1b | 89 | * There are separate instance structure declarations for each of the 3 supported data types. |
xorjoep | 1:24714b45cd1b | 90 | * |
xorjoep | 1:24714b45cd1b | 91 | * \par Init Functions |
xorjoep | 1:24714b45cd1b | 92 | * There is also an associated initialization function for each data type. |
xorjoep | 1:24714b45cd1b | 93 | * The initialization function performs following operations: |
xorjoep | 1:24714b45cd1b | 94 | * - Sets the values of the internal structure fields. |
xorjoep | 1:24714b45cd1b | 95 | * - Zeros out the values in the state buffer. |
xorjoep | 1:24714b45cd1b | 96 | * To do this manually without calling the init function, assign the follow subfields of the instance structure: |
xorjoep | 1:24714b45cd1b | 97 | * numStages, pCoeffs, pState. Also set all of the values in pState to zero. |
xorjoep | 1:24714b45cd1b | 98 | * |
xorjoep | 1:24714b45cd1b | 99 | * \par |
xorjoep | 1:24714b45cd1b | 100 | * Use of the initialization function is optional. |
xorjoep | 1:24714b45cd1b | 101 | * However, if the initialization function is used, then the instance structure cannot be placed into a const data section. |
xorjoep | 1:24714b45cd1b | 102 | * To place an instance structure into a const data section, the instance structure must be manually initialized. |
xorjoep | 1:24714b45cd1b | 103 | * Set the values in the state buffer to zeros before static initialization. |
xorjoep | 1:24714b45cd1b | 104 | * The code below statically initializes each of the 3 different data type filter instance structures |
xorjoep | 1:24714b45cd1b | 105 | * <pre> |
xorjoep | 1:24714b45cd1b | 106 | * arm_biquad_casd_df1_inst_f32 S1 = {numStages, pState, pCoeffs}; |
xorjoep | 1:24714b45cd1b | 107 | * arm_biquad_casd_df1_inst_q15 S2 = {numStages, pState, pCoeffs, postShift}; |
xorjoep | 1:24714b45cd1b | 108 | * arm_biquad_casd_df1_inst_q31 S3 = {numStages, pState, pCoeffs, postShift}; |
xorjoep | 1:24714b45cd1b | 109 | * </pre> |
xorjoep | 1:24714b45cd1b | 110 | * where <code>numStages</code> is the number of Biquad stages in the filter; <code>pState</code> is the address of the state buffer; |
xorjoep | 1:24714b45cd1b | 111 | * <code>pCoeffs</code> is the address of the coefficient buffer; <code>postShift</code> shift to be applied. |
xorjoep | 1:24714b45cd1b | 112 | * |
xorjoep | 1:24714b45cd1b | 113 | * \par Fixed-Point Behavior |
xorjoep | 1:24714b45cd1b | 114 | * Care must be taken when using the Q15 and Q31 versions of the Biquad Cascade filter functions. |
xorjoep | 1:24714b45cd1b | 115 | * Following issues must be considered: |
xorjoep | 1:24714b45cd1b | 116 | * - Scaling of coefficients |
xorjoep | 1:24714b45cd1b | 117 | * - Filter gain |
xorjoep | 1:24714b45cd1b | 118 | * - Overflow and saturation |
xorjoep | 1:24714b45cd1b | 119 | * |
xorjoep | 1:24714b45cd1b | 120 | * \par |
xorjoep | 1:24714b45cd1b | 121 | * <b>Scaling of coefficients: </b> |
xorjoep | 1:24714b45cd1b | 122 | * Filter coefficients are represented as fractional values and |
xorjoep | 1:24714b45cd1b | 123 | * coefficients are restricted to lie in the range <code>[-1 +1)</code>. |
xorjoep | 1:24714b45cd1b | 124 | * The fixed-point functions have an additional scaling parameter <code>postShift</code> |
xorjoep | 1:24714b45cd1b | 125 | * which allow the filter coefficients to exceed the range <code>[+1 -1)</code>. |
xorjoep | 1:24714b45cd1b | 126 | * At the output of the filter's accumulator is a shift register which shifts the result by <code>postShift</code> bits. |
xorjoep | 1:24714b45cd1b | 127 | * \image html BiquadPostshift.gif "Fixed-point Biquad with shift by postShift bits after accumulator" |
xorjoep | 1:24714b45cd1b | 128 | * This essentially scales the filter coefficients by <code>2^postShift</code>. |
xorjoep | 1:24714b45cd1b | 129 | * For example, to realize the coefficients |
xorjoep | 1:24714b45cd1b | 130 | * <pre> |
xorjoep | 1:24714b45cd1b | 131 | * {1.5, -0.8, 1.2, 1.6, -0.9} |
xorjoep | 1:24714b45cd1b | 132 | * </pre> |
xorjoep | 1:24714b45cd1b | 133 | * set the pCoeffs array to: |
xorjoep | 1:24714b45cd1b | 134 | * <pre> |
xorjoep | 1:24714b45cd1b | 135 | * {0.75, -0.4, 0.6, 0.8, -0.45} |
xorjoep | 1:24714b45cd1b | 136 | * </pre> |
xorjoep | 1:24714b45cd1b | 137 | * and set <code>postShift=1</code> |
xorjoep | 1:24714b45cd1b | 138 | * |
xorjoep | 1:24714b45cd1b | 139 | * \par |
xorjoep | 1:24714b45cd1b | 140 | * <b>Filter gain: </b> |
xorjoep | 1:24714b45cd1b | 141 | * The frequency response of a Biquad filter is a function of its coefficients. |
xorjoep | 1:24714b45cd1b | 142 | * It is possible for the gain through the filter to exceed 1.0 meaning that the filter increases the amplitude of certain frequencies. |
xorjoep | 1:24714b45cd1b | 143 | * This means that an input signal with amplitude < 1.0 may result in an output > 1.0 and these are saturated or overflowed based on the implementation of the filter. |
xorjoep | 1:24714b45cd1b | 144 | * To avoid this behavior the filter needs to be scaled down such that its peak gain < 1.0 or the input signal must be scaled down so that the combination of input and filter are never overflowed. |
xorjoep | 1:24714b45cd1b | 145 | * |
xorjoep | 1:24714b45cd1b | 146 | * \par |
xorjoep | 1:24714b45cd1b | 147 | * <b>Overflow and saturation: </b> |
xorjoep | 1:24714b45cd1b | 148 | * For Q15 and Q31 versions, it is described separately as part of the function specific documentation below. |
xorjoep | 1:24714b45cd1b | 149 | */ |
xorjoep | 1:24714b45cd1b | 150 | |
xorjoep | 1:24714b45cd1b | 151 | /** |
xorjoep | 1:24714b45cd1b | 152 | * @addtogroup BiquadCascadeDF1 |
xorjoep | 1:24714b45cd1b | 153 | * @{ |
xorjoep | 1:24714b45cd1b | 154 | */ |
xorjoep | 1:24714b45cd1b | 155 | |
xorjoep | 1:24714b45cd1b | 156 | /** |
xorjoep | 1:24714b45cd1b | 157 | * @param[in] *S points to an instance of the floating-point Biquad cascade structure. |
xorjoep | 1:24714b45cd1b | 158 | * @param[in] *pSrc points to the block of input data. |
xorjoep | 1:24714b45cd1b | 159 | * @param[out] *pDst points to the block of output data. |
xorjoep | 1:24714b45cd1b | 160 | * @param[in] blockSize number of samples to process per call. |
xorjoep | 1:24714b45cd1b | 161 | * @return none. |
xorjoep | 1:24714b45cd1b | 162 | * |
xorjoep | 1:24714b45cd1b | 163 | */ |
xorjoep | 1:24714b45cd1b | 164 | |
xorjoep | 1:24714b45cd1b | 165 | void arm_biquad_cascade_df1_f32( |
xorjoep | 1:24714b45cd1b | 166 | const arm_biquad_casd_df1_inst_f32 * S, |
xorjoep | 1:24714b45cd1b | 167 | float32_t * pSrc, |
xorjoep | 1:24714b45cd1b | 168 | float32_t * pDst, |
xorjoep | 1:24714b45cd1b | 169 | uint32_t blockSize) |
xorjoep | 1:24714b45cd1b | 170 | { |
xorjoep | 1:24714b45cd1b | 171 | float32_t *pIn = pSrc; /* source pointer */ |
xorjoep | 1:24714b45cd1b | 172 | float32_t *pOut = pDst; /* destination pointer */ |
xorjoep | 1:24714b45cd1b | 173 | float32_t *pState = S->pState; /* pState pointer */ |
xorjoep | 1:24714b45cd1b | 174 | float32_t *pCoeffs = S->pCoeffs; /* coefficient pointer */ |
xorjoep | 1:24714b45cd1b | 175 | float32_t acc; /* Simulates the accumulator */ |
xorjoep | 1:24714b45cd1b | 176 | float32_t b0, b1, b2, a1, a2; /* Filter coefficients */ |
xorjoep | 1:24714b45cd1b | 177 | float32_t Xn1, Xn2, Yn1, Yn2; /* Filter pState variables */ |
xorjoep | 1:24714b45cd1b | 178 | float32_t Xn; /* temporary input */ |
xorjoep | 1:24714b45cd1b | 179 | uint32_t sample, stage = S->numStages; /* loop counters */ |
xorjoep | 1:24714b45cd1b | 180 | |
xorjoep | 1:24714b45cd1b | 181 | |
xorjoep | 1:24714b45cd1b | 182 | #if defined (ARM_MATH_DSP) |
xorjoep | 1:24714b45cd1b | 183 | |
xorjoep | 1:24714b45cd1b | 184 | /* Run the below code for Cortex-M4 and Cortex-M3 */ |
xorjoep | 1:24714b45cd1b | 185 | |
xorjoep | 1:24714b45cd1b | 186 | do |
xorjoep | 1:24714b45cd1b | 187 | { |
xorjoep | 1:24714b45cd1b | 188 | /* Reading the coefficients */ |
xorjoep | 1:24714b45cd1b | 189 | b0 = *pCoeffs++; |
xorjoep | 1:24714b45cd1b | 190 | b1 = *pCoeffs++; |
xorjoep | 1:24714b45cd1b | 191 | b2 = *pCoeffs++; |
xorjoep | 1:24714b45cd1b | 192 | a1 = *pCoeffs++; |
xorjoep | 1:24714b45cd1b | 193 | a2 = *pCoeffs++; |
xorjoep | 1:24714b45cd1b | 194 | |
xorjoep | 1:24714b45cd1b | 195 | /* Reading the pState values */ |
xorjoep | 1:24714b45cd1b | 196 | Xn1 = pState[0]; |
xorjoep | 1:24714b45cd1b | 197 | Xn2 = pState[1]; |
xorjoep | 1:24714b45cd1b | 198 | Yn1 = pState[2]; |
xorjoep | 1:24714b45cd1b | 199 | Yn2 = pState[3]; |
xorjoep | 1:24714b45cd1b | 200 | |
xorjoep | 1:24714b45cd1b | 201 | /* Apply loop unrolling and compute 4 output values simultaneously. */ |
xorjoep | 1:24714b45cd1b | 202 | /* The variable acc hold output values that are being computed: |
xorjoep | 1:24714b45cd1b | 203 | * |
xorjoep | 1:24714b45cd1b | 204 | * acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] |
xorjoep | 1:24714b45cd1b | 205 | * acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] |
xorjoep | 1:24714b45cd1b | 206 | * acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] |
xorjoep | 1:24714b45cd1b | 207 | * acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] |
xorjoep | 1:24714b45cd1b | 208 | */ |
xorjoep | 1:24714b45cd1b | 209 | |
xorjoep | 1:24714b45cd1b | 210 | sample = blockSize >> 2U; |
xorjoep | 1:24714b45cd1b | 211 | |
xorjoep | 1:24714b45cd1b | 212 | /* First part of the processing with loop unrolling. Compute 4 outputs at a time. |
xorjoep | 1:24714b45cd1b | 213 | ** a second loop below computes the remaining 1 to 3 samples. */ |
xorjoep | 1:24714b45cd1b | 214 | while (sample > 0U) |
xorjoep | 1:24714b45cd1b | 215 | { |
xorjoep | 1:24714b45cd1b | 216 | /* Read the first input */ |
xorjoep | 1:24714b45cd1b | 217 | Xn = *pIn++; |
xorjoep | 1:24714b45cd1b | 218 | |
xorjoep | 1:24714b45cd1b | 219 | /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */ |
xorjoep | 1:24714b45cd1b | 220 | Yn2 = (b0 * Xn) + (b1 * Xn1) + (b2 * Xn2) + (a1 * Yn1) + (a2 * Yn2); |
xorjoep | 1:24714b45cd1b | 221 | |
xorjoep | 1:24714b45cd1b | 222 | /* Store the result in the accumulator in the destination buffer. */ |
xorjoep | 1:24714b45cd1b | 223 | *pOut++ = Yn2; |
xorjoep | 1:24714b45cd1b | 224 | |
xorjoep | 1:24714b45cd1b | 225 | /* Every time after the output is computed state should be updated. */ |
xorjoep | 1:24714b45cd1b | 226 | /* The states should be updated as: */ |
xorjoep | 1:24714b45cd1b | 227 | /* Xn2 = Xn1 */ |
xorjoep | 1:24714b45cd1b | 228 | /* Xn1 = Xn */ |
xorjoep | 1:24714b45cd1b | 229 | /* Yn2 = Yn1 */ |
xorjoep | 1:24714b45cd1b | 230 | /* Yn1 = acc */ |
xorjoep | 1:24714b45cd1b | 231 | |
xorjoep | 1:24714b45cd1b | 232 | /* Read the second input */ |
xorjoep | 1:24714b45cd1b | 233 | Xn2 = *pIn++; |
xorjoep | 1:24714b45cd1b | 234 | |
xorjoep | 1:24714b45cd1b | 235 | /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */ |
xorjoep | 1:24714b45cd1b | 236 | Yn1 = (b0 * Xn2) + (b1 * Xn) + (b2 * Xn1) + (a1 * Yn2) + (a2 * Yn1); |
xorjoep | 1:24714b45cd1b | 237 | |
xorjoep | 1:24714b45cd1b | 238 | /* Store the result in the accumulator in the destination buffer. */ |
xorjoep | 1:24714b45cd1b | 239 | *pOut++ = Yn1; |
xorjoep | 1:24714b45cd1b | 240 | |
xorjoep | 1:24714b45cd1b | 241 | /* Every time after the output is computed state should be updated. */ |
xorjoep | 1:24714b45cd1b | 242 | /* The states should be updated as: */ |
xorjoep | 1:24714b45cd1b | 243 | /* Xn2 = Xn1 */ |
xorjoep | 1:24714b45cd1b | 244 | /* Xn1 = Xn */ |
xorjoep | 1:24714b45cd1b | 245 | /* Yn2 = Yn1 */ |
xorjoep | 1:24714b45cd1b | 246 | /* Yn1 = acc */ |
xorjoep | 1:24714b45cd1b | 247 | |
xorjoep | 1:24714b45cd1b | 248 | /* Read the third input */ |
xorjoep | 1:24714b45cd1b | 249 | Xn1 = *pIn++; |
xorjoep | 1:24714b45cd1b | 250 | |
xorjoep | 1:24714b45cd1b | 251 | /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */ |
xorjoep | 1:24714b45cd1b | 252 | Yn2 = (b0 * Xn1) + (b1 * Xn2) + (b2 * Xn) + (a1 * Yn1) + (a2 * Yn2); |
xorjoep | 1:24714b45cd1b | 253 | |
xorjoep | 1:24714b45cd1b | 254 | /* Store the result in the accumulator in the destination buffer. */ |
xorjoep | 1:24714b45cd1b | 255 | *pOut++ = Yn2; |
xorjoep | 1:24714b45cd1b | 256 | |
xorjoep | 1:24714b45cd1b | 257 | /* Every time after the output is computed state should be updated. */ |
xorjoep | 1:24714b45cd1b | 258 | /* The states should be updated as: */ |
xorjoep | 1:24714b45cd1b | 259 | /* Xn2 = Xn1 */ |
xorjoep | 1:24714b45cd1b | 260 | /* Xn1 = Xn */ |
xorjoep | 1:24714b45cd1b | 261 | /* Yn2 = Yn1 */ |
xorjoep | 1:24714b45cd1b | 262 | /* Yn1 = acc */ |
xorjoep | 1:24714b45cd1b | 263 | |
xorjoep | 1:24714b45cd1b | 264 | /* Read the forth input */ |
xorjoep | 1:24714b45cd1b | 265 | Xn = *pIn++; |
xorjoep | 1:24714b45cd1b | 266 | |
xorjoep | 1:24714b45cd1b | 267 | /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */ |
xorjoep | 1:24714b45cd1b | 268 | Yn1 = (b0 * Xn) + (b1 * Xn1) + (b2 * Xn2) + (a1 * Yn2) + (a2 * Yn1); |
xorjoep | 1:24714b45cd1b | 269 | |
xorjoep | 1:24714b45cd1b | 270 | /* Store the result in the accumulator in the destination buffer. */ |
xorjoep | 1:24714b45cd1b | 271 | *pOut++ = Yn1; |
xorjoep | 1:24714b45cd1b | 272 | |
xorjoep | 1:24714b45cd1b | 273 | /* Every time after the output is computed state should be updated. */ |
xorjoep | 1:24714b45cd1b | 274 | /* The states should be updated as: */ |
xorjoep | 1:24714b45cd1b | 275 | /* Xn2 = Xn1 */ |
xorjoep | 1:24714b45cd1b | 276 | /* Xn1 = Xn */ |
xorjoep | 1:24714b45cd1b | 277 | /* Yn2 = Yn1 */ |
xorjoep | 1:24714b45cd1b | 278 | /* Yn1 = acc */ |
xorjoep | 1:24714b45cd1b | 279 | Xn2 = Xn1; |
xorjoep | 1:24714b45cd1b | 280 | Xn1 = Xn; |
xorjoep | 1:24714b45cd1b | 281 | |
xorjoep | 1:24714b45cd1b | 282 | /* decrement the loop counter */ |
xorjoep | 1:24714b45cd1b | 283 | sample--; |
xorjoep | 1:24714b45cd1b | 284 | |
xorjoep | 1:24714b45cd1b | 285 | } |
xorjoep | 1:24714b45cd1b | 286 | |
xorjoep | 1:24714b45cd1b | 287 | /* If the blockSize is not a multiple of 4, compute any remaining output samples here. |
xorjoep | 1:24714b45cd1b | 288 | ** No loop unrolling is used. */ |
xorjoep | 1:24714b45cd1b | 289 | sample = blockSize & 0x3U; |
xorjoep | 1:24714b45cd1b | 290 | |
xorjoep | 1:24714b45cd1b | 291 | while (sample > 0U) |
xorjoep | 1:24714b45cd1b | 292 | { |
xorjoep | 1:24714b45cd1b | 293 | /* Read the input */ |
xorjoep | 1:24714b45cd1b | 294 | Xn = *pIn++; |
xorjoep | 1:24714b45cd1b | 295 | |
xorjoep | 1:24714b45cd1b | 296 | /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */ |
xorjoep | 1:24714b45cd1b | 297 | acc = (b0 * Xn) + (b1 * Xn1) + (b2 * Xn2) + (a1 * Yn1) + (a2 * Yn2); |
xorjoep | 1:24714b45cd1b | 298 | |
xorjoep | 1:24714b45cd1b | 299 | /* Store the result in the accumulator in the destination buffer. */ |
xorjoep | 1:24714b45cd1b | 300 | *pOut++ = acc; |
xorjoep | 1:24714b45cd1b | 301 | |
xorjoep | 1:24714b45cd1b | 302 | /* Every time after the output is computed state should be updated. */ |
xorjoep | 1:24714b45cd1b | 303 | /* The states should be updated as: */ |
xorjoep | 1:24714b45cd1b | 304 | /* Xn2 = Xn1 */ |
xorjoep | 1:24714b45cd1b | 305 | /* Xn1 = Xn */ |
xorjoep | 1:24714b45cd1b | 306 | /* Yn2 = Yn1 */ |
xorjoep | 1:24714b45cd1b | 307 | /* Yn1 = acc */ |
xorjoep | 1:24714b45cd1b | 308 | Xn2 = Xn1; |
xorjoep | 1:24714b45cd1b | 309 | Xn1 = Xn; |
xorjoep | 1:24714b45cd1b | 310 | Yn2 = Yn1; |
xorjoep | 1:24714b45cd1b | 311 | Yn1 = acc; |
xorjoep | 1:24714b45cd1b | 312 | |
xorjoep | 1:24714b45cd1b | 313 | /* decrement the loop counter */ |
xorjoep | 1:24714b45cd1b | 314 | sample--; |
xorjoep | 1:24714b45cd1b | 315 | |
xorjoep | 1:24714b45cd1b | 316 | } |
xorjoep | 1:24714b45cd1b | 317 | |
xorjoep | 1:24714b45cd1b | 318 | /* Store the updated state variables back into the pState array */ |
xorjoep | 1:24714b45cd1b | 319 | *pState++ = Xn1; |
xorjoep | 1:24714b45cd1b | 320 | *pState++ = Xn2; |
xorjoep | 1:24714b45cd1b | 321 | *pState++ = Yn1; |
xorjoep | 1:24714b45cd1b | 322 | *pState++ = Yn2; |
xorjoep | 1:24714b45cd1b | 323 | |
xorjoep | 1:24714b45cd1b | 324 | /* The first stage goes from the input buffer to the output buffer. */ |
xorjoep | 1:24714b45cd1b | 325 | /* Subsequent numStages occur in-place in the output buffer */ |
xorjoep | 1:24714b45cd1b | 326 | pIn = pDst; |
xorjoep | 1:24714b45cd1b | 327 | |
xorjoep | 1:24714b45cd1b | 328 | /* Reset the output pointer */ |
xorjoep | 1:24714b45cd1b | 329 | pOut = pDst; |
xorjoep | 1:24714b45cd1b | 330 | |
xorjoep | 1:24714b45cd1b | 331 | /* decrement the loop counter */ |
xorjoep | 1:24714b45cd1b | 332 | stage--; |
xorjoep | 1:24714b45cd1b | 333 | |
xorjoep | 1:24714b45cd1b | 334 | } while (stage > 0U); |
xorjoep | 1:24714b45cd1b | 335 | |
xorjoep | 1:24714b45cd1b | 336 | #else |
xorjoep | 1:24714b45cd1b | 337 | |
xorjoep | 1:24714b45cd1b | 338 | /* Run the below code for Cortex-M0 */ |
xorjoep | 1:24714b45cd1b | 339 | |
xorjoep | 1:24714b45cd1b | 340 | do |
xorjoep | 1:24714b45cd1b | 341 | { |
xorjoep | 1:24714b45cd1b | 342 | /* Reading the coefficients */ |
xorjoep | 1:24714b45cd1b | 343 | b0 = *pCoeffs++; |
xorjoep | 1:24714b45cd1b | 344 | b1 = *pCoeffs++; |
xorjoep | 1:24714b45cd1b | 345 | b2 = *pCoeffs++; |
xorjoep | 1:24714b45cd1b | 346 | a1 = *pCoeffs++; |
xorjoep | 1:24714b45cd1b | 347 | a2 = *pCoeffs++; |
xorjoep | 1:24714b45cd1b | 348 | |
xorjoep | 1:24714b45cd1b | 349 | /* Reading the pState values */ |
xorjoep | 1:24714b45cd1b | 350 | Xn1 = pState[0]; |
xorjoep | 1:24714b45cd1b | 351 | Xn2 = pState[1]; |
xorjoep | 1:24714b45cd1b | 352 | Yn1 = pState[2]; |
xorjoep | 1:24714b45cd1b | 353 | Yn2 = pState[3]; |
xorjoep | 1:24714b45cd1b | 354 | |
xorjoep | 1:24714b45cd1b | 355 | /* The variables acc holds the output value that is computed: |
xorjoep | 1:24714b45cd1b | 356 | * acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] |
xorjoep | 1:24714b45cd1b | 357 | */ |
xorjoep | 1:24714b45cd1b | 358 | |
xorjoep | 1:24714b45cd1b | 359 | sample = blockSize; |
xorjoep | 1:24714b45cd1b | 360 | |
xorjoep | 1:24714b45cd1b | 361 | while (sample > 0U) |
xorjoep | 1:24714b45cd1b | 362 | { |
xorjoep | 1:24714b45cd1b | 363 | /* Read the input */ |
xorjoep | 1:24714b45cd1b | 364 | Xn = *pIn++; |
xorjoep | 1:24714b45cd1b | 365 | |
xorjoep | 1:24714b45cd1b | 366 | /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */ |
xorjoep | 1:24714b45cd1b | 367 | acc = (b0 * Xn) + (b1 * Xn1) + (b2 * Xn2) + (a1 * Yn1) + (a2 * Yn2); |
xorjoep | 1:24714b45cd1b | 368 | |
xorjoep | 1:24714b45cd1b | 369 | /* Store the result in the accumulator in the destination buffer. */ |
xorjoep | 1:24714b45cd1b | 370 | *pOut++ = acc; |
xorjoep | 1:24714b45cd1b | 371 | |
xorjoep | 1:24714b45cd1b | 372 | /* Every time after the output is computed state should be updated. */ |
xorjoep | 1:24714b45cd1b | 373 | /* The states should be updated as: */ |
xorjoep | 1:24714b45cd1b | 374 | /* Xn2 = Xn1 */ |
xorjoep | 1:24714b45cd1b | 375 | /* Xn1 = Xn */ |
xorjoep | 1:24714b45cd1b | 376 | /* Yn2 = Yn1 */ |
xorjoep | 1:24714b45cd1b | 377 | /* Yn1 = acc */ |
xorjoep | 1:24714b45cd1b | 378 | Xn2 = Xn1; |
xorjoep | 1:24714b45cd1b | 379 | Xn1 = Xn; |
xorjoep | 1:24714b45cd1b | 380 | Yn2 = Yn1; |
xorjoep | 1:24714b45cd1b | 381 | Yn1 = acc; |
xorjoep | 1:24714b45cd1b | 382 | |
xorjoep | 1:24714b45cd1b | 383 | /* decrement the loop counter */ |
xorjoep | 1:24714b45cd1b | 384 | sample--; |
xorjoep | 1:24714b45cd1b | 385 | } |
xorjoep | 1:24714b45cd1b | 386 | |
xorjoep | 1:24714b45cd1b | 387 | /* Store the updated state variables back into the pState array */ |
xorjoep | 1:24714b45cd1b | 388 | *pState++ = Xn1; |
xorjoep | 1:24714b45cd1b | 389 | *pState++ = Xn2; |
xorjoep | 1:24714b45cd1b | 390 | *pState++ = Yn1; |
xorjoep | 1:24714b45cd1b | 391 | *pState++ = Yn2; |
xorjoep | 1:24714b45cd1b | 392 | |
xorjoep | 1:24714b45cd1b | 393 | /* The first stage goes from the input buffer to the output buffer. */ |
xorjoep | 1:24714b45cd1b | 394 | /* Subsequent numStages occur in-place in the output buffer */ |
xorjoep | 1:24714b45cd1b | 395 | pIn = pDst; |
xorjoep | 1:24714b45cd1b | 396 | |
xorjoep | 1:24714b45cd1b | 397 | /* Reset the output pointer */ |
xorjoep | 1:24714b45cd1b | 398 | pOut = pDst; |
xorjoep | 1:24714b45cd1b | 399 | |
xorjoep | 1:24714b45cd1b | 400 | /* decrement the loop counter */ |
xorjoep | 1:24714b45cd1b | 401 | stage--; |
xorjoep | 1:24714b45cd1b | 402 | |
xorjoep | 1:24714b45cd1b | 403 | } while (stage > 0U); |
xorjoep | 1:24714b45cd1b | 404 | |
xorjoep | 1:24714b45cd1b | 405 | #endif /* #if defined (ARM_MATH_DSP) */ |
xorjoep | 1:24714b45cd1b | 406 | |
xorjoep | 1:24714b45cd1b | 407 | } |
xorjoep | 1:24714b45cd1b | 408 | |
xorjoep | 1:24714b45cd1b | 409 | |
xorjoep | 1:24714b45cd1b | 410 | /** |
xorjoep | 1:24714b45cd1b | 411 | * @} end of BiquadCascadeDF1 group |
xorjoep | 1:24714b45cd1b | 412 | */ |