2009-09-14 13:52:26 +08:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
2012-07-14 16:04:26 +08:00
|
|
|
// for linear algebra.
|
2009-09-14 13:52:26 +08:00
|
|
|
//
|
|
|
|
// Copyright (C) 2009 Mark Borgerding mark a borgerding net
|
|
|
|
//
|
2012-07-14 02:42:47 +08:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla
|
|
|
|
// Public License v. 2.0. If a copy of the MPL was not distributed
|
|
|
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2009-09-14 13:52:26 +08:00
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
#include <unsupported/Eigen/FFT>
|
|
|
|
|
2010-01-22 13:35:03 +08:00
|
|
|
template <typename T>
|
|
|
|
std::complex<T> RandomCpx() { return std::complex<T>( (T)(rand()/(T)RAND_MAX - .5), (T)(rand()/(T)RAND_MAX - .5) ); }
|
|
|
|
|
2009-09-14 13:52:26 +08:00
|
|
|
using namespace std;
|
2010-01-22 13:35:03 +08:00
|
|
|
using namespace Eigen;
|
2009-09-14 13:52:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
template < typename T>
|
2016-07-08 17:38:11 +08:00
|
|
|
complex<long double> promote(complex<T> x) { return complex<long double>((long double)x.real(),(long double)x.imag()); }
|
2009-09-14 13:52:26 +08:00
|
|
|
|
2016-07-08 17:38:11 +08:00
|
|
|
complex<long double> promote(float x) { return complex<long double>((long double)x); }
|
|
|
|
complex<long double> promote(double x) { return complex<long double>((long double)x); }
|
|
|
|
complex<long double> promote(long double x) { return complex<long double>((long double)x); }
|
2009-09-14 13:52:26 +08:00
|
|
|
|
|
|
|
|
2010-02-17 09:44:48 +08:00
|
|
|
template <typename VT1,typename VT2>
|
|
|
|
long double fft_rmse( const VT1 & fftbuf,const VT2 & timebuf)
|
2009-09-14 13:52:26 +08:00
|
|
|
{
|
|
|
|
long double totalpower=0;
|
|
|
|
long double difpower=0;
|
2009-12-03 11:58:34 +08:00
|
|
|
long double pi = acos((long double)-1 );
|
2010-02-17 09:44:48 +08:00
|
|
|
for (size_t k0=0;k0<(size_t)fftbuf.size();++k0) {
|
2009-09-14 13:52:26 +08:00
|
|
|
complex<long double> acc = 0;
|
2016-07-08 17:38:11 +08:00
|
|
|
long double phinc = (long double)(-2.)*k0* pi / timebuf.size();
|
2010-02-17 09:44:48 +08:00
|
|
|
for (size_t k1=0;k1<(size_t)timebuf.size();++k1) {
|
2009-09-14 13:52:26 +08:00
|
|
|
acc += promote( timebuf[k1] ) * exp( complex<long double>(0,k1*phinc) );
|
|
|
|
}
|
2014-02-17 16:56:46 +08:00
|
|
|
totalpower += numext::abs2(acc);
|
2009-09-14 13:52:26 +08:00
|
|
|
complex<long double> x = promote(fftbuf[k0]);
|
|
|
|
complex<long double> dif = acc - x;
|
2014-02-17 16:56:46 +08:00
|
|
|
difpower += numext::abs2(dif);
|
|
|
|
//cerr << k0 << "\t" << acc << "\t" << x << "\t" << sqrt(numext::abs2(dif)) << endl;
|
2009-09-14 13:52:26 +08:00
|
|
|
}
|
|
|
|
cerr << "rmse:" << sqrt(difpower/totalpower) << endl;
|
|
|
|
return sqrt(difpower/totalpower);
|
|
|
|
}
|
|
|
|
|
2010-02-17 09:44:48 +08:00
|
|
|
template <typename VT1,typename VT2>
|
|
|
|
long double dif_rmse( const VT1 buf1,const VT2 buf2)
|
2009-09-14 13:52:26 +08:00
|
|
|
{
|
|
|
|
long double totalpower=0;
|
|
|
|
long double difpower=0;
|
2011-07-21 17:19:36 +08:00
|
|
|
size_t n = (min)( buf1.size(),buf2.size() );
|
2009-09-14 13:52:26 +08:00
|
|
|
for (size_t k=0;k<n;++k) {
|
2016-07-08 17:38:11 +08:00
|
|
|
totalpower += (long double)((numext::abs2( buf1[k] ) + numext::abs2(buf2[k]) )/2);
|
|
|
|
difpower += (long double)(numext::abs2(buf1[k] - buf2[k]));
|
2009-09-14 13:52:26 +08:00
|
|
|
}
|
|
|
|
return sqrt(difpower/totalpower);
|
|
|
|
}
|
|
|
|
|
2010-02-17 09:44:48 +08:00
|
|
|
enum { StdVectorContainer, EigenVectorContainer };
|
|
|
|
|
|
|
|
template<int Container, typename Scalar> struct VectorType;
|
|
|
|
|
|
|
|
template<typename Scalar> struct VectorType<StdVectorContainer,Scalar>
|
2009-09-14 13:52:26 +08:00
|
|
|
{
|
2010-02-17 09:44:48 +08:00
|
|
|
typedef vector<Scalar> type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Scalar> struct VectorType<EigenVectorContainer,Scalar>
|
|
|
|
{
|
|
|
|
typedef Matrix<Scalar,Dynamic,1> type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <int Container, typename T>
|
|
|
|
void test_scalar_generic(int nfft)
|
|
|
|
{
|
|
|
|
typedef typename FFT<T>::Complex Complex;
|
|
|
|
typedef typename FFT<T>::Scalar Scalar;
|
|
|
|
typedef typename VectorType<Container,Scalar>::type ScalarVector;
|
|
|
|
typedef typename VectorType<Container,Complex>::type ComplexVector;
|
2009-09-14 13:52:26 +08:00
|
|
|
|
|
|
|
FFT<T> fft;
|
2010-02-17 09:44:48 +08:00
|
|
|
ScalarVector tbuf(nfft);
|
|
|
|
ComplexVector freqBuf;
|
2009-09-14 13:52:26 +08:00
|
|
|
for (int k=0;k<nfft;++k)
|
2010-02-17 09:44:48 +08:00
|
|
|
tbuf[k]= (T)( rand()/(double)RAND_MAX - .5);
|
2009-09-14 13:52:26 +08:00
|
|
|
|
2010-02-17 09:44:48 +08:00
|
|
|
// make sure it DOESN'T give the right full spectrum answer
|
|
|
|
// if we've asked for half-spectrum
|
|
|
|
fft.SetFlag(fft.HalfSpectrum );
|
|
|
|
fft.fwd( freqBuf,tbuf);
|
|
|
|
VERIFY((size_t)freqBuf.size() == (size_t)( (nfft>>1)+1) );
|
2016-07-08 17:38:11 +08:00
|
|
|
VERIFY( T(fft_rmse(freqBuf,tbuf)) < test_precision<T>() );// gross check
|
2010-02-17 09:44:48 +08:00
|
|
|
|
|
|
|
fft.ClearFlag(fft.HalfSpectrum );
|
|
|
|
fft.fwd( freqBuf,tbuf);
|
|
|
|
VERIFY( (size_t)freqBuf.size() == (size_t)nfft);
|
2016-07-08 17:38:11 +08:00
|
|
|
VERIFY( T(fft_rmse(freqBuf,tbuf)) < test_precision<T>() );// gross check
|
2010-02-17 09:44:48 +08:00
|
|
|
|
|
|
|
if (nfft&1)
|
|
|
|
return; // odd FFTs get the wrong size inverse FFT
|
|
|
|
|
|
|
|
ScalarVector tbuf2;
|
|
|
|
fft.inv( tbuf2 , freqBuf);
|
2016-07-08 17:38:11 +08:00
|
|
|
VERIFY( T(dif_rmse(tbuf,tbuf2)) < test_precision<T>() );// gross check
|
2010-02-17 09:44:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
// verify that the Unscaled flag takes effect
|
|
|
|
ScalarVector tbuf3;
|
|
|
|
fft.SetFlag(fft.Unscaled);
|
|
|
|
|
|
|
|
fft.inv( tbuf3 , freqBuf);
|
|
|
|
|
|
|
|
for (int k=0;k<nfft;++k)
|
|
|
|
tbuf3[k] *= T(1./nfft);
|
|
|
|
|
|
|
|
|
|
|
|
//for (size_t i=0;i<(size_t) tbuf.size();++i)
|
|
|
|
// cout << "freqBuf=" << freqBuf[i] << " in2=" << tbuf3[i] << " - in=" << tbuf[i] << " => " << (tbuf3[i] - tbuf[i] ) << endl;
|
|
|
|
|
2016-07-08 17:38:11 +08:00
|
|
|
VERIFY( T(dif_rmse(tbuf,tbuf3)) < test_precision<T>() );// gross check
|
2010-02-17 09:44:48 +08:00
|
|
|
|
|
|
|
// verify that ClearFlag works
|
|
|
|
fft.ClearFlag(fft.Unscaled);
|
|
|
|
fft.inv( tbuf2 , freqBuf);
|
2016-07-08 17:38:11 +08:00
|
|
|
VERIFY( T(dif_rmse(tbuf,tbuf2)) < test_precision<T>() );// gross check
|
2009-09-14 13:52:26 +08:00
|
|
|
}
|
|
|
|
|
2010-02-17 09:44:48 +08:00
|
|
|
template <typename T>
|
|
|
|
void test_scalar(int nfft)
|
2009-09-14 13:52:26 +08:00
|
|
|
{
|
2010-02-17 09:44:48 +08:00
|
|
|
test_scalar_generic<StdVectorContainer,T>(nfft);
|
|
|
|
//test_scalar_generic<EigenVectorContainer,T>(nfft);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <int Container, typename T>
|
|
|
|
void test_complex_generic(int nfft)
|
|
|
|
{
|
|
|
|
typedef typename FFT<T>::Complex Complex;
|
|
|
|
typedef typename VectorType<Container,Complex>::type ComplexVector;
|
2009-09-14 13:52:26 +08:00
|
|
|
|
|
|
|
FFT<T> fft;
|
|
|
|
|
2010-02-17 09:44:48 +08:00
|
|
|
ComplexVector inbuf(nfft);
|
|
|
|
ComplexVector outbuf;
|
|
|
|
ComplexVector buf3;
|
2009-09-14 13:52:26 +08:00
|
|
|
for (int k=0;k<nfft;++k)
|
2010-02-17 09:44:48 +08:00
|
|
|
inbuf[k]= Complex( (T)(rand()/(double)RAND_MAX - .5), (T)(rand()/(double)RAND_MAX - .5) );
|
2010-01-23 11:52:13 +08:00
|
|
|
fft.fwd( outbuf , inbuf);
|
2009-09-14 13:52:26 +08:00
|
|
|
|
2016-07-08 17:38:11 +08:00
|
|
|
VERIFY( T(fft_rmse(outbuf,inbuf)) < test_precision<T>() );// gross check
|
2010-01-23 11:52:13 +08:00
|
|
|
fft.inv( buf3 , outbuf);
|
2009-09-14 13:52:26 +08:00
|
|
|
|
2016-07-08 17:38:11 +08:00
|
|
|
VERIFY( T(dif_rmse(inbuf,buf3)) < test_precision<T>() );// gross check
|
2010-02-17 09:44:48 +08:00
|
|
|
|
|
|
|
// verify that the Unscaled flag takes effect
|
|
|
|
ComplexVector buf4;
|
|
|
|
fft.SetFlag(fft.Unscaled);
|
|
|
|
fft.inv( buf4 , outbuf);
|
|
|
|
for (int k=0;k<nfft;++k)
|
|
|
|
buf4[k] *= T(1./nfft);
|
2016-07-08 17:38:11 +08:00
|
|
|
VERIFY( T(dif_rmse(inbuf,buf4)) < test_precision<T>() );// gross check
|
2010-02-17 09:44:48 +08:00
|
|
|
|
|
|
|
// verify that ClearFlag works
|
|
|
|
fft.ClearFlag(fft.Unscaled);
|
|
|
|
fft.inv( buf3 , outbuf);
|
2016-07-08 17:38:11 +08:00
|
|
|
VERIFY( T(dif_rmse(inbuf,buf3)) < test_precision<T>() );// gross check
|
2009-09-14 13:52:26 +08:00
|
|
|
}
|
|
|
|
|
2010-02-17 09:44:48 +08:00
|
|
|
template <typename T>
|
|
|
|
void test_complex(int nfft)
|
|
|
|
{
|
|
|
|
test_complex_generic<StdVectorContainer,T>(nfft);
|
|
|
|
test_complex_generic<EigenVectorContainer,T>(nfft);
|
|
|
|
}
|
|
|
|
/*
|
2010-01-22 13:35:03 +08:00
|
|
|
template <typename T,int nrows,int ncols>
|
|
|
|
void test_complex2d()
|
2009-09-14 13:52:26 +08:00
|
|
|
{
|
2010-01-22 13:35:03 +08:00
|
|
|
typedef typename Eigen::FFT<T>::Complex Complex;
|
|
|
|
FFT<T> fft;
|
2010-01-22 14:17:36 +08:00
|
|
|
Eigen::Matrix<Complex,nrows,ncols> src,src2,dst,dst2;
|
2010-01-22 13:35:03 +08:00
|
|
|
|
2010-01-22 14:17:36 +08:00
|
|
|
src = Eigen::Matrix<Complex,nrows,ncols>::Random();
|
|
|
|
//src = Eigen::Matrix<Complex,nrows,ncols>::Identity();
|
2010-01-22 13:35:03 +08:00
|
|
|
|
|
|
|
for (int k=0;k<ncols;k++) {
|
|
|
|
Eigen::Matrix<Complex,nrows,1> tmpOut;
|
2010-01-23 11:52:13 +08:00
|
|
|
fft.fwd( tmpOut,src.col(k) );
|
2010-01-22 13:35:03 +08:00
|
|
|
dst2.col(k) = tmpOut;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int k=0;k<nrows;k++) {
|
|
|
|
Eigen::Matrix<Complex,1,ncols> tmpOut;
|
2010-01-23 11:52:13 +08:00
|
|
|
fft.fwd( tmpOut, dst2.row(k) );
|
2010-01-22 13:35:03 +08:00
|
|
|
dst2.row(k) = tmpOut;
|
|
|
|
}
|
|
|
|
|
2010-02-17 09:44:48 +08:00
|
|
|
fft.fwd2(dst.data(),src.data(),ncols,nrows);
|
|
|
|
fft.inv2(src2.data(),dst.data(),ncols,nrows);
|
2010-01-22 13:35:03 +08:00
|
|
|
VERIFY( (src-src2).norm() < test_precision<T>() );
|
|
|
|
VERIFY( (dst-dst2).norm() < test_precision<T>() );
|
|
|
|
}
|
2010-02-17 09:44:48 +08:00
|
|
|
*/
|
2010-01-22 13:35:03 +08:00
|
|
|
|
2010-03-08 10:31:06 +08:00
|
|
|
|
2010-03-08 12:46:26 +08:00
|
|
|
void test_return_by_value(int len)
|
2010-03-08 10:31:06 +08:00
|
|
|
{
|
2010-03-08 12:46:26 +08:00
|
|
|
VectorXf in;
|
|
|
|
VectorXf in1;
|
2010-03-08 12:57:02 +08:00
|
|
|
in.setRandom( len );
|
2010-03-08 12:46:26 +08:00
|
|
|
VectorXcf out1,out2;
|
|
|
|
FFT<float> fft;
|
2010-03-08 11:05:48 +08:00
|
|
|
|
|
|
|
fft.SetFlag(fft.HalfSpectrum );
|
|
|
|
|
2010-03-08 10:31:06 +08:00
|
|
|
fft.fwd(out1,in);
|
|
|
|
out2 = fft.fwd(in);
|
2010-03-08 12:46:26 +08:00
|
|
|
VERIFY( (out1-out2).norm() < test_precision<float>() );
|
2010-03-08 10:31:06 +08:00
|
|
|
in1 = fft.inv(out1);
|
2010-03-08 12:46:26 +08:00
|
|
|
VERIFY( (in1-in).norm() < test_precision<float>() );
|
2010-03-08 10:31:06 +08:00
|
|
|
}
|
|
|
|
|
2018-07-17 20:46:15 +08:00
|
|
|
EIGEN_DECLARE_TEST(FFTW)
|
2010-01-22 13:35:03 +08:00
|
|
|
{
|
2011-01-28 23:54:01 +08:00
|
|
|
CALL_SUBTEST( test_return_by_value(32) );
|
2010-02-17 09:44:48 +08:00
|
|
|
//CALL_SUBTEST( ( test_complex2d<float,4,8> () ) ); CALL_SUBTEST( ( test_complex2d<double,4,8> () ) );
|
2010-01-22 13:35:03 +08:00
|
|
|
//CALL_SUBTEST( ( test_complex2d<long double,4,8> () ) );
|
2011-01-28 23:54:01 +08:00
|
|
|
CALL_SUBTEST( test_complex<float>(32) ); CALL_SUBTEST( test_complex<double>(32) );
|
|
|
|
CALL_SUBTEST( test_complex<float>(256) ); CALL_SUBTEST( test_complex<double>(256) );
|
|
|
|
CALL_SUBTEST( test_complex<float>(3*8) ); CALL_SUBTEST( test_complex<double>(3*8) );
|
|
|
|
CALL_SUBTEST( test_complex<float>(5*32) ); CALL_SUBTEST( test_complex<double>(5*32) );
|
|
|
|
CALL_SUBTEST( test_complex<float>(2*3*4) ); CALL_SUBTEST( test_complex<double>(2*3*4) );
|
|
|
|
CALL_SUBTEST( test_complex<float>(2*3*4*5) ); CALL_SUBTEST( test_complex<double>(2*3*4*5) );
|
|
|
|
CALL_SUBTEST( test_complex<float>(2*3*4*5*7) ); CALL_SUBTEST( test_complex<double>(2*3*4*5*7) );
|
|
|
|
|
|
|
|
CALL_SUBTEST( test_scalar<float>(32) ); CALL_SUBTEST( test_scalar<double>(32) );
|
|
|
|
CALL_SUBTEST( test_scalar<float>(45) ); CALL_SUBTEST( test_scalar<double>(45) );
|
|
|
|
CALL_SUBTEST( test_scalar<float>(50) ); CALL_SUBTEST( test_scalar<double>(50) );
|
|
|
|
CALL_SUBTEST( test_scalar<float>(256) ); CALL_SUBTEST( test_scalar<double>(256) );
|
|
|
|
CALL_SUBTEST( test_scalar<float>(2*3*4*5*7) ); CALL_SUBTEST( test_scalar<double>(2*3*4*5*7) );
|
|
|
|
|
|
|
|
#ifdef EIGEN_HAS_FFTWL
|
|
|
|
CALL_SUBTEST( test_complex<long double>(32) );
|
|
|
|
CALL_SUBTEST( test_complex<long double>(256) );
|
|
|
|
CALL_SUBTEST( test_complex<long double>(3*8) );
|
|
|
|
CALL_SUBTEST( test_complex<long double>(5*32) );
|
|
|
|
CALL_SUBTEST( test_complex<long double>(2*3*4) );
|
|
|
|
CALL_SUBTEST( test_complex<long double>(2*3*4*5) );
|
|
|
|
CALL_SUBTEST( test_complex<long double>(2*3*4*5*7) );
|
|
|
|
|
|
|
|
CALL_SUBTEST( test_scalar<long double>(32) );
|
|
|
|
CALL_SUBTEST( test_scalar<long double>(45) );
|
|
|
|
CALL_SUBTEST( test_scalar<long double>(50) );
|
|
|
|
CALL_SUBTEST( test_scalar<long double>(256) );
|
|
|
|
CALL_SUBTEST( test_scalar<long double>(2*3*4*5*7) );
|
|
|
|
#endif
|
2009-09-14 13:52:26 +08:00
|
|
|
}
|