Keyboard firmwares for Atmel AVR and Cortex-M
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.

arm_iir_lattice_f32.c 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /* ----------------------------------------------------------------------
  2. * Copyright (C) 2010-2013 ARM Limited. All rights reserved.
  3. *
  4. * $Date: 17. January 2013
  5. * $Revision: V1.4.1
  6. *
  7. * Project: CMSIS DSP Library
  8. * Title: arm_iir_lattice_f32.c
  9. *
  10. * Description: Floating-point IIR Lattice 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 IIR_Lattice Infinite Impulse Response (IIR) Lattice Filters
  46. *
  47. * This set of functions implements lattice filters
  48. * for Q15, Q31 and floating-point data types. Lattice filters are used in a
  49. * variety of adaptive filter applications. The filter structure has feedforward and
  50. * feedback components and the net impulse response is infinite length.
  51. * The functions operate on blocks
  52. * of input and output data and each call to the function processes
  53. * <code>blockSize</code> samples through the filter. <code>pSrc</code> and
  54. * <code>pDst</code> point to input and output arrays containing <code>blockSize</code> values.
  55. * \par Algorithm:
  56. * \image html IIRLattice.gif "Infinite Impulse Response Lattice filter"
  57. * <pre>
  58. * fN(n) = x(n)
  59. * fm-1(n) = fm(n) - km * gm-1(n-1) for m = N, N-1, ...1
  60. * gm(n) = km * fm-1(n) + gm-1(n-1) for m = N, N-1, ...1
  61. * y(n) = vN * gN(n) + vN-1 * gN-1(n) + ...+ v0 * g0(n)
  62. * </pre>
  63. * \par
  64. * <code>pkCoeffs</code> points to array of reflection coefficients of size <code>numStages</code>.
  65. * Reflection coefficients are stored in time-reversed order.
  66. * \par
  67. * <pre>
  68. * {kN, kN-1, ....k1}
  69. * </pre>
  70. * <code>pvCoeffs</code> points to the array of ladder coefficients of size <code>(numStages+1)</code>.
  71. * Ladder coefficients are stored in time-reversed order.
  72. * \par
  73. * <pre>
  74. * {vN, vN-1, ...v0}
  75. * </pre>
  76. * <code>pState</code> points to a state array of size <code>numStages + blockSize</code>.
  77. * The state variables shown in the figure above (the g values) are stored in the <code>pState</code> array.
  78. * The state variables are updated after each block of data is processed; the coefficients are untouched.
  79. * \par Instance Structure
  80. * The coefficients and state variables for a filter are stored together in an instance data structure.
  81. * A separate instance structure must be defined for each filter.
  82. * Coefficient arrays may be shared among several instances while state variable arrays cannot be shared.
  83. * There are separate instance structure declarations for each of the 3 supported data types.
  84. *
  85. * \par Initialization Functions
  86. * There is also an associated initialization function for each data type.
  87. * The initialization function performs the following operations:
  88. * - Sets the values of the internal structure fields.
  89. * - Zeros out the values in the state buffer.
  90. * To do this manually without calling the init function, assign the follow subfields of the instance structure:
  91. * numStages, pkCoeffs, pvCoeffs, pState. Also set all of the values in pState to zero.
  92. *
  93. * \par
  94. * Use of the initialization function is optional.
  95. * However, if the initialization function is used, then the instance structure cannot be placed into a const data section.
  96. * To place an instance structure into a const data section, the instance structure must be manually initialized.
  97. * Set the values in the state buffer to zeros and then manually initialize the instance structure as follows:
  98. * <pre>
  99. *arm_iir_lattice_instance_f32 S = {numStages, pState, pkCoeffs, pvCoeffs};
  100. *arm_iir_lattice_instance_q31 S = {numStages, pState, pkCoeffs, pvCoeffs};
  101. *arm_iir_lattice_instance_q15 S = {numStages, pState, pkCoeffs, pvCoeffs};
  102. * </pre>
  103. * \par
  104. * where <code>numStages</code> is the number of stages in the filter; <code>pState</code> points to the state buffer array;
  105. * <code>pkCoeffs</code> points to array of the reflection coefficients; <code>pvCoeffs</code> points to the array of ladder coefficients.
  106. * \par Fixed-Point Behavior
  107. * Care must be taken when using the fixed-point versions of the IIR lattice filter functions.
  108. * In particular, the overflow and saturation behavior of the accumulator used in each function must be considered.
  109. * Refer to the function specific documentation below for usage guidelines.
  110. */
  111. /**
  112. * @addtogroup IIR_Lattice
  113. * @{
  114. */
  115. /**
  116. * @brief Processing function for the floating-point IIR lattice filter.
  117. * @param[in] *S points to an instance of the floating-point IIR lattice structure.
  118. * @param[in] *pSrc points to the block of input data.
  119. * @param[out] *pDst points to the block of output data.
  120. * @param[in] blockSize number of samples to process.
  121. * @return none.
  122. */
  123. #ifndef ARM_MATH_CM0_FAMILY
  124. /* Run the below code for Cortex-M4 and Cortex-M3 */
  125. void arm_iir_lattice_f32(
  126. const arm_iir_lattice_instance_f32 * S,
  127. float32_t * pSrc,
  128. float32_t * pDst,
  129. uint32_t blockSize)
  130. {
  131. float32_t fnext1, gcurr1, gnext; /* Temporary variables for lattice stages */
  132. float32_t acc; /* Accumlator */
  133. uint32_t blkCnt, tapCnt; /* temporary variables for counts */
  134. float32_t *px1, *px2, *pk, *pv; /* temporary pointers for state and coef */
  135. uint32_t numStages = S->numStages; /* number of stages */
  136. float32_t *pState; /* State pointer */
  137. float32_t *pStateCurnt; /* State current pointer */
  138. float32_t k1, k2;
  139. float32_t v1, v2, v3, v4;
  140. float32_t gcurr2;
  141. float32_t fnext2;
  142. /* initialise loop count */
  143. blkCnt = blockSize;
  144. /* initialise state pointer */
  145. pState = &S->pState[0];
  146. /* Sample processing */
  147. while(blkCnt > 0u)
  148. {
  149. /* Read Sample from input buffer */
  150. /* fN(n) = x(n) */
  151. fnext2 = *pSrc++;
  152. /* Initialize Ladder coeff pointer */
  153. pv = &S->pvCoeffs[0];
  154. /* Initialize Reflection coeff pointer */
  155. pk = &S->pkCoeffs[0];
  156. /* Initialize state read pointer */
  157. px1 = pState;
  158. /* Initialize state write pointer */
  159. px2 = pState;
  160. /* Set accumulator to zero */
  161. acc = 0.0;
  162. /* Loop unrolling. Process 4 taps at a time. */
  163. tapCnt = (numStages) >> 2;
  164. while(tapCnt > 0u)
  165. {
  166. /* Read gN-1(n-1) from state buffer */
  167. gcurr1 = *px1;
  168. /* read reflection coefficient kN */
  169. k1 = *pk;
  170. /* fN-1(n) = fN(n) - kN * gN-1(n-1) */
  171. fnext1 = fnext2 - (k1 * gcurr1);
  172. /* read ladder coefficient vN */
  173. v1 = *pv;
  174. /* read next reflection coefficient kN-1 */
  175. k2 = *(pk + 1u);
  176. /* Read gN-2(n-1) from state buffer */
  177. gcurr2 = *(px1 + 1u);
  178. /* read next ladder coefficient vN-1 */
  179. v2 = *(pv + 1u);
  180. /* fN-2(n) = fN-1(n) - kN-1 * gN-2(n-1) */
  181. fnext2 = fnext1 - (k2 * gcurr2);
  182. /* gN(n) = kN * fN-1(n) + gN-1(n-1) */
  183. gnext = gcurr1 + (k1 * fnext1);
  184. /* read reflection coefficient kN-2 */
  185. k1 = *(pk + 2u);
  186. /* write gN(n) into state for next sample processing */
  187. *px2++ = gnext;
  188. /* Read gN-3(n-1) from state buffer */
  189. gcurr1 = *(px1 + 2u);
  190. /* y(n) += gN(n) * vN */
  191. acc += (gnext * v1);
  192. /* fN-3(n) = fN-2(n) - kN-2 * gN-3(n-1) */
  193. fnext1 = fnext2 - (k1 * gcurr1);
  194. /* gN-1(n) = kN-1 * fN-2(n) + gN-2(n-1) */
  195. gnext = gcurr2 + (k2 * fnext2);
  196. /* Read gN-4(n-1) from state buffer */
  197. gcurr2 = *(px1 + 3u);
  198. /* y(n) += gN-1(n) * vN-1 */
  199. acc += (gnext * v2);
  200. /* read reflection coefficient kN-3 */
  201. k2 = *(pk + 3u);
  202. /* write gN-1(n) into state for next sample processing */
  203. *px2++ = gnext;
  204. /* fN-4(n) = fN-3(n) - kN-3 * gN-4(n-1) */
  205. fnext2 = fnext1 - (k2 * gcurr2);
  206. /* gN-2(n) = kN-2 * fN-3(n) + gN-3(n-1) */
  207. gnext = gcurr1 + (k1 * fnext1);
  208. /* read ladder coefficient vN-2 */
  209. v3 = *(pv + 2u);
  210. /* y(n) += gN-2(n) * vN-2 */
  211. acc += (gnext * v3);
  212. /* write gN-2(n) into state for next sample processing */
  213. *px2++ = gnext;
  214. /* update pointer */
  215. pk += 4u;
  216. /* gN-3(n) = kN-3 * fN-4(n) + gN-4(n-1) */
  217. gnext = (fnext2 * k2) + gcurr2;
  218. /* read next ladder coefficient vN-3 */
  219. v4 = *(pv + 3u);
  220. /* y(n) += gN-4(n) * vN-4 */
  221. acc += (gnext * v4);
  222. /* write gN-3(n) into state for next sample processing */
  223. *px2++ = gnext;
  224. /* update pointers */
  225. px1 += 4u;
  226. pv += 4u;
  227. tapCnt--;
  228. }
  229. /* If the filter length is not a multiple of 4, compute the remaining filter taps */
  230. tapCnt = (numStages) % 0x4u;
  231. while(tapCnt > 0u)
  232. {
  233. gcurr1 = *px1++;
  234. /* Process sample for last taps */
  235. fnext1 = fnext2 - ((*pk) * gcurr1);
  236. gnext = (fnext1 * (*pk++)) + gcurr1;
  237. /* Output samples for last taps */
  238. acc += (gnext * (*pv++));
  239. *px2++ = gnext;
  240. fnext2 = fnext1;
  241. tapCnt--;
  242. }
  243. /* y(n) += g0(n) * v0 */
  244. acc += (fnext2 * (*pv));
  245. *px2++ = fnext2;
  246. /* write out into pDst */
  247. *pDst++ = acc;
  248. /* Advance the state pointer by 4 to process the next group of 4 samples */
  249. pState = pState + 1u;
  250. blkCnt--;
  251. }
  252. /* Processing is complete. Now copy last S->numStages samples to start of the buffer
  253. for the preperation of next frame process */
  254. /* Points to the start of the state buffer */
  255. pStateCurnt = &S->pState[0];
  256. pState = &S->pState[blockSize];
  257. tapCnt = numStages >> 2u;
  258. /* copy data */
  259. while(tapCnt > 0u)
  260. {
  261. *pStateCurnt++ = *pState++;
  262. *pStateCurnt++ = *pState++;
  263. *pStateCurnt++ = *pState++;
  264. *pStateCurnt++ = *pState++;
  265. /* Decrement the loop counter */
  266. tapCnt--;
  267. }
  268. /* Calculate remaining number of copies */
  269. tapCnt = (numStages) % 0x4u;
  270. /* Copy the remaining q31_t data */
  271. while(tapCnt > 0u)
  272. {
  273. *pStateCurnt++ = *pState++;
  274. /* Decrement the loop counter */
  275. tapCnt--;
  276. }
  277. }
  278. #else
  279. void arm_iir_lattice_f32(
  280. const arm_iir_lattice_instance_f32 * S,
  281. float32_t * pSrc,
  282. float32_t * pDst,
  283. uint32_t blockSize)
  284. {
  285. float32_t fcurr, fnext = 0, gcurr, gnext; /* Temporary variables for lattice stages */
  286. float32_t acc; /* Accumlator */
  287. uint32_t blkCnt, tapCnt; /* temporary variables for counts */
  288. float32_t *px1, *px2, *pk, *pv; /* temporary pointers for state and coef */
  289. uint32_t numStages = S->numStages; /* number of stages */
  290. float32_t *pState; /* State pointer */
  291. float32_t *pStateCurnt; /* State current pointer */
  292. /* Run the below code for Cortex-M0 */
  293. blkCnt = blockSize;
  294. pState = &S->pState[0];
  295. /* Sample processing */
  296. while(blkCnt > 0u)
  297. {
  298. /* Read Sample from input buffer */
  299. /* fN(n) = x(n) */
  300. fcurr = *pSrc++;
  301. /* Initialize state read pointer */
  302. px1 = pState;
  303. /* Initialize state write pointer */
  304. px2 = pState;
  305. /* Set accumulator to zero */
  306. acc = 0.0f;
  307. /* Initialize Ladder coeff pointer */
  308. pv = &S->pvCoeffs[0];
  309. /* Initialize Reflection coeff pointer */
  310. pk = &S->pkCoeffs[0];
  311. /* Process sample for numStages */
  312. tapCnt = numStages;
  313. while(tapCnt > 0u)
  314. {
  315. gcurr = *px1++;
  316. /* Process sample for last taps */
  317. fnext = fcurr - ((*pk) * gcurr);
  318. gnext = (fnext * (*pk++)) + gcurr;
  319. /* Output samples for last taps */
  320. acc += (gnext * (*pv++));
  321. *px2++ = gnext;
  322. fcurr = fnext;
  323. /* Decrementing loop counter */
  324. tapCnt--;
  325. }
  326. /* y(n) += g0(n) * v0 */
  327. acc += (fnext * (*pv));
  328. *px2++ = fnext;
  329. /* write out into pDst */
  330. *pDst++ = acc;
  331. /* Advance the state pointer by 1 to process the next group of samples */
  332. pState = pState + 1u;
  333. blkCnt--;
  334. }
  335. /* Processing is complete. Now copy last S->numStages samples to start of the buffer
  336. for the preperation of next frame process */
  337. /* Points to the start of the state buffer */
  338. pStateCurnt = &S->pState[0];
  339. pState = &S->pState[blockSize];
  340. tapCnt = numStages;
  341. /* Copy the data */
  342. while(tapCnt > 0u)
  343. {
  344. *pStateCurnt++ = *pState++;
  345. /* Decrement the loop counter */
  346. tapCnt--;
  347. }
  348. }
  349. #endif /* #ifndef ARM_MATH_CM0_FAMILY */
  350. /**
  351. * @} end of IIR_Lattice group
  352. */