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_fir_lattice_q31.c 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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_fir_lattice_q31.c
  9. *
  10. * Description: Q31 FIR 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 FIR_Lattice
  46. * @{
  47. */
  48. /**
  49. * @brief Processing function for the Q31 FIR lattice filter.
  50. * @param[in] *S points to an instance of the Q31 FIR 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. * In order to avoid overflows the input signal must be scaled down by 2*log2(numStages) bits.
  59. */
  60. #ifndef ARM_MATH_CM0_FAMILY
  61. /* Run the below code for Cortex-M4 and Cortex-M3 */
  62. void arm_fir_lattice_q31(
  63. const arm_fir_lattice_instance_q31 * S,
  64. q31_t * pSrc,
  65. q31_t * pDst,
  66. uint32_t blockSize)
  67. {
  68. q31_t *pState; /* State pointer */
  69. q31_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */
  70. q31_t *px; /* temporary state pointer */
  71. q31_t *pk; /* temporary coefficient pointer */
  72. q31_t fcurr1, fnext1, gcurr1 = 0, gnext1; /* temporary variables for first sample in loop unrolling */
  73. q31_t fcurr2, fnext2, gnext2; /* temporary variables for second sample in loop unrolling */
  74. uint32_t numStages = S->numStages; /* Length of the filter */
  75. uint32_t blkCnt, stageCnt; /* temporary variables for counts */
  76. q31_t k;
  77. pState = &S->pState[0];
  78. blkCnt = blockSize >> 1u;
  79. /* First part of the processing with loop unrolling. Compute 2 outputs at a time.
  80. a second loop below computes the remaining 1 sample. */
  81. while(blkCnt > 0u)
  82. {
  83. /* f0(n) = x(n) */
  84. fcurr1 = *pSrc++;
  85. /* f0(n) = x(n) */
  86. fcurr2 = *pSrc++;
  87. /* Initialize coeff pointer */
  88. pk = (pCoeffs);
  89. /* Initialize state pointer */
  90. px = pState;
  91. /* read g0(n - 1) from state buffer */
  92. gcurr1 = *px;
  93. /* Read the reflection coefficient */
  94. k = *pk++;
  95. /* for sample 1 processing */
  96. /* f1(n) = f0(n) + K1 * g0(n-1) */
  97. fnext1 = (q31_t) (((q63_t) gcurr1 * k) >> 32);
  98. /* g1(n) = f0(n) * K1 + g0(n-1) */
  99. gnext1 = (q31_t) (((q63_t) fcurr1 * (k)) >> 32);
  100. fnext1 = fcurr1 + (fnext1 << 1u);
  101. gnext1 = gcurr1 + (gnext1 << 1u);
  102. /* for sample 1 processing */
  103. /* f1(n) = f0(n) + K1 * g0(n-1) */
  104. fnext2 = (q31_t) (((q63_t) fcurr1 * k) >> 32);
  105. /* g1(n) = f0(n) * K1 + g0(n-1) */
  106. gnext2 = (q31_t) (((q63_t) fcurr2 * (k)) >> 32);
  107. fnext2 = fcurr2 + (fnext2 << 1u);
  108. gnext2 = fcurr1 + (gnext2 << 1u);
  109. /* save g1(n) in state buffer */
  110. *px++ = fcurr2;
  111. /* f1(n) is saved in fcurr1
  112. for next stage processing */
  113. fcurr1 = fnext1;
  114. fcurr2 = fnext2;
  115. stageCnt = (numStages - 1u);
  116. /* stage loop */
  117. while(stageCnt > 0u)
  118. {
  119. /* Read the reflection coefficient */
  120. k = *pk++;
  121. /* read g2(n) from state buffer */
  122. gcurr1 = *px;
  123. /* save g1(n) in state buffer */
  124. *px++ = gnext2;
  125. /* Sample processing for K2, K3.... */
  126. /* f2(n) = f1(n) + K2 * g1(n-1) */
  127. fnext1 = (q31_t) (((q63_t) gcurr1 * k) >> 32);
  128. fnext2 = (q31_t) (((q63_t) gnext1 * k) >> 32);
  129. fnext1 = fcurr1 + (fnext1 << 1u);
  130. fnext2 = fcurr2 + (fnext2 << 1u);
  131. /* g2(n) = f1(n) * K2 + g1(n-1) */
  132. gnext2 = (q31_t) (((q63_t) fcurr2 * (k)) >> 32);
  133. gnext2 = gnext1 + (gnext2 << 1u);
  134. /* g2(n) = f1(n) * K2 + g1(n-1) */
  135. gnext1 = (q31_t) (((q63_t) fcurr1 * (k)) >> 32);
  136. gnext1 = gcurr1 + (gnext1 << 1u);
  137. /* f1(n) is saved in fcurr1
  138. for next stage processing */
  139. fcurr1 = fnext1;
  140. fcurr2 = fnext2;
  141. stageCnt--;
  142. }
  143. /* y(n) = fN(n) */
  144. *pDst++ = fcurr1;
  145. *pDst++ = fcurr2;
  146. blkCnt--;
  147. }
  148. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  149. ** No loop unrolling is used. */
  150. blkCnt = blockSize % 0x2u;
  151. while(blkCnt > 0u)
  152. {
  153. /* f0(n) = x(n) */
  154. fcurr1 = *pSrc++;
  155. /* Initialize coeff pointer */
  156. pk = (pCoeffs);
  157. /* Initialize state pointer */
  158. px = pState;
  159. /* read g0(n - 1) from state buffer */
  160. gcurr1 = *px;
  161. /* Read the reflection coefficient */
  162. k = *pk++;
  163. /* for sample 1 processing */
  164. /* f1(n) = f0(n) + K1 * g0(n-1) */
  165. fnext1 = (q31_t) (((q63_t) gcurr1 * k) >> 32);
  166. fnext1 = fcurr1 + (fnext1 << 1u);
  167. /* g1(n) = f0(n) * K1 + g0(n-1) */
  168. gnext1 = (q31_t) (((q63_t) fcurr1 * (k)) >> 32);
  169. gnext1 = gcurr1 + (gnext1 << 1u);
  170. /* save g1(n) in state buffer */
  171. *px++ = fcurr1;
  172. /* f1(n) is saved in fcurr1
  173. for next stage processing */
  174. fcurr1 = fnext1;
  175. stageCnt = (numStages - 1u);
  176. /* stage loop */
  177. while(stageCnt > 0u)
  178. {
  179. /* Read the reflection coefficient */
  180. k = *pk++;
  181. /* read g2(n) from state buffer */
  182. gcurr1 = *px;
  183. /* save g1(n) in state buffer */
  184. *px++ = gnext1;
  185. /* Sample processing for K2, K3.... */
  186. /* f2(n) = f1(n) + K2 * g1(n-1) */
  187. fnext1 = (q31_t) (((q63_t) gcurr1 * k) >> 32);
  188. fnext1 = fcurr1 + (fnext1 << 1u);
  189. /* g2(n) = f1(n) * K2 + g1(n-1) */
  190. gnext1 = (q31_t) (((q63_t) fcurr1 * (k)) >> 32);
  191. gnext1 = gcurr1 + (gnext1 << 1u);
  192. /* f1(n) is saved in fcurr1
  193. for next stage processing */
  194. fcurr1 = fnext1;
  195. stageCnt--;
  196. }
  197. /* y(n) = fN(n) */
  198. *pDst++ = fcurr1;
  199. blkCnt--;
  200. }
  201. }
  202. #else
  203. /* Run the below code for Cortex-M0 */
  204. void arm_fir_lattice_q31(
  205. const arm_fir_lattice_instance_q31 * S,
  206. q31_t * pSrc,
  207. q31_t * pDst,
  208. uint32_t blockSize)
  209. {
  210. q31_t *pState; /* State pointer */
  211. q31_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */
  212. q31_t *px; /* temporary state pointer */
  213. q31_t *pk; /* temporary coefficient pointer */
  214. q31_t fcurr, fnext, gcurr, gnext; /* temporary variables */
  215. uint32_t numStages = S->numStages; /* Length of the filter */
  216. uint32_t blkCnt, stageCnt; /* temporary variables for counts */
  217. pState = &S->pState[0];
  218. blkCnt = blockSize;
  219. while(blkCnt > 0u)
  220. {
  221. /* f0(n) = x(n) */
  222. fcurr = *pSrc++;
  223. /* Initialize coeff pointer */
  224. pk = (pCoeffs);
  225. /* Initialize state pointer */
  226. px = pState;
  227. /* read g0(n-1) from state buffer */
  228. gcurr = *px;
  229. /* for sample 1 processing */
  230. /* f1(n) = f0(n) + K1 * g0(n-1) */
  231. fnext = (q31_t) (((q63_t) gcurr * (*pk)) >> 31) + fcurr;
  232. /* g1(n) = f0(n) * K1 + g0(n-1) */
  233. gnext = (q31_t) (((q63_t) fcurr * (*pk++)) >> 31) + gcurr;
  234. /* save g1(n) in state buffer */
  235. *px++ = fcurr;
  236. /* f1(n) is saved in fcurr1
  237. for next stage processing */
  238. fcurr = fnext;
  239. stageCnt = (numStages - 1u);
  240. /* stage loop */
  241. while(stageCnt > 0u)
  242. {
  243. /* read g2(n) from state buffer */
  244. gcurr = *px;
  245. /* save g1(n) in state buffer */
  246. *px++ = gnext;
  247. /* Sample processing for K2, K3.... */
  248. /* f2(n) = f1(n) + K2 * g1(n-1) */
  249. fnext = (q31_t) (((q63_t) gcurr * (*pk)) >> 31) + fcurr;
  250. /* g2(n) = f1(n) * K2 + g1(n-1) */
  251. gnext = (q31_t) (((q63_t) fcurr * (*pk++)) >> 31) + gcurr;
  252. /* f1(n) is saved in fcurr1
  253. for next stage processing */
  254. fcurr = fnext;
  255. stageCnt--;
  256. }
  257. /* y(n) = fN(n) */
  258. *pDst++ = fcurr;
  259. blkCnt--;
  260. }
  261. }
  262. #endif /* #ifndef ARM_MATH_CM0_FAMILY */
  263. /**
  264. * @} end of FIR_Lattice group
  265. */