11 #include <unsupported/Eigen/FFT>
15 return std::complex<T>((
T)(rand() / (
T)RAND_MAX - .5), (
T)(rand() / (
T)RAND_MAX - .5));
19 using namespace Eigen;
22 inline complex<long double>
promote(complex<T>
x) {
23 return complex<long double>((
long double)
x.real(), (
long double)
x.imag());
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); }
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));
42 complex<long double>
x =
promote(fftbuf[k0]);
43 complex<long double> dif = acc -
x;
48 return sqrt(difpower / totalpower);
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) {
60 return sqrt(difpower / totalpower);
65 template <
int Container,
typename Scalar>
68 template <
typename Scalar>
70 typedef vector<Scalar>
type;
73 template <
typename Scalar>
78 template <
int Container,
typename T>
86 ScalarVector tbuf(nfft);
87 ComplexVector freqBuf;
88 for (
int k = 0;
k < nfft; ++
k) tbuf[
k] = (
T)(rand() / (
double)RAND_MAX - .5);
92 fft.SetFlag(fft.HalfSpectrum);
93 fft.fwd(freqBuf, tbuf);
94 VERIFY((
size_t)freqBuf.size() == (
size_t)((nfft >> 1) + 1));
97 fft.ClearFlag(fft.HalfSpectrum);
98 fft.fwd(freqBuf, tbuf);
99 VERIFY((
size_t)freqBuf.size() == (
size_t)nfft);
102 if (nfft & 1)
return;
105 fft.inv(tbuf2, freqBuf);
110 fft.SetFlag(fft.Unscaled);
112 fft.inv(tbuf3, freqBuf);
114 for (
int k = 0;
k < nfft; ++
k) tbuf3[
k] *=
T(1. / nfft);
123 fft.ClearFlag(fft.Unscaled);
124 fft.inv(tbuf2, freqBuf);
128 template <
typename T>
130 test_scalar_generic<StdVectorContainer, T>(nfft);
134 template <
int Container,
typename T>
141 ComplexVector inbuf(nfft);
142 ComplexVector outbuf;
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);
149 fft.inv(buf3, outbuf);
155 fft.SetFlag(fft.Unscaled);
156 fft.inv(buf4, outbuf);
157 for (
int k = 0;
k < nfft; ++
k) buf4[
k] *=
T(1. / nfft);
161 fft.ClearFlag(fft.Unscaled);
162 fft.inv(buf3, outbuf);
166 template <
typename T>
170 constexpr
int kInputStride = 3;
171 constexpr
int kOutputStride = 7;
172 constexpr
int kInvOutputStride = 13;
176 ComplexVector inbuf(nfft * kInputStride);
178 ComplexVector outbuf(nfft * kOutputStride);
180 ComplexVector invoutbuf(nfft * kInvOutputStride);
181 invoutbuf.setRandom();
186 StridedComplexVector inv_output(invoutbuf.data(), nfft,
InnerStride<Dynamic>(kInvOutputStride));
188 for (
int k = 0;
k < nfft; ++
k)
189 input[
k] =
Complex((
T)(rand() / (
double)RAND_MAX - .5), (
T)(rand() / (
double)RAND_MAX - .5));
193 fft.inv(inv_output,
output);
197 template <
typename T>
199 test_complex_generic<StdVectorContainer, T>(nfft);
200 test_complex_generic<EigenVectorContainer, T>(nfft);
201 test_complex_strided<T>(nfft);
204 template <
typename T,
int nrows,
int ncols>
213 for (
int k = 0;
k < ncols;
k++) {
215 fft.fwd(tmpOut, src.col(
k));
216 dst2.col(
k) = tmpOut;
219 for (
int k = 0;
k < nrows;
k++) {
221 fft.fwd(tmpOut, dst2.row(
k));
222 dst2.row(
k) = tmpOut;
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>());
235 VectorXcf out1, out2;
238 fft.SetFlag(fft.HalfSpectrum);
275 #if defined EIGEN_HAS_FFTWL || defined EIGEN_POCKETFFT_DEFAULT
282 CALL_SUBTEST(test_complex<long double>(2 * 3 * 4 * 5 * 7));
288 CALL_SUBTEST(test_scalar<long double>(2 * 3 * 4 * 5 * 7));
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>()));
297 #if defined EIGEN_FFTW_DEFAULT || defined EIGEN_POCKETFFT_DEFAULT || defined EIGEN_MKL_DEFAULT
303 #if defined EIGEN_FFTW_DEFAULT || defined EIGEN_POCKETFFT_DEFAULT || defined EIGEN_MKL_DEFAULT
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