You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

262 lines
8.9 KiB

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