-
Notifications
You must be signed in to change notification settings - Fork 3
/
FFT.cpp
139 lines (117 loc) · 2.72 KB
/
FFT.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "FFT.h"
#include <cassert>
#include <vector>
#include <iostream>
#include <omp.h>
#ifdef USE_CUDA
int fftw_init_threads()
{
return 0;
}
void fftw_plan_with_nthreads(int nthreads)
{
}
void fftw_cleanup_threads()
{
}
void Perform1DR2R(int size, const stratifloat* in, stratifloat* out, f3_r2r_kind kind)
{
int fftSize;
if (kind == FFTW_REDFT00)
{
fftSize = 2*(size-1);
}
else if (kind == FFTW_RODFT00)
{
fftSize = 2*(size+1);
}
else
{
assert(0);
}
std::vector<complex> fftData(fftSize);
if (kind == FFTW_RODFT00)
{
for (int j=0; j<fftSize; j++)
{
complex &outVal = fftData[j];
if (j == 0 || j == size+1)
{
outVal = 0;
}
else if (j<size+1)
{
outVal = in[j+1];
}
else
{
// odd symmetry gives a sine transform in 3rd direction
outVal = -in[fftSize-j-1];
}
}
}
else
{
for (int j=0; j<fftSize; j++)
{
complex &outVal = fftData[j];
if (j<size)
{
outVal = in[j];
}
else
{
// even symmetry gives a cosine transform in 3rd direction
outVal = in[fftSize-j];
}
}
}
auto plan = f3_plan_dft_1d(fftSize,
reinterpret_cast<f3_complex*>(fftData.data()),
reinterpret_cast<f3_complex*>(fftData.data()),
FFTW_FORWARD,
FFTW_ESTIMATE);
assert(plan);
f3_execute(plan);
f3_destroy_plan(plan);
if (kind == FFTW_RODFT00)
{
for (int j=0; j<size; j++)
{
out[j] = -imag(fftData[j+1]);
}
}
else
{
for (int j=0; j<size; j++)
{
out[j] = real(fftData[j]);
}
}
}
#else
void Perform1DR2R(int size, const stratifloat* in, stratifloat* out, f3_r2r_kind kind)
{
// the const_cast is legit when using FFTW_ESTIMATE
auto plan = f3_plan_r2r_1d(size, const_cast<stratifloat*>(in), out, kind, FFTW_ESTIMATE);
assert(plan);
f3_execute(plan);
f3_destroy_plan(plan);
}
#endif
void Setup()
{
// We use printf here because of weird std bugs when using cout
printf("Setting up Stratiflow\n");
if (f3_init_threads() == 0)
{
fprintf(stderr, "Failed to initialise fftw threads.\n");
}
f3_plan_with_nthreads(omp_get_max_threads());
printf("Using %d threads\n", omp_get_max_threads());
}
void Cleanup()
{
f3_cleanup_threads();
}
int InitialiserClass::counter;