-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwfft.cc
More file actions
300 lines (250 loc) · 7.95 KB
/
Copy pathwfft.cc
File metadata and controls
300 lines (250 loc) · 7.95 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include "options.h"
#include <fstream>
#include <fftw.h>
#include <rfftw.h>
#include "fft.h"
inline void fftw_export_wisdom(void (*emitter)(char c, ofstream& s),
ofstream& s)
{
fftw_export_wisdom((void (*) (char, void *)) emitter, (void *) &s);
}
inline fftw_status fftw_import_wisdom(int (*g)(ifstream& s), ifstream &s)
{
return fftw_import_wisdom((int (*) (void *)) g, (void *) &s);
}
inline void put_wisdom(char c, ofstream& s)
{
s.put(c);
}
inline int get_wisdom(ifstream& s)
{
return s.get();
}
static ifstream ifwisdom;
static ofstream ofwisdom;
static const char *wisdom_name="wisdom.txt";
static fftw_plan *plan=NULL, *planinv=NULL;
static size_t nplan=0;
static rfftwnd_plan *crplan=NULL;
static size_t ncrplan=0;
static rfftwnd_plan *rcplan=NULL;
static size_t nrcplan=0;
// Create plans for size n
static int init_plan(size_t n, int)
{
nplan++;
plan=new(plan,nplan) fftw_plan;
planinv=new(planinv,nplan) fftw_plan;
ifwisdom.open(wisdom_name);
fftw_import_wisdom(get_wisdom,ifwisdom);
ifwisdom.close();
int options=FFTW_MEASURE | FFTW_USE_WISDOM | FFTW_IN_PLACE;
plan[nplan-1]=fftw_create_plan(n, (fftw_direction) 1, options);
planinv[nplan-1]=fftw_create_plan(n, (fftw_direction) -1, options);
ofwisdom.open(wisdom_name);
fftw_export_wisdom(put_wisdom,ofwisdom);
ofwisdom.close();
return nplan;
}
// Create complex_to_real plans for size n
static int init_crplan(size_t n)
{
ncrplan++;
crplan=new(crplan,ncrplan) rfftwnd_plan;
ifwisdom.open(wisdom_name);
fftw_import_wisdom(get_wisdom,ifwisdom);
ifwisdom.close();
int options=FFTW_MEASURE | FFTW_USE_WISDOM | FFTW_IN_PLACE;
int size[1]={n};
crplan[ncrplan-1]=rfftwnd_create_plan(1,size,FFTW_COMPLEX_TO_REAL,options);
ofwisdom.open(wisdom_name);
fftw_export_wisdom(put_wisdom,ofwisdom);
ofwisdom.close();
return ncrplan;
}
// Create real_to_complex plans for size n
static int init_rcplan(size_t n)
{
nrcplan++;
rcplan=new(rcplan,nrcplan) rfftwnd_plan;
ifwisdom.open(wisdom_name);
fftw_import_wisdom(get_wisdom,ifwisdom);
ifwisdom.close();
int options=FFTW_MEASURE | FFTW_USE_WISDOM | FFTW_IN_PLACE;
int size[1]={n};
rcplan[nrcplan-1]=rfftwnd_create_plan(1,size,FFTW_REAL_TO_COMPLEX,options);
ofwisdom.open(wisdom_name);
fftw_export_wisdom(put_wisdom,ofwisdom);
ofwisdom.close();
return nrcplan;
}
void scalefft(Complex *data, size_t n, size_t nk,
size_t inc1, size_t inc2, Real scale)
{
size_t kstop=nk*inc2;
if(inc1 == 1) {
for(size_t k=0; k < kstop; k += inc2) {
Complex *p0=data+k, *pstop=p0+n;
//#pragma ivdep
for(Complex *p=p0; p < pstop; p++) {
p->re *= scale;
p->im *= scale;
}
}
} else {
Complex *pstop=data+n*inc1;
for(Complex *p=data; p < pstop; p += inc1) {
//#pragma ivdep
for(size_t k=0; k < kstop; k += inc2) {
p[k].re *= scale;
p[k].im *= scale;
}
}
}
}
void signscalefft(Complex *data, size_t n, int isign, size_t nk,
size_t inc1, size_t inc2, Real scale)
{
if(scale == 1.0) {
if(isign != 1.0) {
size_t kstop=nk*inc2;
if(inc1 == 1) {
for(size_t k=0; k < kstop; k += inc2) {
Complex *p0=data+k, *pstop=p0+n;
//#pragma ivdep
for(Complex *p=p0; p < pstop; p++) {
p->im=-p->im;
}
}
} else {
Complex *pstop=data+n*inc1;
for(Complex *p=data; p < pstop; p += inc1) {
//#pragma ivdep
for(size_t k=0; k < kstop; k += inc2) {
p[k].im=-p[k].im;
}
}
}
}
} else {
if(isign == 1.0) scalefft(data,n,nk,inc1,inc2,scale);
else {
size_t kstop=nk*inc2;
if(inc1 == 1) {
for(size_t k=0; k < kstop; k += inc2) {
Complex *p0=data+k, *pstop=p0+n;
//#pragma ivdep
for(Complex *p=p0; p < pstop; p++) {
p->re *= scale;
p->im *= -scale;
}
}
} else {
Complex *pstop=data+n*inc1;
for(Complex *p=data; p < pstop; p += inc1) {
//#pragma ivdep
for(size_t k=0; k < kstop; k += inc2) {
p[k].re *= scale;
p[k].im *= -scale;
}
}
}
}
}
}
void mfft(Complex *data, size_t log2n, int isign, size_t nk,
size_t inc1, size_t inc2, Real scale, int)
{
static size_t TableSize=0;
static size_t *Table=NULL;
size_t n=1 << log2n;
if(inc1 == 0) inc1=nk;
if(n > TableSize) {
size_t nold=TableSize;
TableSize=n;
Table=new(Table,TableSize) size_t;
for(size_t i=nold; i < TableSize; i++) Table[i]=0;
}
if(Table[n-1] == 0) Table[n-1]=init_plan(n,inc1);
int j=Table[n-1]-1;
fftw((isign == 1) ? plan[j] : planinv[j],nk,
(fftw_complex *) data,inc1,inc2,NULL,1,1);
if(scale != 1.0) scalefft(data,n,nk,inc1,inc2,scale);
}
void fft(Complex *data, size_t log2n, int isign, Real scale, int)
{
mfft(data,log2n,isign,1,1,1,scale);
}
// Return the real inverse Fourier transform of nk Complex vectors, of
// length n/2+1, corresponding to the non-negative part of the frequency
// spectrum. Before calling, data must be allocated as Complex[nk*(n/2+1)].
// On entry: data contains the n/2+1 Complex Fourier transform values for
// each k=0,...,nk-1,
// log2n contains the base-2 logarithm of n;
// isign is the sign (+/- 1) of the phase;
// nk is the number of Complex vectors;
// [inc1 is the stride between the elements of each Complex vector;]
// [inc2 is the stride between first elements of the vectors;]
// [scale is a constant by which the results will be multiplied;]
// [bitreverse is 0 for a true fft of data (default);
// +1 for a fft of bit-reversed data (faster);
// -1 for a bit-reversed fft of data (faster).]
// On exit: data contains the nk*n real inverse Fourier transform values
// stored as Complex arrays of length n/2 for each k=0,...,nk-1.
//
// Note: To compute a true inverse transform, set scale=1.0/n.
void mcrfft(Complex *data, size_t log2n, int isign, size_t nk,
size_t inc1, size_t inc2, Real scale, int)
{
static size_t TableSize=0;
static size_t *Table=NULL;
size_t n=1 << log2n;
if(log2n == 0) return;
if(inc1 == 0) inc1=nk;
signscalefft(data,n/2+1,isign,nk,inc1,inc2,scale);
if(n > TableSize) {
size_t nold=TableSize;
TableSize=n;
Table=new(Table,TableSize) size_t;
for(size_t i=nold; i < TableSize; i++) Table[i]=0;
}
if(Table[n-1] == 0) Table[n-1]=init_crplan(n);
int j=Table[n-1]-1;
rfftwnd_complex_to_real(crplan[j],nk,(fftw_complex *) data,inc1,inc2,
NULL,1,1);
}
// Return the Fourier transform of nk real vectors, each of length n.
// Before calling, data must be allocated as Complex[nk*(n/2+1)].
// On entry: data contains the nk*n real values stored as Complex
// arrays of length n/2 for each k=0,...,nk-1;
// log2n contains the base-2 logarithm of n;
// isign is the sign (+/- 1) of the phase;
// nk is the number of Complex vectors;
// [inc1 is the stride between the elements of each Complex vector;]
// [inc2 is the stride between first elements of the vectors;]
// [scale is a constant by which the results will be multiplied;]
// [bitreverse is 0 for a true fft of data (default);
// +1 for a fft of bit-reversed data (faster);
// -1 for a bit-reversed fft of data (faster).]
// On exit: data contains the nk*(n/2+1) Complex Fourier values.
void mrcfft(Complex *data, size_t log2n, int isign, size_t nk,
size_t inc1, size_t inc2, Real scale, int)
{
static size_t TableSize=0;
static size_t *Table=NULL;
size_t n=1 << log2n;
if(log2n == 0) return;
if(inc1 == 0) inc1=nk;
if(n > TableSize) {
size_t nold=TableSize;
TableSize=n;
Table=new(Table,TableSize) size_t;
for(size_t i=nold; i < TableSize; i++) Table[i]=0;
}
if(Table[n-1] == 0) Table[n-1]=init_rcplan(n);
int j=Table[n-1]-1;
int inc2real=((inc2 == 1) ? inc2 : 2*inc2);
rfftwnd_real_to_complex(rcplan[j],nk,(fftw_real *) data,inc1,inc2real,
NULL,1,1);
signscalefft(data,n/2+1,-isign,nk,inc1,inc2,scale);
}