xref: /aosp_15_r20/external/eigen/unsupported/test/FFTW.cpp (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
1*bf2c3715SXin Li // This file is part of Eigen, a lightweight C++ template library
2*bf2c3715SXin Li // for linear algebra.
3*bf2c3715SXin Li //
4*bf2c3715SXin Li // Copyright (C) 2009 Mark Borgerding mark a borgerding net
5*bf2c3715SXin Li //
6*bf2c3715SXin Li // This Source Code Form is subject to the terms of the Mozilla
7*bf2c3715SXin Li // Public License v. 2.0. If a copy of the MPL was not distributed
8*bf2c3715SXin Li // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9*bf2c3715SXin Li 
10*bf2c3715SXin Li #include "main.h"
11*bf2c3715SXin Li #include <unsupported/Eigen/FFT>
12*bf2c3715SXin Li 
13*bf2c3715SXin Li template <typename T>
RandomCpx()14*bf2c3715SXin Li std::complex<T> RandomCpx() { return std::complex<T>( (T)(rand()/(T)RAND_MAX - .5), (T)(rand()/(T)RAND_MAX - .5) ); }
15*bf2c3715SXin Li 
16*bf2c3715SXin Li using namespace std;
17*bf2c3715SXin Li using namespace Eigen;
18*bf2c3715SXin Li 
19*bf2c3715SXin Li 
20*bf2c3715SXin Li template < typename T>
promote(complex<T> x)21*bf2c3715SXin Li complex<long double>  promote(complex<T> x) { return complex<long double>((long double)x.real(),(long double)x.imag()); }
22*bf2c3715SXin Li 
promote(float x)23*bf2c3715SXin Li complex<long double>  promote(float x) { return complex<long double>((long double)x); }
promote(double x)24*bf2c3715SXin Li complex<long double>  promote(double x) { return complex<long double>((long double)x); }
promote(long double x)25*bf2c3715SXin Li complex<long double>  promote(long double x) { return complex<long double>((long double)x); }
26*bf2c3715SXin Li 
27*bf2c3715SXin Li 
28*bf2c3715SXin Li     template <typename VT1,typename VT2>
fft_rmse(const VT1 & fftbuf,const VT2 & timebuf)29*bf2c3715SXin Li     long double fft_rmse( const VT1 & fftbuf,const VT2 & timebuf)
30*bf2c3715SXin Li     {
31*bf2c3715SXin Li         long double totalpower=0;
32*bf2c3715SXin Li         long double difpower=0;
33*bf2c3715SXin Li         long double pi = acos((long double)-1 );
34*bf2c3715SXin Li         for (size_t k0=0;k0<(size_t)fftbuf.size();++k0) {
35*bf2c3715SXin Li             complex<long double> acc = 0;
36*bf2c3715SXin Li             long double phinc = (long double)(-2.)*k0* pi / timebuf.size();
37*bf2c3715SXin Li             for (size_t k1=0;k1<(size_t)timebuf.size();++k1) {
38*bf2c3715SXin Li                 acc +=  promote( timebuf[k1] ) * exp( complex<long double>(0,k1*phinc) );
39*bf2c3715SXin Li             }
40*bf2c3715SXin Li             totalpower += numext::abs2(acc);
41*bf2c3715SXin Li             complex<long double> x = promote(fftbuf[k0]);
42*bf2c3715SXin Li             complex<long double> dif = acc - x;
43*bf2c3715SXin Li             difpower += numext::abs2(dif);
44*bf2c3715SXin Li             //cerr << k0 << "\t" << acc << "\t" <<  x << "\t" << sqrt(numext::abs2(dif)) << endl;
45*bf2c3715SXin Li         }
46*bf2c3715SXin Li         cerr << "rmse:" << sqrt(difpower/totalpower) << endl;
47*bf2c3715SXin Li         return sqrt(difpower/totalpower);
48*bf2c3715SXin Li     }
49*bf2c3715SXin Li 
50*bf2c3715SXin Li     template <typename VT1,typename VT2>
dif_rmse(const VT1 buf1,const VT2 buf2)51*bf2c3715SXin Li     long double dif_rmse( const VT1 buf1,const VT2 buf2)
52*bf2c3715SXin Li     {
53*bf2c3715SXin Li         long double totalpower=0;
54*bf2c3715SXin Li         long double difpower=0;
55*bf2c3715SXin Li         size_t n = (min)( buf1.size(),buf2.size() );
56*bf2c3715SXin Li         for (size_t k=0;k<n;++k) {
57*bf2c3715SXin Li             totalpower += (long double)((numext::abs2( buf1[k] ) + numext::abs2(buf2[k]) )/2);
58*bf2c3715SXin Li             difpower += (long double)(numext::abs2(buf1[k] - buf2[k]));
59*bf2c3715SXin Li         }
60*bf2c3715SXin Li         return sqrt(difpower/totalpower);
61*bf2c3715SXin Li     }
62*bf2c3715SXin Li 
63*bf2c3715SXin Li enum { StdVectorContainer, EigenVectorContainer };
64*bf2c3715SXin Li 
65*bf2c3715SXin Li template<int Container, typename Scalar> struct VectorType;
66*bf2c3715SXin Li 
67*bf2c3715SXin Li template<typename Scalar> struct VectorType<StdVectorContainer,Scalar>
68*bf2c3715SXin Li {
69*bf2c3715SXin Li   typedef vector<Scalar> type;
70*bf2c3715SXin Li };
71*bf2c3715SXin Li 
72*bf2c3715SXin Li template<typename Scalar> struct VectorType<EigenVectorContainer,Scalar>
73*bf2c3715SXin Li {
74*bf2c3715SXin Li   typedef Matrix<Scalar,Dynamic,1> type;
75*bf2c3715SXin Li };
76*bf2c3715SXin Li 
77*bf2c3715SXin Li template <int Container, typename T>
test_scalar_generic(int nfft)78*bf2c3715SXin Li void test_scalar_generic(int nfft)
79*bf2c3715SXin Li {
80*bf2c3715SXin Li     typedef typename FFT<T>::Complex Complex;
81*bf2c3715SXin Li     typedef typename FFT<T>::Scalar Scalar;
82*bf2c3715SXin Li     typedef typename VectorType<Container,Scalar>::type ScalarVector;
83*bf2c3715SXin Li     typedef typename VectorType<Container,Complex>::type ComplexVector;
84*bf2c3715SXin Li 
85*bf2c3715SXin Li     FFT<T> fft;
86*bf2c3715SXin Li     ScalarVector tbuf(nfft);
87*bf2c3715SXin Li     ComplexVector freqBuf;
88*bf2c3715SXin Li     for (int k=0;k<nfft;++k)
89*bf2c3715SXin Li         tbuf[k]= (T)( rand()/(double)RAND_MAX - .5);
90*bf2c3715SXin Li 
91*bf2c3715SXin Li     // make sure it DOESN'T give the right full spectrum answer
92*bf2c3715SXin Li     // if we've asked for half-spectrum
93*bf2c3715SXin Li     fft.SetFlag(fft.HalfSpectrum );
94*bf2c3715SXin Li     fft.fwd( freqBuf,tbuf);
95*bf2c3715SXin Li     VERIFY((size_t)freqBuf.size() == (size_t)( (nfft>>1)+1) );
96*bf2c3715SXin Li     VERIFY( T(fft_rmse(freqBuf,tbuf)) < test_precision<T>()  );// gross check
97*bf2c3715SXin Li 
98*bf2c3715SXin Li     fft.ClearFlag(fft.HalfSpectrum );
99*bf2c3715SXin Li     fft.fwd( freqBuf,tbuf);
100*bf2c3715SXin Li     VERIFY( (size_t)freqBuf.size() == (size_t)nfft);
101*bf2c3715SXin Li     VERIFY( T(fft_rmse(freqBuf,tbuf)) < test_precision<T>()  );// gross check
102*bf2c3715SXin Li 
103*bf2c3715SXin Li     if (nfft&1)
104*bf2c3715SXin Li         return; // odd FFTs get the wrong size inverse FFT
105*bf2c3715SXin Li 
106*bf2c3715SXin Li     ScalarVector tbuf2;
107*bf2c3715SXin Li     fft.inv( tbuf2 , freqBuf);
108*bf2c3715SXin Li     VERIFY( T(dif_rmse(tbuf,tbuf2)) < test_precision<T>()  );// gross check
109*bf2c3715SXin Li 
110*bf2c3715SXin Li 
111*bf2c3715SXin Li     // verify that the Unscaled flag takes effect
112*bf2c3715SXin Li     ScalarVector tbuf3;
113*bf2c3715SXin Li     fft.SetFlag(fft.Unscaled);
114*bf2c3715SXin Li 
115*bf2c3715SXin Li     fft.inv( tbuf3 , freqBuf);
116*bf2c3715SXin Li 
117*bf2c3715SXin Li     for (int k=0;k<nfft;++k)
118*bf2c3715SXin Li         tbuf3[k] *= T(1./nfft);
119*bf2c3715SXin Li 
120*bf2c3715SXin Li 
121*bf2c3715SXin Li     //for (size_t i=0;i<(size_t) tbuf.size();++i)
122*bf2c3715SXin Li     //    cout << "freqBuf=" << freqBuf[i] << " in2=" << tbuf3[i] << " -  in=" << tbuf[i] << " => " << (tbuf3[i] - tbuf[i] ) <<  endl;
123*bf2c3715SXin Li 
124*bf2c3715SXin Li     VERIFY( T(dif_rmse(tbuf,tbuf3)) < test_precision<T>()  );// gross check
125*bf2c3715SXin Li 
126*bf2c3715SXin Li     // verify that ClearFlag works
127*bf2c3715SXin Li     fft.ClearFlag(fft.Unscaled);
128*bf2c3715SXin Li     fft.inv( tbuf2 , freqBuf);
129*bf2c3715SXin Li     VERIFY( T(dif_rmse(tbuf,tbuf2)) < test_precision<T>()  );// gross check
130*bf2c3715SXin Li }
131*bf2c3715SXin Li 
132*bf2c3715SXin Li template <typename T>
test_scalar(int nfft)133*bf2c3715SXin Li void test_scalar(int nfft)
134*bf2c3715SXin Li {
135*bf2c3715SXin Li   test_scalar_generic<StdVectorContainer,T>(nfft);
136*bf2c3715SXin Li   //test_scalar_generic<EigenVectorContainer,T>(nfft);
137*bf2c3715SXin Li }
138*bf2c3715SXin Li 
139*bf2c3715SXin Li 
140*bf2c3715SXin Li template <int Container, typename T>
test_complex_generic(int nfft)141*bf2c3715SXin Li void test_complex_generic(int nfft)
142*bf2c3715SXin Li {
143*bf2c3715SXin Li     typedef typename FFT<T>::Complex Complex;
144*bf2c3715SXin Li     typedef typename VectorType<Container,Complex>::type ComplexVector;
145*bf2c3715SXin Li 
146*bf2c3715SXin Li     FFT<T> fft;
147*bf2c3715SXin Li 
148*bf2c3715SXin Li     ComplexVector inbuf(nfft);
149*bf2c3715SXin Li     ComplexVector outbuf;
150*bf2c3715SXin Li     ComplexVector buf3;
151*bf2c3715SXin Li     for (int k=0;k<nfft;++k)
152*bf2c3715SXin Li         inbuf[k]= Complex( (T)(rand()/(double)RAND_MAX - .5), (T)(rand()/(double)RAND_MAX - .5) );
153*bf2c3715SXin Li     fft.fwd( outbuf , inbuf);
154*bf2c3715SXin Li 
155*bf2c3715SXin Li     VERIFY( T(fft_rmse(outbuf,inbuf)) < test_precision<T>()  );// gross check
156*bf2c3715SXin Li     fft.inv( buf3 , outbuf);
157*bf2c3715SXin Li 
158*bf2c3715SXin Li     VERIFY( T(dif_rmse(inbuf,buf3)) < test_precision<T>()  );// gross check
159*bf2c3715SXin Li 
160*bf2c3715SXin Li     // verify that the Unscaled flag takes effect
161*bf2c3715SXin Li     ComplexVector buf4;
162*bf2c3715SXin Li     fft.SetFlag(fft.Unscaled);
163*bf2c3715SXin Li     fft.inv( buf4 , outbuf);
164*bf2c3715SXin Li     for (int k=0;k<nfft;++k)
165*bf2c3715SXin Li         buf4[k] *= T(1./nfft);
166*bf2c3715SXin Li     VERIFY( T(dif_rmse(inbuf,buf4)) < test_precision<T>()  );// gross check
167*bf2c3715SXin Li 
168*bf2c3715SXin Li     // verify that ClearFlag works
169*bf2c3715SXin Li     fft.ClearFlag(fft.Unscaled);
170*bf2c3715SXin Li     fft.inv( buf3 , outbuf);
171*bf2c3715SXin Li     VERIFY( T(dif_rmse(inbuf,buf3)) < test_precision<T>()  );// gross check
172*bf2c3715SXin Li }
173*bf2c3715SXin Li 
174*bf2c3715SXin Li template <typename T>
test_complex(int nfft)175*bf2c3715SXin Li void test_complex(int nfft)
176*bf2c3715SXin Li {
177*bf2c3715SXin Li   test_complex_generic<StdVectorContainer,T>(nfft);
178*bf2c3715SXin Li   test_complex_generic<EigenVectorContainer,T>(nfft);
179*bf2c3715SXin Li }
180*bf2c3715SXin Li /*
181*bf2c3715SXin Li template <typename T,int nrows,int ncols>
182*bf2c3715SXin Li void test_complex2d()
183*bf2c3715SXin Li {
184*bf2c3715SXin Li     typedef typename Eigen::FFT<T>::Complex Complex;
185*bf2c3715SXin Li     FFT<T> fft;
186*bf2c3715SXin Li     Eigen::Matrix<Complex,nrows,ncols> src,src2,dst,dst2;
187*bf2c3715SXin Li 
188*bf2c3715SXin Li     src = Eigen::Matrix<Complex,nrows,ncols>::Random();
189*bf2c3715SXin Li     //src =  Eigen::Matrix<Complex,nrows,ncols>::Identity();
190*bf2c3715SXin Li 
191*bf2c3715SXin Li     for (int k=0;k<ncols;k++) {
192*bf2c3715SXin Li         Eigen::Matrix<Complex,nrows,1> tmpOut;
193*bf2c3715SXin Li         fft.fwd( tmpOut,src.col(k) );
194*bf2c3715SXin Li         dst2.col(k) = tmpOut;
195*bf2c3715SXin Li     }
196*bf2c3715SXin Li 
197*bf2c3715SXin Li     for (int k=0;k<nrows;k++) {
198*bf2c3715SXin Li         Eigen::Matrix<Complex,1,ncols> tmpOut;
199*bf2c3715SXin Li         fft.fwd( tmpOut,  dst2.row(k) );
200*bf2c3715SXin Li         dst2.row(k) = tmpOut;
201*bf2c3715SXin Li     }
202*bf2c3715SXin Li 
203*bf2c3715SXin Li     fft.fwd2(dst.data(),src.data(),ncols,nrows);
204*bf2c3715SXin Li     fft.inv2(src2.data(),dst.data(),ncols,nrows);
205*bf2c3715SXin Li     VERIFY( (src-src2).norm() < test_precision<T>() );
206*bf2c3715SXin Li     VERIFY( (dst-dst2).norm() < test_precision<T>() );
207*bf2c3715SXin Li }
208*bf2c3715SXin Li */
209*bf2c3715SXin Li 
210*bf2c3715SXin Li 
test_return_by_value(int len)211*bf2c3715SXin Li void test_return_by_value(int len)
212*bf2c3715SXin Li {
213*bf2c3715SXin Li     VectorXf in;
214*bf2c3715SXin Li     VectorXf in1;
215*bf2c3715SXin Li     in.setRandom( len );
216*bf2c3715SXin Li     VectorXcf out1,out2;
217*bf2c3715SXin Li     FFT<float> fft;
218*bf2c3715SXin Li 
219*bf2c3715SXin Li     fft.SetFlag(fft.HalfSpectrum );
220*bf2c3715SXin Li 
221*bf2c3715SXin Li     fft.fwd(out1,in);
222*bf2c3715SXin Li     out2 = fft.fwd(in);
223*bf2c3715SXin Li     VERIFY( (out1-out2).norm() < test_precision<float>() );
224*bf2c3715SXin Li     in1 = fft.inv(out1);
225*bf2c3715SXin Li     VERIFY( (in1-in).norm() < test_precision<float>() );
226*bf2c3715SXin Li }
227*bf2c3715SXin Li 
EIGEN_DECLARE_TEST(FFTW)228*bf2c3715SXin Li EIGEN_DECLARE_TEST(FFTW)
229*bf2c3715SXin Li {
230*bf2c3715SXin Li   CALL_SUBTEST( test_return_by_value(32) );
231*bf2c3715SXin Li   //CALL_SUBTEST( ( test_complex2d<float,4,8> () ) ); CALL_SUBTEST( ( test_complex2d<double,4,8> () ) );
232*bf2c3715SXin Li   //CALL_SUBTEST( ( test_complex2d<long double,4,8> () ) );
233*bf2c3715SXin Li   CALL_SUBTEST( test_complex<float>(32) ); CALL_SUBTEST( test_complex<double>(32) );
234*bf2c3715SXin Li   CALL_SUBTEST( test_complex<float>(256) ); CALL_SUBTEST( test_complex<double>(256) );
235*bf2c3715SXin Li   CALL_SUBTEST( test_complex<float>(3*8) ); CALL_SUBTEST( test_complex<double>(3*8) );
236*bf2c3715SXin Li   CALL_SUBTEST( test_complex<float>(5*32) ); CALL_SUBTEST( test_complex<double>(5*32) );
237*bf2c3715SXin Li   CALL_SUBTEST( test_complex<float>(2*3*4) ); CALL_SUBTEST( test_complex<double>(2*3*4) );
238*bf2c3715SXin Li   CALL_SUBTEST( test_complex<float>(2*3*4*5) ); CALL_SUBTEST( test_complex<double>(2*3*4*5) );
239*bf2c3715SXin Li   CALL_SUBTEST( test_complex<float>(2*3*4*5*7) ); CALL_SUBTEST( test_complex<double>(2*3*4*5*7) );
240*bf2c3715SXin Li 
241*bf2c3715SXin Li   CALL_SUBTEST( test_scalar<float>(32) ); CALL_SUBTEST( test_scalar<double>(32) );
242*bf2c3715SXin Li   CALL_SUBTEST( test_scalar<float>(45) ); CALL_SUBTEST( test_scalar<double>(45) );
243*bf2c3715SXin Li   CALL_SUBTEST( test_scalar<float>(50) ); CALL_SUBTEST( test_scalar<double>(50) );
244*bf2c3715SXin Li   CALL_SUBTEST( test_scalar<float>(256) ); CALL_SUBTEST( test_scalar<double>(256) );
245*bf2c3715SXin Li   CALL_SUBTEST( test_scalar<float>(2*3*4*5*7) ); CALL_SUBTEST( test_scalar<double>(2*3*4*5*7) );
246*bf2c3715SXin Li 
247*bf2c3715SXin Li   #ifdef EIGEN_HAS_FFTWL
248*bf2c3715SXin Li   CALL_SUBTEST( test_complex<long double>(32) );
249*bf2c3715SXin Li   CALL_SUBTEST( test_complex<long double>(256) );
250*bf2c3715SXin Li   CALL_SUBTEST( test_complex<long double>(3*8) );
251*bf2c3715SXin Li   CALL_SUBTEST( test_complex<long double>(5*32) );
252*bf2c3715SXin Li   CALL_SUBTEST( test_complex<long double>(2*3*4) );
253*bf2c3715SXin Li   CALL_SUBTEST( test_complex<long double>(2*3*4*5) );
254*bf2c3715SXin Li   CALL_SUBTEST( test_complex<long double>(2*3*4*5*7) );
255*bf2c3715SXin Li 
256*bf2c3715SXin Li   CALL_SUBTEST( test_scalar<long double>(32) );
257*bf2c3715SXin Li   CALL_SUBTEST( test_scalar<long double>(45) );
258*bf2c3715SXin Li   CALL_SUBTEST( test_scalar<long double>(50) );
259*bf2c3715SXin Li   CALL_SUBTEST( test_scalar<long double>(256) );
260*bf2c3715SXin Li   CALL_SUBTEST( test_scalar<long double>(2*3*4*5*7) );
261*bf2c3715SXin Li   #endif
262*bf2c3715SXin Li }
263