Advertisement
Guest User

Untitled

a guest
Aug 16th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. /*****************************************************************************
  2. * *
  3. * Small test program for FFTReal class. *
  4. * *
  5. ******************************************************************************/
  6.  
  7.  
  8.  
  9. #include "FFTReal.h"
  10.  
  11. #include <new>
  12.  
  13. #include <cassert>
  14. #include <cmath>
  15. #include <cstdio>
  16. #include <cstdlib>
  17. #include <ctime>
  18.  
  19.  
  20.  
  21. /* Another MS VC++ trick: "new" doesn't throw exception by default */
  22. #if defined (_MSC_VER)
  23.  
  24. #include <new.h>
  25. int my_new_handler (size_t dummy)
  26. {
  27. throw std::bad_alloc ();
  28. return (0);
  29. }
  30.  
  31. #endif // _MSC_VER
  32.  
  33.  
  34.  
  35. int main (void)
  36. {
  37. #if defined (_MSC_VER)
  38. _set_new_handler (my_new_handler);
  39. #endif // _MSC_VER
  40.  
  41. try
  42. {
  43.  
  44. /*______________________________________________
  45. *
  46. * Exactness test
  47. *______________________________________________
  48. */
  49.  
  50. {
  51. printf ("Accuracy test:\n\n");
  52.  
  53. const long nbr_points = 16; // Power of 2
  54. float * const x = new float [nbr_points];
  55. float * const f = new float [nbr_points];
  56. FFTReal fft (nbr_points); // FFT object initialized here
  57. long i;
  58.  
  59. /* Test signal */
  60. const double PI = atan (1) * 4;
  61. for (i = 0; i < nbr_points; ++ i)
  62. {
  63. x [i] = (float) (-1
  64. + sin (3*2*PI*i/nbr_points)
  65. + cos (5*2*PI*i/nbr_points) * 2
  66. - sin (7*2*PI*i/nbr_points) * 3
  67. + cos (8*2*PI*i/nbr_points) * 5);
  68. }
  69.  
  70. /* Compute FFT and IFFT */
  71. fft.do_fft (f, x);
  72. fft.do_ifft (f, x);
  73. fft.rescale (x);
  74.  
  75. /* Display the result */
  76. printf ("FFT:\n");
  77. for (i = 0; i <= nbr_points/2; i ++)
  78. {
  79. double img;
  80.  
  81. const double real = f [i];
  82. if (i > 0 && i < nbr_points/2)
  83. {
  84. img = f [i + nbr_points/2];
  85. }
  86. else
  87. {
  88. img = 0;
  89. }
  90.  
  91. const double f_abs = sqrt (real * real + img * img);
  92. printf ("%5d: %+12.6f %+12.6f (%12.6f)\n", i, real, img, f_abs);
  93. }
  94. printf ("\n");
  95.  
  96. printf ("IFFT:\n");
  97. for (i = 0; i < nbr_points; i ++)
  98. {
  99. printf ("%5d: %+f\n", i, double (x [i]));
  100. }
  101. printf ("\n");
  102.  
  103. delete [] x;
  104. delete [] f;
  105. }
  106.  
  107. /*______________________________________________
  108. *
  109. * Speed test
  110. *______________________________________________
  111. */
  112.  
  113. {
  114. printf ("Speed test:\nPlease wait...\n\n");
  115.  
  116. const long nbr_points = 1024; // Power of 2
  117. const long buffer_size = 256*1024L; // Number of float (float or double)
  118. const long nbr_tests = 10000;
  119.  
  120. assert (nbr_points <= buffer_size);
  121. float * const x = new float [buffer_size];
  122. float * const f = new float [buffer_size];
  123. FFTReal fft (nbr_points); // FFT object initialized here
  124. long i;
  125.  
  126. /* Test signal: noise */
  127. for (i = 0; i < nbr_points; ++ i)
  128. {
  129. x [i] = (float) rand () - (RAND_MAX >> 1);
  130. }
  131.  
  132. clock_t tt0 = clock ();
  133. for (i = 0; i < nbr_tests; ++ i)
  134. {
  135. const long offset = (i * nbr_points) & (buffer_size - 1);
  136. fft.do_fft (f + offset, x + offset);
  137. }
  138. clock_t tt1 = clock ();
  139. for (i = 0; i < nbr_tests; ++ i)
  140. {
  141. const long offset = (i * nbr_points) & (buffer_size - 1);
  142. fft.do_ifft (f + offset, x + offset);
  143. fft.rescale (x);
  144. }
  145. clock_t tt2 = clock ();
  146.  
  147. const double t1 = double (tt1 - tt0) / nbr_tests / CLOCKS_PER_SEC;
  148. const double t2 = double (tt2 - tt1) / nbr_tests / CLOCKS_PER_SEC;
  149. printf ("%ld-points FFT : %.0f us.\n", nbr_points, t1 * 1000000);
  150. printf ("%ld-points IFFT + scaling: %.0f us.\n", nbr_points, t2 * 1000000);
  151.  
  152. const long nbr_s_chn = long (floor (nbr_points / ((t1 + t2) * 44100 * 2)));
  153. printf ("Peak performance: FFT+IFFT on %ld mono channels at 44.1 KHz (with overlapping).\n", nbr_s_chn);
  154. printf ("\n");
  155.  
  156. delete [] x;
  157. delete [] f;
  158. }
  159.  
  160. printf ("Press [Return] key to terminate...\n");
  161. getchar ();
  162. }
  163.  
  164. catch (std::exception e)
  165. {
  166. printf ("Exception: %s\n", e.what ());
  167. }
  168.  
  169. catch (...)
  170. {
  171. printf ("Unknown execption captured. Program termination.\n");
  172. exit (-1);
  173. }
  174.  
  175. return (0);
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement