annotate ext/kissfft/test/testcpp.cc @ 184:76ec2365b250

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