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_q7.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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_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++);
  136. /* Read x[n-numTaps-4] sample */
  137. x0 = *(px++);
  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++);
  145. /* Read x[n-numTaps-5] sample */
  146. x1 = *(px++);
  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++);
  154. /* Read x[n-numTaps-6] sample */
  155. x2 = *(px++);
  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. i--;
  162. }
  163. /* If the filter length is not a multiple of 4, compute the remaining filter taps */
  164. i = numTaps - (tapCnt * 4u);
  165. while(i > 0u)
  166. {
  167. /* Read coefficients */
  168. c0 = *(pb++);
  169. /* Fetch 1 state variable */
  170. x3 = *(px++);
  171. /* Perform the multiply-accumulates */
  172. acc0 += ((q15_t) x0 * c0);
  173. acc1 += ((q15_t) x1 * c0);
  174. acc2 += ((q15_t) x2 * c0);
  175. acc3 += ((q15_t) x3 * c0);
  176. /* Reuse the present sample states for next sample */
  177. x0 = x1;
  178. x1 = x2;
  179. x2 = x3;
  180. /* Decrement the loop counter */
  181. i--;
  182. }
  183. /* Advance the state pointer by 4 to process the next group of 4 samples */
  184. pState = pState + 4;
  185. /* The results in the 4 accumulators are in 2.62 format. Convert to 1.31
  186. ** Then store the 4 outputs in the destination buffer. */
  187. acc0 = __SSAT((acc0 >> 7u), 8);
  188. *pDst++ = acc0;
  189. acc1 = __SSAT((acc1 >> 7u), 8);
  190. *pDst++ = acc1;
  191. acc2 = __SSAT((acc2 >> 7u), 8);
  192. *pDst++ = acc2;
  193. acc3 = __SSAT((acc3 >> 7u), 8);
  194. *pDst++ = acc3;
  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. acc0 += (q15_t) * (px++) * (*(pb++));
  216. i--;
  217. } while(i > 0u);
  218. /* The result is in 2.14 format. Convert to 1.7
  219. ** Then store the output in the destination buffer. */
  220. *pDst++ = __SSAT((acc0 >> 7u), 8);
  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 satrt 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. tapCnt = (numTaps - 1u) >> 2u;
  232. /* copy data */
  233. while(tapCnt > 0u)
  234. {
  235. *pStateCurnt++ = *pState++;
  236. *pStateCurnt++ = *pState++;
  237. *pStateCurnt++ = *pState++;
  238. *pStateCurnt++ = *pState++;
  239. /* Decrement the loop counter */
  240. tapCnt--;
  241. }
  242. /* Calculate remaining number of copies */
  243. tapCnt = (numTaps - 1u) % 0x4u;
  244. /* Copy the remaining q31_t data */
  245. while(tapCnt > 0u)
  246. {
  247. *pStateCurnt++ = *pState++;
  248. /* Decrement the loop counter */
  249. tapCnt--;
  250. }
  251. #else
  252. /* Run the below code for Cortex-M0 */
  253. uint32_t numTaps = S->numTaps; /* Number of taps in the filter */
  254. uint32_t i, blkCnt; /* Loop counters */
  255. q7_t *pState = S->pState; /* State pointer */
  256. q7_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */
  257. q7_t *px, *pb; /* Temporary pointers to state and coeff */
  258. q31_t acc = 0; /* Accumlator */
  259. q7_t *pStateCurnt; /* Points to the current sample of the state */
  260. /* S->pState points to state array which contains previous frame (numTaps - 1) samples */
  261. /* pStateCurnt points to the location where the new input data should be written */
  262. pStateCurnt = S->pState + (numTaps - 1u);
  263. /* Initialize blkCnt with blockSize */
  264. blkCnt = blockSize;
  265. /* Perform filtering upto BlockSize - BlockSize%4 */
  266. while(blkCnt > 0u)
  267. {
  268. /* Copy one sample at a time into state buffer */
  269. *pStateCurnt++ = *pSrc++;
  270. /* Set accumulator to zero */
  271. acc = 0;
  272. /* Initialize state pointer of type q7 */
  273. px = pState;
  274. /* Initialize coeff pointer of type q7 */
  275. pb = pCoeffs;
  276. i = numTaps;
  277. while(i > 0u)
  278. {
  279. /* 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] */
  280. acc += (q15_t) * px++ * *pb++;
  281. i--;
  282. }
  283. /* Store the 1.7 format filter output in destination buffer */
  284. *pDst++ = (q7_t) __SSAT((acc >> 7), 8);
  285. /* Advance the state pointer by 1 to process the next sample */
  286. pState = pState + 1;
  287. /* Decrement the loop counter */
  288. blkCnt--;
  289. }
  290. /* Processing is complete.
  291. ** Now copy the last numTaps - 1 samples to the satrt of the state buffer.
  292. ** This prepares the state buffer for the next function call. */
  293. /* Points to the start of the state buffer */
  294. pStateCurnt = S->pState;
  295. /* Copy numTaps number of values */
  296. i = (numTaps - 1u);
  297. /* Copy q7_t data */
  298. while(i > 0u)
  299. {
  300. *pStateCurnt++ = *pState++;
  301. i--;
  302. }
  303. #endif /* #ifndef ARM_MATH_CM0_FAMILY */
  304. }
  305. /**
  306. * @} end of FIR group
  307. */