annotate ext/kissfft/test/testcpp.cc @ 409:1f1999b0f577

Bring in kissfft into this repo (formerly a subrepo, but the remote is not responding)
author Chris Cannam <c.cannam@qmul.ac.uk>
date Tue, 21 Jul 2015 07:34:15 +0100
parents
children
rev   line source
c@409 1 #include "kissfft.hh"
c@409 2 #include <iostream>
c@409 3 #include <cstdlib>
c@409 4 #include <typeinfo>
c@409 5
c@409 6 #include <sys/time.h>
c@409 7 static inline
c@409 8 double curtime(void)
c@409 9 {
c@409 10 struct timeval tv;
c@409 11 gettimeofday(&tv, NULL);
c@409 12 return (double)tv.tv_sec + (double)tv.tv_usec*.000001;
c@409 13 }
c@409 14
c@409 15 using namespace std;
c@409 16
c@409 17 template <class T>
c@409 18 void dotest(int nfft)
c@409 19 {
c@409 20 typedef kissfft<T> FFT;
c@409 21 typedef std::complex<T> cpx_type;
c@409 22
c@409 23 cout << "type:" << typeid(T).name() << " nfft:" << nfft;
c@409 24
c@409 25 FFT fft(nfft,false);
c@409 26
c@409 27 vector<cpx_type> inbuf(nfft);
c@409 28 vector<cpx_type> outbuf(nfft);
c@409 29 for (int k=0;k<nfft;++k)
c@409 30 inbuf[k]= cpx_type(
c@409 31 (T)(rand()/(double)RAND_MAX - .5),
c@409 32 (T)(rand()/(double)RAND_MAX - .5) );
c@409 33 fft.transform( &inbuf[0] , &outbuf[0] );
c@409 34
c@409 35 long double totalpower=0;
c@409 36 long double difpower=0;
c@409 37 for (int k0=0;k0<nfft;++k0) {
c@409 38 complex<long double> acc = 0;
c@409 39 long double phinc = 2*k0* M_PIl / nfft;
c@409 40 for (int k1=0;k1<nfft;++k1) {
c@409 41 complex<long double> x(inbuf[k1].real(),inbuf[k1].imag());
c@409 42 acc += x * exp( complex<long double>(0,-k1*phinc) );
c@409 43 }
c@409 44 totalpower += norm(acc);
c@409 45 complex<long double> x(outbuf[k0].real(),outbuf[k0].imag());
c@409 46 complex<long double> dif = acc - x;
c@409 47 difpower += norm(dif);
c@409 48 }
c@409 49 cout << " RMSE:" << sqrt(difpower/totalpower) << "\t";
c@409 50
c@409 51 double t0 = curtime();
c@409 52 int nits=20e6/nfft;
c@409 53 for (int k=0;k<nits;++k) {
c@409 54 fft.transform( &inbuf[0] , &outbuf[0] );
c@409 55 }
c@409 56 double t1 = curtime();
c@409 57 cout << " MSPS:" << ( (nits*nfft)*1e-6/ (t1-t0) ) << endl;
c@409 58 }
c@409 59
c@409 60 int main(int argc,char ** argv)
c@409 61 {
c@409 62 if (argc>1) {
c@409 63 for (int k=1;k<argc;++k) {
c@409 64 int nfft = atoi(argv[k]);
c@409 65 dotest<float>(nfft); dotest<double>(nfft); dotest<long double>(nfft);
c@409 66 }
c@409 67 }else{
c@409 68 dotest<float>(32); dotest<double>(32); dotest<long double>(32);
c@409 69 dotest<float>(1024); dotest<double>(1024); dotest<long double>(1024);
c@409 70 dotest<float>(840); dotest<double>(840); dotest<long double>(840);
c@409 71 }
c@409 72 return 0;
c@409 73 }