fft_test_shared.h File Reference
#include "main.h"
#include <unsupported/Eigen/FFT>

Go to the source code of this file.

Classes

struct  VectorType< StdVectorContainer, Scalar >
 
struct  VectorType< EigenVectorContainer, Scalar >
 

Enumerations

enum  { StdVectorContainer , EigenVectorContainer }
 

Functions

template<typename T >
std::complex< TRandomCpx ()
 
template<typename T >
complex< long doublepromote (complex< T > x)
 
complex< long doublepromote (float x)
 
complex< long doublepromote (double x)
 
complex< long doublepromote (long double x)
 
template<typename VT1 , typename VT2 >
long double fft_rmse (const VT1 &fftbuf, const VT2 &timebuf)
 
template<typename VT1 , typename VT2 >
long double dif_rmse (const VT1 buf1, const VT2 buf2)
 
template<int Container, typename T >
void test_scalar_generic (int nfft)
 
template<typename T >
void test_scalar (int nfft)
 
template<int Container, typename T >
void test_complex_generic (int nfft)
 
template<typename T >
void test_complex_strided (int nfft)
 
template<typename T >
void test_complex (int nfft)
 
template<typename T , int nrows, int ncols>
void test_complex2d ()
 
void test_return_by_value (int len)
 
 EIGEN_DECLARE_TEST (FFTW)
 

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
StdVectorContainer 
EigenVectorContainer 
@ EigenVectorContainer
Definition: fft_test_shared.h:63
@ StdVectorContainer
Definition: fft_test_shared.h:63

Function Documentation

◆ dif_rmse()

