A bundled STM32F10x Std Periph and CMSIS library
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 

352 строки
11 KiB

  1. /* ----------------------------------------------------------------------
  2. * Copyright (C) 2010-2014 ARM Limited. All rights reserved.
  3. *
  4. * $Date: 12. March 2014
  5. * $Revision: V1.4.4
  6. *
  7. * Project: CMSIS DSP Library
  8. * Title: arm_fir_decimate_fast_q31.c
  9. *
  10. * Description: Fast Q31 FIR Decimator.
  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_decimate
  46. * @{
  47. */
  48. /**
  49. * @brief Processing function for the Q31 FIR decimator (fast variant) for Cortex-M3 and Cortex-M4.
  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. *
  58. * \par
  59. * This function is optimized for speed at the expense of fixed-point precision and overflow protection.
  60. * The result of each 1.31 x 1.31 multiplication is truncated to 2.30 format.
  61. * These intermediate results are added to a 2.30 accumulator.
  62. * Finally, the accumulator is saturated and converted to a 1.31 result.
  63. * 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.
  64. * 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).
  65. *
  66. * \par
  67. * Refer to the function <code>arm_fir_decimate_q31()</code> for a slower implementation of this function which uses a 64-bit accumulator to provide higher precision.
  68. * Both the slow and the fast versions use the same instance structure.
  69. * Use the function <code>arm_fir_decimate_init_q31()</code> to initialize the filter structure.
  70. */
  71. void arm_fir_decimate_fast_q31(
  72. arm_fir_decimate_instance_q31 * S,
  73. q31_t * pSrc,
  74. q31_t * pDst,
  75. uint32_t blockSize)
  76. {
  77. q31_t *pState = S->pState; /* State pointer */
  78. q31_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */
  79. q31_t *pStateCurnt; /* Points to the current sample of the state */
  80. q31_t x0, c0; /* Temporary variables to hold state and coefficient values */
  81. q31_t *px; /* Temporary pointers for state buffer */
  82. q31_t *pb; /* Temporary pointers for coefficient buffer */
  83. q31_t sum0; /* Accumulator */
  84. uint32_t numTaps = S->numTaps; /* Number of taps */
  85. uint32_t i, tapCnt, blkCnt, outBlockSize = blockSize / S->M; /* Loop counters */
  86. uint32_t blkCntN2;
  87. q31_t x1;
  88. q31_t acc0, acc1;
  89. q31_t *px0, *px1;
  90. /* S->pState buffer contains previous frame (numTaps - 1) samples */
  91. /* pStateCurnt points to the location where the new input data should be written */
  92. pStateCurnt = S->pState + (numTaps - 1u);
  93. /* Total number of output samples to be computed */
  94. blkCnt = outBlockSize / 2;
  95. blkCntN2 = outBlockSize - (2 * blkCnt);
  96. while(blkCnt > 0u)
  97. {
  98. /* Copy decimation factor number of new input samples into the state buffer */
  99. i = 2 * S->M;
  100. do
  101. {
  102. *pStateCurnt++ = *pSrc++;
  103. } while(--i);
  104. /* Set accumulator to zero */
  105. acc0 = 0;
  106. acc1 = 0;
  107. /* Initialize state pointer */
  108. px0 = pState;
  109. px1 = pState + S->M;
  110. /* Initialize coeff pointer */
  111. pb = pCoeffs;
  112. /* Loop unrolling. Process 4 taps at a time. */
  113. tapCnt = numTaps >> 2;
  114. /* Loop over the number of taps. Unroll by a factor of 4.
  115. ** Repeat until we've computed numTaps-4 coefficients. */
  116. while(tapCnt > 0u)
  117. {
  118. /* Read the b[numTaps-1] coefficient */
  119. c0 = *(pb);
  120. /* Read x[n-numTaps-1] for sample 0 sample 1 */
  121. x0 = *(px0);
  122. x1 = *(px1);
  123. /* Perform the multiply-accumulate */
  124. acc0 = (q31_t) ((((q63_t) acc0 << 32) + ((q63_t) x0 * c0)) >> 32);
  125. acc1 = (q31_t) ((((q63_t) acc1 << 32) + ((q63_t) x1 * c0)) >> 32);
  126. /* Read the b[numTaps-2] coefficient */
  127. c0 = *(pb + 1u);
  128. /* Read x[n-numTaps-2] for sample 0 sample 1 */
  129. x0 = *(px0 + 1u);
  130. x1 = *(px1 + 1u);
  131. /* Perform the multiply-accumulate */
  132. acc0 = (q31_t) ((((q63_t) acc0 << 32) + ((q63_t) x0 * c0)) >> 32);
  133. acc1 = (q31_t) ((((q63_t) acc1 << 32) + ((q63_t) x1 * c0)) >> 32);
  134. /* Read the b[numTaps-3] coefficient */
  135. c0 = *(pb + 2u);
  136. /* Read x[n-numTaps-3] for sample 0 sample 1 */
  137. x0 = *(px0 + 2u);
  138. x1 = *(px1 + 2u);
  139. pb += 4u;
  140. /* Perform the multiply-accumulate */
  141. acc0 = (q31_t) ((((q63_t) acc0 << 32) + ((q63_t) x0 * c0)) >> 32);
  142. acc1 = (q31_t) ((((q63_t) acc1 << 32) + ((q63_t) x1 * c0)) >> 32);
  143. /* Read the b[numTaps-4] coefficient */
  144. c0 = *(pb - 1u);
  145. /* Read x[n-numTaps-4] for sample 0 sample 1 */
  146. x0 = *(px0 + 3u);
  147. x1 = *(px1 + 3u);
  148. /* Perform the multiply-accumulate */
  149. acc0 = (q31_t) ((((q63_t) acc0 << 32) + ((q63_t) x0 * c0)) >> 32);
  150. acc1 = (q31_t) ((((q63_t) acc1 << 32) + ((q63_t) x1 * c0)) >> 32);
  151. /* update state pointers */
  152. px0 += 4u;
  153. px1 += 4u;
  154. /* Decrement the loop counter */
  155. tapCnt--;
  156. }
  157. /* If the filter length is not a multiple of 4, compute the remaining filter taps */
  158. tapCnt = numTaps % 0x4u;
  159. while(tapCnt > 0u)
  160. {
  161. /* Read coefficients */
  162. c0 = *(pb++);
  163. /* Fetch 1 state variable */
  164. x0 = *(px0++);
  165. x1 = *(px1++);
  166. /* Perform the multiply-accumulate */
  167. acc0 = (q31_t) ((((q63_t) acc0 << 32) + ((q63_t) x0 * c0)) >> 32);
  168. acc1 = (q31_t) ((((q63_t) acc1 << 32) + ((q63_t) x1 * c0)) >> 32);
  169. /* Decrement the loop counter */
  170. tapCnt--;
  171. }
  172. /* Advance the state pointer by the decimation factor
  173. * to process the next group of decimation factor number samples */
  174. pState = pState + S->M * 2;
  175. /* The result is in the accumulator, store in the destination buffer. */
  176. *pDst++ = (q31_t) (acc0 << 1);
  177. *pDst++ = (q31_t) (acc1 << 1);
  178. /* Decrement the loop counter */
  179. blkCnt--;
  180. }
  181. while(blkCntN2 > 0u)
  182. {
  183. /* Copy decimation factor number of new input samples into the state buffer */
  184. i = S->M;
  185. do
  186. {
  187. *pStateCurnt++ = *pSrc++;
  188. } while(--i);
  189. /* Set accumulator to zero */
  190. sum0 = 0;
  191. /* Initialize state pointer */
  192. px = pState;
  193. /* Initialize coeff pointer */
  194. pb = pCoeffs;
  195. /* Loop unrolling. Process 4 taps at a time. */
  196. tapCnt = numTaps >> 2;
  197. /* Loop over the number of taps. Unroll by a factor of 4.
  198. ** Repeat until we've computed numTaps-4 coefficients. */
  199. while(tapCnt > 0u)
  200. {
  201. /* Read the b[numTaps-1] coefficient */
  202. c0 = *(pb++);
  203. /* Read x[n-numTaps-1] sample */
  204. x0 = *(px++);
  205. /* Perform the multiply-accumulate */
  206. sum0 = (q31_t) ((((q63_t) sum0 << 32) + ((q63_t) x0 * c0)) >> 32);
  207. /* Read the b[numTaps-2] coefficient */
  208. c0 = *(pb++);
  209. /* Read x[n-numTaps-2] sample */
  210. x0 = *(px++);
  211. /* Perform the multiply-accumulate */
  212. sum0 = (q31_t) ((((q63_t) sum0 << 32) + ((q63_t) x0 * c0)) >> 32);
  213. /* Read the b[numTaps-3] coefficient */
  214. c0 = *(pb++);
  215. /* Read x[n-numTaps-3] sample */
  216. x0 = *(px++);
  217. /* Perform the multiply-accumulate */
  218. sum0 = (q31_t) ((((q63_t) sum0 << 32) + ((q63_t) x0 * c0)) >> 32);
  219. /* Read the b[numTaps-4] coefficient */
  220. c0 = *(pb++);
  221. /* Read x[n-numTaps-4] sample */
  222. x0 = *(px++);
  223. /* Perform the multiply-accumulate */
  224. sum0 = (q31_t) ((((q63_t) sum0 << 32) + ((q63_t) x0 * c0)) >> 32);
  225. /* Decrement the loop counter */
  226. tapCnt--;
  227. }
  228. /* If the filter length is not a multiple of 4, compute the remaining filter taps */
  229. tapCnt = numTaps % 0x4u;
  230. while(tapCnt > 0u)
  231. {
  232. /* Read coefficients */
  233. c0 = *(pb++);
  234. /* Fetch 1 state variable */
  235. x0 = *(px++);
  236. /* Perform the multiply-accumulate */
  237. sum0 = (q31_t) ((((q63_t) sum0 << 32) + ((q63_t) x0 * c0)) >> 32);
  238. /* Decrement the loop counter */
  239. tapCnt--;
  240. }
  241. /* Advance the state pointer by the decimation factor
  242. * to process the next group of decimation factor number samples */
  243. pState = pState + S->M;
  244. /* The result is in the accumulator, store in the destination buffer. */
  245. *pDst++ = (q31_t) (sum0 << 1);
  246. /* Decrement the loop counter */
  247. blkCntN2--;
  248. }
  249. /* Processing is complete.
  250. ** Now copy the last numTaps - 1 samples to the satrt of the state buffer.
  251. ** This prepares the state buffer for the next function call. */
  252. /* Points to the start of the state buffer */
  253. pStateCurnt = S->pState;
  254. i = (numTaps - 1u) >> 2u;
  255. /* copy data */
  256. while(i > 0u)
  257. {
  258. *pStateCurnt++ = *pState++;
  259. *pStateCurnt++ = *pState++;
  260. *pStateCurnt++ = *pState++;
  261. *pStateCurnt++ = *pState++;
  262. /* Decrement the loop counter */
  263. i--;
  264. }
  265. i = (numTaps - 1u) % 0x04u;
  266. /* copy data */
  267. while(i > 0u)
  268. {
  269. *pStateCurnt++ = *pState++;
  270. /* Decrement the loop counter */
  271. i--;
  272. }
  273. }
  274. /**
  275. * @} end of FIR_decimate group
  276. */