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_decimate_q31.c 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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_decimate_q31.c
  9. *
  10. * Description: Q31 FIR Decimator.
  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_decimate
  46. * @{
  47. */
  48. /**
  49. * @brief Processing function for the Q31 FIR decimator.
  50. * @param[in] *S points to an instance of the Q31 FIR decimator 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 input samples to process per call.
  54. * @return none
  55. *
  56. * <b>Scaling and Overflow Behavior:</b>
  57. * \par
  58. * The function is implemented using an internal 64-bit accumulator.
  59. * The accumulator has a 2.62 format and maintains full precision of the intermediate multiplication results but provides only a single guard bit.
  60. * Thus, if the accumulator result overflows it wraps around rather than clip.
  61. * In order to avoid overflows completely the input signal must be scaled down by log2(numTaps) bits (where log2 is read as log to the base 2).
  62. * After all multiply-accumulates are performed, the 2.62 accumulator is truncated to 1.32 format and then saturated to 1.31 format.
  63. *
  64. * \par
  65. * Refer to the function <code>arm_fir_decimate_fast_q31()</code> for a faster but less precise implementation of this function for Cortex-M3 and Cortex-M4.
  66. */
  67. void arm_fir_decimate_q31(
  68. const arm_fir_decimate_instance_q31 * S,
  69. q31_t * pSrc,
  70. q31_t * pDst,
  71. uint32_t blockSize)
  72. {
  73. q31_t *pState = S->pState; /* State pointer */
  74. q31_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */
  75. q31_t *pStateCurnt; /* Points to the current sample of the state */
  76. q31_t x0, c0; /* Temporary variables to hold state and coefficient values */
  77. q31_t *px; /* Temporary pointers for state buffer */
  78. q31_t *pb; /* Temporary pointers for coefficient buffer */
  79. q63_t sum0; /* Accumulator */
  80. uint32_t numTaps = S->numTaps; /* Number of taps */
  81. uint32_t i, tapCnt, blkCnt, outBlockSize = blockSize / S->M; /* Loop counters */
  82. #ifndef ARM_MATH_CM0_FAMILY
  83. /* Run the below code for Cortex-M4 and Cortex-M3 */
  84. /* S->pState buffer contains previous frame (numTaps - 1) samples */
  85. /* pStateCurnt points to the location where the new input data should be written */
  86. pStateCurnt = S->pState + (numTaps - 1u);
  87. /* Total number of output samples to be computed */
  88. blkCnt = outBlockSize;
  89. while(blkCnt > 0u)
  90. {
  91. /* Copy decimation factor number of new input samples into the state buffer */
  92. i = S->M;
  93. do
  94. {
  95. *pStateCurnt++ = *pSrc++;
  96. } while(--i);
  97. /* Set accumulator to zero */
  98. sum0 = 0;
  99. /* Initialize state pointer */
  100. px = pState;
  101. /* Initialize coeff pointer */
  102. pb = pCoeffs;
  103. /* Loop unrolling. Process 4 taps at a time. */
  104. tapCnt = numTaps >> 2;
  105. /* Loop over the number of taps. Unroll by a factor of 4.
  106. ** Repeat until we've computed numTaps-4 coefficients. */
  107. while(tapCnt > 0u)
  108. {
  109. /* Read the b[numTaps-1] coefficient */
  110. c0 = *(pb++);
  111. /* Read x[n-numTaps-1] sample */
  112. x0 = *(px++);
  113. /* Perform the multiply-accumulate */
  114. sum0 += (q63_t) x0 *c0;
  115. /* Read the b[numTaps-2] coefficient */
  116. c0 = *(pb++);
  117. /* Read x[n-numTaps-2] sample */
  118. x0 = *(px++);
  119. /* Perform the multiply-accumulate */
  120. sum0 += (q63_t) x0 *c0;
  121. /* Read the b[numTaps-3] coefficient */
  122. c0 = *(pb++);
  123. /* Read x[n-numTaps-3] sample */
  124. x0 = *(px++);
  125. /* Perform the multiply-accumulate */
  126. sum0 += (q63_t) x0 *c0;
  127. /* Read the b[numTaps-4] coefficient */
  128. c0 = *(pb++);
  129. /* Read x[n-numTaps-4] sample */
  130. x0 = *(px++);
  131. /* Perform the multiply-accumulate */
  132. sum0 += (q63_t) x0 *c0;
  133. /* Decrement the loop counter */
  134. tapCnt--;
  135. }
  136. /* If the filter length is not a multiple of 4, compute the remaining filter taps */
  137. tapCnt = numTaps % 0x4u;
  138. while(tapCnt > 0u)
  139. {
  140. /* Read coefficients */
  141. c0 = *(pb++);
  142. /* Fetch 1 state variable */
  143. x0 = *(px++);
  144. /* Perform the multiply-accumulate */
  145. sum0 += (q63_t) x0 *c0;
  146. /* Decrement the loop counter */
  147. tapCnt--;
  148. }
  149. /* Advance the state pointer by the decimation factor
  150. * to process the next group of decimation factor number samples */
  151. pState = pState + S->M;
  152. /* The result is in the accumulator, store in the destination buffer. */
  153. *pDst++ = (q31_t) (sum0 >> 31);
  154. /* Decrement the loop counter */
  155. blkCnt--;
  156. }
  157. /* Processing is complete.
  158. ** Now copy the last numTaps - 1 samples to the satrt of the state buffer.
  159. ** This prepares the state buffer for the next function call. */
  160. /* Points to the start of the state buffer */
  161. pStateCurnt = S->pState;
  162. i = (numTaps - 1u) >> 2u;
  163. /* copy data */
  164. while(i > 0u)
  165. {
  166. *pStateCurnt++ = *pState++;
  167. *pStateCurnt++ = *pState++;
  168. *pStateCurnt++ = *pState++;
  169. *pStateCurnt++ = *pState++;
  170. /* Decrement the loop counter */
  171. i--;
  172. }
  173. i = (numTaps - 1u) % 0x04u;
  174. /* copy data */
  175. while(i > 0u)
  176. {
  177. *pStateCurnt++ = *pState++;
  178. /* Decrement the loop counter */
  179. i--;
  180. }
  181. #else
  182. /* Run the below code for Cortex-M0 */
  183. /* S->pState buffer contains previous frame (numTaps - 1) samples */
  184. /* pStateCurnt points to the location where the new input data should be written */
  185. pStateCurnt = S->pState + (numTaps - 1u);
  186. /* Total number of output samples to be computed */
  187. blkCnt = outBlockSize;
  188. while(blkCnt > 0u)
  189. {
  190. /* Copy decimation factor number of new input samples into the state buffer */
  191. i = S->M;
  192. do
  193. {
  194. *pStateCurnt++ = *pSrc++;
  195. } while(--i);
  196. /* Set accumulator to zero */
  197. sum0 = 0;
  198. /* Initialize state pointer */
  199. px = pState;
  200. /* Initialize coeff pointer */
  201. pb = pCoeffs;
  202. tapCnt = numTaps;
  203. while(tapCnt > 0u)
  204. {
  205. /* Read coefficients */
  206. c0 = *pb++;
  207. /* Fetch 1 state variable */
  208. x0 = *px++;
  209. /* Perform the multiply-accumulate */
  210. sum0 += (q63_t) x0 *c0;
  211. /* Decrement the loop counter */
  212. tapCnt--;
  213. }
  214. /* Advance the state pointer by the decimation factor
  215. * to process the next group of decimation factor number samples */
  216. pState = pState + S->M;
  217. /* The result is in the accumulator, store in the destination buffer. */
  218. *pDst++ = (q31_t) (sum0 >> 31);
  219. /* Decrement the loop counter */
  220. blkCnt--;
  221. }
  222. /* Processing is complete.
  223. ** Now copy the last numTaps - 1 samples to the start of the state buffer.
  224. ** This prepares the state buffer for the next function call. */
  225. /* Points to the start of the state buffer */
  226. pStateCurnt = S->pState;
  227. i = numTaps - 1u;
  228. /* copy data */
  229. while(i > 0u)
  230. {
  231. *pStateCurnt++ = *pState++;
  232. /* Decrement the loop counter */
  233. i--;
  234. }
  235. #endif /* #ifndef ARM_MATH_CM0_FAMILY */
  236. }
  237. /**
  238. * @} end of FIR_decimate group
  239. */