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_sparse_f32.c 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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_sparse_f32.c
  9. *
  10. * Description: Floating-point sparse 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. * @defgroup FIR_Sparse Finite Impulse Response (FIR) Sparse Filters
  46. *
  47. * This group of functions implements sparse FIR filters.
  48. * Sparse FIR filters are equivalent to standard FIR filters except that most of the coefficients are equal to zero.
  49. * Sparse filters are used for simulating reflections in communications and audio applications.
  50. *
  51. * There are separate functions for Q7, Q15, Q31, and floating-point data types.
  52. * The functions operate on blocks of input and output data and each call to the function processes
  53. * <code>blockSize</code> samples through the filter. <code>pSrc</code> and
  54. * <code>pDst</code> points to input and output arrays respectively containing <code>blockSize</code> values.
  55. *
  56. * \par Algorithm:
  57. * The sparse filter instant structure contains an array of tap indices <code>pTapDelay</code> which specifies the locations of the non-zero coefficients.
  58. * This is in addition to the coefficient array <code>b</code>.
  59. * The implementation essentially skips the multiplications by zero and leads to an efficient realization.
  60. * <pre>
  61. * y[n] = b[0] * x[n-pTapDelay[0]] + b[1] * x[n-pTapDelay[1]] + b[2] * x[n-pTapDelay[2]] + ...+ b[numTaps-1] * x[n-pTapDelay[numTaps-1]]
  62. * </pre>
  63. * \par
  64. * \image html FIRSparse.gif "Sparse FIR filter. b[n] represents the filter coefficients"
  65. * \par
  66. * <code>pCoeffs</code> points to a coefficient array of size <code>numTaps</code>;
  67. * <code>pTapDelay</code> points to an array of nonzero indices and is also of size <code>numTaps</code>;
  68. * <code>pState</code> points to a state array of size <code>maxDelay + blockSize</code>, where
  69. * <code>maxDelay</code> is the largest offset value that is ever used in the <code>pTapDelay</code> array.
  70. * Some of the processing functions also require temporary working buffers.
  71. *
  72. * \par Instance Structure
  73. * The coefficients and state variables for a filter are stored together in an instance data structure.
  74. * A separate instance structure must be defined for each filter.
  75. * Coefficient and offset arrays may be shared among several instances while state variable arrays cannot be shared.
  76. * There are separate instance structure declarations for each of the 4 supported data types.
  77. *
  78. * \par Initialization Functions
  79. * There is also an associated initialization function for each data type.
  80. * The initialization function performs the following operations:
  81. * - Sets the values of the internal structure fields.
  82. * - Zeros out the values in the state buffer.
  83. * To do this manually without calling the init function, assign the follow subfields of the instance structure:
  84. * numTaps, pCoeffs, pTapDelay, maxDelay, stateIndex, pState. Also set all of the values in pState to zero.
  85. *
  86. * \par
  87. * Use of the initialization function is optional.
  88. * However, if the initialization function is used, then the instance structure cannot be placed into a const data section.
  89. * To place an instance structure into a const data section, the instance structure must be manually initialized.
  90. * Set the values in the state buffer to zeros before static initialization.
  91. * The code below statically initializes each of the 4 different data type filter instance structures
  92. * <pre>
  93. *arm_fir_sparse_instance_f32 S = {numTaps, 0, pState, pCoeffs, maxDelay, pTapDelay};
  94. *arm_fir_sparse_instance_q31 S = {numTaps, 0, pState, pCoeffs, maxDelay, pTapDelay};
  95. *arm_fir_sparse_instance_q15 S = {numTaps, 0, pState, pCoeffs, maxDelay, pTapDelay};
  96. *arm_fir_sparse_instance_q7 S = {numTaps, 0, pState, pCoeffs, maxDelay, pTapDelay};
  97. * </pre>
  98. * \par
  99. *
  100. * \par Fixed-Point Behavior
  101. * Care must be taken when using the fixed-point versions of the sparse FIR filter functions.
  102. * In particular, the overflow and saturation behavior of the accumulator used in each function must be considered.
  103. * Refer to the function specific documentation below for usage guidelines.
  104. */
  105. /**
  106. * @addtogroup FIR_Sparse
  107. * @{
  108. */
  109. /**
  110. * @brief Processing function for the floating-point sparse FIR filter.
  111. * @param[in] *S points to an instance of the floating-point sparse FIR structure.
  112. * @param[in] *pSrc points to the block of input data.
  113. * @param[out] *pDst points to the block of output data
  114. * @param[in] *pScratchIn points to a temporary buffer of size blockSize.
  115. * @param[in] blockSize number of input samples to process per call.
  116. * @return none.
  117. */
  118. void arm_fir_sparse_f32(
  119. arm_fir_sparse_instance_f32 * S,
  120. float32_t * pSrc,
  121. float32_t * pDst,
  122. float32_t * pScratchIn,
  123. uint32_t blockSize)
  124. {
  125. float32_t *pState = S->pState; /* State pointer */
  126. float32_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */
  127. float32_t *px; /* Scratch buffer pointer */
  128. float32_t *py = pState; /* Temporary pointers for state buffer */
  129. float32_t *pb = pScratchIn; /* Temporary pointers for scratch buffer */
  130. float32_t *pOut; /* Destination pointer */
  131. int32_t *pTapDelay = S->pTapDelay; /* Pointer to the array containing offset of the non-zero tap values. */
  132. uint32_t delaySize = S->maxDelay + blockSize; /* state length */
  133. uint16_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */
  134. int32_t readIndex; /* Read index of the state buffer */
  135. uint32_t tapCnt, blkCnt; /* loop counters */
  136. float32_t coeff = *pCoeffs++; /* Read the first coefficient value */
  137. /* BlockSize of Input samples are copied into the state buffer */
  138. /* StateIndex points to the starting position to write in the state buffer */
  139. arm_circularWrite_f32((int32_t *) py, delaySize, &S->stateIndex, 1,
  140. (int32_t *) pSrc, 1, blockSize);
  141. /* Read Index, from where the state buffer should be read, is calculated. */
  142. readIndex = ((int32_t) S->stateIndex - (int32_t) blockSize) - *pTapDelay++;
  143. /* Wraparound of readIndex */
  144. if(readIndex < 0)
  145. {
  146. readIndex += (int32_t) delaySize;
  147. }
  148. /* Working pointer for state buffer is updated */
  149. py = pState;
  150. /* blockSize samples are read from the state buffer */
  151. arm_circularRead_f32((int32_t *) py, delaySize, &readIndex, 1,
  152. (int32_t *) pb, (int32_t *) pb, blockSize, 1,
  153. blockSize);
  154. /* Working pointer for the scratch buffer */
  155. px = pb;
  156. /* Working pointer for destination buffer */
  157. pOut = pDst;
  158. #ifndef ARM_MATH_CM0_FAMILY
  159. /* Run the below code for Cortex-M4 and Cortex-M3 */
  160. /* Loop over the blockSize. Unroll by a factor of 4.
  161. * Compute 4 Multiplications at a time. */
  162. blkCnt = blockSize >> 2u;
  163. while(blkCnt > 0u)
  164. {
  165. /* Perform Multiplications and store in destination buffer */
  166. *pOut++ = *px++ * coeff;
  167. *pOut++ = *px++ * coeff;
  168. *pOut++ = *px++ * coeff;
  169. *pOut++ = *px++ * coeff;
  170. /* Decrement the loop counter */
  171. blkCnt--;
  172. }
  173. /* If the blockSize is not a multiple of 4,
  174. * compute the remaining samples */
  175. blkCnt = blockSize % 0x4u;
  176. while(blkCnt > 0u)
  177. {
  178. /* Perform Multiplications and store in destination buffer */
  179. *pOut++ = *px++ * coeff;
  180. /* Decrement the loop counter */
  181. blkCnt--;
  182. }
  183. /* Load the coefficient value and
  184. * increment the coefficient buffer for the next set of state values */
  185. coeff = *pCoeffs++;
  186. /* Read Index, from where the state buffer should be read, is calculated. */
  187. readIndex = ((int32_t) S->stateIndex - (int32_t) blockSize) - *pTapDelay++;
  188. /* Wraparound of readIndex */
  189. if(readIndex < 0)
  190. {
  191. readIndex += (int32_t) delaySize;
  192. }
  193. /* Loop over the number of taps. */
  194. tapCnt = (uint32_t) numTaps - 1u;
  195. while(tapCnt > 0u)
  196. {
  197. /* Working pointer for state buffer is updated */
  198. py = pState;
  199. /* blockSize samples are read from the state buffer */
  200. arm_circularRead_f32((int32_t *) py, delaySize, &readIndex, 1,
  201. (int32_t *) pb, (int32_t *) pb, blockSize, 1,
  202. blockSize);
  203. /* Working pointer for the scratch buffer */
  204. px = pb;
  205. /* Working pointer for destination buffer */
  206. pOut = pDst;
  207. /* Loop over the blockSize. Unroll by a factor of 4.
  208. * Compute 4 MACS at a time. */
  209. blkCnt = blockSize >> 2u;
  210. while(blkCnt > 0u)
  211. {
  212. /* Perform Multiply-Accumulate */
  213. *pOut++ += *px++ * coeff;
  214. *pOut++ += *px++ * coeff;
  215. *pOut++ += *px++ * coeff;
  216. *pOut++ += *px++ * coeff;
  217. /* Decrement the loop counter */
  218. blkCnt--;
  219. }
  220. /* If the blockSize is not a multiple of 4,
  221. * compute the remaining samples */
  222. blkCnt = blockSize % 0x4u;
  223. while(blkCnt > 0u)
  224. {
  225. /* Perform Multiply-Accumulate */
  226. *pOut++ += *px++ * coeff;
  227. /* Decrement the loop counter */
  228. blkCnt--;
  229. }
  230. /* Load the coefficient value and
  231. * increment the coefficient buffer for the next set of state values */
  232. coeff = *pCoeffs++;
  233. /* Read Index, from where the state buffer should be read, is calculated. */
  234. readIndex = ((int32_t) S->stateIndex -
  235. (int32_t) blockSize) - *pTapDelay++;
  236. /* Wraparound of readIndex */
  237. if(readIndex < 0)
  238. {
  239. readIndex += (int32_t) delaySize;
  240. }
  241. /* Decrement the tap loop counter */
  242. tapCnt--;
  243. }
  244. #else
  245. /* Run the below code for Cortex-M0 */
  246. blkCnt = blockSize;
  247. while(blkCnt > 0u)
  248. {
  249. /* Perform Multiplications and store in destination buffer */
  250. *pOut++ = *px++ * coeff;
  251. /* Decrement the loop counter */
  252. blkCnt--;
  253. }
  254. /* Load the coefficient value and
  255. * increment the coefficient buffer for the next set of state values */
  256. coeff = *pCoeffs++;
  257. /* Read Index, from where the state buffer should be read, is calculated. */
  258. readIndex = ((int32_t) S->stateIndex - (int32_t) blockSize) - *pTapDelay++;
  259. /* Wraparound of readIndex */
  260. if(readIndex < 0)
  261. {
  262. readIndex += (int32_t) delaySize;
  263. }
  264. /* Loop over the number of taps. */
  265. tapCnt = (uint32_t) numTaps - 1u;
  266. while(tapCnt > 0u)
  267. {
  268. /* Working pointer for state buffer is updated */
  269. py = pState;
  270. /* blockSize samples are read from the state buffer */
  271. arm_circularRead_f32((int32_t *) py, delaySize, &readIndex, 1,
  272. (int32_t *) pb, (int32_t *) pb, blockSize, 1,
  273. blockSize);
  274. /* Working pointer for the scratch buffer */
  275. px = pb;
  276. /* Working pointer for destination buffer */
  277. pOut = pDst;
  278. blkCnt = blockSize;
  279. while(blkCnt > 0u)
  280. {
  281. /* Perform Multiply-Accumulate */
  282. *pOut++ += *px++ * coeff;
  283. /* Decrement the loop counter */
  284. blkCnt--;
  285. }
  286. /* Load the coefficient value and
  287. * increment the coefficient buffer for the next set of state values */
  288. coeff = *pCoeffs++;
  289. /* Read Index, from where the state buffer should be read, is calculated. */
  290. readIndex =
  291. ((int32_t) S->stateIndex - (int32_t) blockSize) - *pTapDelay++;
  292. /* Wraparound of readIndex */
  293. if(readIndex < 0)
  294. {
  295. readIndex += (int32_t) delaySize;
  296. }
  297. /* Decrement the tap loop counter */
  298. tapCnt--;
  299. }
  300. #endif /* #ifndef ARM_MATH_CM0_FAMILY */
  301. }
  302. /**
  303. * @} end of FIR_Sparse group
  304. */