testcpp.cc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "kissfft.hh"
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <typeinfo>
  5. #include <sys/time.h>
  6. static inline
  7. double curtime(void)
  8. {
  9. struct timeval tv;
  10. gettimeofday(&tv, NULL);
  11. return (double)tv.tv_sec + (double)tv.tv_usec*.000001;
  12. }
  13. using namespace std;
  14. template <class T>
  15. void dotest(int nfft)
  16. {
  17. typedef kissfft<T> FFT;
  18. typedef std::complex<T> cpx_type;
  19. cout << "type:" << typeid(T).name() << " nfft:" << nfft;
  20. FFT fft(nfft,false);
  21. vector<cpx_type> inbuf(nfft);
  22. vector<cpx_type> outbuf(nfft);
  23. for (int k=0;k<nfft;++k)
  24. inbuf[k]= cpx_type(
  25. (T)(rand()/(double)RAND_MAX - .5),
  26. (T)(rand()/(double)RAND_MAX - .5) );
  27. fft.transform( &inbuf[0] , &outbuf[0] );
  28. long double totalpower=0;
  29. long double difpower=0;
  30. for (int k0=0;k0<nfft;++k0) {
  31. complex<long double> acc = 0;
  32. long double phinc = 2*k0* M_PIl / nfft;
  33. for (int k1=0;k1<nfft;++k1) {
  34. complex<long double> x(inbuf[k1].real(),inbuf[k1].imag());
  35. acc += x * exp( complex<long double>(0,-k1*phinc) );
  36. }
  37. totalpower += norm(acc);
  38. complex<long double> x(outbuf[k0].real(),outbuf[k0].imag());
  39. complex<long double> dif = acc - x;
  40. difpower += norm(dif);
  41. }
  42. cout << " RMSE:" << sqrt(difpower/totalpower) << "\t";
  43. double t0 = curtime();
  44. int nits=20e6/nfft;
  45. for (int k=0;k<nits;++k) {
  46. fft.transform( &inbuf[0] , &outbuf[0] );
  47. }
  48. double t1 = curtime();
  49. cout << " MSPS:" << ( (nits*nfft)*1e-6/ (t1-t0) ) << endl;
  50. }
  51. int main(int argc,char ** argv)
  52. {
  53. if (argc>1) {
  54. for (int k=1;k<argc;++k) {
  55. int nfft = atoi(argv[k]);
  56. dotest<float>(nfft); dotest<double>(nfft); dotest<long double>(nfft);
  57. }
  58. }else{
  59. dotest<float>(32); dotest<double>(32); dotest<long double>(32);
  60. dotest<float>(1024); dotest<double>(1024); dotest<long double>(1024);
  61. dotest<float>(840); dotest<double>(840); dotest<long double>(840);
  62. }
  63. return 0;
  64. }