Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of dsp by
arm_biquad_cascade_df1_32x64_q31.c
00001 /* ---------------------------------------------------------------------- 00002 * Copyright (C) 2010 ARM Limited. All rights reserved. 00003 * 00004 * $Date: 29. November 2010 00005 * $Revision: V1.0.3 00006 * 00007 * Project: CMSIS DSP Library 00008 * Title: arm_biquad_cascade_df1_32x64_q31.c 00009 * 00010 * Description: High precision Q31 Biquad cascade filter processing function 00011 * 00012 * Target Processor: Cortex-M4/Cortex-M3 00013 * 00014 * Version 1.0.3 2010/11/29 00015 * Re-organized the CMSIS folders and updated documentation. 00016 * 00017 * Version 1.0.2 2010/11/11 00018 * Documentation updated. 00019 * 00020 * Version 1.0.1 2010/10/05 00021 * Production release and review comments incorporated. 00022 * 00023 * Version 1.0.0 2010/09/20 00024 * Production release and review comments incorporated. 00025 * 00026 * Version 0.0.7 2010/06/10 00027 * Misra-C changes done 00028 * -------------------------------------------------------------------- */ 00029 00030 #include "arm_math.h" 00031 00032 /** 00033 * @ingroup groupFilters 00034 */ 00035 00036 /** 00037 * @defgroup BiquadCascadeDF1_32x64 High Precision Q31 Biquad Cascade Filter 00038 * 00039 * This function implements a high precision Biquad cascade filter which operates on 00040 * Q31 data values. The filter coefficients are in 1.31 format and the state variables 00041 * are in 1.63 format. The double precision state variables reduce quantization noise 00042 * in the filter and provide a cleaner output. 00043 * These filters are particularly useful when implementing filters in which the 00044 * singularities are close to the unit circle. This is common for low pass or high 00045 * pass filters with very low cutoff frequencies. 00046 * 00047 * The function operates on blocks of input and output data 00048 * and each call to the function processes <code>blockSize</code> samples through 00049 * the filter. <code>pSrc</code> and <code>pDst</code> points to input and output arrays 00050 * containing <code>blockSize</code> Q31 values. 00051 * 00052 * \par Algorithm 00053 * Each Biquad stage implements a second order filter using the difference equation: 00054 * <pre> 00055 * y[n] = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] 00056 * </pre> 00057 * A Direct Form I algorithm is used with 5 coefficients and 4 state variables per stage. 00058 * \image html Biquad.gif "Single Biquad filter stage" 00059 * Coefficients <code>b0, b1, and b2 </code> multiply the input signal <code>x[n]</code> and are referred to as the feedforward coefficients. 00060 * Coefficients <code>a1</code> and <code>a2</code> multiply the output signal <code>y[n]</code> and are referred to as the feedback coefficients. 00061 * Pay careful attention to the sign of the feedback coefficients. 00062 * Some design tools use the difference equation 00063 * <pre> 00064 * y[n] = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] - a1 * y[n-1] - a2 * y[n-2] 00065 * </pre> 00066 * In this case the feedback coefficients <code>a1</code> and <code>a2</code> must be negated when used with the CMSIS DSP Library. 00067 * 00068 * \par 00069 * Higher order filters are realized as a cascade of second order sections. 00070 * <code>numStages</code> refers to the number of second order stages used. 00071 * For example, an 8th order filter would be realized with <code>numStages=4</code> second order stages. 00072 * \image html BiquadCascade.gif "8th order filter using a cascade of Biquad stages" 00073 * 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>). 00074 * 00075 * \par 00076 * The <code>pState</code> points to state variables array . 00077 * Each Biquad stage has 4 state variables <code>x[n-1], x[n-2], y[n-1],</code> and <code>y[n-2]</code> and each state variable in 1.63 format to improve precision. 00078 * The state variables are arranged in the array as: 00079 * <pre> 00080 * {x[n-1], x[n-2], y[n-1], y[n-2]} 00081 * </pre> 00082 * 00083 * \par 00084 * The 4 state variables for stage 1 are first, then the 4 state variables for stage 2, and so on. 00085 * The state array has a total length of <code>4*numStages</code> values of data in 1.63 format. 00086 * The state variables are updated after each block of data is processed; the coefficients are untouched. 00087 * 00088 * \par Instance Structure 00089 * The coefficients and state variables for a filter are stored together in an instance data structure. 00090 * A separate instance structure must be defined for each filter. 00091 * Coefficient arrays may be shared among several instances while state variable arrays cannot be shared. 00092 * 00093 * \par Init Function 00094 * There is also an associated initialization function which performs the following operations: 00095 * - Sets the values of the internal structure fields. 00096 * - Zeros out the values in the state buffer. 00097 * \par 00098 * Use of the initialization function is optional. 00099 * However, if the initialization function is used, then the instance structure cannot be placed into a const data section. 00100 * To place an instance structure into a const data section, the instance structure must be manually initialized. 00101 * Set the values in the state buffer to zeros before static initialization. 00102 * For example, to statically initialize the filter instance structure use 00103 * <pre> 00104 * arm_biquad_cas_df1_32x64_ins_q31 S1 = {numStages, pState, pCoeffs, postShift}; 00105 * </pre> 00106 * where <code>numStages</code> is the number of Biquad stages in the filter; <code>pState</code> is the address of the state buffer; 00107 * <code>pCoeffs</code> is the address of the coefficient buffer; <code>postShift</code> shift to be applied which is described in detail below. 00108 * \par Fixed-Point Behavior 00109 * Care must be taken while using Biquad Cascade 32x64 filter function. 00110 * Following issues must be considered: 00111 * - Scaling of coefficients 00112 * - Filter gain 00113 * - Overflow and saturation 00114 * 00115 * \par 00116 * Filter coefficients are represented as fractional values and 00117 * restricted to lie in the range <code>[-1 +1)</code>. 00118 * The processing function has an additional scaling parameter <code>postShift</code> 00119 * which allows the filter coefficients to exceed the range <code>[+1 -1)</code>. 00120 * At the output of the filter's accumulator is a shift register which shifts the result by <code>postShift</code> bits. 00121 * \image html BiquadPostshift.gif "Fixed-point Biquad with shift by postShift bits after accumulator" 00122 * This essentially scales the filter coefficients by <code>2^postShift</code>. 00123 * For example, to realize the coefficients 00124 * <pre> 00125 * {1.5, -0.8, 1.2, 1.6, -0.9} 00126 * </pre> 00127 * set the Coefficient array to: 00128 * <pre> 00129 * {0.75, -0.4, 0.6, 0.8, -0.45} 00130 * </pre> 00131 * and set <code>postShift=1</code> 00132 * 00133 * \par 00134 * The second thing to keep in mind is the gain through the filter. 00135 * The frequency response of a Biquad filter is a function of its coefficients. 00136 * It is possible for the gain through the filter to exceed 1.0 meaning that the filter increases the amplitude of certain frequencies. 00137 * 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. 00138 * 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. 00139 * 00140 * \par 00141 * The third item to consider is the overflow and saturation behavior of the fixed-point Q31 version. 00142 * This is described in the function specific documentation below. 00143 */ 00144 00145 /** 00146 * @addtogroup BiquadCascadeDF1_32x64 00147 * @{ 00148 */ 00149 00150 /** 00151 * @details 00152 00153 * @param[in] *S points to an instance of the high precision Q31 Biquad cascade filter. 00154 * @param[in] *pSrc points to the block of input data. 00155 * @param[out] *pDst points to the block of output data. 00156 * @param[in] blockSize number of samples to process. 00157 * @return none. 00158 * 00159 * \par 00160 * The function is implemented using an internal 64-bit accumulator. 00161 * The accumulator has a 2.62 format and maintains full precision of the intermediate multiplication results but provides only a single guard bit. 00162 * Thus, if the accumulator result overflows it wraps around rather than clip. 00163 * 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). 00164 * After all 5 multiply-accumulates are performed, the 2.62 accumulator is shifted by <code>postShift</code> bits and the result truncated to 00165 * 1.31 format by discarding the low 32 bits. 00166 * 00167 * \par 00168 * Two related functions are provided in the CMSIS DSP library. 00169 * <code>arm_biquad_cascade_df1_q31()</code> implements a Biquad cascade with 32-bit coefficients and state variables with a Q63 accumulator. 00170 * <code>arm_biquad_cascade_df1_fast_q31()</code> implements a Biquad cascade with 32-bit coefficients and state variables with a Q31 accumulator. 00171 */ 00172 00173 void arm_biquad_cas_df1_32x64_q31 ( 00174 const arm_biquad_cas_df1_32x64_ins_q31 * S, 00175 q31_t * pSrc, 00176 q31_t * pDst, 00177 uint32_t blockSize) 00178 { 00179 q31_t *pIn = pSrc; /* input pointer initialization */ 00180 q31_t *pOut = pDst; /* output pointer initialization */ 00181 q63_t *pState = S->pState; /* state pointer initialization */ 00182 q31_t *pCoeffs = S->pCoeffs; /* coeff pointer initialization */ 00183 q63_t acc; /* accumulator */ 00184 q63_t Xn1, Xn2, Yn1, Yn2; /* Filter state variables */ 00185 q31_t b0, b1, b2, a1, a2; /* Filter coefficients */ 00186 q63_t Xn; /* temporary input */ 00187 int32_t shift = (int32_t) S->postShift + 1; /* Shift to be applied to the output */ 00188 uint32_t sample, stage = S->numStages; /* loop counters */ 00189 00190 00191 do 00192 { 00193 /* Reading the coefficients */ 00194 b0 = *pCoeffs++; 00195 b1 = *pCoeffs++; 00196 b2 = *pCoeffs++; 00197 a1 = *pCoeffs++; 00198 a2 = *pCoeffs++; 00199 00200 /* Reading the state values */ 00201 Xn1 = pState[0]; 00202 Xn2 = pState[1]; 00203 Yn1 = pState[2]; 00204 Yn2 = pState[3]; 00205 00206 /* Apply loop unrolling and compute 4 output values simultaneously. */ 00207 /* The variable acc hold output value that is being computed and 00208 * stored in the destination buffer 00209 * acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] 00210 */ 00211 00212 sample = blockSize >> 2u; 00213 00214 /* First part of the processing with loop unrolling. Compute 4 outputs at a time. 00215 ** a second loop below computes the remaining 1 to 3 samples. */ 00216 while(sample > 0u) 00217 { 00218 /* Read the input */ 00219 Xn = *pIn++; 00220 00221 /* The value is shifted to the MSB to perform 32x64 multiplication */ 00222 Xn = Xn << 32; 00223 00224 /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */ 00225 00226 /* acc = b0 * x[n] */ 00227 acc = mult32x64(Xn, b0); 00228 /* acc += b1 * x[n-1] */ 00229 acc += mult32x64(Xn1, b1); 00230 /* acc += b[2] * x[n-2] */ 00231 acc += mult32x64(Xn2, b2); 00232 /* acc += a1 * y[n-1] */ 00233 acc += mult32x64(Yn1, a1); 00234 /* acc += a2 * y[n-2] */ 00235 acc += mult32x64(Yn2, a2); 00236 00237 /* The result is converted to 1.63 , Yn2 variable is reused */ 00238 Yn2 = acc << shift; 00239 00240 /* Store the output in the destination buffer in 1.31 format. */ 00241 *pOut++ = (q31_t) (acc >> (32 - shift)); 00242 00243 /* Read the second input into Xn2, to reuse the value */ 00244 Xn2 = *pIn++; 00245 00246 /* The value is shifted to the MSB to perform 32x64 multiplication */ 00247 Xn2 = Xn2 << 32; 00248 00249 /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */ 00250 00251 /* acc = b0 * x[n] */ 00252 acc = mult32x64(Xn2, b0); 00253 /* acc += b1 * x[n-1] */ 00254 acc += mult32x64(Xn, b1); 00255 /* acc += b[2] * x[n-2] */ 00256 acc += mult32x64(Xn1, b2); 00257 /* acc += a1 * y[n-1] */ 00258 acc += mult32x64(Yn2, a1); 00259 /* acc += a2 * y[n-2] */ 00260 acc += mult32x64(Yn1, a2); 00261 00262 /* The result is converted to 1.63, Yn1 variable is reused */ 00263 Yn1 = acc << shift; 00264 00265 /* The result is converted to 1.31 */ 00266 /* Store the output in the destination buffer. */ 00267 *pOut++ = (q31_t) (acc >> (32 - shift)); 00268 00269 /* Read the third input into Xn1, to reuse the value */ 00270 Xn1 = *pIn++; 00271 00272 /* The value is shifted to the MSB to perform 32x64 multiplication */ 00273 Xn1 = Xn1 << 32; 00274 00275 /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */ 00276 /* acc = b0 * x[n] */ 00277 acc = mult32x64(Xn1, b0); 00278 /* acc += b1 * x[n-1] */ 00279 acc += mult32x64(Xn2, b1); 00280 /* acc += b[2] * x[n-2] */ 00281 acc += mult32x64(Xn, b2); 00282 /* acc += a1 * y[n-1] */ 00283 acc += mult32x64(Yn1, a1); 00284 /* acc += a2 * y[n-2] */ 00285 acc += mult32x64(Yn2, a2); 00286 00287 /* The result is converted to 1.63, Yn2 variable is reused */ 00288 Yn2 = acc << shift; 00289 00290 /* Store the output in the destination buffer in 1.31 format. */ 00291 *pOut++ = (q31_t) (acc >> (32 - shift)); 00292 00293 /* Read the fourth input into Xn, to reuse the value */ 00294 Xn = *pIn++; 00295 00296 /* The value is shifted to the MSB to perform 32x64 multiplication */ 00297 Xn = Xn << 32; 00298 00299 /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */ 00300 /* acc = b0 * x[n] */ 00301 acc = mult32x64(Xn, b0); 00302 /* acc += b1 * x[n-1] */ 00303 acc += mult32x64(Xn1, b1); 00304 /* acc += b[2] * x[n-2] */ 00305 acc += mult32x64(Xn2, b2); 00306 /* acc += a1 * y[n-1] */ 00307 acc += mult32x64(Yn2, a1); 00308 /* acc += a2 * y[n-2] */ 00309 acc += mult32x64(Yn1, a2); 00310 00311 /* The result is converted to 1.63, Yn1 variable is reused */ 00312 Yn1 = acc << shift; 00313 00314 /* Every time after the output is computed state should be updated. */ 00315 /* The states should be updated as: */ 00316 /* Xn2 = Xn1 */ 00317 /* Xn1 = Xn */ 00318 /* Yn2 = Yn1 */ 00319 /* Yn1 = acc */ 00320 Xn2 = Xn1; 00321 Xn1 = Xn; 00322 00323 /* Store the output in the destination buffer in 1.31 format. */ 00324 *pOut++ = (q31_t) (acc >> (32 - shift)); 00325 00326 /* decrement the loop counter */ 00327 sample--; 00328 } 00329 00330 /* If the blockSize is not a multiple of 4, compute any remaining output samples here. 00331 ** No loop unrolling is used. */ 00332 sample = (blockSize & 0x3u); 00333 00334 while(sample > 0u) 00335 { 00336 /* Read the input */ 00337 Xn = *pIn++; 00338 00339 /* The value is shifted to the MSB to perform 32x64 multiplication */ 00340 Xn = Xn << 32; 00341 00342 /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */ 00343 /* acc = b0 * x[n] */ 00344 acc = mult32x64(Xn, b0); 00345 /* acc += b1 * x[n-1] */ 00346 acc += mult32x64(Xn1, b1); 00347 /* acc += b[2] * x[n-2] */ 00348 acc += mult32x64(Xn2, b2); 00349 /* acc += a1 * y[n-1] */ 00350 acc += mult32x64(Yn1, a1); 00351 /* acc += a2 * y[n-2] */ 00352 acc += mult32x64(Yn2, a2); 00353 00354 /* Every time after the output is computed state should be updated. */ 00355 /* The states should be updated as: */ 00356 /* Xn2 = Xn1 */ 00357 /* Xn1 = Xn */ 00358 /* Yn2 = Yn1 */ 00359 /* Yn1 = acc */ 00360 Xn2 = Xn1; 00361 Xn1 = Xn; 00362 Yn2 = Yn1; 00363 Yn1 = acc << shift; 00364 00365 /* Store the output in the destination buffer in 1.31 format. */ 00366 *pOut++ = (q31_t) (acc >> (32 - shift)); 00367 00368 /* decrement the loop counter */ 00369 sample--; 00370 } 00371 00372 /* The first stage output is given as input to the second stage. */ 00373 pIn = pDst; 00374 00375 /* Reset to destination buffer working pointer */ 00376 pOut = pDst; 00377 00378 /* Store the updated state variables back into the pState array */ 00379 *pState++ = Xn1; 00380 *pState++ = Xn2; 00381 *pState++ = Yn1; 00382 *pState++ = Yn2; 00383 00384 } while(--stage); 00385 } 00386 00387 /** 00388 * @} end of BiquadCascadeDF1_32x64 group 00389 */
Generated on Tue Jul 12 2022 19:55:42 by
1.7.2
