diff --git a/unsupported/Eigen/FFT b/unsupported/Eigen/FFT index 97ec8e49b..36afdde8d 100644 --- a/unsupported/Eigen/FFT +++ b/unsupported/Eigen/FFT @@ -25,29 +25,39 @@ #ifndef EIGEN_FFT_H #define EIGEN_FFT_H -// ei_kissfft_impl: small, free, reasonably efficient default, derived from kissfft -#include "src/FFT/ei_kissfft_impl.h" -#define DEFAULT_FFT_IMPL ei_kissfft_impl +#include +#include +#include +#ifdef EIGEN_FFTW_DEFAULT // FFTW: faster, GPL -- incompatible with Eigen in LGPL form, bigger code size -#ifdef FFTW_ESTIMATE // definition of FFTW_ESTIMATE indicates the caller has included fftw3.h, we can use FFTW routines -#include "src/FFT/ei_fftw_impl.h" -#undef DEFAULT_FFT_IMPL -#define DEFAULT_FFT_IMPL ei_fftw_impl -#endif - -// intel Math Kernel Library: fastest, commercial -- incompatible with Eigen in GPL form -#ifdef _MKL_DFTI_H_ // mkl_dfti.h has been included, we can use MKL FFT routines +# include + namespace Eigen { +# include "src/FFT/ei_fftw_impl.h" + //template typedef struct ei_fftw_impl default_fft_impl; this does not work + template struct default_fft_impl : public ei_fftw_impl {}; + } +#elif defined EIGEN_MKL_DEFAULT // TODO -// #include "src/FFT/ei_imkl_impl.h" -// #define DEFAULT_FFT_IMPL ei_imkl_impl +// intel Math Kernel Library: fastest, commercial -- may be incompatible with Eigen in GPL form + namespace Eigen { +# include "src/FFT/ei_imklfft_impl.h" + template struct default_fft_impl : public ei_imklfft_impl {}; + } +#else +// ei_kissfft_impl: small, free, reasonably efficient default, derived from kissfft +// + namespace Eigen { +# include "src/FFT/ei_kissfft_impl.h" + template + struct default_fft_impl : public ei_kissfft_impl {}; + } #endif namespace Eigen { template - > + typename _Impl=default_fft_impl<_Scalar> > class FFT { public: @@ -120,7 +130,6 @@ class FFT private: impl_type m_impl; }; -#undef DEFAULT_FFT_IMPL } #endif /* vim: set filetype=cpp et sw=2 ts=2 ai: */ diff --git a/unsupported/Eigen/src/FFT/ei_fftw_impl.h b/unsupported/Eigen/src/FFT/ei_fftw_impl.h index d592bbb20..e1f67f334 100644 --- a/unsupported/Eigen/src/FFT/ei_fftw_impl.h +++ b/unsupported/Eigen/src/FFT/ei_fftw_impl.h @@ -22,7 +22,8 @@ // License and a copy of the GNU General Public License along with // Eigen. If not, see . -namespace Eigen { + + // FFTW uses non-const arguments // so we must use ugly const_cast calls for all the args it uses // @@ -32,21 +33,25 @@ namespace Eigen { // 2. fftw_complex is compatible with std::complex // This assumes std::complex layout is array of size 2 with real,imag template + inline T * ei_fftw_cast(const T* p) { return const_cast( p); } + inline fftw_complex * ei_fftw_cast( const std::complex * p) - { + { return const_cast( reinterpret_cast(p) ); } + inline fftwf_complex * ei_fftw_cast( const std::complex * p) { return const_cast( reinterpret_cast(p) ); } + inline fftwl_complex * ei_fftw_cast( const std::complex * p) { return const_cast( reinterpret_cast(p) ); @@ -64,18 +69,22 @@ namespace Eigen { ei_fftw_plan() :m_plan(NULL) {} ~ei_fftw_plan() {if (m_plan) fftwf_destroy_plan(m_plan);} + inline void fwd(complex_type * dst,complex_type * src,int nfft) { if (m_plan==NULL) m_plan = fftwf_plan_dft_1d(nfft,src,dst, FFTW_FORWARD, FFTW_ESTIMATE); fftwf_execute_dft( m_plan, src,dst); } + inline void inv(complex_type * dst,complex_type * src,int nfft) { if (m_plan==NULL) m_plan = fftwf_plan_dft_1d(nfft,src,dst, FFTW_BACKWARD , FFTW_ESTIMATE); fftwf_execute_dft( m_plan, src,dst); } + inline void fwd(complex_type * dst,scalar_type * src,int nfft) { if (m_plan==NULL) m_plan = fftwf_plan_dft_r2c_1d(nfft,src,dst,FFTW_ESTIMATE); fftwf_execute_dft_r2c( m_plan,src,dst); } + inline void inv(scalar_type * dst,complex_type * src,int nfft) { if (m_plan==NULL) m_plan = fftwf_plan_dft_c2r_1d(nfft,src,dst,FFTW_ESTIMATE); @@ -91,18 +100,22 @@ namespace Eigen { ei_fftw_plan() :m_plan(NULL) {} ~ei_fftw_plan() {if (m_plan) fftw_destroy_plan(m_plan);} + inline void fwd(complex_type * dst,complex_type * src,int nfft) { if (m_plan==NULL) m_plan = fftw_plan_dft_1d(nfft,src,dst, FFTW_FORWARD, FFTW_ESTIMATE); fftw_execute_dft( m_plan, src,dst); } + inline void inv(complex_type * dst,complex_type * src,int nfft) { if (m_plan==NULL) m_plan = fftw_plan_dft_1d(nfft,src,dst, FFTW_BACKWARD , FFTW_ESTIMATE); fftw_execute_dft( m_plan, src,dst); } + inline void fwd(complex_type * dst,scalar_type * src,int nfft) { if (m_plan==NULL) m_plan = fftw_plan_dft_r2c_1d(nfft,src,dst,FFTW_ESTIMATE); fftw_execute_dft_r2c( m_plan,src,dst); } + inline void inv(scalar_type * dst,complex_type * src,int nfft) { if (m_plan==NULL) m_plan = fftw_plan_dft_c2r_1d(nfft,src,dst,FFTW_ESTIMATE); @@ -118,18 +131,22 @@ namespace Eigen { ei_fftw_plan() :m_plan(NULL) {} ~ei_fftw_plan() {if (m_plan) fftwl_destroy_plan(m_plan);} + inline void fwd(complex_type * dst,complex_type * src,int nfft) { if (m_plan==NULL) m_plan = fftwl_plan_dft_1d(nfft,src,dst, FFTW_FORWARD, FFTW_ESTIMATE); fftwl_execute_dft( m_plan, src,dst); } + inline void inv(complex_type * dst,complex_type * src,int nfft) { if (m_plan==NULL) m_plan = fftwl_plan_dft_1d(nfft,src,dst, FFTW_BACKWARD , FFTW_ESTIMATE); fftwl_execute_dft( m_plan, src,dst); } + inline void fwd(complex_type * dst,scalar_type * src,int nfft) { if (m_plan==NULL) m_plan = fftwl_plan_dft_r2c_1d(nfft,src,dst,FFTW_ESTIMATE); fftwl_execute_dft_r2c( m_plan,src,dst); } + inline void inv(scalar_type * dst,complex_type * src,int nfft) { if (m_plan==NULL) m_plan = fftwl_plan_dft_c2r_1d(nfft,src,dst,FFTW_ESTIMATE); @@ -143,17 +160,20 @@ namespace Eigen { typedef _Scalar Scalar; typedef std::complex Complex; + inline void clear() { m_plans.clear(); } + inline void fwd( Complex * dst,const Complex *src,int nfft) { get_plan(nfft,false,dst,src).fwd(ei_fftw_cast(dst), ei_fftw_cast(src),nfft ); } // real-to-complex forward FFT + inline void fwd( Complex * dst,const Scalar * src,int nfft) { get_plan(nfft,false,dst,src).fwd(ei_fftw_cast(dst), ei_fftw_cast(src) ,nfft); @@ -163,30 +183,37 @@ namespace Eigen { } // inverse complex-to-complex + inline void inv(Complex * dst,const Complex *src,int nfft) { get_plan(nfft,true,dst,src).inv(ei_fftw_cast(dst), ei_fftw_cast(src),nfft ); + + //TODO move scaling to Eigen::FFT // scaling - Scalar s = 1./nfft; + Scalar s = Scalar(1.)/nfft; for (int k=0;k PlanData; typedef std::map PlanMap; PlanMap m_plans; + inline PlanData & get_plan(int nfft,bool inverse,void * dst,const void * src) { bool inplace = (dst==src); @@ -195,4 +222,3 @@ namespace Eigen { return m_plans[key]; } }; -} diff --git a/unsupported/Eigen/src/FFT/ei_kissfft_impl.h b/unsupported/Eigen/src/FFT/ei_kissfft_impl.h index a84ac68a0..c068d8765 100644 --- a/unsupported/Eigen/src/FFT/ei_kissfft_impl.h +++ b/unsupported/Eigen/src/FFT/ei_kissfft_impl.h @@ -22,11 +22,7 @@ // License and a copy of the GNU General Public License along with // Eigen. If not, see . -#include -#include -#include -namespace Eigen { // This FFT implementation was derived from kissfft http:sourceforge.net/projects/kissfft // Copyright 2003-2009 Mark Borgerding @@ -51,13 +47,6 @@ namespace Eigen { m_twiddles[i] = exp( Complex(0,i*phinc) ); } - void conjugate() - { - m_inverse = !m_inverse; - for ( size_t i=0;i + inline void fwd( Complex * dst,const _Src *src,int nfft) { get_plan(nfft,false).work(0, dst, src, 1,1); @@ -299,6 +294,7 @@ namespace Eigen { // perform two FFTs of src even and src odd // then twiddle to recombine them into the half-spectrum format // then fill in the conjugate symmetric half + inline void fwd( Complex * dst,const Scalar * src,int nfft) { if ( nfft&3 ) { @@ -334,6 +330,7 @@ namespace Eigen { } // inverse complex-to-complex + inline void inv(Complex * dst,const Complex *src,int nfft) { get_plan(nfft,true).work(0, dst, src, 1,1); @@ -341,6 +338,7 @@ namespace Eigen { } // half-complex to scalar + inline void inv( Scalar * dst,const Complex * src,int nfft) { if (nfft&3) { @@ -369,7 +367,7 @@ namespace Eigen { } } - private: + protected: typedef ei_kiss_cpx_fft PlanData; typedef std::map PlanMap; @@ -377,8 +375,10 @@ namespace Eigen { std::map > m_realTwiddles; std::vector m_tmpBuf; + inline int PlanKey(int nfft,bool isinverse) const { return (nfft<<1) | isinverse; } + inline PlanData & get_plan(int nfft,bool inverse) { // TODO look for PlanKey(nfft, ! inverse) and conjugate the twiddles @@ -390,6 +390,7 @@ namespace Eigen { return pd; } + inline Complex * real_twiddles(int ncfft2) { std::vector & twidref = m_realTwiddles[ncfft2];// creates new if not there @@ -403,10 +404,11 @@ namespace Eigen { return &twidref[0]; } + // TODO move scaling up into Eigen::FFT + inline void scale(Complex *dst,int n,Scalar s) { for (int k=0;k