A bundled STM32F10x Std Periph and CMSIS library
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 

648 lignes
19 KiB

  1. /* ----------------------------------------------------------------------------
  2. * Copyright (C) 2010-2014 ARM Limited. All rights reserved.
  3. *
  4. * $Date: 12. March 2014
  5. * $Revision: V1.4.4
  6. *
  7. * Project: CMSIS DSP Library
  8. * Title: arm_conv_f32.c
  9. *
  10. * Description: Convolution of floating-point sequences.
  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 Conv Convolution
  46. *
  47. * Convolution is a mathematical operation that operates on two finite length vectors to generate a finite length output vector.
  48. * Convolution is similar to correlation and is frequently used in filtering and data analysis.
  49. * The CMSIS DSP library contains functions for convolving Q7, Q15, Q31, and floating-point data types.
  50. * The library also provides fast versions of the Q15 and Q31 functions on Cortex-M4 and Cortex-M3.
  51. *
  52. * \par Algorithm
  53. * Let <code>a[n]</code> and <code>b[n]</code> be sequences of length <code>srcALen</code> and <code>srcBLen</code> samples respectively.
  54. * Then the convolution
  55. *
  56. * <pre>
  57. * c[n] = a[n] * b[n]
  58. * </pre>
  59. *
  60. * \par
  61. * is defined as
  62. * \image html ConvolutionEquation.gif
  63. * \par
  64. * Note that <code>c[n]</code> is of length <code>srcALen + srcBLen - 1</code> and is defined over the interval <code>n=0, 1, 2, ..., srcALen + srcBLen - 2</code>.
  65. * <code>pSrcA</code> points to the first input vector of length <code>srcALen</code> and
  66. * <code>pSrcB</code> points to the second input vector of length <code>srcBLen</code>.
  67. * The output result is written to <code>pDst</code> and the calling function must allocate <code>srcALen+srcBLen-1</code> words for the result.
  68. *
  69. * \par
  70. * Conceptually, when two signals <code>a[n]</code> and <code>b[n]</code> are convolved,
  71. * the signal <code>b[n]</code> slides over <code>a[n]</code>.
  72. * For each offset \c n, the overlapping portions of a[n] and b[n] are multiplied and summed together.
  73. *
  74. * \par
  75. * Note that convolution is a commutative operation:
  76. *
  77. * <pre>
  78. * a[n] * b[n] = b[n] * a[n].
  79. * </pre>
  80. *
  81. * \par
  82. * This means that switching the A and B arguments to the convolution functions has no effect.
  83. *
  84. * <b>Fixed-Point Behavior</b>
  85. *
  86. * \par
  87. * Convolution requires summing up a large number of intermediate products.
  88. * As such, the Q7, Q15, and Q31 functions run a risk of overflow and saturation.
  89. * Refer to the function specific documentation below for further details of the particular algorithm used.
  90. *
  91. *
  92. * <b>Fast Versions</b>
  93. *
  94. * \par
  95. * Fast versions are supported for Q31 and Q15. Cycles for Fast versions are less compared to Q31 and Q15 of conv and the design requires
  96. * the input signals should be scaled down to avoid intermediate overflows.
  97. *
  98. *
  99. * <b>Opt Versions</b>
  100. *
  101. * \par
  102. * Opt versions are supported for Q15 and Q7. Design uses internal scratch buffer for getting good optimisation.
  103. * These versions are optimised in cycles and consumes more memory(Scratch memory) compared to Q15 and Q7 versions
  104. */
  105. /**
  106. * @addtogroup Conv
  107. * @{
  108. */
  109. /**
  110. * @brief Convolution of floating-point sequences.
  111. * @param[in] *pSrcA points to the first input sequence.
  112. * @param[in] srcALen length of the first input sequence.
  113. * @param[in] *pSrcB points to the second input sequence.
  114. * @param[in] srcBLen length of the second input sequence.
  115. * @param[out] *pDst points to the location where the output result is written. Length srcALen+srcBLen-1.
  116. * @return none.
  117. */
  118. void arm_conv_f32(
  119. float32_t * pSrcA,
  120. uint32_t srcALen,
  121. float32_t * pSrcB,
  122. uint32_t srcBLen,
  123. float32_t * pDst)
  124. {
  125. #ifndef ARM_MATH_CM0_FAMILY
  126. /* Run the below code for Cortex-M4 and Cortex-M3 */
  127. float32_t *pIn1; /* inputA pointer */
  128. float32_t *pIn2; /* inputB pointer */
  129. float32_t *pOut = pDst; /* output pointer */
  130. float32_t *px; /* Intermediate inputA pointer */
  131. float32_t *py; /* Intermediate inputB pointer */
  132. float32_t *pSrc1, *pSrc2; /* Intermediate pointers */
  133. float32_t sum, acc0, acc1, acc2, acc3; /* Accumulator */
  134. float32_t x0, x1, x2, x3, c0; /* Temporary variables to hold state and coefficient values */
  135. uint32_t j, k, count, blkCnt, blockSize1, blockSize2, blockSize3; /* loop counters */
  136. /* The algorithm implementation is based on the lengths of the inputs. */
  137. /* srcB is always made to slide across srcA. */
  138. /* So srcBLen is always considered as shorter or equal to srcALen */
  139. if(srcALen >= srcBLen)
  140. {
  141. /* Initialization of inputA pointer */
  142. pIn1 = pSrcA;
  143. /* Initialization of inputB pointer */
  144. pIn2 = pSrcB;
  145. }
  146. else
  147. {
  148. /* Initialization of inputA pointer */
  149. pIn1 = pSrcB;
  150. /* Initialization of inputB pointer */
  151. pIn2 = pSrcA;
  152. /* srcBLen is always considered as shorter or equal to srcALen */
  153. j = srcBLen;
  154. srcBLen = srcALen;
  155. srcALen = j;
  156. }
  157. /* conv(x,y) at n = x[n] * y[0] + x[n-1] * y[1] + x[n-2] * y[2] + ...+ x[n-N+1] * y[N -1] */
  158. /* The function is internally
  159. * divided into three stages according to the number of multiplications that has to be
  160. * taken place between inputA samples and inputB samples. In the first stage of the
  161. * algorithm, the multiplications increase by one for every iteration.
  162. * In the second stage of the algorithm, srcBLen number of multiplications are done.
  163. * In the third stage of the algorithm, the multiplications decrease by one
  164. * for every iteration. */
  165. /* The algorithm is implemented in three stages.
  166. The loop counters of each stage is initiated here. */
  167. blockSize1 = srcBLen - 1u;
  168. blockSize2 = srcALen - (srcBLen - 1u);
  169. blockSize3 = blockSize1;
  170. /* --------------------------
  171. * initializations of stage1
  172. * -------------------------*/
  173. /* sum = x[0] * y[0]
  174. * sum = x[0] * y[1] + x[1] * y[0]
  175. * ....
  176. * sum = x[0] * y[srcBlen - 1] + x[1] * y[srcBlen - 2] +...+ x[srcBLen - 1] * y[0]
  177. */
  178. /* In this stage the MAC operations are increased by 1 for every iteration.
  179. The count variable holds the number of MAC operations performed */
  180. count = 1u;
  181. /* Working pointer of inputA */
  182. px = pIn1;
  183. /* Working pointer of inputB */
  184. py = pIn2;
  185. /* ------------------------
  186. * Stage1 process
  187. * ----------------------*/
  188. /* The first stage starts here */
  189. while(blockSize1 > 0u)
  190. {
  191. /* Accumulator is made zero for every iteration */
  192. sum = 0.0f;
  193. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  194. k = count >> 2u;
  195. /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
  196. ** a second loop below computes MACs for the remaining 1 to 3 samples. */
  197. while(k > 0u)
  198. {
  199. /* x[0] * y[srcBLen - 1] */
  200. sum += *px++ * *py--;
  201. /* x[1] * y[srcBLen - 2] */
  202. sum += *px++ * *py--;
  203. /* x[2] * y[srcBLen - 3] */
  204. sum += *px++ * *py--;
  205. /* x[3] * y[srcBLen - 4] */
  206. sum += *px++ * *py--;
  207. /* Decrement the loop counter */
  208. k--;
  209. }
  210. /* If the count is not a multiple of 4, compute any remaining MACs here.
  211. ** No loop unrolling is used. */
  212. k = count % 0x4u;
  213. while(k > 0u)
  214. {
  215. /* Perform the multiply-accumulate */
  216. sum += *px++ * *py--;
  217. /* Decrement the loop counter */
  218. k--;
  219. }
  220. /* Store the result in the accumulator in the destination buffer. */
  221. *pOut++ = sum;
  222. /* Update the inputA and inputB pointers for next MAC calculation */
  223. py = pIn2 + count;
  224. px = pIn1;
  225. /* Increment the MAC count */
  226. count++;
  227. /* Decrement the loop counter */
  228. blockSize1--;
  229. }
  230. /* --------------------------
  231. * Initializations of stage2
  232. * ------------------------*/
  233. /* sum = x[0] * y[srcBLen-1] + x[1] * y[srcBLen-2] +...+ x[srcBLen-1] * y[0]
  234. * sum = x[1] * y[srcBLen-1] + x[2] * y[srcBLen-2] +...+ x[srcBLen] * y[0]
  235. * ....
  236. * sum = x[srcALen-srcBLen-2] * y[srcBLen-1] + x[srcALen] * y[srcBLen-2] +...+ x[srcALen-1] * y[0]
  237. */
  238. /* Working pointer of inputA */
  239. px = pIn1;
  240. /* Working pointer of inputB */
  241. pSrc2 = pIn2 + (srcBLen - 1u);
  242. py = pSrc2;
  243. /* count is index by which the pointer pIn1 to be incremented */
  244. count = 0u;
  245. /* -------------------
  246. * Stage2 process
  247. * ------------------*/
  248. /* Stage2 depends on srcBLen as in this stage srcBLen number of MACS are performed.
  249. * So, to loop unroll over blockSize2,
  250. * srcBLen should be greater than or equal to 4 */
  251. if(srcBLen >= 4u)
  252. {
  253. /* Loop unroll over blockSize2, by 4 */
  254. blkCnt = blockSize2 >> 2u;
  255. while(blkCnt > 0u)
  256. {
  257. /* Set all accumulators to zero */
  258. acc0 = 0.0f;
  259. acc1 = 0.0f;
  260. acc2 = 0.0f;
  261. acc3 = 0.0f;
  262. /* read x[0], x[1], x[2] samples */
  263. x0 = *(px++);
  264. x1 = *(px++);
  265. x2 = *(px++);
  266. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  267. k = srcBLen >> 2u;
  268. /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
  269. ** a second loop below computes MACs for the remaining 1 to 3 samples. */
  270. do
  271. {
  272. /* Read y[srcBLen - 1] sample */
  273. c0 = *(py--);
  274. /* Read x[3] sample */
  275. x3 = *(px);
  276. /* Perform the multiply-accumulate */
  277. /* acc0 += x[0] * y[srcBLen - 1] */
  278. acc0 += x0 * c0;
  279. /* acc1 += x[1] * y[srcBLen - 1] */
  280. acc1 += x1 * c0;
  281. /* acc2 += x[2] * y[srcBLen - 1] */
  282. acc2 += x2 * c0;
  283. /* acc3 += x[3] * y[srcBLen - 1] */
  284. acc3 += x3 * c0;
  285. /* Read y[srcBLen - 2] sample */
  286. c0 = *(py--);
  287. /* Read x[4] sample */
  288. x0 = *(px + 1u);
  289. /* Perform the multiply-accumulate */
  290. /* acc0 += x[1] * y[srcBLen - 2] */
  291. acc0 += x1 * c0;
  292. /* acc1 += x[2] * y[srcBLen - 2] */
  293. acc1 += x2 * c0;
  294. /* acc2 += x[3] * y[srcBLen - 2] */
  295. acc2 += x3 * c0;
  296. /* acc3 += x[4] * y[srcBLen - 2] */
  297. acc3 += x0 * c0;
  298. /* Read y[srcBLen - 3] sample */
  299. c0 = *(py--);
  300. /* Read x[5] sample */
  301. x1 = *(px + 2u);
  302. /* Perform the multiply-accumulates */
  303. /* acc0 += x[2] * y[srcBLen - 3] */
  304. acc0 += x2 * c0;
  305. /* acc1 += x[3] * y[srcBLen - 2] */
  306. acc1 += x3 * c0;
  307. /* acc2 += x[4] * y[srcBLen - 2] */
  308. acc2 += x0 * c0;
  309. /* acc3 += x[5] * y[srcBLen - 2] */
  310. acc3 += x1 * c0;
  311. /* Read y[srcBLen - 4] sample */
  312. c0 = *(py--);
  313. /* Read x[6] sample */
  314. x2 = *(px + 3u);
  315. px += 4u;
  316. /* Perform the multiply-accumulates */
  317. /* acc0 += x[3] * y[srcBLen - 4] */
  318. acc0 += x3 * c0;
  319. /* acc1 += x[4] * y[srcBLen - 4] */
  320. acc1 += x0 * c0;
  321. /* acc2 += x[5] * y[srcBLen - 4] */
  322. acc2 += x1 * c0;
  323. /* acc3 += x[6] * y[srcBLen - 4] */
  324. acc3 += x2 * c0;
  325. } while(--k);
  326. /* If the srcBLen is not a multiple of 4, compute any remaining MACs here.
  327. ** No loop unrolling is used. */
  328. k = srcBLen % 0x4u;
  329. while(k > 0u)
  330. {
  331. /* Read y[srcBLen - 5] sample */
  332. c0 = *(py--);
  333. /* Read x[7] sample */
  334. x3 = *(px++);
  335. /* Perform the multiply-accumulates */
  336. /* acc0 += x[4] * y[srcBLen - 5] */
  337. acc0 += x0 * c0;
  338. /* acc1 += x[5] * y[srcBLen - 5] */
  339. acc1 += x1 * c0;
  340. /* acc2 += x[6] * y[srcBLen - 5] */
  341. acc2 += x2 * c0;
  342. /* acc3 += x[7] * y[srcBLen - 5] */
  343. acc3 += x3 * c0;
  344. /* Reuse the present samples for the next MAC */
  345. x0 = x1;
  346. x1 = x2;
  347. x2 = x3;
  348. /* Decrement the loop counter */
  349. k--;
  350. }
  351. /* Store the result in the accumulator in the destination buffer. */
  352. *pOut++ = acc0;
  353. *pOut++ = acc1;
  354. *pOut++ = acc2;
  355. *pOut++ = acc3;
  356. /* Increment the pointer pIn1 index, count by 4 */
  357. count += 4u;
  358. /* Update the inputA and inputB pointers for next MAC calculation */
  359. px = pIn1 + count;
  360. py = pSrc2;
  361. /* Decrement the loop counter */
  362. blkCnt--;
  363. }
  364. /* If the blockSize2 is not a multiple of 4, compute any remaining output samples here.
  365. ** No loop unrolling is used. */
  366. blkCnt = blockSize2 % 0x4u;
  367. while(blkCnt > 0u)
  368. {
  369. /* Accumulator is made zero for every iteration */
  370. sum = 0.0f;
  371. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  372. k = srcBLen >> 2u;
  373. /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
  374. ** a second loop below computes MACs for the remaining 1 to 3 samples. */
  375. while(k > 0u)
  376. {
  377. /* Perform the multiply-accumulates */
  378. sum += *px++ * *py--;
  379. sum += *px++ * *py--;
  380. sum += *px++ * *py--;
  381. sum += *px++ * *py--;
  382. /* Decrement the loop counter */
  383. k--;
  384. }
  385. /* If the srcBLen is not a multiple of 4, compute any remaining MACs here.
  386. ** No loop unrolling is used. */
  387. k = srcBLen % 0x4u;
  388. while(k > 0u)
  389. {
  390. /* Perform the multiply-accumulate */
  391. sum += *px++ * *py--;
  392. /* Decrement the loop counter */
  393. k--;
  394. }
  395. /* Store the result in the accumulator in the destination buffer. */
  396. *pOut++ = sum;
  397. /* Increment the MAC count */
  398. count++;
  399. /* Update the inputA and inputB pointers for next MAC calculation */
  400. px = pIn1 + count;
  401. py = pSrc2;
  402. /* Decrement the loop counter */
  403. blkCnt--;
  404. }
  405. }
  406. else
  407. {
  408. /* If the srcBLen is not a multiple of 4,
  409. * the blockSize2 loop cannot be unrolled by 4 */
  410. blkCnt = blockSize2;
  411. while(blkCnt > 0u)
  412. {
  413. /* Accumulator is made zero for every iteration */
  414. sum = 0.0f;
  415. /* srcBLen number of MACS should be performed */
  416. k = srcBLen;
  417. while(k > 0u)
  418. {
  419. /* Perform the multiply-accumulate */
  420. sum += *px++ * *py--;
  421. /* Decrement the loop counter */
  422. k--;
  423. }
  424. /* Store the result in the accumulator in the destination buffer. */
  425. *pOut++ = sum;
  426. /* Increment the MAC count */
  427. count++;
  428. /* Update the inputA and inputB pointers for next MAC calculation */
  429. px = pIn1 + count;
  430. py = pSrc2;
  431. /* Decrement the loop counter */
  432. blkCnt--;
  433. }
  434. }
  435. /* --------------------------
  436. * Initializations of stage3
  437. * -------------------------*/
  438. /* sum += x[srcALen-srcBLen+1] * y[srcBLen-1] + x[srcALen-srcBLen+2] * y[srcBLen-2] +...+ x[srcALen-1] * y[1]
  439. * sum += x[srcALen-srcBLen+2] * y[srcBLen-1] + x[srcALen-srcBLen+3] * y[srcBLen-2] +...+ x[srcALen-1] * y[2]
  440. * ....
  441. * sum += x[srcALen-2] * y[srcBLen-1] + x[srcALen-1] * y[srcBLen-2]
  442. * sum += x[srcALen-1] * y[srcBLen-1]
  443. */
  444. /* In this stage the MAC operations are decreased by 1 for every iteration.
  445. The blockSize3 variable holds the number of MAC operations performed */
  446. /* Working pointer of inputA */
  447. pSrc1 = (pIn1 + srcALen) - (srcBLen - 1u);
  448. px = pSrc1;
  449. /* Working pointer of inputB */
  450. pSrc2 = pIn2 + (srcBLen - 1u);
  451. py = pSrc2;
  452. /* -------------------
  453. * Stage3 process
  454. * ------------------*/
  455. while(blockSize3 > 0u)
  456. {
  457. /* Accumulator is made zero for every iteration */
  458. sum = 0.0f;
  459. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  460. k = blockSize3 >> 2u;
  461. /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
  462. ** a second loop below computes MACs for the remaining 1 to 3 samples. */
  463. while(k > 0u)
  464. {
  465. /* sum += x[srcALen - srcBLen + 1] * y[srcBLen - 1] */
  466. sum += *px++ * *py--;
  467. /* sum += x[srcALen - srcBLen + 2] * y[srcBLen - 2] */
  468. sum += *px++ * *py--;
  469. /* sum += x[srcALen - srcBLen + 3] * y[srcBLen - 3] */
  470. sum += *px++ * *py--;
  471. /* sum += x[srcALen - srcBLen + 4] * y[srcBLen - 4] */
  472. sum += *px++ * *py--;
  473. /* Decrement the loop counter */
  474. k--;
  475. }
  476. /* If the blockSize3 is not a multiple of 4, compute any remaining MACs here.
  477. ** No loop unrolling is used. */
  478. k = blockSize3 % 0x4u;
  479. while(k > 0u)
  480. {
  481. /* Perform the multiply-accumulates */
  482. /* sum += x[srcALen-1] * y[srcBLen-1] */
  483. sum += *px++ * *py--;
  484. /* Decrement the loop counter */
  485. k--;
  486. }
  487. /* Store the result in the accumulator in the destination buffer. */
  488. *pOut++ = sum;
  489. /* Update the inputA and inputB pointers for next MAC calculation */
  490. px = ++pSrc1;
  491. py = pSrc2;
  492. /* Decrement the loop counter */
  493. blockSize3--;
  494. }
  495. #else
  496. /* Run the below code for Cortex-M0 */
  497. float32_t *pIn1 = pSrcA; /* inputA pointer */
  498. float32_t *pIn2 = pSrcB; /* inputB pointer */
  499. float32_t sum; /* Accumulator */
  500. uint32_t i, j; /* loop counters */
  501. /* Loop to calculate convolution for output length number of times */
  502. for (i = 0u; i < ((srcALen + srcBLen) - 1u); i++)
  503. {
  504. /* Initialize sum with zero to carry out MAC operations */
  505. sum = 0.0f;
  506. /* Loop to perform MAC operations according to convolution equation */
  507. for (j = 0u; j <= i; j++)
  508. {
  509. /* Check the array limitations */
  510. if((((i - j) < srcBLen) && (j < srcALen)))
  511. {
  512. /* z[i] += x[i-j] * y[j] */
  513. sum += pIn1[j] * pIn2[i - j];
  514. }
  515. }
  516. /* Store the output in the destination buffer */
  517. pDst[i] = sum;
  518. }
  519. #endif /* #ifndef ARM_MATH_CM0_FAMILY */
  520. }
  521. /**
  522. * @} end of Conv group
  523. */