PageRenderTime 54ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/dsp/cmsis_dsp/FilteringFunctions/arm_fir_q7.c

https://gitlab.com/YaoQ/mbed-for-linknode
C | 397 lines | 160 code | 86 blank | 151 comment | 0 complexity | b4e302e33fed573619ef5e0a83c265aa 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_fir_q7.c
  9. *
  10. * Description: Q7 FIR 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
  46. * @{
  47. */
  48. /**
  49. * @param[in] *S points to an instance of the Q7 FIR filter structure.
  50. * @param[in] *pSrc points to the block of input data.
  51. * @param[out] *pDst points to the block of 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. * \par
  57. * The function is implemented using a 32-bit internal accumulator.
  58. * Both coefficients and state variables are represented in 1.7 format and multiplications yield a 2.14 result.
  59. * The 2.14 intermediate results are accumulated in a 32-bit accumulator in 18.14 format.
  60. * There is no risk of internal overflow with this approach and the full precision of intermediate multiplications is preserved.
  61. * The accumulator is converted to 18.7 format by discarding the low 7 bits.
  62. * Finally, the result is truncated to 1.7 format.
  63. */
  64. void arm_fir_q7(
  65. const arm_fir_instance_q7 * S,
  66. q7_t * pSrc,
  67. q7_t * pDst,
  68. uint32_t blockSize)
  69. {
  70. #ifndef ARM_MATH_CM0_FAMILY
  71. /* Run the below code for Cortex-M4 and Cortex-M3 */
  72. q7_t *pState = S->pState; /* State pointer */
  73. q7_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */
  74. q7_t *pStateCurnt; /* Points to the current sample of the state */
  75. q7_t x0, x1, x2, x3; /* Temporary variables to hold state */
  76. q7_t c0; /* Temporary variable to hold coefficient value */
  77. q7_t *px; /* Temporary pointer for state */
  78. q7_t *pb; /* Temporary pointer for coefficient buffer */
  79. q31_t acc0, acc1, acc2, acc3; /* Accumulators */
  80. uint32_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */
  81. uint32_t i, tapCnt, blkCnt; /* Loop counters */
  82. /* S->pState points to state array which contains previous frame (numTaps - 1) samples */
  83. /* pStateCurnt points to the location where the new input data should be written */
  84. pStateCurnt = &(S->pState[(numTaps - 1u)]);
  85. /* Apply loop unrolling and compute 4 output values simultaneously.
  86. * The variables acc0 ... acc3 hold output values that are being computed:
  87. *
  88. * 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]
  89. * 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]
  90. * 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]
  91. * 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]
  92. */
  93. blkCnt = blockSize >> 2;
  94. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  95. ** a second loop below computes the remaining 1 to 3 samples. */
  96. while(blkCnt > 0u)
  97. {
  98. /* Copy four new input samples into the state buffer */
  99. *pStateCurnt++ = *pSrc++;
  100. *pStateCurnt++ = *pSrc++;
  101. *pStateCurnt++ = *pSrc++;
  102. *pStateCurnt++ = *pSrc++;
  103. /* Set all accumulators to zero */
  104. acc0 = 0;
  105. acc1 = 0;
  106. acc2 = 0;
  107. acc3 = 0;
  108. /* Initialize state pointer */
  109. px = pState;
  110. /* Initialize coefficient pointer */
  111. pb = pCoeffs;
  112. /* Read the first three samples from the state buffer:
  113. * x[n-numTaps], x[n-numTaps-1], x[n-numTaps-2] */
  114. x0 = *(px++);
  115. x1 = *(px++);
  116. x2 = *(px++);
  117. /* Loop unrolling. Process 4 taps at a time. */
  118. tapCnt = numTaps >> 2;
  119. i = tapCnt;
  120. while(i > 0u)
  121. {
  122. /* Read the b[numTaps] coefficient */
  123. c0 = *pb;
  124. /* Read x[n-numTaps-3] sample */
  125. x3 = *px;
  126. /* acc0 += b[numTaps] * x[n-numTaps] */
  127. acc0 += ((q15_t) x0 * c0);
  128. /* acc1 += b[numTaps] * x[n-numTaps-1] */
  129. acc1 += ((q15_t) x1 * c0);
  130. /* acc2 += b[numTaps] * x[n-numTaps-2] */
  131. acc2 += ((q15_t) x2 * c0);
  132. /* acc3 += b[numTaps] * x[n-numTaps-3] */
  133. acc3 += ((q15_t) x3 * c0);
  134. /* Read the b[numTaps-1] coefficient */
  135. c0 = *(pb + 1u);
  136. /* Read x[n-numTaps-4] sample */
  137. x0 = *(px + 1u);
  138. /* Perform the multiply-accumulates */
  139. acc0 += ((q15_t) x1 * c0);
  140. acc1 += ((q15_t) x2 * c0);
  141. acc2 += ((q15_t) x3 * c0);
  142. acc3 += ((q15_t) x0 * c0);
  143. /* Read the b[numTaps-2] coefficient */
  144. c0 = *(pb + 2u);
  145. /* Read x[n-numTaps-5] sample */
  146. x1 = *(px + 2u);
  147. /* Perform the multiply-accumulates */
  148. acc0 += ((q15_t) x2 * c0);
  149. acc1 += ((q15_t) x3 * c0);
  150. acc2 += ((q15_t) x0 * c0);
  151. acc3 += ((q15_t) x1 * c0);
  152. /* Read the b[numTaps-3] coefficients */
  153. c0 = *(pb + 3u);
  154. /* Read x[n-numTaps-6] sample */
  155. x2 = *(px + 3u);
  156. /* Perform the multiply-accumulates */
  157. acc0 += ((q15_t) x3 * c0);
  158. acc1 += ((q15_t) x0 * c0);
  159. acc2 += ((q15_t) x1 * c0);
  160. acc3 += ((q15_t) x2 * c0);
  161. /* update coefficient pointer */
  162. pb += 4u;
  163. px += 4u;
  164. /* Decrement the loop counter */
  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. acc0 += ((q15_t) x0 * c0);
  177. acc1 += ((q15_t) x1 * c0);
  178. acc2 += ((q15_t) x2 * c0);
  179. acc3 += ((q15_t) 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.62 format. Convert to 1.31
  190. ** Then store the 4 outputs in the destination buffer. */
  191. acc0 = __SSAT((acc0 >> 7u), 8);
  192. *pDst++ = acc0;
  193. acc1 = __SSAT((acc1 >> 7u), 8);
  194. *pDst++ = acc1;
  195. acc2 = __SSAT((acc2 >> 7u), 8);
  196. *pDst++ = acc2;
  197. acc3 = __SSAT((acc3 >> 7u), 8);
  198. *pDst++ = acc3;
  199. /* Decrement the samples loop counter */
  200. blkCnt--;
  201. }
  202. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  203. ** No loop unrolling is used. */
  204. blkCnt = blockSize % 4u;
  205. while(blkCnt > 0u)
  206. {
  207. /* Copy one sample at a time into state buffer */
  208. *pStateCurnt++ = *pSrc++;
  209. /* Set the accumulator to zero */
  210. acc0 = 0;
  211. /* Initialize state pointer */
  212. px = pState;
  213. /* Initialize Coefficient pointer */
  214. pb = (pCoeffs);
  215. i = numTaps;
  216. /* Perform the multiply-accumulates */
  217. do
  218. {
  219. acc0 += (q15_t) * (px++) * (*(pb++));
  220. i--;
  221. } while(i > 0u);
  222. /* The result is in 2.14 format. Convert to 1.7
  223. ** Then store the output in the destination buffer. */
  224. *pDst++ = __SSAT((acc0 >> 7u), 8);
  225. /* Advance state pointer by 1 for the next sample */
  226. pState = pState + 1;
  227. /* Decrement the samples loop counter */
  228. blkCnt--;
  229. }
  230. /* Processing is complete.
  231. ** Now copy the last numTaps - 1 samples to the satrt of the state buffer.
  232. ** This prepares the state buffer for the next function call. */
  233. /* Points to the start of the state buffer */
  234. pStateCurnt = S->pState;
  235. tapCnt = (numTaps - 1u) >> 2u;
  236. /* copy data */
  237. while(tapCnt > 0u)
  238. {
  239. *pStateCurnt++ = *pState++;
  240. *pStateCurnt++ = *pState++;
  241. *pStateCurnt++ = *pState++;
  242. *pStateCurnt++ = *pState++;
  243. /* Decrement the loop counter */
  244. tapCnt--;
  245. }
  246. /* Calculate remaining number of copies */
  247. tapCnt = (numTaps - 1u) % 0x4u;
  248. /* Copy the remaining q31_t data */
  249. while(tapCnt > 0u)
  250. {
  251. *pStateCurnt++ = *pState++;
  252. /* Decrement the loop counter */
  253. tapCnt--;
  254. }
  255. #else
  256. /* Run the below code for Cortex-M0 */
  257. uint32_t numTaps = S->numTaps; /* Number of taps in the filter */
  258. uint32_t i, blkCnt; /* Loop counters */
  259. q7_t *pState = S->pState; /* State pointer */
  260. q7_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */
  261. q7_t *px, *pb; /* Temporary pointers to state and coeff */
  262. q31_t acc = 0; /* Accumlator */
  263. q7_t *pStateCurnt; /* Points to the current sample of the state */
  264. /* S->pState points to state array which contains previous frame (numTaps - 1) samples */
  265. /* pStateCurnt points to the location where the new input data should be written */
  266. pStateCurnt = S->pState + (numTaps - 1u);
  267. /* Initialize blkCnt with blockSize */
  268. blkCnt = blockSize;
  269. /* Perform filtering upto BlockSize - BlockSize%4 */
  270. while(blkCnt > 0u)
  271. {
  272. /* Copy one sample at a time into state buffer */
  273. *pStateCurnt++ = *pSrc++;
  274. /* Set accumulator to zero */
  275. acc = 0;
  276. /* Initialize state pointer of type q7 */
  277. px = pState;
  278. /* Initialize coeff pointer of type q7 */
  279. pb = pCoeffs;
  280. i = numTaps;
  281. while(i > 0u)
  282. {
  283. /* acc = 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] */
  284. acc += (q15_t) * px++ * *pb++;
  285. i--;
  286. }
  287. /* Store the 1.7 format filter output in destination buffer */
  288. *pDst++ = (q7_t) __SSAT((acc >> 7), 8);
  289. /* Advance the state pointer by 1 to process the next sample */
  290. pState = pState + 1;
  291. /* Decrement the loop counter */
  292. blkCnt--;
  293. }
  294. /* Processing is complete.
  295. ** Now copy the last numTaps - 1 samples to the satrt of the state buffer.
  296. ** This prepares the state buffer for the next function call. */
  297. /* Points to the start of the state buffer */
  298. pStateCurnt = S->pState;
  299. /* Copy numTaps number of values */
  300. i = (numTaps - 1u);
  301. /* Copy q7_t data */
  302. while(i > 0u)
  303. {
  304. *pStateCurnt++ = *pState++;
  305. i--;
  306. }
  307. #endif /* #ifndef ARM_MATH_CM0_FAMILY */
  308. }
  309. /**
  310. * @} end of FIR group
  311. */