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.
 
 
 
 
 

604 lines
19 KiB

  1. /* ----------------------------------------------------------------------
  2. * Copyright (C) 2010-2014 ARM Limited. All rights reserved.
  3. *
  4. * $Date: 31. July 2014
  5. * $Revision: V1.4.4
  6. *
  7. * Project: CMSIS DSP Library
  8. * Title: arm_biquad_cascade_df2T_f32.c
  9. *
  10. * Description: Processing function for the floating-point transposed
  11. * direct form II Biquad cascade filter.
  12. *
  13. * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions
  17. * are met:
  18. * - Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. * - Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in
  22. * the documentation and/or other materials provided with the
  23. * distribution.
  24. * - Neither the name of ARM LIMITED nor the names of its contributors
  25. * may be used to endorse or promote products derived from this
  26. * software without specific prior written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  29. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  30. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  31. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  32. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  33. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  34. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  35. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  36. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  37. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  38. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  39. * POSSIBILITY OF SUCH DAMAGE.
  40. * -------------------------------------------------------------------- */
  41. #include "arm_math.h"
  42. /**
  43. * @ingroup groupFilters
  44. */
  45. /**
  46. * @defgroup BiquadCascadeDF2T Biquad Cascade IIR Filters Using a Direct Form II Transposed Structure
  47. *
  48. * This set of functions implements arbitrary order recursive (IIR) filters using a transposed direct form II structure.
  49. * The filters are implemented as a cascade of second order Biquad sections.
  50. * These functions provide a slight memory savings as compared to the direct form I Biquad filter functions.
  51. * Only floating-point data is supported.
  52. *
  53. * This function operate on blocks of input and output data and each call to the function
  54. * processes <code>blockSize</code> samples through the filter.
  55. * <code>pSrc</code> points to the array of input data and
  56. * <code>pDst</code> points to the array of output data.
  57. * Both arrays contain <code>blockSize</code> values.
  58. *
  59. * \par Algorithm
  60. * Each Biquad stage implements a second order filter using the difference equation:
  61. * <pre>
  62. * y[n] = b0 * x[n] + d1
  63. * d1 = b1 * x[n] + a1 * y[n] + d2
  64. * d2 = b2 * x[n] + a2 * y[n]
  65. * </pre>
  66. * where d1 and d2 represent the two state values.
  67. *
  68. * \par
  69. * A Biquad filter using a transposed Direct Form II structure is shown below.
  70. * \image html BiquadDF2Transposed.gif "Single transposed Direct Form II Biquad"
  71. * Coefficients <code>b0, b1, and b2 </code> multiply the input signal <code>x[n]</code> and are referred to as the feedforward coefficients.
  72. * Coefficients <code>a1</code> and <code>a2</code> multiply the output signal <code>y[n]</code> and are referred to as the feedback coefficients.
  73. * Pay careful attention to the sign of the feedback coefficients.
  74. * Some design tools flip the sign of the feedback coefficients:
  75. * <pre>
  76. * y[n] = b0 * x[n] + d1;
  77. * d1 = b1 * x[n] - a1 * y[n] + d2;
  78. * d2 = b2 * x[n] - a2 * y[n];
  79. * </pre>
  80. * In this case the feedback coefficients <code>a1</code> and <code>a2</code> must be negated when used with the CMSIS DSP Library.
  81. *
  82. * \par
  83. * Higher order filters are realized as a cascade of second order sections.
  84. * <code>numStages</code> refers to the number of second order stages used.
  85. * For example, an 8th order filter would be realized with <code>numStages=4</code> second order stages.
  86. * A 9th order filter would be realized with <code>numStages=5</code> second order stages with the
  87. * coefficients for one of the stages configured as a first order filter (<code>b2=0</code> and <code>a2=0</code>).
  88. *
  89. * \par
  90. * <code>pState</code> points to the state variable array.
  91. * Each Biquad stage has 2 state variables <code>d1</code> and <code>d2</code>.
  92. * The state variables are arranged in the <code>pState</code> array as:
  93. * <pre>
  94. * {d11, d12, d21, d22, ...}
  95. * </pre>
  96. * where <code>d1x</code> refers to the state variables for the first Biquad and
  97. * <code>d2x</code> refers to the state variables for the second Biquad.
  98. * The state array has a total length of <code>2*numStages</code> values.
  99. * The state variables are updated after each block of data is processed; the coefficients are untouched.
  100. *
  101. * \par
  102. * The CMSIS library contains Biquad filters in both Direct Form I and transposed Direct Form II.
  103. * The advantage of the Direct Form I structure is that it is numerically more robust for fixed-point data types.
  104. * That is why the Direct Form I structure supports Q15 and Q31 data types.
  105. * The transposed Direct Form II structure, on the other hand, requires a wide dynamic range for the state variables <code>d1</code> and <code>d2</code>.
  106. * Because of this, the CMSIS library only has a floating-point version of the Direct Form II Biquad.
  107. * The advantage of the Direct Form II Biquad is that it requires half the number of state variables, 2 rather than 4, per Biquad stage.
  108. *
  109. * \par Instance Structure
  110. * The coefficients and state variables for a filter are stored together in an instance data structure.
  111. * A separate instance structure must be defined for each filter.
  112. * Coefficient arrays may be shared among several instances while state variable arrays cannot be shared.
  113. *
  114. * \par Init Functions
  115. * There is also an associated initialization function.
  116. * The initialization function performs following operations:
  117. * - Sets the values of the internal structure fields.
  118. * - Zeros out the values in the state buffer.
  119. * To do this manually without calling the init function, assign the follow subfields of the instance structure:
  120. * numStages, pCoeffs, pState. Also set all of the values in pState to zero.
  121. *
  122. * \par
  123. * Use of the initialization function is optional.
  124. * However, if the initialization function is used, then the instance structure cannot be placed into a const data section.
  125. * To place an instance structure into a const data section, the instance structure must be manually initialized.
  126. * Set the values in the state buffer to zeros before static initialization.
  127. * For example, to statically initialize the instance structure use
  128. * <pre>
  129. * arm_biquad_cascade_df2T_instance_f32 S1 = {numStages, pState, pCoeffs};
  130. * </pre>
  131. * where <code>numStages</code> is the number of Biquad stages in the filter; <code>pState</code> is the address of the state buffer.
  132. * <code>pCoeffs</code> is the address of the coefficient buffer;
  133. *
  134. */
  135. /**
  136. * @addtogroup BiquadCascadeDF2T
  137. * @{
  138. */
  139. /**
  140. * @brief Processing function for the floating-point transposed direct form II Biquad cascade filter.
  141. * @param[in] *S points to an instance of the filter data structure.
  142. * @param[in] *pSrc points to the block of input data.
  143. * @param[out] *pDst points to the block of output data
  144. * @param[in] blockSize number of samples to process.
  145. * @return none.
  146. */
  147. LOW_OPTIMIZATION_ENTER
  148. void arm_biquad_cascade_df2T_f32(
  149. const arm_biquad_cascade_df2T_instance_f32 * S,
  150. float32_t * pSrc,
  151. float32_t * pDst,
  152. uint32_t blockSize)
  153. {
  154. float32_t *pIn = pSrc; /* source pointer */
  155. float32_t *pOut = pDst; /* destination pointer */
  156. float32_t *pState = S->pState; /* State pointer */
  157. float32_t *pCoeffs = S->pCoeffs; /* coefficient pointer */
  158. float32_t acc1; /* accumulator */
  159. float32_t b0, b1, b2, a1, a2; /* Filter coefficients */
  160. float32_t Xn1; /* temporary input */
  161. float32_t d1, d2; /* state variables */
  162. uint32_t sample, stage = S->numStages; /* loop counters */
  163. #if defined(ARM_MATH_CM7)
  164. float32_t Xn2, Xn3, Xn4, Xn5, Xn6, Xn7, Xn8; /* Input State variables */
  165. float32_t Xn9, Xn10, Xn11, Xn12, Xn13, Xn14, Xn15, Xn16;
  166. float32_t acc2, acc3, acc4, acc5, acc6, acc7; /* Simulates the accumulator */
  167. float32_t acc8, acc9, acc10, acc11, acc12, acc13, acc14, acc15, acc16;
  168. do
  169. {
  170. /* Reading the coefficients */
  171. b0 = pCoeffs[0];
  172. b1 = pCoeffs[1];
  173. b2 = pCoeffs[2];
  174. a1 = pCoeffs[3];
  175. /* Apply loop unrolling and compute 16 output values simultaneously. */
  176. sample = blockSize >> 4u;
  177. a2 = pCoeffs[4];
  178. /*Reading the state values */
  179. d1 = pState[0];
  180. d2 = pState[1];
  181. pCoeffs += 5u;
  182. /* First part of the processing with loop unrolling. Compute 16 outputs at a time.
  183. ** a second loop below computes the remaining 1 to 15 samples. */
  184. while(sample > 0u) {
  185. /* y[n] = b0 * x[n] + d1 */
  186. /* d1 = b1 * x[n] + a1 * y[n] + d2 */
  187. /* d2 = b2 * x[n] + a2 * y[n] */
  188. /* Read the first 2 inputs. 2 cycles */
  189. Xn1 = pIn[0 ];
  190. Xn2 = pIn[1 ];
  191. /* Sample 1. 5 cycles */
  192. Xn3 = pIn[2 ];
  193. acc1 = b0 * Xn1 + d1;
  194. Xn4 = pIn[3 ];
  195. d1 = b1 * Xn1 + d2;
  196. Xn5 = pIn[4 ];
  197. d2 = b2 * Xn1;
  198. Xn6 = pIn[5 ];
  199. d1 += a1 * acc1;
  200. Xn7 = pIn[6 ];
  201. d2 += a2 * acc1;
  202. /* Sample 2. 5 cycles */
  203. Xn8 = pIn[7 ];
  204. acc2 = b0 * Xn2 + d1;
  205. Xn9 = pIn[8 ];
  206. d1 = b1 * Xn2 + d2;
  207. Xn10 = pIn[9 ];
  208. d2 = b2 * Xn2;
  209. Xn11 = pIn[10];
  210. d1 += a1 * acc2;
  211. Xn12 = pIn[11];
  212. d2 += a2 * acc2;
  213. /* Sample 3. 5 cycles */
  214. Xn13 = pIn[12];
  215. acc3 = b0 * Xn3 + d1;
  216. Xn14 = pIn[13];
  217. d1 = b1 * Xn3 + d2;
  218. Xn15 = pIn[14];
  219. d2 = b2 * Xn3;
  220. Xn16 = pIn[15];
  221. d1 += a1 * acc3;
  222. pIn += 16;
  223. d2 += a2 * acc3;
  224. /* Sample 4. 5 cycles */
  225. acc4 = b0 * Xn4 + d1;
  226. d1 = b1 * Xn4 + d2;
  227. d2 = b2 * Xn4;
  228. d1 += a1 * acc4;
  229. d2 += a2 * acc4;
  230. /* Sample 5. 5 cycles */
  231. acc5 = b0 * Xn5 + d1;
  232. d1 = b1 * Xn5 + d2;
  233. d2 = b2 * Xn5;
  234. d1 += a1 * acc5;
  235. d2 += a2 * acc5;
  236. /* Sample 6. 5 cycles */
  237. acc6 = b0 * Xn6 + d1;
  238. d1 = b1 * Xn6 + d2;
  239. d2 = b2 * Xn6;
  240. d1 += a1 * acc6;
  241. d2 += a2 * acc6;
  242. /* Sample 7. 5 cycles */
  243. acc7 = b0 * Xn7 + d1;
  244. d1 = b1 * Xn7 + d2;
  245. d2 = b2 * Xn7;
  246. d1 += a1 * acc7;
  247. d2 += a2 * acc7;
  248. /* Sample 8. 5 cycles */
  249. acc8 = b0 * Xn8 + d1;
  250. d1 = b1 * Xn8 + d2;
  251. d2 = b2 * Xn8;
  252. d1 += a1 * acc8;
  253. d2 += a2 * acc8;
  254. /* Sample 9. 5 cycles */
  255. acc9 = b0 * Xn9 + d1;
  256. d1 = b1 * Xn9 + d2;
  257. d2 = b2 * Xn9;
  258. d1 += a1 * acc9;
  259. d2 += a2 * acc9;
  260. /* Sample 10. 5 cycles */
  261. acc10 = b0 * Xn10 + d1;
  262. d1 = b1 * Xn10 + d2;
  263. d2 = b2 * Xn10;
  264. d1 += a1 * acc10;
  265. d2 += a2 * acc10;
  266. /* Sample 11. 5 cycles */
  267. acc11 = b0 * Xn11 + d1;
  268. d1 = b1 * Xn11 + d2;
  269. d2 = b2 * Xn11;
  270. d1 += a1 * acc11;
  271. d2 += a2 * acc11;
  272. /* Sample 12. 5 cycles */
  273. acc12 = b0 * Xn12 + d1;
  274. d1 = b1 * Xn12 + d2;
  275. d2 = b2 * Xn12;
  276. d1 += a1 * acc12;
  277. d2 += a2 * acc12;
  278. /* Sample 13. 5 cycles */
  279. acc13 = b0 * Xn13 + d1;
  280. d1 = b1 * Xn13 + d2;
  281. d2 = b2 * Xn13;
  282. pOut[0 ] = acc1 ;
  283. d1 += a1 * acc13;
  284. pOut[1 ] = acc2 ;
  285. d2 += a2 * acc13;
  286. /* Sample 14. 5 cycles */
  287. pOut[2 ] = acc3 ;
  288. acc14 = b0 * Xn14 + d1;
  289. pOut[3 ] = acc4 ;
  290. d1 = b1 * Xn14 + d2;
  291. pOut[4 ] = acc5 ;
  292. d2 = b2 * Xn14;
  293. pOut[5 ] = acc6 ;
  294. d1 += a1 * acc14;
  295. pOut[6 ] = acc7 ;
  296. d2 += a2 * acc14;
  297. /* Sample 15. 5 cycles */
  298. pOut[7 ] = acc8 ;
  299. pOut[8 ] = acc9 ;
  300. acc15 = b0 * Xn15 + d1;
  301. pOut[9 ] = acc10;
  302. d1 = b1 * Xn15 + d2;
  303. pOut[10] = acc11;
  304. d2 = b2 * Xn15;
  305. pOut[11] = acc12;
  306. d1 += a1 * acc15;
  307. pOut[12] = acc13;
  308. d2 += a2 * acc15;
  309. /* Sample 16. 5 cycles */
  310. pOut[13] = acc14;
  311. acc16 = b0 * Xn16 + d1;
  312. pOut[14] = acc15;
  313. d1 = b1 * Xn16 + d2;
  314. pOut[15] = acc16;
  315. d2 = b2 * Xn16;
  316. sample--;
  317. d1 += a1 * acc16;
  318. pOut += 16;
  319. d2 += a2 * acc16;
  320. }
  321. sample = blockSize & 0xFu;
  322. while(sample > 0u) {
  323. Xn1 = *pIn;
  324. acc1 = b0 * Xn1 + d1;
  325. pIn++;
  326. d1 = b1 * Xn1 + d2;
  327. *pOut = acc1;
  328. d2 = b2 * Xn1;
  329. pOut++;
  330. d1 += a1 * acc1;
  331. sample--;
  332. d2 += a2 * acc1;
  333. }
  334. /* Store the updated state variables back into the state array */
  335. pState[0] = d1;
  336. /* The current stage input is given as the output to the next stage */
  337. pIn = pDst;
  338. pState[1] = d2;
  339. /* decrement the loop counter */
  340. stage--;
  341. pState += 2u;
  342. /*Reset the output working pointer */
  343. pOut = pDst;
  344. } while(stage > 0u);
  345. #elif defined(ARM_MATH_CM0_FAMILY)
  346. /* Run the below code for Cortex-M0 */
  347. do
  348. {
  349. /* Reading the coefficients */
  350. b0 = *pCoeffs++;
  351. b1 = *pCoeffs++;
  352. b2 = *pCoeffs++;
  353. a1 = *pCoeffs++;
  354. a2 = *pCoeffs++;
  355. /*Reading the state values */
  356. d1 = pState[0];
  357. d2 = pState[1];
  358. sample = blockSize;
  359. while(sample > 0u)
  360. {
  361. /* Read the input */
  362. Xn1 = *pIn++;
  363. /* y[n] = b0 * x[n] + d1 */
  364. acc1 = (b0 * Xn1) + d1;
  365. /* Store the result in the accumulator in the destination buffer. */
  366. *pOut++ = acc1;
  367. /* Every time after the output is computed state should be updated. */
  368. /* d1 = b1 * x[n] + a1 * y[n] + d2 */
  369. d1 = ((b1 * Xn1) + (a1 * acc1)) + d2;
  370. /* d2 = b2 * x[n] + a2 * y[n] */
  371. d2 = (b2 * Xn1) + (a2 * acc1);
  372. /* decrement the loop counter */
  373. sample--;
  374. }
  375. /* Store the updated state variables back into the state array */
  376. *pState++ = d1;
  377. *pState++ = d2;
  378. /* The current stage input is given as the output to the next stage */
  379. pIn = pDst;
  380. /*Reset the output working pointer */
  381. pOut = pDst;
  382. /* decrement the loop counter */
  383. stage--;
  384. } while(stage > 0u);
  385. #else
  386. float32_t Xn2, Xn3, Xn4; /* Input State variables */
  387. float32_t acc2, acc3, acc4; /* accumulator */
  388. float32_t p0, p1, p2, p3, p4, A1;
  389. /* Run the below code for Cortex-M4 and Cortex-M3 */
  390. do
  391. {
  392. /* Reading the coefficients */
  393. b0 = *pCoeffs++;
  394. b1 = *pCoeffs++;
  395. b2 = *pCoeffs++;
  396. a1 = *pCoeffs++;
  397. a2 = *pCoeffs++;
  398. /*Reading the state values */
  399. d1 = pState[0];
  400. d2 = pState[1];
  401. /* Apply loop unrolling and compute 4 output values simultaneously. */
  402. sample = blockSize >> 2u;
  403. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  404. ** a second loop below computes the remaining 1 to 3 samples. */
  405. while(sample > 0u) {
  406. /* y[n] = b0 * x[n] + d1 */
  407. /* d1 = b1 * x[n] + a1 * y[n] + d2 */
  408. /* d2 = b2 * x[n] + a2 * y[n] */
  409. /* Read the four inputs */
  410. Xn1 = pIn[0];
  411. Xn2 = pIn[1];
  412. Xn3 = pIn[2];
  413. Xn4 = pIn[3];
  414. pIn += 4;
  415. p0 = b0 * Xn1;
  416. p1 = b1 * Xn1;
  417. acc1 = p0 + d1;
  418. p0 = b0 * Xn2;
  419. p3 = a1 * acc1;
  420. p2 = b2 * Xn1;
  421. A1 = p1 + p3;
  422. p4 = a2 * acc1;
  423. d1 = A1 + d2;
  424. d2 = p2 + p4;
  425. p1 = b1 * Xn2;
  426. acc2 = p0 + d1;
  427. p0 = b0 * Xn3;
  428. p3 = a1 * acc2;
  429. p2 = b2 * Xn2;
  430. A1 = p1 + p3;
  431. p4 = a2 * acc2;
  432. d1 = A1 + d2;
  433. d2 = p2 + p4;
  434. p1 = b1 * Xn3;
  435. acc3 = p0 + d1;
  436. p0 = b0 * Xn4;
  437. p3 = a1 * acc3;
  438. p2 = b2 * Xn3;
  439. A1 = p1 + p3;
  440. p4 = a2 * acc3;
  441. d1 = A1 + d2;
  442. d2 = p2 + p4;
  443. acc4 = p0 + d1;
  444. p1 = b1 * Xn4;
  445. p3 = a1 * acc4;
  446. p2 = b2 * Xn4;
  447. A1 = p1 + p3;
  448. p4 = a2 * acc4;
  449. d1 = A1 + d2;
  450. d2 = p2 + p4;
  451. pOut[0] = acc1;
  452. pOut[1] = acc2;
  453. pOut[2] = acc3;
  454. pOut[3] = acc4;
  455. pOut += 4;
  456. sample--;
  457. }
  458. sample = blockSize & 0x3u;
  459. while(sample > 0u) {
  460. Xn1 = *pIn++;
  461. p0 = b0 * Xn1;
  462. p1 = b1 * Xn1;
  463. acc1 = p0 + d1;
  464. p3 = a1 * acc1;
  465. p2 = b2 * Xn1;
  466. A1 = p1 + p3;
  467. p4 = a2 * acc1;
  468. d1 = A1 + d2;
  469. d2 = p2 + p4;
  470. *pOut++ = acc1;
  471. sample--;
  472. }
  473. /* Store the updated state variables back into the state array */
  474. *pState++ = d1;
  475. *pState++ = d2;
  476. /* The current stage input is given as the output to the next stage */
  477. pIn = pDst;
  478. /*Reset the output working pointer */
  479. pOut = pDst;
  480. /* decrement the loop counter */
  481. stage--;
  482. } while(stage > 0u);
  483. #endif
  484. }
  485. LOW_OPTIMIZATION_EXIT
  486. /**
  487. * @} end of BiquadCascadeDF2T group
  488. */