arm_convolution_example_f32.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* ----------------------------------------------------------------------
  2. * Copyright (C) 2010-2012 ARM Limited. All rights reserved.
  3. *
  4. * $Date: 17. January 2013
  5. * $Revision: V1.4.0
  6. *
  7. * Project: CMSIS DSP Library
  8. * Title: arm_convolution_example_f32.c
  9. *
  10. * Description: Example code demonstrating Convolution of two input signals using fft.
  11. *
  12. * Target Processor: Cortex-M4/Cortex-M3
  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. /**
  41. * @ingroup groupExamples
  42. */
  43. /**
  44. * @defgroup ConvolutionExample Convolution Example
  45. *
  46. * \par Description:
  47. * \par
  48. * Demonstrates the convolution theorem with the use of the Complex FFT, Complex-by-Complex
  49. * Multiplication, and Support Functions.
  50. *
  51. * \par Algorithm:
  52. * \par
  53. * The convolution theorem states that convolution in the time domain corresponds to
  54. * multiplication in the frequency domain. Therefore, the Fourier transform of the convoution of
  55. * two signals is equal to the product of their individual Fourier transforms.
  56. * The Fourier transform of a signal can be evaluated efficiently using the Fast Fourier Transform (FFT).
  57. * \par
  58. * Two input signals, <code>a[n]</code> and <code>b[n]</code>, with lengths \c n1 and \c n2 respectively,
  59. * are zero padded so that their lengths become \c N, which is greater than or equal to <code>(n1+n2-1)</code>
  60. * and is a power of 4 as FFT implementation is radix-4.
  61. * The convolution of <code>a[n]</code> and <code>b[n]</code> is obtained by taking the FFT of the input
  62. * signals, multiplying the Fourier transforms of the two signals, and taking the inverse FFT of
  63. * the multiplied result.
  64. * \par
  65. * This is denoted by the following equations:
  66. * <pre> A[k] = FFT(a[n],N)
  67. * B[k] = FFT(b[n],N)
  68. * conv(a[n], b[n]) = IFFT(A[k] * B[k], N)</pre>
  69. * where <code>A[k]</code> and <code>B[k]</code> are the N-point FFTs of the signals <code>a[n]</code>
  70. * and <code>b[n]</code> respectively.
  71. * The length of the convolved signal is <code>(n1+n2-1)</code>.
  72. *
  73. * \par Block Diagram:
  74. * \par
  75. * \image html Convolution.gif
  76. *
  77. * \par Variables Description:
  78. * \par
  79. * \li \c testInputA_f32 points to the first input sequence
  80. * \li \c srcALen length of the first input sequence
  81. * \li \c testInputB_f32 points to the second input sequence
  82. * \li \c srcBLen length of the second input sequence
  83. * \li \c outLen length of convolution output sequence, <code>(srcALen + srcBLen - 1)</code>
  84. * \li \c AxB points to the output array where the product of individual FFTs of inputs is stored.
  85. *
  86. * \par CMSIS DSP Software Library Functions Used:
  87. * \par
  88. * - arm_fill_f32()
  89. * - arm_copy_f32()
  90. * - arm_cfft_radix4_init_f32()
  91. * - arm_cfft_radix4_f32()
  92. * - arm_cmplx_mult_cmplx_f32()
  93. *
  94. * <b> Refer </b>
  95. * \link arm_convolution_example_f32.c \endlink
  96. *
  97. */
  98. /** \example arm_convolution_example_f32.c
  99. */
  100. #include "arm_math.h"
  101. #include "math_helper.h"
  102. #if defined(SEMIHOSTING)
  103. #include <stdio.h>
  104. #endif
  105. /* ----------------------------------------------------------------------
  106. * Defines each of the tests performed
  107. * ------------------------------------------------------------------- */
  108. #define MAX_BLOCKSIZE 128
  109. #define DELTA (0.000001f)
  110. #define SNR_THRESHOLD 90
  111. /* ----------------------------------------------------------------------
  112. * Declare I/O buffers
  113. * ------------------------------------------------------------------- */
  114. float32_t Ak[MAX_BLOCKSIZE]; /* Input A */
  115. float32_t Bk[MAX_BLOCKSIZE]; /* Input B */
  116. float32_t AxB[MAX_BLOCKSIZE * 2]; /* Output */
  117. /* ----------------------------------------------------------------------
  118. * Test input data for Floating point Convolution example for 32-blockSize
  119. * Generated by the MATLAB randn() function
  120. * ------------------------------------------------------------------- */
  121. float32_t testInputA_f32[64] =
  122. {
  123. -0.808920, 1.357369, 1.180861, -0.504544, 1.762637, -0.703285,
  124. 1.696966, 0.620571, -0.151093, -0.100235, -0.872382, -0.403579,
  125. -0.860749, -0.382648, -1.052338, 0.128113, -0.646269, 1.093377,
  126. -2.209198, 0.471706, 0.408901, 1.266242, 0.598252, 1.176827,
  127. -0.203421, 0.213596, -0.851964, -0.466958, 0.021841, -0.698938,
  128. -0.604107, 0.461778, -0.318219, 0.942520, 0.577585, 0.417619,
  129. 0.614665, 0.563679, -1.295073, -0.764437, 0.952194, -0.859222,
  130. -0.618554, -2.268542, -1.210592, 1.655853, -2.627219, -0.994249,
  131. -1.374704, 0.343799, 0.025619, 1.227481, -0.708031, 0.069355,
  132. -1.845228, -1.570886, 1.010668, -1.802084, 1.630088, 1.286090,
  133. -0.161050, -0.940794, 0.367961, 0.291907
  134. };
  135. float32_t testInputB_f32[64] =
  136. {
  137. 0.933724, 0.046881, 1.316470, 0.438345, 0.332682, 2.094885,
  138. 0.512081, 0.035546, 0.050894, -2.320371, 0.168711, -1.830493,
  139. -0.444834, -1.003242, -0.531494, -1.365600, -0.155420, -0.757692,
  140. -0.431880, -0.380021, 0.096243, -0.695835, 0.558850, -1.648962,
  141. 0.020369, -0.363630, 0.887146, 0.845503, -0.252864, -0.330397,
  142. 1.269131, -1.109295, -1.027876, 0.135940, 0.116721, -0.293399,
  143. -1.349799, 0.166078, -0.802201, 0.369367, -0.964568, -2.266011,
  144. 0.465178, 0.651222, -0.325426, 0.320245, -0.784178, -0.579456,
  145. 0.093374, 0.604778, -0.048225, 0.376297, -0.394412, 0.578182,
  146. -1.218141, -1.387326, 0.692462, -0.631297, 0.153137, -0.638952,
  147. 0.635474, -0.970468, 1.334057, -0.111370
  148. };
  149. const float testRefOutput_f32[127] =
  150. {
  151. -0.818943, 1.229484, -0.533664, 1.016604, 0.341875, -1.963656,
  152. 5.171476, 3.478033, 7.616361, 6.648384, 0.479069, 1.792012,
  153. -1.295591, -7.447818, 0.315830, -10.657445, -2.483469, -6.524236,
  154. -7.380591, -3.739005, -8.388957, 0.184147, -1.554888, 3.786508,
  155. -1.684421, 5.400610, -1.578126, 7.403361, 8.315999, 2.080267,
  156. 11.077776, 2.749673, 7.138962, 2.748762, 0.660363, 0.981552,
  157. 1.442275, 0.552721, -2.576892, 4.703989, 0.989156, 8.759344,
  158. -0.564825, -3.994680, 0.954710, -5.014144, 6.592329, 1.599488,
  159. -13.979146, -0.391891, -4.453369, -2.311242, -2.948764, 1.761415,
  160. -0.138322, 10.433007, -2.309103, 4.297153, 8.535523, 3.209462,
  161. 8.695819, 5.569919, 2.514304, 5.582029, 2.060199, 0.642280,
  162. 7.024616, 1.686615, -6.481756, 1.343084, -3.526451, 1.099073,
  163. -2.965764, -0.173723, -4.111484, 6.528384, -6.965658, 1.726291,
  164. 1.535172, 11.023435, 2.338401, -4.690188, 1.298210, 3.943885,
  165. 8.407885, 5.168365, 0.684131, 1.559181, 1.859998, 2.852417,
  166. 8.574070, -6.369078, 6.023458, 11.837963, -6.027632, 4.469678,
  167. -6.799093, -2.674048, 6.250367, -6.809971, -3.459360, 9.112410,
  168. -2.711621, -1.336678, 1.564249, -1.564297, -1.296760, 8.904013,
  169. -3.230109, 6.878013, -7.819823, 3.369909, -1.657410, -2.007358,
  170. -4.112825, 1.370685, -3.420525, -6.276605, 3.244873, -3.352638,
  171. 1.545372, 0.902211, 0.197489, -1.408732, 0.523390, 0.348440, 0
  172. };
  173. /* ----------------------------------------------------------------------
  174. * Declare Global variables
  175. * ------------------------------------------------------------------- */
  176. uint32_t srcALen = 64; /* Length of Input A */
  177. uint32_t srcBLen = 64; /* Length of Input B */
  178. uint32_t outLen; /* Length of convolution output */
  179. float32_t snr; /* output SNR */
  180. int32_t main(void)
  181. {
  182. arm_status status; /* Status of the example */
  183. arm_cfft_radix4_instance_f32 cfft_instance; /* CFFT Structure instance */
  184. #if defined(SEMIHOSTING)
  185. printf("START\n");
  186. #endif
  187. /* CFFT Structure instance pointer */
  188. arm_cfft_radix4_instance_f32 *cfft_instance_ptr =
  189. (arm_cfft_radix4_instance_f32*) &cfft_instance;
  190. /* output length of convolution */
  191. outLen = srcALen + srcBLen - 1;
  192. /* Initialise the fft input buffers with all zeros */
  193. arm_fill_f32(0.0, Ak, MAX_BLOCKSIZE);
  194. arm_fill_f32(0.0, Bk, MAX_BLOCKSIZE);
  195. /* Copy the input values to the fft input buffers */
  196. arm_copy_f32(testInputA_f32, Ak, MAX_BLOCKSIZE/2);
  197. arm_copy_f32(testInputB_f32, Bk, MAX_BLOCKSIZE/2);
  198. /* Initialize the CFFT function to compute 64 point fft */
  199. status = arm_cfft_radix4_init_f32(cfft_instance_ptr, 64, 0, 1);
  200. /* Transform input a[n] from time domain to frequency domain A[k] */
  201. arm_cfft_radix4_f32(cfft_instance_ptr, Ak);
  202. /* Transform input b[n] from time domain to frequency domain B[k] */
  203. arm_cfft_radix4_f32(cfft_instance_ptr, Bk);
  204. /* Complex Multiplication of the two input buffers in frequency domain */
  205. arm_cmplx_mult_cmplx_f32(Ak, Bk, AxB, MAX_BLOCKSIZE/2);
  206. /* Initialize the CIFFT function to compute 64 point ifft */
  207. status = arm_cfft_radix4_init_f32(cfft_instance_ptr, 64, 1, 1);
  208. /* Transform the multiplication output from frequency domain to time domain,
  209. that gives the convolved output. */
  210. arm_cfft_radix4_f32(cfft_instance_ptr, AxB);
  211. /* SNR Calculation */
  212. snr = arm_snr_f32((float32_t *)testRefOutput_f32, AxB, srcALen + srcBLen - 1);
  213. /* Compare the SNR with threshold to test whether the
  214. computed output is matched with the reference output values. */
  215. status = (snr <= SNR_THRESHOLD) ? ARM_MATH_TEST_FAILURE : ARM_MATH_SUCCESS;
  216. if (status != ARM_MATH_SUCCESS)
  217. {
  218. #if defined (SEMIHOSTING)
  219. printf("FAILURE\n");
  220. #else
  221. while (1); /* main function does not return */
  222. #endif
  223. }
  224. else
  225. {
  226. #if defined (SEMIHOSTING)
  227. printf("SUCCESS\n");
  228. #else
  229. while (1); /* main function does not return */
  230. #endif
  231. }
  232. }
  233. /** \endlink */