Keyboard firmwares for Atmel AVR and Cortex-M
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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_lms_q31.c
  9. *
  10. * Description: Processing function for the Q31 LMS filter.
  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 LMS
  46. * @{
  47. */
  48. /**
  49. * @brief Processing function for Q31 LMS filter.
  50. * @param[in] *S points to an instance of the Q15 LMS filter structure.
  51. * @param[in] *pSrc points to the block of input data.
  52. * @param[in] *pRef points to the block of reference data.
  53. * @param[out] *pOut points to the block of output data.
  54. * @param[out] *pErr points to the block of error data.
  55. * @param[in] blockSize number of samples to process.
  56. * @return none.
  57. *
  58. * \par Scaling and Overflow Behavior:
  59. * The function is implemented using an internal 64-bit accumulator.
  60. * The accumulator has a 2.62 format and maintains full precision of the intermediate
  61. * multiplication results but provides only a single guard bit.
  62. * Thus, if the accumulator result overflows it wraps around rather than clips.
  63. * In order to avoid overflows completely the input signal must be scaled down by
  64. * log2(numTaps) bits.
  65. * The reference signal should not be scaled down.
  66. * After all multiply-accumulates are performed, the 2.62 accumulator is shifted
  67. * and saturated to 1.31 format to yield the final result.
  68. * The output signal and error signal are in 1.31 format.
  69. *
  70. * \par
  71. * In this filter, filter coefficients are updated for each sample and the updation of filter cofficients are saturted.
  72. */
  73. void arm_lms_q31(
  74. const arm_lms_instance_q31 * S,
  75. q31_t * pSrc,
  76. q31_t * pRef,
  77. q31_t * pOut,
  78. q31_t * pErr,
  79. uint32_t blockSize)
  80. {
  81. q31_t *pState = S->pState; /* State pointer */
  82. uint32_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */
  83. q31_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */
  84. q31_t *pStateCurnt; /* Points to the current sample of the state */
  85. q31_t mu = S->mu; /* Adaptive factor */
  86. q31_t *px; /* Temporary pointer for state */
  87. q31_t *pb; /* Temporary pointer for coefficient buffer */
  88. uint32_t tapCnt, blkCnt; /* Loop counters */
  89. q63_t acc; /* Accumulator */
  90. q31_t e = 0; /* error of data sample */
  91. q31_t alpha; /* Intermediate constant for taps update */
  92. q31_t coef; /* Temporary variable for coef */
  93. q31_t acc_l, acc_h; /* temporary input */
  94. uint32_t uShift = ((uint32_t) S->postShift + 1u);
  95. uint32_t lShift = 32u - uShift; /* Shift to be applied to the output */
  96. /* S->pState points to buffer which contains previous frame (numTaps - 1) samples */
  97. /* pStateCurnt points to the location where the new input data should be written */
  98. pStateCurnt = &(S->pState[(numTaps - 1u)]);
  99. /* Initializing blkCnt with blockSize */
  100. blkCnt = blockSize;
  101. #ifndef ARM_MATH_CM0_FAMILY
  102. /* Run the below code for Cortex-M4 and Cortex-M3 */
  103. while(blkCnt > 0u)
  104. {
  105. /* Copy the new input sample into the state buffer */
  106. *pStateCurnt++ = *pSrc++;
  107. /* Initialize state pointer */
  108. px = pState;
  109. /* Initialize coefficient pointer */
  110. pb = pCoeffs;
  111. /* Set the accumulator to zero */
  112. acc = 0;
  113. /* Loop unrolling. Process 4 taps at a time. */
  114. tapCnt = numTaps >> 2;
  115. while(tapCnt > 0u)
  116. {
  117. /* Perform the multiply-accumulate */
  118. /* acc += b[N] * x[n-N] */
  119. acc += ((q63_t) (*px++)) * (*pb++);
  120. /* acc += b[N-1] * x[n-N-1] */
  121. acc += ((q63_t) (*px++)) * (*pb++);
  122. /* acc += b[N-2] * x[n-N-2] */
  123. acc += ((q63_t) (*px++)) * (*pb++);
  124. /* acc += b[N-3] * x[n-N-3] */
  125. acc += ((q63_t) (*px++)) * (*pb++);
  126. /* Decrement the loop counter */
  127. tapCnt--;
  128. }
  129. /* If the filter length is not a multiple of 4, compute the remaining filter taps */
  130. tapCnt = numTaps % 0x4u;
  131. while(tapCnt > 0u)
  132. {
  133. /* Perform the multiply-accumulate */
  134. acc += ((q63_t) (*px++)) * (*pb++);
  135. /* Decrement the loop counter */
  136. tapCnt--;
  137. }
  138. /* Converting the result to 1.31 format */
  139. /* Calc lower part of acc */
  140. acc_l = acc & 0xffffffff;
  141. /* Calc upper part of acc */
  142. acc_h = (acc >> 32) & 0xffffffff;
  143. acc = (uint32_t) acc_l >> lShift | acc_h << uShift;
  144. /* Store the result from accumulator into the destination buffer. */
  145. *pOut++ = (q31_t) acc;
  146. /* Compute and store error */
  147. e = *pRef++ - (q31_t) acc;
  148. *pErr++ = (q31_t) e;
  149. /* Compute alpha i.e. intermediate constant for taps update */
  150. alpha = (q31_t) (((q63_t) e * mu) >> 31);
  151. /* Initialize state pointer */
  152. /* Advance state pointer by 1 for the next sample */
  153. px = pState++;
  154. /* Initialize coefficient pointer */
  155. pb = pCoeffs;
  156. /* Loop unrolling. Process 4 taps at a time. */
  157. tapCnt = numTaps >> 2;
  158. /* Update filter coefficients */
  159. while(tapCnt > 0u)
  160. {
  161. /* coef is in 2.30 format */
  162. coef = (q31_t) (((q63_t) alpha * (*px++)) >> (32));
  163. /* get coef in 1.31 format by left shifting */
  164. *pb = clip_q63_to_q31((q63_t) * pb + (coef << 1u));
  165. /* update coefficient buffer to next coefficient */
  166. pb++;
  167. coef = (q31_t) (((q63_t) alpha * (*px++)) >> (32));
  168. *pb = clip_q63_to_q31((q63_t) * pb + (coef << 1u));
  169. pb++;
  170. coef = (q31_t) (((q63_t) alpha * (*px++)) >> (32));
  171. *pb = clip_q63_to_q31((q63_t) * pb + (coef << 1u));
  172. pb++;
  173. coef = (q31_t) (((q63_t) alpha * (*px++)) >> (32));
  174. *pb = clip_q63_to_q31((q63_t) * pb + (coef << 1u));
  175. pb++;
  176. /* Decrement the loop counter */
  177. tapCnt--;
  178. }
  179. /* If the filter length is not a multiple of 4, compute the remaining filter taps */
  180. tapCnt = numTaps % 0x4u;
  181. while(tapCnt > 0u)
  182. {
  183. /* Perform the multiply-accumulate */
  184. coef = (q31_t) (((q63_t) alpha * (*px++)) >> (32));
  185. *pb = clip_q63_to_q31((q63_t) * pb + (coef << 1u));
  186. pb++;
  187. /* Decrement the loop counter */
  188. tapCnt--;
  189. }
  190. /* Decrement the loop counter */
  191. blkCnt--;
  192. }
  193. /* Processing is complete. Now copy the last numTaps - 1 samples to the
  194. satrt of the state buffer. This prepares the state buffer for the
  195. next function call. */
  196. /* Points to the start of the pState buffer */
  197. pStateCurnt = S->pState;
  198. /* Loop unrolling for (numTaps - 1u) samples copy */
  199. tapCnt = (numTaps - 1u) >> 2u;
  200. /* copy data */
  201. while(tapCnt > 0u)
  202. {
  203. *pStateCurnt++ = *pState++;
  204. *pStateCurnt++ = *pState++;
  205. *pStateCurnt++ = *pState++;
  206. *pStateCurnt++ = *pState++;
  207. /* Decrement the loop counter */
  208. tapCnt--;
  209. }
  210. /* Calculate remaining number of copies */
  211. tapCnt = (numTaps - 1u) % 0x4u;
  212. /* Copy the remaining q31_t data */
  213. while(tapCnt > 0u)
  214. {
  215. *pStateCurnt++ = *pState++;
  216. /* Decrement the loop counter */
  217. tapCnt--;
  218. }
  219. #else
  220. /* Run the below code for Cortex-M0 */
  221. while(blkCnt > 0u)
  222. {
  223. /* Copy the new input sample into the state buffer */
  224. *pStateCurnt++ = *pSrc++;
  225. /* Initialize pState pointer */
  226. px = pState;
  227. /* Initialize pCoeffs pointer */
  228. pb = pCoeffs;
  229. /* Set the accumulator to zero */
  230. acc = 0;
  231. /* Loop over numTaps number of values */
  232. tapCnt = numTaps;
  233. while(tapCnt > 0u)
  234. {
  235. /* Perform the multiply-accumulate */
  236. acc += ((q63_t) (*px++)) * (*pb++);
  237. /* Decrement the loop counter */
  238. tapCnt--;
  239. }
  240. /* Converting the result to 1.31 format */
  241. /* Store the result from accumulator into the destination buffer. */
  242. /* Calc lower part of acc */
  243. acc_l = acc & 0xffffffff;
  244. /* Calc upper part of acc */
  245. acc_h = (acc >> 32) & 0xffffffff;
  246. acc = (uint32_t) acc_l >> lShift | acc_h << uShift;
  247. *pOut++ = (q31_t) acc;
  248. /* Compute and store error */
  249. e = *pRef++ - (q31_t) acc;
  250. *pErr++ = (q31_t) e;
  251. /* Weighting factor for the LMS version */
  252. alpha = (q31_t) (((q63_t) e * mu) >> 31);
  253. /* Initialize pState pointer */
  254. /* Advance state pointer by 1 for the next sample */
  255. px = pState++;
  256. /* Initialize pCoeffs pointer */
  257. pb = pCoeffs;
  258. /* Loop over numTaps number of values */
  259. tapCnt = numTaps;
  260. while(tapCnt > 0u)
  261. {
  262. /* Perform the multiply-accumulate */
  263. coef = (q31_t) (((q63_t) alpha * (*px++)) >> (32));
  264. *pb += (coef << 1u);
  265. pb++;
  266. /* Decrement the loop counter */
  267. tapCnt--;
  268. }
  269. /* Decrement the loop counter */
  270. blkCnt--;
  271. }
  272. /* Processing is complete. Now copy the last numTaps - 1 samples to the
  273. start of the state buffer. This prepares the state buffer for the
  274. next function call. */
  275. /* Points to the start of the pState buffer */
  276. pStateCurnt = S->pState;
  277. /* Copy (numTaps - 1u) samples */
  278. tapCnt = (numTaps - 1u);
  279. /* Copy the data */
  280. while(tapCnt > 0u)
  281. {
  282. *pStateCurnt++ = *pState++;
  283. /* Decrement the loop counter */
  284. tapCnt--;
  285. }
  286. #endif /* #ifndef ARM_MATH_CM0_FAMILY */
  287. }
  288. /**
  289. * @} end of LMS group
  290. */