fft_test_shared.h
Go to the documentation of this file.
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 
10 #include "main.h"
11 #include <unsupported/Eigen/FFT>
12 
13 template <typename T>
14 inline std::complex<T> RandomCpx() {
15  return std::complex<T>((T)(rand() / (T)RAND_MAX - .5), (T)(rand() / (T)RAND_MAX - .5));
16 }
17 
18 using namespace std;
19 using namespace Eigen;
20 
21 template <typename T>
22 inline complex<long double> promote(complex<T> x) {
23  return complex<long double>((long double)x.real(), (long double)x.imag());
24 }
25 
26 inline complex<long double> promote(float x) { return complex<long double>((long double)x); }
27 inline complex<long double> promote(double x) { return complex<long double>((long double)x); }
28 inline complex<long double> promote(long double x) { return complex<long double>((long double)x); }
29 
30 template <typename VT1, typename VT2>
31 long double fft_rmse(const VT1& fftbuf, const VT2& timebuf) {
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 }
50 
51 template <typename VT1, typename VT2>
52 long double dif_rmse(const VT1 buf1, const VT2 buf2) {
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 }
62 
64 
65 template <int Container, typename Scalar>
66 struct VectorType;
67 
68 template <typename Scalar>
70  typedef vector<Scalar> type;
71 };
72 
73 template <typename Scalar>
76 };
77 
78 template <int Container, typename T>
79 void test_scalar_generic(int nfft) {
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 }
127 
128 template <typename T>
129 void test_scalar(int nfft) {
130  test_scalar_generic<StdVectorContainer, T>(nfft);
131  // test_scalar_generic<EigenVectorContainer,T>(nfft);
132 }
133 
134 template <int Container, typename T>
135 void test_complex_generic(int nfft) {
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 }
165 
166 template <typename T>
167 void test_complex_strided(int nfft) {
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 }
196 
197 template <typename T>
198 void test_complex(int nfft) {
199  test_complex_generic<StdVectorContainer, T>(nfft);
200  test_complex_generic<EigenVectorContainer, T>(nfft);
201  test_complex_strided<T>(nfft);
202 }
203 
204 template <typename T, int nrows, int ncols>
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 }
230 
231 inline void test_return_by_value(int len) {
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 }
246 
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 }
AnnoyingScalar acos(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:138
AnnoyingScalar sqrt(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:134
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11
Eigen::Triplet< double > T
Definition: EigenUnitTest.cpp:11
SCALAR Scalar
Definition: bench_gemm.cpp:45
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
constexpr EIGEN_DEVICE_FUNC const Scalar * data() const
Definition: PlainObjectBase.h:273
std::complex< RealScalar > Complex
Definition: common.h:71
#define min(a, b)
Definition: datatypes.h:22
void test_complex_generic(int nfft)
Definition: fft_test_shared.h:135
long double dif_rmse(const VT1 buf1, const VT2 buf2)
Definition: fft_test_shared.h:52
std::complex< T > RandomCpx()
Definition: fft_test_shared.h:14
long double fft_rmse(const VT1 &fftbuf, const VT2 &timebuf)
Definition: fft_test_shared.h:31
complex< long double > promote(complex< T > x)
Definition: fft_test_shared.h:22
@ EigenVectorContainer
Definition: fft_test_shared.h:63
@ StdVectorContainer
Definition: fft_test_shared.h:63
void test_return_by_value(int len)
Definition: fft_test_shared.h:231
void test_scalar_generic(int nfft)
Definition: fft_test_shared.h:79
void test_scalar(int nfft)
Definition: fft_test_shared.h:129
void test_complex2d()
Definition: fft_test_shared.h:205
void test_complex_strided(int nfft)
Definition: fft_test_shared.h:167
void test_complex(int nfft)
Definition: fft_test_shared.h:198
EIGEN_DECLARE_TEST(FFTW)
Definition: fft_test_shared.h:247
char char char int int * k
Definition: level2_impl.h:374
#define VERIFY(a)
Definition: main.h:362
#define CALL_SUBTEST(FUNC)
Definition: main.h:382
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 exp(const bfloat16 &a)
Definition: BFloat16.h:615
EIGEN_DEVICE_FUNC bool abs2(bool x)
Definition: MathFunctions.h:1102
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
const Mdouble pi
Definition: ExtendedMath.h:23
list x
Definition: plotDoE.py:28
void output(std::ostream &outfile, const unsigned &nplot)
Overload output function.
Definition: overloaded_element_body.h:490
float test_precision< float >()
Definition: spbenchsolver.h:93
Matrix< Scalar, Dynamic, 1 > type
Definition: fft_test_shared.h:75
vector< Scalar > type
Definition: fft_test_shared.h:70
Definition: fft_test_shared.h:66