blob: 0e040ad706bb0ce21c1631d50a486281e4874a0a [file] [log] [blame]
C. Antonio Sanchez56634d82022-07-07 08:21:18 -07001// 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
13template <typename T>
14inline std::complex<T> RandomCpx() {
15 return std::complex<T>((T)(rand() / (T)RAND_MAX - .5), (T)(rand() / (T)RAND_MAX - .5));
16}
17
18using namespace std;
19using namespace Eigen;
20
21template <typename T>
22inline complex<long double> promote(complex<T> x) {
23 return complex<long double>((long double)x.real(), (long double)x.imag());
24}
25
26inline complex<long double> promote(float x) { return complex<long double>((long double)x); }
27inline complex<long double> promote(double x) { return complex<long double>((long double)x); }
28inline complex<long double> promote(long double x) { return complex<long double>((long double)x); }
29
30template <typename VT1, typename VT2>
31long 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
51template <typename VT1, typename VT2>
52long 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
63enum { StdVectorContainer, EigenVectorContainer };
64
65template <int Container, typename Scalar>
66struct VectorType;
67
68template <typename Scalar>
69struct VectorType<StdVectorContainer, Scalar> {
70 typedef vector<Scalar> type;
71};
72
73template <typename Scalar>
74struct VectorType<EigenVectorContainer, Scalar> {
75 typedef Matrix<Scalar, Dynamic, 1> type;
76};
77
78template <int Container, typename T>
79void 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
128template <typename T>
129void test_scalar(int nfft) {
130 test_scalar_generic<StdVectorContainer, T>(nfft);
131 // test_scalar_generic<EigenVectorContainer,T>(nfft);
132}
133
134template <int Container, typename T>
135void 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
166template <typename T>
167void test_complex(int nfft) {
168 test_complex_generic<StdVectorContainer, T>(nfft);
169 test_complex_generic<EigenVectorContainer, T>(nfft);
170}
171
172template <typename T, int nrows, int ncols>
173void test_complex2d() {
174 typedef typename Eigen::FFT<T>::Complex Complex;
175 FFT<T> fft;
176 Eigen::Matrix<Complex, nrows, ncols> src, src2, dst, dst2;
177
178 src = Eigen::Matrix<Complex, nrows, ncols>::Random();
179 // src = Eigen::Matrix<Complex,nrows,ncols>::Identity();
180
181 for (int k = 0; k < ncols; k++) {
182 Eigen::Matrix<Complex, nrows, 1> tmpOut;
183 fft.fwd(tmpOut, src.col(k));
184 dst2.col(k) = tmpOut;
185 }
186
187 for (int k = 0; k < nrows; k++) {
188 Eigen::Matrix<Complex, 1, ncols> tmpOut;
189 fft.fwd(tmpOut, dst2.row(k));
190 dst2.row(k) = tmpOut;
191 }
192
193 fft.fwd2(dst.data(), src.data(), ncols, nrows);
194 fft.inv2(src2.data(), dst.data(), ncols, nrows);
195 VERIFY((src - src2).norm() < test_precision<T>());
196 VERIFY((dst - dst2).norm() < test_precision<T>());
197}
198
199inline void test_return_by_value(int len) {
200 VectorXf in;
201 VectorXf in1;
202 in.setRandom(len);
203 VectorXcf out1, out2;
204 FFT<float> fft;
205
206 fft.SetFlag(fft.HalfSpectrum);
207
208 fft.fwd(out1, in);
209 out2 = fft.fwd(in);
210 VERIFY((out1 - out2).norm() < test_precision<float>());
211 in1 = fft.inv(out1);
212 VERIFY((in1 - in).norm() < test_precision<float>());
213}
214
215EIGEN_DECLARE_TEST(FFTW) {
216 CALL_SUBTEST(test_return_by_value(32));
217 CALL_SUBTEST(test_complex<float>(32));
218 CALL_SUBTEST(test_complex<double>(32));
219 CALL_SUBTEST(test_complex<float>(256));
220 CALL_SUBTEST(test_complex<double>(256));
221 CALL_SUBTEST(test_complex<float>(3 * 8));
222 CALL_SUBTEST(test_complex<double>(3 * 8));
223 CALL_SUBTEST(test_complex<float>(5 * 32));
224 CALL_SUBTEST(test_complex<double>(5 * 32));
225 CALL_SUBTEST(test_complex<float>(2 * 3 * 4));
226 CALL_SUBTEST(test_complex<double>(2 * 3 * 4));
227 CALL_SUBTEST(test_complex<float>(2 * 3 * 4 * 5));
228 CALL_SUBTEST(test_complex<double>(2 * 3 * 4 * 5));
229 CALL_SUBTEST(test_complex<float>(2 * 3 * 4 * 5 * 7));
230 CALL_SUBTEST(test_complex<double>(2 * 3 * 4 * 5 * 7));
231
232 CALL_SUBTEST(test_scalar<float>(32));
233 CALL_SUBTEST(test_scalar<double>(32));
234 CALL_SUBTEST(test_scalar<float>(45));
235 CALL_SUBTEST(test_scalar<double>(45));
236 CALL_SUBTEST(test_scalar<float>(50));
237 CALL_SUBTEST(test_scalar<double>(50));
238 CALL_SUBTEST(test_scalar<float>(256));
239 CALL_SUBTEST(test_scalar<double>(256));
240 CALL_SUBTEST(test_scalar<float>(2 * 3 * 4 * 5 * 7));
241 CALL_SUBTEST(test_scalar<double>(2 * 3 * 4 * 5 * 7));
242
243#if defined EIGEN_HAS_FFTWL || defined EIGEN_POCKETFFT_DEFAULT
244 CALL_SUBTEST(test_complex<long double>(32));
245 CALL_SUBTEST(test_complex<long double>(256));
246 CALL_SUBTEST(test_complex<long double>(3 * 8));
247 CALL_SUBTEST(test_complex<long double>(5 * 32));
248 CALL_SUBTEST(test_complex<long double>(2 * 3 * 4));
249 CALL_SUBTEST(test_complex<long double>(2 * 3 * 4 * 5));
250 CALL_SUBTEST(test_complex<long double>(2 * 3 * 4 * 5 * 7));
251
252 CALL_SUBTEST(test_scalar<long double>(32));
253 CALL_SUBTEST(test_scalar<long double>(45));
254 CALL_SUBTEST(test_scalar<long double>(50));
255 CALL_SUBTEST(test_scalar<long double>(256));
256 CALL_SUBTEST(test_scalar<long double>(2 * 3 * 4 * 5 * 7));
257
258 CALL_SUBTEST((test_complex2d<long double, 2 * 3 * 4, 2 * 3 * 4>()));
259 CALL_SUBTEST((test_complex2d<long double, 3 * 4 * 5, 3 * 4 * 5>()));
260 CALL_SUBTEST((test_complex2d<long double, 24, 60>()));
261 CALL_SUBTEST((test_complex2d<long double, 60, 24>()));
262// fail to build since Eigen limit the stack allocation size,too big here.
263// CALL_SUBTEST( ( test_complex2d<long double, 256, 256> () ) );
264#endif
265#if defined EIGEN_FFTW_DEFAULT || defined EIGEN_POCKETFFT_DEFAULT || defined EIGEN_MKL_DEFAULT
266 CALL_SUBTEST((test_complex2d<float, 24, 24>()));
267 CALL_SUBTEST((test_complex2d<float, 60, 60>()));
268 CALL_SUBTEST((test_complex2d<float, 24, 60>()));
269 CALL_SUBTEST((test_complex2d<float, 60, 24>()));
270#endif
271#if defined EIGEN_FFTW_DEFAULT || defined EIGEN_POCKETFFT_DEFAULT || defined EIGEN_MKL_DEFAULT
272 CALL_SUBTEST((test_complex2d<double, 24, 24>()));
273 CALL_SUBTEST((test_complex2d<double, 60, 60>()));
274 CALL_SUBTEST((test_complex2d<double, 24, 60>()));
275 CALL_SUBTEST((test_complex2d<double, 60, 24>()));
276#endif
277}