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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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_q15.c
  9. *
  10. * Description: Q15 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 Q15 normalized LMS filter.
  50. * @param[in] *S points to an instance of the Q15 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 a 64-bit internal accumulator.
  61. * Both coefficients and state variables are represented in 1.15 format and
  62. * multiplications yield a 2.30 result. The 2.30 intermediate results are
  63. * accumulated in a 64-bit accumulator in 34.30 format.
  64. * There is no risk of internal overflow with this approach and the full
  65. * precision of intermediate multiplications is preserved. After all additions
  66. * have been performed, the accumulator is truncated to 34.15 format by
  67. * discarding low 15 bits. Lastly, the accumulator is saturated to yield a
  68. * result in 1.15 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. */
  74. void arm_lms_norm_q15(
  75. arm_lms_norm_instance_q15 * S,
  76. q15_t * pSrc,
  77. q15_t * pRef,
  78. q15_t * pOut,
  79. q15_t * pErr,
  80. uint32_t blockSize)
  81. {
  82. q15_t *pState = S->pState; /* State pointer */
  83. q15_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */
  84. q15_t *pStateCurnt; /* Points to the current sample of the state */
  85. q15_t *px, *pb; /* Temporary pointers for state and coefficient buffers */
  86. q15_t mu = S->mu; /* Adaptive factor */
  87. uint32_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */
  88. uint32_t tapCnt, blkCnt; /* Loop counters */
  89. q31_t energy; /* Energy of the input */
  90. q63_t acc; /* Accumulator */
  91. q15_t e = 0, d = 0; /* error, reference data sample */
  92. q15_t w = 0, in; /* weight factor and state */
  93. q15_t x0; /* temporary variable to hold input sample */
  94. //uint32_t shift = (uint32_t) S->postShift + 1u; /* Shift to be applied to the output */
  95. q15_t errorXmu, oneByEnergy; /* Temporary variables to store error and mu product and reciprocal of energy */
  96. q15_t postShift; /* Post shift to be applied to weight after reciprocal calculation */
  97. q31_t coef; /* Teporary variable for coefficient */
  98. q31_t acc_l, acc_h;
  99. int32_t lShift = (15 - (int32_t) S->postShift); /* Post shift */
  100. int32_t uShift = (32 - lShift);
  101. energy = S->energy;
  102. x0 = S->x0;
  103. /* S->pState points to buffer which contains previous frame (numTaps - 1) samples */
  104. /* pStateCurnt points to the location where the new input data should be written */
  105. pStateCurnt = &(S->pState[(numTaps - 1u)]);
  106. /* Loop over blockSize number of values */
  107. blkCnt = blockSize;
  108. #ifndef ARM_MATH_CM0_FAMILY
  109. /* Run the below code for Cortex-M4 and Cortex-M3 */
  110. while(blkCnt > 0u)
  111. {
  112. /* Copy the new input sample into the state buffer */
  113. *pStateCurnt++ = *pSrc;
  114. /* Initialize pState pointer */
  115. px = pState;
  116. /* Initialize coeff pointer */
  117. pb = (pCoeffs);
  118. /* Read the sample from input buffer */
  119. in = *pSrc++;
  120. /* Update the energy calculation */
  121. energy -= (((q31_t) x0 * (x0)) >> 15);
  122. energy += (((q31_t) in * (in)) >> 15);
  123. /* Set the accumulator to zero */
  124. acc = 0;
  125. /* Loop unrolling. Process 4 taps at a time. */
  126. tapCnt = numTaps >> 2;
  127. while(tapCnt > 0u)
  128. {
  129. /* Perform the multiply-accumulate */
  130. #ifndef UNALIGNED_SUPPORT_DISABLE
  131. acc = __SMLALD(*__SIMD32(px)++, (*__SIMD32(pb)++), acc);
  132. acc = __SMLALD(*__SIMD32(px)++, (*__SIMD32(pb)++), acc);
  133. #else
  134. acc += (((q31_t) * px++ * (*pb++)));
  135. acc += (((q31_t) * px++ * (*pb++)));
  136. acc += (((q31_t) * px++ * (*pb++)));
  137. acc += (((q31_t) * px++ * (*pb++)));
  138. #endif /* #ifndef UNALIGNED_SUPPORT_DISABLE */
  139. /* Decrement the loop counter */
  140. tapCnt--;
  141. }
  142. /* If the filter length is not a multiple of 4, compute the remaining filter taps */
  143. tapCnt = numTaps % 0x4u;
  144. while(tapCnt > 0u)
  145. {
  146. /* Perform the multiply-accumulate */
  147. acc += (((q31_t) * px++ * (*pb++)));
  148. /* Decrement the loop counter */
  149. tapCnt--;
  150. }
  151. /* Calc lower part of acc */
  152. acc_l = acc & 0xffffffff;
  153. /* Calc upper part of acc */
  154. acc_h = (acc >> 32) & 0xffffffff;
  155. /* Apply shift for lower part of acc and upper part of acc */
  156. acc = (uint32_t) acc_l >> lShift | acc_h << uShift;
  157. /* Converting the result to 1.15 format and saturate the output */
  158. acc = __SSAT(acc, 16u);
  159. /* Store the result from accumulator into the destination buffer. */
  160. *pOut++ = (q15_t) acc;
  161. /* Compute and store error */
  162. d = *pRef++;
  163. e = d - (q15_t) acc;
  164. *pErr++ = e;
  165. /* Calculation of 1/energy */
  166. postShift = arm_recip_q15((q15_t) energy + DELTA_Q15,
  167. &oneByEnergy, S->recipTable);
  168. /* Calculation of e * mu value */
  169. errorXmu = (q15_t) (((q31_t) e * mu) >> 15);
  170. /* Calculation of (e * mu) * (1/energy) value */
  171. acc = (((q31_t) errorXmu * oneByEnergy) >> (15 - postShift));
  172. /* Weighting factor for the normalized version */
  173. w = (q15_t) __SSAT((q31_t) acc, 16);
  174. /* Initialize pState pointer */
  175. px = pState;
  176. /* Initialize coeff pointer */
  177. pb = (pCoeffs);
  178. /* Loop unrolling. Process 4 taps at a time. */
  179. tapCnt = numTaps >> 2;
  180. /* Update filter coefficients */
  181. while(tapCnt > 0u)
  182. {
  183. coef = *pb + (((q31_t) w * (*px++)) >> 15);
  184. *pb++ = (q15_t) __SSAT((coef), 16);
  185. coef = *pb + (((q31_t) w * (*px++)) >> 15);
  186. *pb++ = (q15_t) __SSAT((coef), 16);
  187. coef = *pb + (((q31_t) w * (*px++)) >> 15);
  188. *pb++ = (q15_t) __SSAT((coef), 16);
  189. coef = *pb + (((q31_t) w * (*px++)) >> 15);
  190. *pb++ = (q15_t) __SSAT((coef), 16);
  191. /* Decrement the loop counter */
  192. tapCnt--;
  193. }
  194. /* If the filter length is not a multiple of 4, compute the remaining filter taps */
  195. tapCnt = numTaps % 0x4u;
  196. while(tapCnt > 0u)
  197. {
  198. /* Perform the multiply-accumulate */
  199. coef = *pb + (((q31_t) w * (*px++)) >> 15);
  200. *pb++ = (q15_t) __SSAT((coef), 16);
  201. /* Decrement the loop counter */
  202. tapCnt--;
  203. }
  204. /* Read the sample from state buffer */
  205. x0 = *pState;
  206. /* Advance state pointer by 1 for the next sample */
  207. pState = pState + 1u;
  208. /* Decrement the loop counter */
  209. blkCnt--;
  210. }
  211. /* Save energy and x0 values for the next frame */
  212. S->energy = (q15_t) energy;
  213. S->x0 = x0;
  214. /* Processing is complete. Now copy the last numTaps - 1 samples to the
  215. satrt of the state buffer. This prepares the state buffer for the
  216. next function call. */
  217. /* Points to the start of the pState buffer */
  218. pStateCurnt = S->pState;
  219. /* Calculation of count for copying integer writes */
  220. tapCnt = (numTaps - 1u) >> 2;
  221. while(tapCnt > 0u)
  222. {
  223. #ifndef UNALIGNED_SUPPORT_DISABLE
  224. *__SIMD32(pStateCurnt)++ = *__SIMD32(pState)++;
  225. *__SIMD32(pStateCurnt)++ = *__SIMD32(pState)++;
  226. #else
  227. *pStateCurnt++ = *pState++;
  228. *pStateCurnt++ = *pState++;
  229. *pStateCurnt++ = *pState++;
  230. *pStateCurnt++ = *pState++;
  231. #endif
  232. tapCnt--;
  233. }
  234. /* Calculation of count for remaining q15_t data */
  235. tapCnt = (numTaps - 1u) % 0x4u;
  236. /* copy data */
  237. while(tapCnt > 0u)
  238. {
  239. *pStateCurnt++ = *pState++;
  240. /* Decrement the loop counter */
  241. tapCnt--;
  242. }
  243. #else
  244. /* Run the below code for Cortex-M0 */
  245. while(blkCnt > 0u)
  246. {
  247. /* Copy the new input sample into the state buffer */
  248. *pStateCurnt++ = *pSrc;
  249. /* Initialize pState pointer */
  250. px = pState;
  251. /* Initialize pCoeffs pointer */
  252. pb = pCoeffs;
  253. /* Read the sample from input buffer */
  254. in = *pSrc++;
  255. /* Update the energy calculation */
  256. energy -= (((q31_t) x0 * (x0)) >> 15);
  257. energy += (((q31_t) in * (in)) >> 15);
  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 += (((q31_t) * px++ * (*pb++)));
  266. /* Decrement the loop counter */
  267. tapCnt--;
  268. }
  269. /* Calc lower part of acc */
  270. acc_l = acc & 0xffffffff;
  271. /* Calc upper part of acc */
  272. acc_h = (acc >> 32) & 0xffffffff;
  273. /* Apply shift for lower part of acc and upper part of acc */
  274. acc = (uint32_t) acc_l >> lShift | acc_h << uShift;
  275. /* Converting the result to 1.15 format and saturate the output */
  276. acc = __SSAT(acc, 16u);
  277. /* Converting the result to 1.15 format */
  278. //acc = __SSAT((acc >> (16u - shift)), 16u);
  279. /* Store the result from accumulator into the destination buffer. */
  280. *pOut++ = (q15_t) acc;
  281. /* Compute and store error */
  282. d = *pRef++;
  283. e = d - (q15_t) acc;
  284. *pErr++ = e;
  285. /* Calculation of 1/energy */
  286. postShift = arm_recip_q15((q15_t) energy + DELTA_Q15,
  287. &oneByEnergy, S->recipTable);
  288. /* Calculation of e * mu value */
  289. errorXmu = (q15_t) (((q31_t) e * mu) >> 15);
  290. /* Calculation of (e * mu) * (1/energy) value */
  291. acc = (((q31_t) errorXmu * oneByEnergy) >> (15 - postShift));
  292. /* Weighting factor for the normalized version */
  293. w = (q15_t) __SSAT((q31_t) acc, 16);
  294. /* Initialize pState pointer */
  295. px = pState;
  296. /* Initialize coeff pointer */
  297. pb = (pCoeffs);
  298. /* Loop over numTaps number of values */
  299. tapCnt = numTaps;
  300. while(tapCnt > 0u)
  301. {
  302. /* Perform the multiply-accumulate */
  303. coef = *pb + (((q31_t) w * (*px++)) >> 15);
  304. *pb++ = (q15_t) __SSAT((coef), 16);
  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 + 1u;
  312. /* Decrement the loop counter */
  313. blkCnt--;
  314. }
  315. /* Save energy and x0 values for the next frame */
  316. S->energy = (q15_t) energy;
  317. S->x0 = x0;
  318. /* Processing is complete. Now copy the last numTaps - 1 samples to the
  319. satrt 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. /* copy (numTaps - 1u) data */
  324. tapCnt = (numTaps - 1u);
  325. /* copy 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. */