Keyboard firmwares for Atmel AVR and Cortex-M
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

arm_fir_fast_q31.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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_fast_q31.c
  9. *
  10. * Description: Processing function for the Q31 Fast FIR filter.
  11. *
  12. * Target Processor: Cortex-M4/Cortex-M3
  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
  46. * @{
  47. */
  48. /**
  49. * @param[in] *S points to an instance of the Q31 structure.
  50. * @param[in] *pSrc points to the block of input data.
  51. * @param[out] *pDst points to the block output data.
  52. * @param[in] blockSize number of samples to process per call.
  53. * @return none.
  54. *
  55. * <b>Scaling and Overflow Behavior:</b>
  56. *
  57. * \par
  58. * This function is optimized for speed at the expense of fixed-point precision and overflow protection.
  59. * The result of each 1.31 x 1.31 multiplication is truncated to 2.30 format.
  60. * These intermediate results are added to a 2.30 accumulator.
  61. * Finally, the accumulator is saturated and converted to a 1.31 result.
  62. * The fast version has the same overflow behavior as the standard version and provides less precision since it discards the low 32 bits of each multiplication result.
  63. * In order to avoid overflows completely the input signal must be scaled down by log2(numTaps) bits.
  64. *
  65. * \par
  66. * Refer to the function <code>arm_fir_q31()</code> for a slower implementation of this function which uses a 64-bit accumulator to provide higher precision. Both the slow and the fast versions use the same instance structure.
  67. * Use the function <code>arm_fir_init_q31()</code> to initialize the filter structure.
  68. */
  69. IAR_ONLY_LOW_OPTIMIZATION_ENTER
  70. void arm_fir_fast_q31(
  71. const arm_fir_instance_q31 * S,
  72. q31_t * pSrc,
  73. q31_t * pDst,
  74. uint32_t blockSize)
  75. {
  76. q31_t *pState = S->pState; /* State pointer */
  77. q31_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */
  78. q31_t *pStateCurnt; /* Points to the current sample of the state */
  79. q31_t x0, x1, x2, x3; /* Temporary variables to hold state */
  80. q31_t c0; /* Temporary variable to hold coefficient value */
  81. q31_t *px; /* Temporary pointer for state */
  82. q31_t *pb; /* Temporary pointer for coefficient buffer */
  83. q31_t acc0, acc1, acc2, acc3; /* Accumulators */
  84. uint32_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */
  85. uint32_t i, tapCnt, blkCnt; /* Loop counters */
  86. /* S->pState points to buffer which contains previous frame (numTaps - 1) samples */
  87. /* pStateCurnt points to the location where the new input data should be written */
  88. pStateCurnt = &(S->pState[(numTaps - 1u)]);
  89. /* Apply loop unrolling and compute 4 output values simultaneously.
  90. * The variables acc0 ... acc3 hold output values that are being computed:
  91. *
  92. * acc0 = b[numTaps-1] * x[n-numTaps-1] + b[numTaps-2] * x[n-numTaps-2] + b[numTaps-3] * x[n-numTaps-3] +...+ b[0] * x[0]
  93. * acc1 = b[numTaps-1] * x[n-numTaps] + b[numTaps-2] * x[n-numTaps-1] + b[numTaps-3] * x[n-numTaps-2] +...+ b[0] * x[1]
  94. * acc2 = b[numTaps-1] * x[n-numTaps+1] + b[numTaps-2] * x[n-numTaps] + b[numTaps-3] * x[n-numTaps-1] +...+ b[0] * x[2]
  95. * acc3 = b[numTaps-1] * x[n-numTaps+2] + b[numTaps-2] * x[n-numTaps+1] + b[numTaps-3] * x[n-numTaps] +...+ b[0] * x[3]
  96. */
  97. blkCnt = blockSize >> 2;
  98. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  99. ** a second loop below computes the remaining 1 to 3 samples. */
  100. while(blkCnt > 0u)
  101. {
  102. /* Copy four new input samples into the state buffer */
  103. *pStateCurnt++ = *pSrc++;
  104. *pStateCurnt++ = *pSrc++;
  105. *pStateCurnt++ = *pSrc++;
  106. *pStateCurnt++ = *pSrc++;
  107. /* Set all accumulators to zero */
  108. acc0 = 0;
  109. acc1 = 0;
  110. acc2 = 0;
  111. acc3 = 0;
  112. /* Initialize state pointer */
  113. px = pState;
  114. /* Initialize coefficient pointer */
  115. pb = pCoeffs;
  116. /* Read the first three samples from the state buffer:
  117. * x[n-numTaps], x[n-numTaps-1], x[n-numTaps-2] */
  118. x0 = *(px++);
  119. x1 = *(px++);
  120. x2 = *(px++);
  121. /* Loop unrolling. Process 4 taps at a time. */
  122. tapCnt = numTaps >> 2;
  123. i = tapCnt;
  124. while(i > 0u)
  125. {
  126. /* Read the b[numTaps] coefficient */
  127. c0 = *(pb++);
  128. /* Read x[n-numTaps-3] sample */
  129. x3 = *(px++);
  130. /* acc0 += b[numTaps] * x[n-numTaps] */
  131. multAcc_32x32_keep32_R(acc0, x0, c0);
  132. /* acc1 += b[numTaps] * x[n-numTaps-1] */
  133. multAcc_32x32_keep32_R(acc1, x1, c0);
  134. /* acc2 += b[numTaps] * x[n-numTaps-2] */
  135. multAcc_32x32_keep32_R(acc2, x2, c0);
  136. /* acc3 += b[numTaps] * x[n-numTaps-3] */
  137. multAcc_32x32_keep32_R(acc3, x3, c0);
  138. /* Read the b[numTaps-1] coefficient */
  139. c0 = *(pb++);
  140. /* Read x[n-numTaps-4] sample */
  141. x0 = *(px++);
  142. /* Perform the multiply-accumulates */
  143. multAcc_32x32_keep32_R(acc0, x1, c0);
  144. multAcc_32x32_keep32_R(acc1, x2, c0);
  145. multAcc_32x32_keep32_R(acc2, x3, c0);
  146. multAcc_32x32_keep32_R(acc3, x0, c0);
  147. /* Read the b[numTaps-2] coefficient */
  148. c0 = *(pb++);
  149. /* Read x[n-numTaps-5] sample */
  150. x1 = *(px++);
  151. /* Perform the multiply-accumulates */
  152. multAcc_32x32_keep32_R(acc0, x2, c0);
  153. multAcc_32x32_keep32_R(acc1, x3, c0);
  154. multAcc_32x32_keep32_R(acc2, x0, c0);
  155. multAcc_32x32_keep32_R(acc3, x1, c0);
  156. /* Read the b[numTaps-3] coefficients */
  157. c0 = *(pb++);
  158. /* Read x[n-numTaps-6] sample */
  159. x2 = *(px++);
  160. /* Perform the multiply-accumulates */
  161. multAcc_32x32_keep32_R(acc0, x3, c0);
  162. multAcc_32x32_keep32_R(acc1, x0, c0);
  163. multAcc_32x32_keep32_R(acc2, x1, c0);
  164. multAcc_32x32_keep32_R(acc3, x2, c0);
  165. i--;
  166. }
  167. /* If the filter length is not a multiple of 4, compute the remaining filter taps */
  168. i = numTaps - (tapCnt * 4u);
  169. while(i > 0u)
  170. {
  171. /* Read coefficients */
  172. c0 = *(pb++);
  173. /* Fetch 1 state variable */
  174. x3 = *(px++);
  175. /* Perform the multiply-accumulates */
  176. multAcc_32x32_keep32_R(acc0, x0, c0);
  177. multAcc_32x32_keep32_R(acc1, x1, c0);
  178. multAcc_32x32_keep32_R(acc2, x2, c0);
  179. multAcc_32x32_keep32_R(acc3, x3, c0);
  180. /* Reuse the present sample states for next sample */
  181. x0 = x1;
  182. x1 = x2;
  183. x2 = x3;
  184. /* Decrement the loop counter */
  185. i--;
  186. }
  187. /* Advance the state pointer by 4 to process the next group of 4 samples */
  188. pState = pState + 4;
  189. /* The results in the 4 accumulators are in 2.30 format. Convert to 1.31
  190. ** Then store the 4 outputs in the destination buffer. */
  191. *pDst++ = (q31_t) (acc0 << 1);
  192. *pDst++ = (q31_t) (acc1 << 1);
  193. *pDst++ = (q31_t) (acc2 << 1);
  194. *pDst++ = (q31_t) (acc3 << 1);
  195. /* Decrement the samples loop counter */
  196. blkCnt--;
  197. }
  198. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  199. ** No loop unrolling is used. */
  200. blkCnt = blockSize % 4u;
  201. while(blkCnt > 0u)
  202. {
  203. /* Copy one sample at a time into state buffer */
  204. *pStateCurnt++ = *pSrc++;
  205. /* Set the accumulator to zero */
  206. acc0 = 0;
  207. /* Initialize state pointer */
  208. px = pState;
  209. /* Initialize Coefficient pointer */
  210. pb = (pCoeffs);
  211. i = numTaps;
  212. /* Perform the multiply-accumulates */
  213. do
  214. {
  215. multAcc_32x32_keep32_R(acc0, (*px++), (*(pb++)));
  216. i--;
  217. } while(i > 0u);
  218. /* The result is in 2.30 format. Convert to 1.31
  219. ** Then store the output in the destination buffer. */
  220. *pDst++ = (q31_t) (acc0 << 1);
  221. /* Advance state pointer by 1 for the next sample */
  222. pState = pState + 1;
  223. /* Decrement the samples loop counter */
  224. blkCnt--;
  225. }
  226. /* Processing is complete.
  227. ** Now copy the last numTaps - 1 samples to the start of the state buffer.
  228. ** This prepares the state buffer for the next function call. */
  229. /* Points to the start of the state buffer */
  230. pStateCurnt = S->pState;
  231. /* Calculate remaining number of copies */
  232. tapCnt = (numTaps - 1u);
  233. /* Copy the remaining q31_t data */
  234. while(tapCnt > 0u)
  235. {
  236. *pStateCurnt++ = *pState++;
  237. /* Decrement the loop counter */
  238. tapCnt--;
  239. }
  240. }
  241. IAR_ONLY_LOW_OPTIMIZATION_EXIT
  242. /**
  243. * @} end of FIR group
  244. */