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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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_norm_q31.c
  9. *
  10. * Description: Processing function for the Q31 NLMS 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_NORM
  46. * @{
  47. */
  48. /**
  49. * @brief Processing function for Q31 normalized LMS filter.
  50. * @param[in] *S points to an instance of the Q31 normalized 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. * <b>Scaling and Overflow Behavior:</b>
  59. * \par
  60. * The function is implemented using an internal 64-bit accumulator.
  61. * The accumulator has a 2.62 format and maintains full precision of the intermediate
  62. * multiplication results but provides only a single guard bit.
  63. * Thus, if the accumulator result overflows it wraps around rather than clip.
  64. * In order to avoid overflows completely the input signal must be scaled down by
  65. * log2(numTaps) bits. 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
  72. * updation of filter cofficients are saturted.
  73. *
  74. */
  75. void arm_lms_norm_q31(
  76. arm_lms_norm_instance_q31 * S,
  77. q31_t * pSrc,
  78. q31_t * pRef,
  79. q31_t * pOut,
  80. q31_t * pErr,
  81. uint32_t blockSize)
  82. {
  83. q31_t *pState = S->pState; /* State pointer */
  84. q31_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */
  85. q31_t *pStateCurnt; /* Points to the current sample of the state */
  86. q31_t *px, *pb; /* Temporary pointers for state and coefficient buffers */
  87. q31_t mu = S->mu; /* Adaptive factor */
  88. uint32_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */
  89. uint32_t tapCnt, blkCnt; /* Loop counters */
  90. q63_t energy; /* Energy of the input */
  91. q63_t acc; /* Accumulator */
  92. q31_t e = 0, d = 0; /* error, reference data sample */
  93. q31_t w = 0, in; /* weight factor and state */
  94. q31_t x0; /* temporary variable to hold input sample */
  95. // uint32_t shift = 32u - ((uint32_t) S->postShift + 1u); /* Shift to be applied to the output */
  96. q31_t errorXmu, oneByEnergy; /* Temporary variables to store error and mu product and reciprocal of energy */
  97. q31_t postShift; /* Post shift to be applied to weight after reciprocal calculation */
  98. q31_t coef; /* Temporary variable for coef */
  99. q31_t acc_l, acc_h; /* temporary input */
  100. uint32_t uShift = ((uint32_t) S->postShift + 1u);
  101. uint32_t lShift = 32u - uShift; /* Shift to be applied to the output */
  102. energy = S->energy;
  103. x0 = S->x0;
  104. /* S->pState points to buffer which contains previous frame (numTaps - 1) samples */
  105. /* pStateCurnt points to the location where the new input data should be written */
  106. pStateCurnt = &(S->pState[(numTaps - 1u)]);
  107. /* Loop over blockSize number of values */
  108. blkCnt = blockSize;
  109. #ifndef ARM_MATH_CM0_FAMILY
  110. /* Run the below code for Cortex-M4 and Cortex-M3 */
  111. while(blkCnt > 0u)
  112. {
  113. /* Copy the new input sample into the state buffer */
  114. *pStateCurnt++ = *pSrc;
  115. /* Initialize pState pointer */
  116. px = pState;
  117. /* Initialize coeff pointer */
  118. pb = (pCoeffs);
  119. /* Read the sample from input buffer */
  120. in = *pSrc++;
  121. /* Update the energy calculation */
  122. energy = (q31_t) ((((q63_t) energy << 32) -
  123. (((q63_t) x0 * x0) << 1)) >> 32);
  124. energy = (q31_t) (((((q63_t) in * in) << 1) + (energy << 32)) >> 32);
  125. /* Set the accumulator to zero */
  126. acc = 0;
  127. /* Loop unrolling. Process 4 taps at a time. */
  128. tapCnt = numTaps >> 2;
  129. while(tapCnt > 0u)
  130. {
  131. /* Perform the multiply-accumulate */
  132. acc += ((q63_t) (*px++)) * (*pb++);
  133. acc += ((q63_t) (*px++)) * (*pb++);
  134. acc += ((q63_t) (*px++)) * (*pb++);
  135. acc += ((q63_t) (*px++)) * (*pb++);
  136. /* Decrement the loop counter */
  137. tapCnt--;
  138. }
  139. /* If the filter length is not a multiple of 4, compute the remaining filter taps */
  140. tapCnt = numTaps % 0x4u;
  141. while(tapCnt > 0u)
  142. {
  143. /* Perform the multiply-accumulate */
  144. acc += ((q63_t) (*px++)) * (*pb++);
  145. /* Decrement the loop counter */
  146. tapCnt--;
  147. }
  148. /* Converting the result to 1.31 format */
  149. /* Calc lower part of acc */
  150. acc_l = acc & 0xffffffff;
  151. /* Calc upper part of acc */
  152. acc_h = (acc >> 32) & 0xffffffff;
  153. acc = (uint32_t) acc_l >> lShift | acc_h << uShift;
  154. /* Store the result from accumulator into the destination buffer. */
  155. *pOut++ = (q31_t) acc;
  156. /* Compute and store error */
  157. d = *pRef++;
  158. e = d - (q31_t) acc;
  159. *pErr++ = e;
  160. /* Calculates the reciprocal of energy */
  161. postShift = arm_recip_q31(energy + DELTA_Q31,
  162. &oneByEnergy, &S->recipTable[0]);
  163. /* Calculation of product of (e * mu) */
  164. errorXmu = (q31_t) (((q63_t) e * mu) >> 31);
  165. /* Weighting factor for the normalized version */
  166. w = clip_q63_to_q31(((q63_t) errorXmu * oneByEnergy) >> (31 - postShift));
  167. /* Initialize pState pointer */
  168. px = pState;
  169. /* Initialize coeff pointer */
  170. pb = (pCoeffs);
  171. /* Loop unrolling. Process 4 taps at a time. */
  172. tapCnt = numTaps >> 2;
  173. /* Update filter coefficients */
  174. while(tapCnt > 0u)
  175. {
  176. /* Perform the multiply-accumulate */
  177. /* coef is in 2.30 format */
  178. coef = (q31_t) (((q63_t) w * (*px++)) >> (32));
  179. /* get coef in 1.31 format by left shifting */
  180. *pb = clip_q63_to_q31((q63_t) * pb + (coef << 1u));
  181. /* update coefficient buffer to next coefficient */
  182. pb++;
  183. coef = (q31_t) (((q63_t) w * (*px++)) >> (32));
  184. *pb = clip_q63_to_q31((q63_t) * pb + (coef << 1u));
  185. pb++;
  186. coef = (q31_t) (((q63_t) w * (*px++)) >> (32));
  187. *pb = clip_q63_to_q31((q63_t) * pb + (coef << 1u));
  188. pb++;
  189. coef = (q31_t) (((q63_t) w * (*px++)) >> (32));
  190. *pb = clip_q63_to_q31((q63_t) * pb + (coef << 1u));
  191. pb++;
  192. /* Decrement the loop counter */
  193. tapCnt--;
  194. }
  195. /* If the filter length is not a multiple of 4, compute the remaining filter taps */
  196. tapCnt = numTaps % 0x4u;
  197. while(tapCnt > 0u)
  198. {
  199. /* Perform the multiply-accumulate */
  200. coef = (q31_t) (((q63_t) w * (*px++)) >> (32));
  201. *pb = clip_q63_to_q31((q63_t) * pb + (coef << 1u));
  202. pb++;
  203. /* Decrement the loop counter */
  204. tapCnt--;
  205. }
  206. /* Read the sample from state buffer */
  207. x0 = *pState;
  208. /* Advance state pointer by 1 for the next sample */
  209. pState = pState + 1;
  210. /* Decrement the loop counter */
  211. blkCnt--;
  212. }
  213. /* Save energy and x0 values for the next frame */
  214. S->energy = (q31_t) energy;
  215. S->x0 = x0;
  216. /* Processing is complete. Now copy the last numTaps - 1 samples to the
  217. satrt of the state buffer. This prepares the state buffer for the
  218. next function call. */
  219. /* Points to the start of the pState buffer */
  220. pStateCurnt = S->pState;
  221. /* Loop unrolling for (numTaps - 1u) samples copy */
  222. tapCnt = (numTaps - 1u) >> 2u;
  223. /* copy data */
  224. while(tapCnt > 0u)
  225. {
  226. *pStateCurnt++ = *pState++;
  227. *pStateCurnt++ = *pState++;
  228. *pStateCurnt++ = *pState++;
  229. *pStateCurnt++ = *pState++;
  230. /* Decrement the loop counter */
  231. tapCnt--;
  232. }
  233. /* Calculate remaining number of copies */
  234. tapCnt = (numTaps - 1u) % 0x4u;
  235. /* Copy the remaining q31_t data */
  236. while(tapCnt > 0u)
  237. {
  238. *pStateCurnt++ = *pState++;
  239. /* Decrement the loop counter */
  240. tapCnt--;
  241. }
  242. #else
  243. /* Run the below code for Cortex-M0 */
  244. while(blkCnt > 0u)
  245. {
  246. /* Copy the new input sample into the state buffer */
  247. *pStateCurnt++ = *pSrc;
  248. /* Initialize pState pointer */
  249. px = pState;
  250. /* Initialize pCoeffs pointer */
  251. pb = pCoeffs;
  252. /* Read the sample from input buffer */
  253. in = *pSrc++;
  254. /* Update the energy calculation */
  255. energy =
  256. (q31_t) ((((q63_t) energy << 32) - (((q63_t) x0 * x0) << 1)) >> 32);
  257. energy = (q31_t) (((((q63_t) in * in) << 1) + (energy << 32)) >> 32);
  258. /* Set the accumulator to zero */
  259. acc = 0;
  260. /* Loop over numTaps number of values */
  261. tapCnt = numTaps;
  262. while(tapCnt > 0u)
  263. {
  264. /* Perform the multiply-accumulate */
  265. acc += ((q63_t) (*px++)) * (*pb++);
  266. /* Decrement the loop counter */
  267. tapCnt--;
  268. }
  269. /* Converting the result to 1.31 format */
  270. /* Converting the result to 1.31 format */
  271. /* Calc lower part of acc */
  272. acc_l = acc & 0xffffffff;
  273. /* Calc upper part of acc */
  274. acc_h = (acc >> 32) & 0xffffffff;
  275. acc = (uint32_t) acc_l >> lShift | acc_h << uShift;
  276. //acc = (q31_t) (acc >> shift);
  277. /* Store the result from accumulator into the destination buffer. */
  278. *pOut++ = (q31_t) acc;
  279. /* Compute and store error */
  280. d = *pRef++;
  281. e = d - (q31_t) acc;
  282. *pErr++ = e;
  283. /* Calculates the reciprocal of energy */
  284. postShift =
  285. arm_recip_q31(energy + DELTA_Q31, &oneByEnergy, &S->recipTable[0]);
  286. /* Calculation of product of (e * mu) */
  287. errorXmu = (q31_t) (((q63_t) e * mu) >> 31);
  288. /* Weighting factor for the normalized version */
  289. w = clip_q63_to_q31(((q63_t) errorXmu * oneByEnergy) >> (31 - postShift));
  290. /* Initialize pState pointer */
  291. px = pState;
  292. /* Initialize coeff pointer */
  293. pb = (pCoeffs);
  294. /* Loop over numTaps number of values */
  295. tapCnt = numTaps;
  296. while(tapCnt > 0u)
  297. {
  298. /* Perform the multiply-accumulate */
  299. /* coef is in 2.30 format */
  300. coef = (q31_t) (((q63_t) w * (*px++)) >> (32));
  301. /* get coef in 1.31 format by left shifting */
  302. *pb = clip_q63_to_q31((q63_t) * pb + (coef << 1u));
  303. /* update coefficient buffer to next coefficient */
  304. pb++;
  305. /* Decrement the loop counter */
  306. tapCnt--;
  307. }
  308. /* Read the sample from state buffer */
  309. x0 = *pState;
  310. /* Advance state pointer by 1 for the next sample */
  311. pState = pState + 1;
  312. /* Decrement the loop counter */
  313. blkCnt--;
  314. }
  315. /* Save energy and x0 values for the next frame */
  316. S->energy = (q31_t) energy;
  317. S->x0 = x0;
  318. /* Processing is complete. Now copy the last numTaps - 1 samples to the
  319. start of the state buffer. This prepares the state buffer for the
  320. next function call. */
  321. /* Points to the start of the pState buffer */
  322. pStateCurnt = S->pState;
  323. /* Loop for (numTaps - 1u) samples copy */
  324. tapCnt = (numTaps - 1u);
  325. /* Copy the remaining q31_t data */
  326. while(tapCnt > 0u)
  327. {
  328. *pStateCurnt++ = *pState++;
  329. /* Decrement the loop counter */
  330. tapCnt--;
  331. }
  332. #endif /* #ifndef ARM_MATH_CM0_FAMILY */
  333. }
  334. /**
  335. * @} end of LMS_NORM group
  336. */