template<typename VT1 , typename VT2 >
long double dif_rmse ( const VT1  buf1,
const VT2  buf2 
)
52  {
53  long double totalpower = 0;
54  long double difpower = 0;
55  size_t n = (min)(buf1.size(), buf2.size());
56  for (size_t k = 0; k < n; ++k) {
57  totalpower += (long double)((numext::abs2(buf1[k]) + numext::abs2(buf2[k])) / 2);
58  difpower += (long double)(numext::abs2(buf1[k] - buf2[k]));
59  }
60  return sqrt(difpower / totalpower);
61 }
AnnoyingScalar sqrt(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:134
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11
#define min(a, b)
Definition: datatypes.h:22
char char char int int * k
Definition: level2_impl.h:374
EIGEN_DEVICE_FUNC bool abs2(bool x)
Definition: MathFunctions.h:1102

References Eigen::numext::abs2(), k, min, n, and sqrt().

Referenced by test_complex_generic(), test_complex_strided(), and test_scalar_generic().

◆ EIGEN_DECLARE_TEST()

EIGEN_DECLARE_TEST ( FFTW  )
247  {
249  CALL_SUBTEST(test_complex<float>(32));
250  CALL_SUBTEST(test_complex<double>(32));
251  CALL_SUBTEST(test_complex<float>(256));
252  CALL_SUBTEST(test_complex<double>(256));
253  CALL_SUBTEST(test_complex<float>(3 * 8));
254  CALL_SUBTEST(test_complex<double>(3 * 8));
255  CALL_SUBTEST(test_complex<float>(5 * 32));
256  CALL_SUBTEST(test_complex<double>(5 * 32));
257  CALL_SUBTEST(test_complex<float>(2 * 3 * 4));
258  CALL_SUBTEST(test_complex<double>(2 * 3 * 4));
259  CALL_SUBTEST(test_complex<float>(2 * 3 * 4 * 5));
260  CALL_SUBTEST(test_complex<double>(2 * 3 * 4 * 5));
261  CALL_SUBTEST(test_complex<float>(2 * 3 * 4 * 5 * 7));
262  CALL_SUBTEST(test_complex<double>(2 * 3 * 4 * 5 * 7));
263 
264  CALL_SUBTEST(test_scalar<float>(32));
265  CALL_SUBTEST(test_scalar<double>(32));
266  CALL_SUBTEST(test_scalar<float>(45));
267  CALL_SUBTEST(test_scalar<double>(45));
268  CALL_SUBTEST(test_scalar<float>(50));
269  CALL_SUBTEST(test_scalar<double>(50));
270  CALL_SUBTEST(test_scalar<float>(256));
271  CALL_SUBTEST(test_scalar<double>(256));
272  CALL_SUBTEST(test_scalar<float>(2 * 3 * 4 * 5 * 7));
273  CALL_SUBTEST(test_scalar<double>(2 * 3 * 4 * 5 * 7));
274 
275 #if defined EIGEN_HAS_FFTWL || defined EIGEN_POCKETFFT_DEFAULT
276  CALL_SUBTEST(test_complex<long double>(32));
277  CALL_SUBTEST(test_complex<long double>(256));
278  CALL_SUBTEST(test_complex<long double>(3 * 8));
279  CALL_SUBTEST(test_complex<long double>(5 * 32));
280  CALL_SUBTEST(test_complex<long double>(2 * 3 * 4));
281  CALL_SUBTEST(test_complex<long double>(2 * 3 * 4 * 5));
282  CALL_SUBTEST(test_complex<long double>(2 * 3 * 4 * 5 * 7));
283 
284  CALL_SUBTEST(test_scalar<long double>(32));
285  CALL_SUBTEST(test_scalar<long double>(45));
286  CALL_SUBTEST(test_scalar<long double>(50));
287  CALL_SUBTEST(test_scalar<long double>(256));
288  CALL_SUBTEST(test_scalar<long double>(2 * 3 * 4 * 5 * 7));
289 
290  CALL_SUBTEST((test_complex2d<long double, 2 * 3 * 4, 2 * 3 * 4>()));
291  CALL_SUBTEST((test_complex2d<long double, 3 * 4 * 5, 3 * 4 * 5>()));
292  CALL_SUBTEST((test_complex2d<long double, 24, 60>()));
293  CALL_SUBTEST((test_complex2d<long double, 60, 24>()));
294 // fail to build since Eigen limit the stack allocation size,too big here.
295 // CALL_SUBTEST( ( test_complex2d<long double, 256, 256> () ) );
296 #endif
297 #if defined EIGEN_FFTW_DEFAULT || defined EIGEN_POCKETFFT_DEFAULT || defined EIGEN_MKL_DEFAULT
298  CALL_SUBTEST((test_complex2d<float, 24, 24>()));
299  CALL_SUBTEST((test_complex2d<float, 60, 60>()));
300  CALL_SUBTEST((test_complex2d<float, 24, 60>()));
301  CALL_SUBTEST((test_complex2d<float, 60, 24>()));
302 #endif
303 #if defined EIGEN_FFTW_DEFAULT || defined EIGEN_POCKETFFT_DEFAULT || defined EIGEN_MKL_DEFAULT
304  CALL_SUBTEST((test_complex2d<double, 24, 24>()));
305  CALL_SUBTEST((test_complex2d<double, 60, 60>()));
306  CALL_SUBTEST((test_complex2d<double, 24, 60>()));
307  CALL_SUBTEST((test_complex2d<double, 60, 24>()));
308 #endif
309 }
void test_return_by_value(int len)
Definition: fft_test_shared.h:231
#define CALL_SUBTEST(FUNC)
Definition: main.h:382

References CALL_SUBTEST, and test_return_by_value().

◆ fft_rmse()

template<typename VT1 , typename VT2 >
long double fft_rmse ( const VT1 &  fftbuf,
const VT2 &  timebuf 
)
31  {
32  long double totalpower = 0;
33  long double difpower = 0;
34  long double pi = acos((long double)-1);
35  for (size_t k0 = 0; k0 < (size_t)fftbuf.size(); ++k0) {
36  complex<long double> acc = 0;
37  long double phinc = (long double)(-2.) * k0 * pi / timebuf.size();
38  for (size_t k1 = 0; k1 < (size_t)timebuf.size(); ++k1) {
39  acc += promote(timebuf[k1]) * exp(complex<long double>(0, k1 * phinc));
40  }
41  totalpower += numext::abs2(acc);
42  complex<long double> x = promote(fftbuf[k0]);
43  complex<long double> dif = acc - x;
44  difpower += numext::abs2(dif);
45  // cerr << k0 << "\t" << acc << "\t" << x << "\t" << sqrt(numext::abs2(dif)) << endl;
46  }
47  // cerr << "rmse:" << sqrt(difpower/totalpower) << endl;
48  return sqrt(difpower / totalpower);
49 }
AnnoyingScalar acos(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:138
complex< long double > promote(complex< T > x)
Definition: fft_test_shared.h:22
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 exp(const bfloat16 &a)
Definition: BFloat16.h:615
const Mdouble pi
Definition: ExtendedMath.h:23
list x
Definition: plotDoE.py:28

References Eigen::numext::abs2(), acos(), Eigen::bfloat16_impl::exp(), constants::pi, promote(), sqrt(), and plotDoE::x.

Referenced by test_complex_generic(), test_complex_strided(), and test_scalar_generic().

◆ promote() [1/4]

template<typename T >
complex<long double> promote ( complex< T x)
inline
22  {
23  return complex<long double>((long double)x.real(), (long double)x.imag());
24 }

References plotDoE::x.

Referenced by fft_rmse().

◆ promote() [2/4]

complex<long double> promote ( double  x)
inline
27 { return complex<long double>((long double)x); }

References plotDoE::x.

◆ promote() [3/4]

complex<long double> promote ( float  x)
inline
26 { return complex<long double>((long double)x); }

References plotDoE::x.

◆ promote() [4/4]

complex<long double> promote ( long double  x)
inline
28 { return complex<long double>((long double)x); }

References plotDoE::x.

◆ RandomCpx()

template<typename T >
std::complex<T> RandomCpx ( )
inline
14  {
15  return std::complex<T>((T)(rand() / (T)RAND_MAX - .5), (T)(rand() / (T)RAND_MAX - .5));
16 }

◆ test_complex()

template<typename T >
void test_complex ( int  nfft)
198  {
199  test_complex_generic<StdVectorContainer, T>(nfft);
200  test_complex_generic<EigenVectorContainer, T>(nfft);
201  test_complex_strided<T>(nfft);
202 }

◆ test_complex2d()

template<typename T , int nrows, int ncols>
void test_complex2d ( )
205  {
206  typedef typename Eigen::FFT<T>::Complex Complex;
207  FFT<T> fft;
208  Eigen::Matrix<Complex, nrows, ncols> src, src2, dst, dst2;
209 
211  // src = Eigen::Matrix<Complex,nrows,ncols>::Identity();
212 
213  for (int k = 0; k < ncols; k++) {
215  fft.fwd(tmpOut, src.col(k));
216  dst2.col(k) = tmpOut;
217  }
218 
219  for (int k = 0; k < nrows; k++) {
221  fft.fwd(tmpOut, dst2.row(k));
222  dst2.row(k) = tmpOut;
223  }
224 
225  fft.fwd2(dst.data(), src.data(), ncols, nrows);
226  fft.inv2(src2.data(), dst.data(), ncols, nrows);
227  VERIFY((src - src2).norm() < test_precision<T>());
228  VERIFY((dst - dst2).norm() < test_precision<T>());
229 }
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
constexpr EIGEN_DEVICE_FUNC const Scalar * data() const
Definition: PlainObjectBase.h:273
std::complex< RealScalar > Complex
Definition: common.h:71
#define VERIFY(a)
Definition: main.h:362

References Eigen::PlainObjectBase< Derived >::data(), k, and VERIFY.

◆ test_complex_generic()

template<int Container, typename T >
void test_complex_generic ( int  nfft)
135  {
136  typedef typename FFT<T>::Complex Complex;
137  typedef typename VectorType<Container, Complex>::type ComplexVector;
138 
139  FFT<T> fft;
140 
141  ComplexVector inbuf(nfft);
142  ComplexVector outbuf;
143  ComplexVector buf3;
144  for (int k = 0; k < nfft; ++k)
145  inbuf[k] = Complex((T)(rand() / (double)RAND_MAX - .5), (T)(rand() / (double)RAND_MAX - .5));
146  fft.fwd(outbuf, inbuf);
147 
148  VERIFY(T(fft_rmse(outbuf, inbuf)) < test_precision<T>()); // gross check
149  fft.inv(buf3, outbuf);
150 
151  VERIFY(T(dif_rmse(inbuf, buf3)) < test_precision<T>()); // gross check
152 
153  // verify that the Unscaled flag takes effect
154  ComplexVector buf4;
155  fft.SetFlag(fft.Unscaled);
156  fft.inv(buf4, outbuf);
157  for (int k = 0; k < nfft; ++k) buf4[k] *= T(1. / nfft);
158  VERIFY(T(dif_rmse(inbuf, buf4)) < test_precision<T>()); // gross check
159 
160  // verify that ClearFlag works
161  fft.ClearFlag(fft.Unscaled);
162  fft.inv(buf3, outbuf);
163  VERIFY(T(dif_rmse(inbuf, buf3)) < test_precision<T>()); // gross check
164 }
Eigen::Triplet< double > T
Definition: EigenUnitTest.cpp:11
long double dif_rmse(const VT1 buf1, const VT2 buf2)
Definition: fft_test_shared.h:52
long double fft_rmse(const VT1 &fftbuf, const VT2 &timebuf)
Definition: fft_test_shared.h:31
Definition: fft_test_shared.h:66

References dif_rmse(), fft_rmse(), k, and VERIFY.

◆ test_complex_strided()

template<typename T >
void test_complex_strided ( int  nfft)
167  {
168  typedef typename FFT<T>::Complex Complex;
169  typedef typename Eigen::Vector<Complex, Dynamic> ComplexVector;
170  constexpr int kInputStride = 3;
171  constexpr int kOutputStride = 7;
172  constexpr int kInvOutputStride = 13;
173 
174  FFT<T> fft;
175 
176  ComplexVector inbuf(nfft * kInputStride);
177  inbuf.setRandom();
178  ComplexVector outbuf(nfft * kOutputStride);
179  outbuf.setRandom();
180  ComplexVector invoutbuf(nfft * kInvOutputStride);
181  invoutbuf.setRandom();
182 
183  using StridedComplexVector = Map<ComplexVector, /*MapOptions=*/0, InnerStride<Dynamic>>;
184  StridedComplexVector input(inbuf.data(), nfft, InnerStride<Dynamic>(kInputStride));
185  StridedComplexVector output(outbuf.data(), nfft, InnerStride<Dynamic>(kOutputStride));
186  StridedComplexVector inv_output(invoutbuf.data(), nfft, InnerStride<Dynamic>(kInvOutputStride));
187 
188  for (int k = 0; k < nfft; ++k)
189  input[k] = Complex((T)(rand() / (double)RAND_MAX - .5), (T)(rand() / (double)RAND_MAX - .5));
190  fft.fwd(output, input);
191 
192  VERIFY(T(fft_rmse(output, input)) < test_precision<T>()); // gross check
193  fft.inv(inv_output, output);
194  VERIFY(T(dif_rmse(inv_output, input)) < test_precision<T>()); // gross check
195 }
Convenience specialization of Stride to specify only an inner stride See class Map for some examples.
Definition: Stride.h:93
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:96
void output(std::ostream &outfile, const unsigned &nplot)
Overload output function.
Definition: overloaded_element_body.h:490

References dif_rmse(), fft_rmse(), k, output(), and VERIFY.

◆ test_return_by_value()

void test_return_by_value ( int  len)
inline
231  {
232  VectorXf in;
233  VectorXf in1;
234  in.setRandom(len);
235  VectorXcf out1, out2;
236  FFT<float> fft;
237 
238  fft.SetFlag(fft.HalfSpectrum);
239 
240  fft.fwd(out1, in);
241  out2 = fft.fwd(in);
242  VERIFY((out1 - out2).norm() < test_precision<float>());
243  in1 = fft.inv(out1);
244  VERIFY((in1 - in).norm() < test_precision<float>());
245 }
float test_precision< float >()
Definition: spbenchsolver.h:93

References test_precision< float >(), and VERIFY.

Referenced by EIGEN_DECLARE_TEST().

◆ test_scalar()

template<typename T >
void test_scalar ( int  nfft)
129  {
130  test_scalar_generic<StdVectorContainer, T>(nfft);
131  // test_scalar_generic<EigenVectorContainer,T>(nfft);
132 }

◆ test_scalar_generic()

template<int Container, typename T >
void test_scalar_generic ( int  nfft)
79  {
80  typedef typename FFT<T>::Complex Complex;
81  typedef typename FFT<T>::Scalar Scalar;
82  typedef typename VectorType<Container, Scalar>::type ScalarVector;
83  typedef typename VectorType<Container, Complex>::type ComplexVector;
84 
85  FFT<T> fft;
86  ScalarVector tbuf(nfft);
87  ComplexVector freqBuf;
88  for (int k = 0; k < nfft; ++k) tbuf[k] = (T)(rand() / (double)RAND_MAX - .5);
89 
90  // make sure it DOESN'T give the right full spectrum answer
91  // if we've asked for half-spectrum
92  fft.SetFlag(fft.HalfSpectrum);
93  fft.fwd(freqBuf, tbuf);
94  VERIFY((size_t)freqBuf.size() == (size_t)((nfft >> 1) + 1));
95  VERIFY(T(fft_rmse(freqBuf, tbuf)) < test_precision<T>()); // gross check
96 
97  fft.ClearFlag(fft.HalfSpectrum);
98  fft.fwd(freqBuf, tbuf);
99  VERIFY((size_t)freqBuf.size() == (size_t)nfft);
100  VERIFY(T(fft_rmse(freqBuf, tbuf)) < test_precision<T>()); // gross check
101 
102  if (nfft & 1) return; // odd FFTs get the wrong size inverse FFT
103 
104  ScalarVector tbuf2;
105  fft.inv(tbuf2, freqBuf);
106  VERIFY(T(dif_rmse(tbuf, tbuf2)) < test_precision<T>()); // gross check
107 
108  // verify that the Unscaled flag takes effect
109  ScalarVector tbuf3;
110  fft.SetFlag(fft.Unscaled);
111 
112  fft.inv(tbuf3, freqBuf);
113 
114  for (int k = 0; k < nfft; ++k) tbuf3[k] *= T(1. / nfft);
115 
116  // for (size_t i=0;i<(size_t) tbuf.size();++i)
117  // cout << "freqBuf=" << freqBuf[i] << " in2=" << tbuf3[i] << " - in=" << tbuf[i] << " => " << (tbuf3[i] -
118  // tbuf[i] ) << endl;
119 
120  VERIFY(T(dif_rmse(tbuf, tbuf3)) < test_precision<T>()); // gross check
121 
122  // verify that ClearFlag works
123  fft.ClearFlag(fft.Unscaled);
124  fft.inv(tbuf2, freqBuf);
125  VERIFY(T(dif_rmse(tbuf, tbuf2)) < test_precision<T>()); // gross check
126 }
SCALAR Scalar
Definition: bench_gemm.cpp:45

References dif_rmse(), fft_rmse(), k, and VERIFY.