// This file is part of Eigen, a lightweight C++ template library // for linear algebra. Eigen itself is part of the KDE project. // // Copyright (C) 2009 Mark Borgerding mark a borgerding net // // Eigen is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 3 of the License, or (at your option) any later version. // // Alternatively, you can redistribute it and/or // modify it under the terms of the GNU General Public License as // published by the Free Software Foundation; either version 2 of // the License, or (at your option) any later version. // // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the // GNU General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License and a copy of the GNU General Public License along with // Eigen. If not, see . #include "main.h" #include #include template std::complex RandomCpx() { return std::complex( (T)(rand()/(T)RAND_MAX - .5), (T)(rand()/(T)RAND_MAX - .5) ); } using namespace std; using namespace Eigen; float norm(float x) {return x*x;} double norm(double x) {return x*x;} long double norm(long double x) {return x*x;} template < typename T> complex promote(complex x) { return complex(x.real(),x.imag()); } complex promote(float x) { return complex( x); } complex promote(double x) { return complex( x); } complex promote(long double x) { return complex( x); } template long double fft_rmse( const vector & fftbuf,const vector & timebuf) { long double totalpower=0; long double difpower=0; long double pi = acos((long double)-1 ); cerr <<"idx\ttruth\t\tvalue\t|dif|=\n"; for (size_t k0=0;k0 acc = 0; long double phinc = -2.*k0* pi / timebuf.size(); for (size_t k1=0;k1(0,k1*phinc) ); } totalpower += norm(acc); complex x = promote(fftbuf[k0]); complex dif = acc - x; difpower += norm(dif); cerr << k0 << "\t" << acc << "\t" << x << "\t" << sqrt(norm(dif)) << endl; } cerr << "rmse:" << sqrt(difpower/totalpower) << endl; return sqrt(difpower/totalpower); } template long double dif_rmse( const vector buf1,const vector buf2) { long double totalpower=0; long double difpower=0; size_t n = min( buf1.size(),buf2.size() ); for (size_t k=0;k void test_scalar(int nfft) { typedef typename Eigen::FFT::Complex Complex; typedef typename Eigen::FFT::Scalar Scalar; FFT fft; vector inbuf(nfft); vector outbuf; for (int k=0;k() );// gross check vector buf3; fft.inv( &buf3 , outbuf); VERIFY( dif_rmse(inbuf,buf3) < test_precision() );// gross check } template void test_complex(int nfft) { typedef typename Eigen::FFT::Complex Complex; FFT fft; vector inbuf(nfft); vector outbuf; vector buf3; for (int k=0;k(); fft.fwd( &outbuf , inbuf); VERIFY( fft_rmse(outbuf,inbuf) < test_precision() );// gross check fft.inv( &buf3 , outbuf); VERIFY( dif_rmse(inbuf,buf3) < test_precision() );// gross check } template void test_complex2d() { typedef typename Eigen::FFT::Complex Complex; FFT fft; Eigen::Matrix src; Eigen::Matrix dst; Eigen::Matrix src2; Eigen::Matrix dst2; //src = Eigen::Matrix::Random(); src = Eigen::Matrix::Identity(); for (int k=0;k tmpIn = src.col(k); Eigen::Matrix tmpOut; fft.fwd( &tmpOut,tmpIn ); dst2.col(k) = tmpOut; } //cout << "dst2: " << dst2 << "\n\n"; for (int k=0;k tmpIn = dst2.row(k); Eigen::Matrix tmpOut; fft.fwd( &tmpOut, tmpIn); dst2.row(k) = tmpOut; } /* */ fft.fwd2(dst.data(),src.data(),nrows,ncols); fft.inv2(src2.data(),dst.data(),nrows,ncols); /* cout << "src: " << src << "\n\n"; cout << "dst: " << dst << "\n\n"; cout << "src2: " << src2 << "\n\n"; cout << "dst2: " << dst2 << "\n\n"; */ VERIFY( (src-src2).norm() < test_precision() ); VERIFY( (dst-dst2).norm() < test_precision() ); } void test_FFTW() { CALL_SUBTEST( ( test_complex2d () ) ); CALL_SUBTEST( ( test_complex2d () ) ); //CALL_SUBTEST( ( test_complex2d () ) ); CALL_SUBTEST( test_complex(32) ); CALL_SUBTEST( test_complex(32) ); CALL_SUBTEST( test_complex(32) ); CALL_SUBTEST( test_complex(256) ); CALL_SUBTEST( test_complex(256) ); CALL_SUBTEST( test_complex(256) ); CALL_SUBTEST( test_complex(3*8) ); CALL_SUBTEST( test_complex(3*8) ); CALL_SUBTEST( test_complex(3*8) ); CALL_SUBTEST( test_complex(5*32) ); CALL_SUBTEST( test_complex(5*32) ); CALL_SUBTEST( test_complex(5*32) ); CALL_SUBTEST( test_complex(2*3*4) ); CALL_SUBTEST( test_complex(2*3*4) ); CALL_SUBTEST( test_complex(2*3*4) ); CALL_SUBTEST( test_complex(2*3*4*5) ); CALL_SUBTEST( test_complex(2*3*4*5) ); CALL_SUBTEST( test_complex(2*3*4*5) ); CALL_SUBTEST( test_complex(2*3*4*5*7) ); CALL_SUBTEST( test_complex(2*3*4*5*7) ); CALL_SUBTEST( test_complex(2*3*4*5*7) ); CALL_SUBTEST( test_scalar(32) ); CALL_SUBTEST( test_scalar(32) ); CALL_SUBTEST( test_scalar(32) ); CALL_SUBTEST( test_scalar(45) ); CALL_SUBTEST( test_scalar(45) ); CALL_SUBTEST( test_scalar(45) ); CALL_SUBTEST( test_scalar(50) ); CALL_SUBTEST( test_scalar(50) ); CALL_SUBTEST( test_scalar(50) ); CALL_SUBTEST( test_scalar(256) ); CALL_SUBTEST( test_scalar(256) ); CALL_SUBTEST( test_scalar(256) ); CALL_SUBTEST( test_scalar(2*3*4*5*7) ); CALL_SUBTEST( test_scalar(2*3*4*5*7) ); CALL_SUBTEST( test_scalar(2*3*4*5*7) ); }