A bundled STM32F10x Std Periph and CMSIS library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

562 lines
21 KiB

  1. /* ----------------------------------------------------------------------
  2. * Copyright (C) 2010-2014 ARM Limited. All rights reserved.
  3. *
  4. * $Date: 12. March 2014
  5. * $Revision: V1.4.4
  6. *
  7. * Project: CMSIS DSP Library
  8. * Title: arm_biquad_cascade_df1_32x64_q31.c
  9. *
  10. * Description: High precision Q31 Biquad cascade filter processing function
  11. *
  12. * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in
  21. * the documentation and/or other materials provided with the
  22. * distribution.
  23. * - Neither the name of ARM LIMITED nor the names of its contributors
  24. * may be used to endorse or promote products derived from this
  25. * software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  30. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  31. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  32. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  33. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  35. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. * -------------------------------------------------------------------- */
  40. #include "arm_math.h"
  41. /**
  42. * @ingroup groupFilters
  43. */
  44. /**
  45. * @defgroup BiquadCascadeDF1_32x64 High Precision Q31 Biquad Cascade Filter
  46. *
  47. * This function implements a high precision Biquad cascade filter which operates on
  48. * Q31 data values. The filter coefficients are in 1.31 format and the state variables
  49. * are in 1.63 format. The double precision state variables reduce quantization noise
  50. * in the filter and provide a cleaner output.
  51. * These filters are particularly useful when implementing filters in which the
  52. * singularities are close to the unit circle. This is common for low pass or high
  53. * pass filters with very low cutoff frequencies.
  54. *
  55. * The function operates on blocks of input and output data
  56. * and each call to the function processes <code>blockSize</code> samples through
  57. * the filter. <code>pSrc</code> and <code>pDst</code> points to input and output arrays
  58. * containing <code>blockSize</code> Q31 values.
  59. *
  60. * \par Algorithm
  61. * Each Biquad stage implements a second order filter using the difference equation:
  62. * <pre>
  63. * y[n] = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2]
  64. * </pre>
  65. * A Direct Form I algorithm is used with 5 coefficients and 4 state variables per stage.
  66. * \image html Biquad.gif "Single Biquad filter stage"
  67. * Coefficients <code>b0, b1, and b2 </code> multiply the input signal <code>x[n]</code> and are referred to as the feedforward coefficients.
  68. * Coefficients <code>a1</code> and <code>a2</code> multiply the output signal <code>y[n]</code> and are referred to as the feedback coefficients.
  69. * Pay careful attention to the sign of the feedback coefficients.
  70. * Some design tools use the difference equation
  71. * <pre>
  72. * y[n] = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] - a1 * y[n-1] - a2 * y[n-2]
  73. * </pre>
  74. * In this case the feedback coefficients <code>a1</code> and <code>a2</code> must be negated when used with the CMSIS DSP Library.
  75. *
  76. * \par
  77. * Higher order filters are realized as a cascade of second order sections.
  78. * <code>numStages</code> refers to the number of second order stages used.
  79. * For example, an 8th order filter would be realized with <code>numStages=4</code> second order stages.
  80. * \image html BiquadCascade.gif "8th order filter using a cascade of Biquad stages"
  81. * 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>).
  82. *
  83. * \par
  84. * The <code>pState</code> points to state variables array .
  85. * 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.
  86. * The state variables are arranged in the array as:
  87. * <pre>
  88. * {x[n-1], x[n-2], y[n-1], y[n-2]}
  89. * </pre>
  90. *
  91. * \par
  92. * The 4 state variables for stage 1 are first, then the 4 state variables for stage 2, and so on.
  93. * The state array has a total length of <code>4*numStages</code> values of data in 1.63 format.
  94. * The state variables are updated after each block of data is processed; the coefficients are untouched.
  95. *
  96. * \par Instance Structure
  97. * The coefficients and state variables for a filter are stored together in an instance data structure.
  98. * A separate instance structure must be defined for each filter.
  99. * Coefficient arrays may be shared among several instances while state variable arrays cannot be shared.
  100. *
  101. * \par Init Function
  102. * There is also an associated initialization function which performs the following operations:
  103. * - Sets the values of the internal structure fields.
  104. * - Zeros out the values in the state buffer.
  105. * To do this manually without calling the init function, assign the follow subfields of the instance structure:
  106. * numStages, pCoeffs, postShift, pState. Also set all of the values in pState to zero.
  107. *
  108. * \par
  109. * Use of the initialization function is optional.
  110. * However, if the initialization function is used, then the instance structure cannot be placed into a const data section.
  111. * To place an instance structure into a const data section, the instance structure must be manually initialized.
  112. * Set the values in the state buffer to zeros before static initialization.
  113. * For example, to statically initialize the filter instance structure use
  114. * <pre>
  115. * arm_biquad_cas_df1_32x64_ins_q31 S1 = {numStages, pState, pCoeffs, postShift};
  116. * </pre>
  117. * where <code>numStages</code> is the number of Biquad stages in the filter; <code>pState</code> is the address of the state buffer;
  118. * <code>pCoeffs</code> is the address of the coefficient buffer; <code>postShift</code> shift to be applied which is described in detail below.
  119. * \par Fixed-Point Behavior
  120. * Care must be taken while using Biquad Cascade 32x64 filter function.
  121. * Following issues must be considered:
  122. * - Scaling of coefficients
  123. * - Filter gain
  124. * - Overflow and saturation
  125. *
  126. * \par
  127. * Filter coefficients are represented as fractional values and
  128. * restricted to lie in the range <code>[-1 +1)</code>.
  129. * The processing function has an additional scaling parameter <code>postShift</code>
  130. * which allows the filter coefficients to exceed the range <code>[+1 -1)</code>.
  131. * At the output of the filter's accumulator is a shift register which shifts the result by <code>postShift</code> bits.
  132. * \image html BiquadPostshift.gif "Fixed-point Biquad with shift by postShift bits after accumulator"
  133. * This essentially scales the filter coefficients by <code>2^postShift</code>.
  134. * For example, to realize the coefficients
  135. * <pre>
  136. * {1.5, -0.8, 1.2, 1.6, -0.9}
  137. * </pre>
  138. * set the Coefficient array to:
  139. * <pre>
  140. * {0.75, -0.4, 0.6, 0.8, -0.45}
  141. * </pre>
  142. * and set <code>postShift=1</code>
  143. *
  144. * \par
  145. * The second thing to keep in mind is the gain through the filter.
  146. * The frequency response of a Biquad filter is a function of its coefficients.
  147. * It is possible for the gain through the filter to exceed 1.0 meaning that the filter increases the amplitude of certain frequencies.
  148. * 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.
  149. * 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.
  150. *
  151. * \par
  152. * The third item to consider is the overflow and saturation behavior of the fixed-point Q31 version.
  153. * This is described in the function specific documentation below.
  154. */
  155. /**
  156. * @addtogroup BiquadCascadeDF1_32x64
  157. * @{
  158. */
  159. /**
  160. * @details
  161. * @param[in] *S points to an instance of the high precision Q31 Biquad cascade filter.
  162. * @param[in] *pSrc points to the block of input data.
  163. * @param[out] *pDst points to the block of output data.
  164. * @param[in] blockSize number of samples to process.
  165. * @return none.
  166. *
  167. * \par
  168. * The function is implemented using an internal 64-bit accumulator.
  169. * The accumulator has a 2.62 format and maintains full precision of the intermediate multiplication results but provides only a single guard bit.
  170. * Thus, if the accumulator result overflows it wraps around rather than clip.
  171. * 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).
  172. * After all 5 multiply-accumulates are performed, the 2.62 accumulator is shifted by <code>postShift</code> bits and the result truncated to
  173. * 1.31 format by discarding the low 32 bits.
  174. *
  175. * \par
  176. * Two related functions are provided in the CMSIS DSP library.
  177. * <code>arm_biquad_cascade_df1_q31()</code> implements a Biquad cascade with 32-bit coefficients and state variables with a Q63 accumulator.
  178. * <code>arm_biquad_cascade_df1_fast_q31()</code> implements a Biquad cascade with 32-bit coefficients and state variables with a Q31 accumulator.
  179. */
  180. void arm_biquad_cas_df1_32x64_q31(
  181. const arm_biquad_cas_df1_32x64_ins_q31 * S,
  182. q31_t * pSrc,
  183. q31_t * pDst,
  184. uint32_t blockSize)
  185. {
  186. q31_t *pIn = pSrc; /* input pointer initialization */
  187. q31_t *pOut = pDst; /* output pointer initialization */
  188. q63_t *pState = S->pState; /* state pointer initialization */
  189. q31_t *pCoeffs = S->pCoeffs; /* coeff pointer initialization */
  190. q63_t acc; /* accumulator */
  191. q31_t Xn1, Xn2; /* Input Filter state variables */
  192. q63_t Yn1, Yn2; /* Output Filter state variables */
  193. q31_t b0, b1, b2, a1, a2; /* Filter coefficients */
  194. q31_t Xn; /* temporary input */
  195. int32_t shift = (int32_t) S->postShift + 1; /* Shift to be applied to the output */
  196. uint32_t sample, stage = S->numStages; /* loop counters */
  197. q31_t acc_l, acc_h; /* temporary output */
  198. uint32_t uShift = ((uint32_t) S->postShift + 1u);
  199. uint32_t lShift = 32u - uShift; /* Shift to be applied to the output */
  200. #ifndef ARM_MATH_CM0_FAMILY
  201. /* Run the below code for Cortex-M4 and Cortex-M3 */
  202. do
  203. {
  204. /* Reading the coefficients */
  205. b0 = *pCoeffs++;
  206. b1 = *pCoeffs++;
  207. b2 = *pCoeffs++;
  208. a1 = *pCoeffs++;
  209. a2 = *pCoeffs++;
  210. /* Reading the state values */
  211. Xn1 = (q31_t) (pState[0]);
  212. Xn2 = (q31_t) (pState[1]);
  213. Yn1 = pState[2];
  214. Yn2 = pState[3];
  215. /* Apply loop unrolling and compute 4 output values simultaneously. */
  216. /* The variable acc hold output value that is being computed and
  217. * stored in the destination buffer
  218. * acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2]
  219. */
  220. sample = blockSize >> 2u;
  221. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  222. ** a second loop below computes the remaining 1 to 3 samples. */
  223. while(sample > 0u)
  224. {
  225. /* Read the input */
  226. Xn = *pIn++;
  227. /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */
  228. /* acc = b0 * x[n] */
  229. acc = (q63_t) Xn *b0;
  230. /* acc += b1 * x[n-1] */
  231. acc += (q63_t) Xn1 *b1;
  232. /* acc += b[2] * x[n-2] */
  233. acc += (q63_t) Xn2 *b2;
  234. /* acc += a1 * y[n-1] */
  235. acc += mult32x64(Yn1, a1);
  236. /* acc += a2 * y[n-2] */
  237. acc += mult32x64(Yn2, a2);
  238. /* The result is converted to 1.63 , Yn2 variable is reused */
  239. Yn2 = acc << shift;
  240. /* Calc lower part of acc */
  241. acc_l = acc & 0xffffffff;
  242. /* Calc upper part of acc */
  243. acc_h = (acc >> 32) & 0xffffffff;
  244. /* Apply shift for lower part of acc and upper part of acc */
  245. acc_h = (uint32_t) acc_l >> lShift | acc_h << uShift;
  246. /* Store the output in the destination buffer in 1.31 format. */
  247. *pOut = acc_h;
  248. /* Read the second input into Xn2, to reuse the value */
  249. Xn2 = *pIn++;
  250. /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */
  251. /* acc += b1 * x[n-1] */
  252. acc = (q63_t) Xn *b1;
  253. /* acc = b0 * x[n] */
  254. acc += (q63_t) Xn2 *b0;
  255. /* acc += b[2] * x[n-2] */
  256. acc += (q63_t) Xn1 *b2;
  257. /* acc += a1 * y[n-1] */
  258. acc += mult32x64(Yn2, a1);
  259. /* acc += a2 * y[n-2] */
  260. acc += mult32x64(Yn1, a2);
  261. /* The result is converted to 1.63, Yn1 variable is reused */
  262. Yn1 = acc << shift;
  263. /* Calc lower part of acc */
  264. acc_l = acc & 0xffffffff;
  265. /* Calc upper part of acc */
  266. acc_h = (acc >> 32) & 0xffffffff;
  267. /* Apply shift for lower part of acc and upper part of acc */
  268. acc_h = (uint32_t) acc_l >> lShift | acc_h << uShift;
  269. /* Read the third input into Xn1, to reuse the value */
  270. Xn1 = *pIn++;
  271. /* The result is converted to 1.31 */
  272. /* Store the output in the destination buffer. */
  273. *(pOut + 1u) = acc_h;
  274. /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */
  275. /* acc = b0 * x[n] */
  276. acc = (q63_t) Xn1 *b0;
  277. /* acc += b1 * x[n-1] */
  278. acc += (q63_t) Xn2 *b1;
  279. /* acc += b[2] * x[n-2] */
  280. acc += (q63_t) Xn *b2;
  281. /* acc += a1 * y[n-1] */
  282. acc += mult32x64(Yn1, a1);
  283. /* acc += a2 * y[n-2] */
  284. acc += mult32x64(Yn2, a2);
  285. /* The result is converted to 1.63, Yn2 variable is reused */
  286. Yn2 = acc << shift;
  287. /* Calc lower part of acc */
  288. acc_l = acc & 0xffffffff;
  289. /* Calc upper part of acc */
  290. acc_h = (acc >> 32) & 0xffffffff;
  291. /* Apply shift for lower part of acc and upper part of acc */
  292. acc_h = (uint32_t) acc_l >> lShift | acc_h << uShift;
  293. /* Store the output in the destination buffer in 1.31 format. */
  294. *(pOut + 2u) = acc_h;
  295. /* Read the fourth input into Xn, to reuse the value */
  296. Xn = *pIn++;
  297. /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */
  298. /* acc = b0 * x[n] */
  299. acc = (q63_t) Xn *b0;
  300. /* acc += b1 * x[n-1] */
  301. acc += (q63_t) Xn1 *b1;
  302. /* acc += b[2] * x[n-2] */
  303. acc += (q63_t) Xn2 *b2;
  304. /* acc += a1 * y[n-1] */
  305. acc += mult32x64(Yn2, a1);
  306. /* acc += a2 * y[n-2] */
  307. acc += mult32x64(Yn1, a2);
  308. /* The result is converted to 1.63, Yn1 variable is reused */
  309. Yn1 = acc << shift;
  310. /* Calc lower part of acc */
  311. acc_l = acc & 0xffffffff;
  312. /* Calc upper part of acc */
  313. acc_h = (acc >> 32) & 0xffffffff;
  314. /* Apply shift for lower part of acc and upper part of acc */
  315. acc_h = (uint32_t) acc_l >> lShift | acc_h << uShift;
  316. /* Store the output in the destination buffer in 1.31 format. */
  317. *(pOut + 3u) = acc_h;
  318. /* Every time after the output is computed state should be updated. */
  319. /* The states should be updated as: */
  320. /* Xn2 = Xn1 */
  321. /* Xn1 = Xn */
  322. /* Yn2 = Yn1 */
  323. /* Yn1 = acc */
  324. Xn2 = Xn1;
  325. Xn1 = Xn;
  326. /* update output pointer */
  327. pOut += 4u;
  328. /* decrement the loop counter */
  329. sample--;
  330. }
  331. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  332. ** No loop unrolling is used. */
  333. sample = (blockSize & 0x3u);
  334. while(sample > 0u)
  335. {
  336. /* Read the input */
  337. Xn = *pIn++;
  338. /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */
  339. /* acc = b0 * x[n] */
  340. acc = (q63_t) Xn *b0;
  341. /* acc += b1 * x[n-1] */
  342. acc += (q63_t) Xn1 *b1;
  343. /* acc += b[2] * x[n-2] */
  344. acc += (q63_t) Xn2 *b2;
  345. /* acc += a1 * y[n-1] */
  346. acc += mult32x64(Yn1, a1);
  347. /* acc += a2 * y[n-2] */
  348. acc += mult32x64(Yn2, a2);
  349. /* Every time after the output is computed state should be updated. */
  350. /* The states should be updated as: */
  351. /* Xn2 = Xn1 */
  352. /* Xn1 = Xn */
  353. /* Yn2 = Yn1 */
  354. /* Yn1 = acc */
  355. Xn2 = Xn1;
  356. Xn1 = Xn;
  357. Yn2 = Yn1;
  358. /* The result is converted to 1.63, Yn1 variable is reused */
  359. Yn1 = acc << shift;
  360. /* Calc lower part of acc */
  361. acc_l = acc & 0xffffffff;
  362. /* Calc upper part of acc */
  363. acc_h = (acc >> 32) & 0xffffffff;
  364. /* Apply shift for lower part of acc and upper part of acc */
  365. acc_h = (uint32_t) acc_l >> lShift | acc_h << uShift;
  366. /* Store the output in the destination buffer in 1.31 format. */
  367. *pOut++ = acc_h;
  368. //Yn1 = acc << shift;
  369. /* Store the output in the destination buffer in 1.31 format. */
  370. // *pOut++ = (q31_t) (acc >> (32 - shift));
  371. /* decrement the loop counter */
  372. sample--;
  373. }
  374. /* The first stage output is given as input to the second stage. */
  375. pIn = pDst;
  376. /* Reset to destination buffer working pointer */
  377. pOut = pDst;
  378. /* Store the updated state variables back into the pState array */
  379. /* Store the updated state variables back into the pState array */
  380. *pState++ = (q63_t) Xn1;
  381. *pState++ = (q63_t) Xn2;
  382. *pState++ = Yn1;
  383. *pState++ = Yn2;
  384. } while(--stage);
  385. #else
  386. /* Run the below code for Cortex-M0 */
  387. do
  388. {
  389. /* Reading the coefficients */
  390. b0 = *pCoeffs++;
  391. b1 = *pCoeffs++;
  392. b2 = *pCoeffs++;
  393. a1 = *pCoeffs++;
  394. a2 = *pCoeffs++;
  395. /* Reading the state values */
  396. Xn1 = pState[0];
  397. Xn2 = pState[1];
  398. Yn1 = pState[2];
  399. Yn2 = pState[3];
  400. /* The variable acc hold output value that is being computed and
  401. * stored in the destination buffer
  402. * acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2]
  403. */
  404. sample = blockSize;
  405. while(sample > 0u)
  406. {
  407. /* Read the input */
  408. Xn = *pIn++;
  409. /* acc = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] + a1 * y[n-1] + a2 * y[n-2] */
  410. /* acc = b0 * x[n] */
  411. acc = (q63_t) Xn *b0;
  412. /* acc += b1 * x[n-1] */
  413. acc += (q63_t) Xn1 *b1;
  414. /* acc += b[2] * x[n-2] */
  415. acc += (q63_t) Xn2 *b2;
  416. /* acc += a1 * y[n-1] */
  417. acc += mult32x64(Yn1, a1);
  418. /* acc += a2 * y[n-2] */
  419. acc += mult32x64(Yn2, a2);
  420. /* Every time after the output is computed state should be updated. */
  421. /* The states should be updated as: */
  422. /* Xn2 = Xn1 */
  423. /* Xn1 = Xn */
  424. /* Yn2 = Yn1 */
  425. /* Yn1 = acc */
  426. Xn2 = Xn1;
  427. Xn1 = Xn;
  428. Yn2 = Yn1;
  429. /* The result is converted to 1.63, Yn1 variable is reused */
  430. Yn1 = acc << shift;
  431. /* Calc lower part of acc */
  432. acc_l = acc & 0xffffffff;
  433. /* Calc upper part of acc */
  434. acc_h = (acc >> 32) & 0xffffffff;
  435. /* Apply shift for lower part of acc and upper part of acc */
  436. acc_h = (uint32_t) acc_l >> lShift | acc_h << uShift;
  437. /* Store the output in the destination buffer in 1.31 format. */
  438. *pOut++ = acc_h;
  439. //Yn1 = acc << shift;
  440. /* Store the output in the destination buffer in 1.31 format. */
  441. //*pOut++ = (q31_t) (acc >> (32 - shift));
  442. /* decrement the loop counter */
  443. sample--;
  444. }
  445. /* The first stage output is given as input to the second stage. */
  446. pIn = pDst;
  447. /* Reset to destination buffer working pointer */
  448. pOut = pDst;
  449. /* Store the updated state variables back into the pState array */
  450. *pState++ = (q63_t) Xn1;
  451. *pState++ = (q63_t) Xn2;
  452. *pState++ = Yn1;
  453. *pState++ = Yn2;
  454. } while(--stage);
  455. #endif /* #ifndef ARM_MATH_CM0_FAMILY */
  456. }
  457. /**
  458. * @} end of BiquadCascadeDF1_32x64 group
  459. */