PageRenderTime 55ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/dsp/cmsis_dsp/FilteringFunctions/arm_iir_lattice_q31.c

https://gitlab.com/YaoQ/mbed-for-linknode
C | 350 lines | 133 code | 66 blank | 151 comment | 0 complexity | 087a1e601e1ea2cd95fce2d05bf0424d MD5 | raw file
  1. /* ----------------------------------------------------------------------
  2. * Copyright (C) 2010-2014 ARM Limited. All rights reserved.
  3. *
  4. * $Date: 19. March 2015
  5. * $Revision: V.1.4.5
  6. *
  7. * Project: CMSIS DSP Library
  8. * Title: arm_iir_lattice_q31.c
  9. *
  10. * Description: Q31 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. * @addtogroup IIR_Lattice
  46. * @{
  47. */
  48. /**
  49. * @brief Processing function for the Q31 IIR lattice filter.
  50. * @param[in] *S points to an instance of the Q31 IIR lattice structure.
  51. * @param[in] *pSrc points to the block of input data.
  52. * @param[out] *pDst points to the block of output data.
  53. * @param[in] blockSize number of samples to process.
  54. * @return none.
  55. *
  56. * @details
  57. * <b>Scaling and Overflow Behavior:</b>
  58. * \par
  59. * The function is implemented using an internal 64-bit accumulator.
  60. * The accumulator has a 2.62 format and maintains full precision of the intermediate multiplication results but provides only a single guard bit.
  61. * Thus, if the accumulator result overflows it wraps around rather than clip.
  62. * In order to avoid overflows completely the input signal must be scaled down by 2*log2(numStages) bits.
  63. * After all multiply-accumulates are performed, the 2.62 accumulator is saturated to 1.32 format and then truncated to 1.31 format.
  64. */
  65. void arm_iir_lattice_q31(
  66. const arm_iir_lattice_instance_q31 * S,
  67. q31_t * pSrc,
  68. q31_t * pDst,
  69. uint32_t blockSize)
  70. {
  71. q31_t fcurr, fnext = 0, gcurr = 0, gnext; /* Temporary variables for lattice stages */
  72. q63_t acc; /* Accumlator */
  73. uint32_t blkCnt, tapCnt; /* Temporary variables for counts */
  74. q31_t *px1, *px2, *pk, *pv; /* Temporary pointers for state and coef */
  75. uint32_t numStages = S->numStages; /* number of stages */
  76. q31_t *pState; /* State pointer */
  77. q31_t *pStateCurnt; /* State current pointer */
  78. blkCnt = blockSize;
  79. pState = &S->pState[0];
  80. #ifndef ARM_MATH_CM0_FAMILY
  81. /* Run the below code for Cortex-M4 and Cortex-M3 */
  82. /* Sample processing */
  83. while(blkCnt > 0u)
  84. {
  85. /* Read Sample from input buffer */
  86. /* fN(n) = x(n) */
  87. fcurr = *pSrc++;
  88. /* Initialize state read pointer */
  89. px1 = pState;
  90. /* Initialize state write pointer */
  91. px2 = pState;
  92. /* Set accumulator to zero */
  93. acc = 0;
  94. /* Initialize Ladder coeff pointer */
  95. pv = &S->pvCoeffs[0];
  96. /* Initialize Reflection coeff pointer */
  97. pk = &S->pkCoeffs[0];
  98. /* Process sample for first tap */
  99. gcurr = *px1++;
  100. /* fN-1(n) = fN(n) - kN * gN-1(n-1) */
  101. fnext = __QSUB(fcurr, (q31_t) (((q63_t) gcurr * (*pk)) >> 31));
  102. /* gN(n) = kN * fN-1(n) + gN-1(n-1) */
  103. gnext = __QADD(gcurr, (q31_t) (((q63_t) fnext * (*pk++)) >> 31));
  104. /* write gN-1(n-1) into state for next sample processing */
  105. *px2++ = gnext;
  106. /* y(n) += gN(n) * vN */
  107. acc += ((q63_t) gnext * *pv++);
  108. /* Update f values for next coefficient processing */
  109. fcurr = fnext;
  110. /* Loop unrolling. Process 4 taps at a time. */
  111. tapCnt = (numStages - 1u) >> 2;
  112. while(tapCnt > 0u)
  113. {
  114. /* Process sample for 2nd, 6th .. taps */
  115. /* Read gN-2(n-1) from state buffer */
  116. gcurr = *px1++;
  117. /* fN-2(n) = fN-1(n) - kN-1 * gN-2(n-1) */
  118. fnext = __QSUB(fcurr, (q31_t) (((q63_t) gcurr * (*pk)) >> 31));
  119. /* gN-1(n) = kN-1 * fN-2(n) + gN-2(n-1) */
  120. gnext = __QADD(gcurr, (q31_t) (((q63_t) fnext * (*pk++)) >> 31));
  121. /* y(n) += gN-1(n) * vN-1 */
  122. /* process for gN-5(n) * vN-5, gN-9(n) * vN-9 ... */
  123. acc += ((q63_t) gnext * *pv++);
  124. /* write gN-1(n) into state for next sample processing */
  125. *px2++ = gnext;
  126. /* Process sample for 3nd, 7th ...taps */
  127. /* Read gN-3(n-1) from state buffer */
  128. gcurr = *px1++;
  129. /* Process sample for 3rd, 7th .. taps */
  130. /* fN-3(n) = fN-2(n) - kN-2 * gN-3(n-1) */
  131. fcurr = __QSUB(fnext, (q31_t) (((q63_t) gcurr * (*pk)) >> 31));
  132. /* gN-2(n) = kN-2 * fN-3(n) + gN-3(n-1) */
  133. gnext = __QADD(gcurr, (q31_t) (((q63_t) fcurr * (*pk++)) >> 31));
  134. /* y(n) += gN-2(n) * vN-2 */
  135. /* process for gN-6(n) * vN-6, gN-10(n) * vN-10 ... */
  136. acc += ((q63_t) gnext * *pv++);
  137. /* write gN-2(n) into state for next sample processing */
  138. *px2++ = gnext;
  139. /* Process sample for 4th, 8th ...taps */
  140. /* Read gN-4(n-1) from state buffer */
  141. gcurr = *px1++;
  142. /* Process sample for 4th, 8th .. taps */
  143. /* fN-4(n) = fN-3(n) - kN-3 * gN-4(n-1) */
  144. fnext = __QSUB(fcurr, (q31_t) (((q63_t) gcurr * (*pk)) >> 31));
  145. /* gN-3(n) = kN-3 * fN-4(n) + gN-4(n-1) */
  146. gnext = __QADD(gcurr, (q31_t) (((q63_t) fnext * (*pk++)) >> 31));
  147. /* y(n) += gN-3(n) * vN-3 */
  148. /* process for gN-7(n) * vN-7, gN-11(n) * vN-11 ... */
  149. acc += ((q63_t) gnext * *pv++);
  150. /* write gN-3(n) into state for next sample processing */
  151. *px2++ = gnext;
  152. /* Process sample for 5th, 9th ...taps */
  153. /* Read gN-5(n-1) from state buffer */
  154. gcurr = *px1++;
  155. /* Process sample for 5th, 9th .. taps */
  156. /* fN-5(n) = fN-4(n) - kN-4 * gN-1(n-1) */
  157. fcurr = __QSUB(fnext, (q31_t) (((q63_t) gcurr * (*pk)) >> 31));
  158. /* gN-4(n) = kN-4 * fN-5(n) + gN-5(n-1) */
  159. gnext = __QADD(gcurr, (q31_t) (((q63_t) fcurr * (*pk++)) >> 31));
  160. /* y(n) += gN-4(n) * vN-4 */
  161. /* process for gN-8(n) * vN-8, gN-12(n) * vN-12 ... */
  162. acc += ((q63_t) gnext * *pv++);
  163. /* write gN-4(n) into state for next sample processing */
  164. *px2++ = gnext;
  165. tapCnt--;
  166. }
  167. fnext = fcurr;
  168. /* If the filter length is not a multiple of 4, compute the remaining filter taps */
  169. tapCnt = (numStages - 1u) % 0x4u;
  170. while(tapCnt > 0u)
  171. {
  172. gcurr = *px1++;
  173. /* Process sample for last taps */
  174. fnext = __QSUB(fcurr, (q31_t) (((q63_t) gcurr * (*pk)) >> 31));
  175. gnext = __QADD(gcurr, (q31_t) (((q63_t) fnext * (*pk++)) >> 31));
  176. /* Output samples for last taps */
  177. acc += ((q63_t) gnext * *pv++);
  178. *px2++ = gnext;
  179. fcurr = fnext;
  180. tapCnt--;
  181. }
  182. /* y(n) += g0(n) * v0 */
  183. acc += (q63_t) fnext *(
  184. *pv++);
  185. *px2++ = fnext;
  186. /* write out into pDst */
  187. *pDst++ = (q31_t) (acc >> 31u);
  188. /* Advance the state pointer by 4 to process the next group of 4 samples */
  189. pState = pState + 1u;
  190. blkCnt--;
  191. }
  192. /* Processing is complete. Now copy last S->numStages samples to start of the buffer
  193. for the preperation of next frame process */
  194. /* Points to the start of the state buffer */
  195. pStateCurnt = &S->pState[0];
  196. pState = &S->pState[blockSize];
  197. tapCnt = numStages >> 2u;
  198. /* copy data */
  199. while(tapCnt > 0u)
  200. {
  201. *pStateCurnt++ = *pState++;
  202. *pStateCurnt++ = *pState++;
  203. *pStateCurnt++ = *pState++;
  204. *pStateCurnt++ = *pState++;
  205. /* Decrement the loop counter */
  206. tapCnt--;
  207. }
  208. /* Calculate remaining number of copies */
  209. tapCnt = (numStages) % 0x4u;
  210. /* Copy the remaining q31_t data */
  211. while(tapCnt > 0u)
  212. {
  213. *pStateCurnt++ = *pState++;
  214. /* Decrement the loop counter */
  215. tapCnt--;
  216. };
  217. #else
  218. /* Run the below code for Cortex-M0 */
  219. /* Sample processing */
  220. while(blkCnt > 0u)
  221. {
  222. /* Read Sample from input buffer */
  223. /* fN(n) = x(n) */
  224. fcurr = *pSrc++;
  225. /* Initialize state read pointer */
  226. px1 = pState;
  227. /* Initialize state write pointer */
  228. px2 = pState;
  229. /* Set accumulator to zero */
  230. acc = 0;
  231. /* Initialize Ladder coeff pointer */
  232. pv = &S->pvCoeffs[0];
  233. /* Initialize Reflection coeff pointer */
  234. pk = &S->pkCoeffs[0];
  235. tapCnt = numStages;
  236. while(tapCnt > 0u)
  237. {
  238. gcurr = *px1++;
  239. /* Process sample */
  240. /* fN-1(n) = fN(n) - kN * gN-1(n-1) */
  241. fnext =
  242. clip_q63_to_q31(((q63_t) fcurr -
  243. ((q31_t) (((q63_t) gcurr * (*pk)) >> 31))));
  244. /* gN(n) = kN * fN-1(n) + gN-1(n-1) */
  245. gnext =
  246. clip_q63_to_q31(((q63_t) gcurr +
  247. ((q31_t) (((q63_t) fnext * (*pk++)) >> 31))));
  248. /* Output samples */
  249. /* y(n) += gN(n) * vN */
  250. acc += ((q63_t) gnext * *pv++);
  251. /* write gN-1(n-1) into state for next sample processing */
  252. *px2++ = gnext;
  253. /* Update f values for next coefficient processing */
  254. fcurr = fnext;
  255. tapCnt--;
  256. }
  257. /* y(n) += g0(n) * v0 */
  258. acc += (q63_t) fnext *(
  259. *pv++);
  260. *px2++ = fnext;
  261. /* write out into pDst */
  262. *pDst++ = (q31_t) (acc >> 31u);
  263. /* Advance the state pointer by 1 to process the next group of samples */
  264. pState = pState + 1u;
  265. blkCnt--;
  266. }
  267. /* Processing is complete. Now copy last S->numStages samples to start of the buffer
  268. for the preperation of next frame process */
  269. /* Points to the start of the state buffer */
  270. pStateCurnt = &S->pState[0];
  271. pState = &S->pState[blockSize];
  272. tapCnt = numStages;
  273. /* Copy the remaining q31_t data */
  274. while(tapCnt > 0u)
  275. {
  276. *pStateCurnt++ = *pState++;
  277. /* Decrement the loop counter */
  278. tapCnt--;
  279. }
  280. #endif /* #ifndef ARM_MATH_CM0_FAMILY */
  281. }
  282. /**
  283. * @} end of IIR_Lattice group
  284. */