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_f32.c 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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_f32.c
  9. *
  10. * Description: Processing function for the floating-point 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. * @defgroup LMS Least Mean Square (LMS) Filters
  46. *
  47. * LMS filters are a class of adaptive filters that are able to "learn" an unknown transfer functions.
  48. * LMS filters use a gradient descent method in which the filter coefficients are updated based on the instantaneous error signal.
  49. * Adaptive filters are often used in communication systems, equalizers, and noise removal.
  50. * The CMSIS DSP Library contains LMS filter functions that operate on Q15, Q31, and floating-point data types.
  51. * The library also contains normalized LMS filters in which the filter coefficient adaptation is indepedent of the level of the input signal.
  52. *
  53. * An LMS filter consists of two components as shown below.
  54. * The first component is a standard transversal or FIR filter.
  55. * The second component is a coefficient update mechanism.
  56. * The LMS filter has two input signals.
  57. * The "input" feeds the FIR filter while the "reference input" corresponds to the desired output of the FIR filter.
  58. * That is, the FIR filter coefficients are updated so that the output of the FIR filter matches the reference input.
  59. * The filter coefficient update mechanism is based on the difference between the FIR filter output and the reference input.
  60. * This "error signal" tends towards zero as the filter adapts.
  61. * The LMS processing functions accept the input and reference input signals and generate the filter output and error signal.
  62. * \image html LMS.gif "Internal structure of the Least Mean Square filter"
  63. *
  64. * The functions operate on blocks of data and each call to the function processes
  65. * <code>blockSize</code> samples through the filter.
  66. * <code>pSrc</code> points to input signal, <code>pRef</code> points to reference signal,
  67. * <code>pOut</code> points to output signal and <code>pErr</code> points to error signal.
  68. * All arrays contain <code>blockSize</code> values.
  69. *
  70. * The functions operate on a block-by-block basis.
  71. * Internally, the filter coefficients <code>b[n]</code> are updated on a sample-by-sample basis.
  72. * The convergence of the LMS filter is slower compared to the normalized LMS algorithm.
  73. *
  74. * \par Algorithm:
  75. * The output signal <code>y[n]</code> is computed by a standard FIR filter:
  76. * <pre>
  77. * y[n] = b[0] * x[n] + b[1] * x[n-1] + b[2] * x[n-2] + ...+ b[numTaps-1] * x[n-numTaps+1]
  78. * </pre>
  79. *
  80. * \par
  81. * The error signal equals the difference between the reference signal <code>d[n]</code> and the filter output:
  82. * <pre>
  83. * e[n] = d[n] - y[n].
  84. * </pre>
  85. *
  86. * \par
  87. * After each sample of the error signal is computed, the filter coefficients <code>b[k]</code> are updated on a sample-by-sample basis:
  88. * <pre>
  89. * b[k] = b[k] + e[n] * mu * x[n-k], for k=0, 1, ..., numTaps-1
  90. * </pre>
  91. * where <code>mu</code> is the step size and controls the rate of coefficient convergence.
  92. *\par
  93. * In the APIs, <code>pCoeffs</code> points to a coefficient array of size <code>numTaps</code>.
  94. * Coefficients are stored in time reversed order.
  95. * \par
  96. * <pre>
  97. * {b[numTaps-1], b[numTaps-2], b[N-2], ..., b[1], b[0]}
  98. * </pre>
  99. * \par
  100. * <code>pState</code> points to a state array of size <code>numTaps + blockSize - 1</code>.
  101. * Samples in the state buffer are stored in the order:
  102. * \par
  103. * <pre>
  104. * {x[n-numTaps+1], x[n-numTaps], x[n-numTaps-1], x[n-numTaps-2]....x[0], x[1], ..., x[blockSize-1]}
  105. * </pre>
  106. * \par
  107. * Note that the length of the state buffer exceeds the length of the coefficient array by <code>blockSize-1</code> samples.
  108. * The increased state buffer length allows circular addressing, which is traditionally used in FIR filters,
  109. * to be avoided and yields a significant speed improvement.
  110. * The state variables are updated after each block of data is processed.
  111. * \par Instance Structure
  112. * The coefficients and state variables for a filter are stored together in an instance data structure.
  113. * A separate instance structure must be defined for each filter and
  114. * coefficient and state arrays cannot be shared among instances.
  115. * There are separate instance structure declarations for each of the 3 supported data types.
  116. *
  117. * \par Initialization Functions
  118. * There is also an associated initialization function for each data type.
  119. * The initialization function performs the following operations:
  120. * - Sets the values of the internal structure fields.
  121. * - Zeros out the values in the state buffer.
  122. * To do this manually without calling the init function, assign the follow subfields of the instance structure:
  123. * numTaps, pCoeffs, mu, postShift (not for f32), pState. Also set all of the values in pState to zero.
  124. *
  125. * \par
  126. * Use of the initialization function is optional.
  127. * However, if the initialization function is used, then the instance structure cannot be placed into a const data section.
  128. * To place an instance structure into a const data section, the instance structure must be manually initialized.
  129. * Set the values in the state buffer to zeros before static initialization.
  130. * The code below statically initializes each of the 3 different data type filter instance structures
  131. * <pre>
  132. * arm_lms_instance_f32 S = {numTaps, pState, pCoeffs, mu};
  133. * arm_lms_instance_q31 S = {numTaps, pState, pCoeffs, mu, postShift};
  134. * arm_lms_instance_q15 S = {numTaps, pState, pCoeffs, mu, postShift};
  135. * </pre>
  136. * where <code>numTaps</code> is the number of filter coefficients in the filter; <code>pState</code> is the address of the state buffer;
  137. * <code>pCoeffs</code> is the address of the coefficient buffer; <code>mu</code> is the step size parameter; and <code>postShift</code> is the shift applied to coefficients.
  138. *
  139. * \par Fixed-Point Behavior:
  140. * Care must be taken when using the Q15 and Q31 versions of the LMS filter.
  141. * The following issues must be considered:
  142. * - Scaling of coefficients
  143. * - Overflow and saturation
  144. *
  145. * \par Scaling of Coefficients:
  146. * Filter coefficients are represented as fractional values and
  147. * coefficients are restricted to lie in the range <code>[-1 +1)</code>.
  148. * The fixed-point functions have an additional scaling parameter <code>postShift</code>.
  149. * At the output of the filter's accumulator is a shift register which shifts the result by <code>postShift</code> bits.
  150. * This essentially scales the filter coefficients by <code>2^postShift</code> and
  151. * allows the filter coefficients to exceed the range <code>[+1 -1)</code>.
  152. * The value of <code>postShift</code> is set by the user based on the expected gain through the system being modeled.
  153. *
  154. * \par Overflow and Saturation:
  155. * Overflow and saturation behavior of the fixed-point Q15 and Q31 versions are
  156. * described separately as part of the function specific documentation below.
  157. */
  158. /**
  159. * @addtogroup LMS
  160. * @{
  161. */
  162. /**
  163. * @details
  164. * This function operates on floating-point data types.
  165. *
  166. * @brief Processing function for floating-point LMS filter.
  167. * @param[in] *S points to an instance of the floating-point LMS filter structure.
  168. * @param[in] *pSrc points to the block of input data.
  169. * @param[in] *pRef points to the block of reference data.
  170. * @param[out] *pOut points to the block of output data.
  171. * @param[out] *pErr points to the block of error data.
  172. * @param[in] blockSize number of samples to process.
  173. * @return none.
  174. */
  175. void arm_lms_f32(
  176. const arm_lms_instance_f32 * S,
  177. float32_t * pSrc,
  178. float32_t * pRef,
  179. float32_t * pOut,
  180. float32_t * pErr,
  181. uint32_t blockSize)
  182. {
  183. float32_t *pState = S->pState; /* State pointer */
  184. float32_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */
  185. float32_t *pStateCurnt; /* Points to the current sample of the state */
  186. float32_t *px, *pb; /* Temporary pointers for state and coefficient buffers */
  187. float32_t mu = S->mu; /* Adaptive factor */
  188. uint32_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */
  189. uint32_t tapCnt, blkCnt; /* Loop counters */
  190. float32_t sum, e, d; /* accumulator, error, reference data sample */
  191. float32_t w = 0.0f; /* weight factor */
  192. e = 0.0f;
  193. d = 0.0f;
  194. /* S->pState points to state array which contains previous frame (numTaps - 1) samples */
  195. /* pStateCurnt points to the location where the new input data should be written */
  196. pStateCurnt = &(S->pState[(numTaps - 1u)]);
  197. blkCnt = blockSize;
  198. #ifndef ARM_MATH_CM0_FAMILY
  199. /* Run the below code for Cortex-M4 and Cortex-M3 */
  200. while(blkCnt > 0u)
  201. {
  202. /* Copy the new input sample into the state buffer */
  203. *pStateCurnt++ = *pSrc++;
  204. /* Initialize pState pointer */
  205. px = pState;
  206. /* Initialize coeff pointer */
  207. pb = (pCoeffs);
  208. /* Set the accumulator to zero */
  209. sum = 0.0f;
  210. /* Loop unrolling. Process 4 taps at a time. */
  211. tapCnt = numTaps >> 2;
  212. while(tapCnt > 0u)
  213. {
  214. /* Perform the multiply-accumulate */
  215. sum += (*px++) * (*pb++);
  216. sum += (*px++) * (*pb++);
  217. sum += (*px++) * (*pb++);
  218. sum += (*px++) * (*pb++);
  219. /* Decrement the loop counter */
  220. tapCnt--;
  221. }
  222. /* If the filter length is not a multiple of 4, compute the remaining filter taps */
  223. tapCnt = numTaps % 0x4u;
  224. while(tapCnt > 0u)
  225. {
  226. /* Perform the multiply-accumulate */
  227. sum += (*px++) * (*pb++);
  228. /* Decrement the loop counter */
  229. tapCnt--;
  230. }
  231. /* The result in the accumulator, store in the destination buffer. */
  232. *pOut++ = sum;
  233. /* Compute and store error */
  234. d = (float32_t) (*pRef++);
  235. e = d - sum;
  236. *pErr++ = e;
  237. /* Calculation of Weighting factor for the updating filter coefficients */
  238. w = e * mu;
  239. /* Initialize pState pointer */
  240. px = pState;
  241. /* Initialize coeff pointer */
  242. pb = (pCoeffs);
  243. /* Loop unrolling. Process 4 taps at a time. */
  244. tapCnt = numTaps >> 2;
  245. /* Update filter coefficients */
  246. while(tapCnt > 0u)
  247. {
  248. /* Perform the multiply-accumulate */
  249. *pb = *pb + (w * (*px++));
  250. pb++;
  251. *pb = *pb + (w * (*px++));
  252. pb++;
  253. *pb = *pb + (w * (*px++));
  254. pb++;
  255. *pb = *pb + (w * (*px++));
  256. pb++;
  257. /* Decrement the loop counter */
  258. tapCnt--;
  259. }
  260. /* If the filter length is not a multiple of 4, compute the remaining filter taps */
  261. tapCnt = numTaps % 0x4u;
  262. while(tapCnt > 0u)
  263. {
  264. /* Perform the multiply-accumulate */
  265. *pb = *pb + (w * (*px++));
  266. pb++;
  267. /* Decrement the loop counter */
  268. tapCnt--;
  269. }
  270. /* Advance state pointer by 1 for the next sample */
  271. pState = pState + 1;
  272. /* Decrement the loop counter */
  273. blkCnt--;
  274. }
  275. /* Processing is complete. Now copy the last numTaps - 1 samples to the
  276. satrt of the state buffer. This prepares the state buffer for the
  277. next function call. */
  278. /* Points to the start of the pState buffer */
  279. pStateCurnt = S->pState;
  280. /* Loop unrolling for (numTaps - 1u) samples copy */
  281. tapCnt = (numTaps - 1u) >> 2u;
  282. /* copy data */
  283. while(tapCnt > 0u)
  284. {
  285. *pStateCurnt++ = *pState++;
  286. *pStateCurnt++ = *pState++;
  287. *pStateCurnt++ = *pState++;
  288. *pStateCurnt++ = *pState++;
  289. /* Decrement the loop counter */
  290. tapCnt--;
  291. }
  292. /* Calculate remaining number of copies */
  293. tapCnt = (numTaps - 1u) % 0x4u;
  294. /* Copy the remaining q31_t data */
  295. while(tapCnt > 0u)
  296. {
  297. *pStateCurnt++ = *pState++;
  298. /* Decrement the loop counter */
  299. tapCnt--;
  300. }
  301. #else
  302. /* Run the below code for Cortex-M0 */
  303. while(blkCnt > 0u)
  304. {
  305. /* Copy the new input sample into the state buffer */
  306. *pStateCurnt++ = *pSrc++;
  307. /* Initialize pState pointer */
  308. px = pState;
  309. /* Initialize pCoeffs pointer */
  310. pb = pCoeffs;
  311. /* Set the accumulator to zero */
  312. sum = 0.0f;
  313. /* Loop over numTaps number of values */
  314. tapCnt = numTaps;
  315. while(tapCnt > 0u)
  316. {
  317. /* Perform the multiply-accumulate */
  318. sum += (*px++) * (*pb++);
  319. /* Decrement the loop counter */
  320. tapCnt--;
  321. }
  322. /* The result is stored in the destination buffer. */
  323. *pOut++ = sum;
  324. /* Compute and store error */
  325. d = (float32_t) (*pRef++);
  326. e = d - sum;
  327. *pErr++ = e;
  328. /* Weighting factor for the LMS version */
  329. w = e * mu;
  330. /* Initialize pState pointer */
  331. px = pState;
  332. /* Initialize pCoeffs pointer */
  333. pb = pCoeffs;
  334. /* Loop over numTaps number of values */
  335. tapCnt = numTaps;
  336. while(tapCnt > 0u)
  337. {
  338. /* Perform the multiply-accumulate */
  339. *pb = *pb + (w * (*px++));
  340. pb++;
  341. /* Decrement the loop counter */
  342. tapCnt--;
  343. }
  344. /* Advance state pointer by 1 for the next sample */
  345. pState = pState + 1;
  346. /* Decrement the loop counter */
  347. blkCnt--;
  348. }
  349. /* Processing is complete. Now copy the last numTaps - 1 samples to the
  350. * start of the state buffer. This prepares the state buffer for the
  351. * next function call. */
  352. /* Points to the start of the pState buffer */
  353. pStateCurnt = S->pState;
  354. /* Copy (numTaps - 1u) samples */
  355. tapCnt = (numTaps - 1u);
  356. /* Copy the data */
  357. while(tapCnt > 0u)
  358. {
  359. *pStateCurnt++ = *pState++;
  360. /* Decrement the loop counter */
  361. tapCnt--;
  362. }
  363. #endif /* #ifndef ARM_MATH_CM0_FAMILY */
  364. }
  365. /**
  366. * @} end of LMS group
  367. */