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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
|
/* Fast Fourier transform C++ header class for the FFTW3 Library
Copyright (C) 2004 John C. Bowman, University of Alberta
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifndef __fftwpp_h__
#define __fftwpp_h__ 1
#define __FFTWPP_H_VERSION__ 1.04
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <fftw3.h>
#ifndef __Complex_h__
#include <complex>
typedef std::complex<double> Complex;
#endif
#ifdef __Array_h__
using Array::array1;
using Array::array2;
using Array::array3;
static array1<Complex> NULL1;
static array2<Complex> NULL2;
static array3<Complex> NULL3;
#endif
inline Complex *FFTWComplex(size_t size)
{
static const size_t offset = sizeof(size_t)/sizeof(Complex)+
(sizeof(size_t) % sizeof(Complex) > 0);
void *alloc=fftw_malloc((size+offset)*sizeof(Complex));
if(size && !alloc) std::cerr << std::endl << "Memory limits exceeded"
<< std::endl;
*(size_t *) alloc=size;
Complex*p=(Complex *)alloc+offset;
for(size_t i=0; i < size; i++) new(p+i) Complex;
return p;
}
inline double *FFTWdouble(size_t size)
{
static const size_t offset = sizeof(size_t)/sizeof(Complex)+
(sizeof(size_t) % sizeof(Complex) > 0);
void *alloc=fftw_malloc(size*sizeof(double)+offset*sizeof(Complex));
if(size && !alloc) std::cerr << std::endl << "Memory limits exceeded"
<< std::endl;
*(size_t *) alloc=size;
double*p=(double*)((Complex *)alloc+offset);
for(size_t i=0; i < size; i++) new(p+i) double;
return p;
}
template<class T>
inline void FFTWdelete(T *p)
{
static const size_t offset = sizeof(size_t)/sizeof(Complex)+
(sizeof(size_t) % sizeof(Complex) > 0);
void *alloc=(Complex *)p-offset;
size_t size=*(size_t *) alloc;
for(size_t i=size-1; i > 0; i--) ((T *) p)[i].~T();
((T *) p)[0].~T();
fftw_free(alloc);
}
inline void fftwpp_export_wisdom(void (*emitter)(char c, std::ofstream& s),
std::ofstream& s)
{
fftw_export_wisdom((void (*) (char, void *)) emitter,(void *) &s);
}
inline int fftwpp_import_wisdom(int (*g)(std::ifstream& s), std::ifstream &s)
{
return fftw_import_wisdom((int (*) (void *)) g,(void *) &s);
}
inline void PutWisdom(char c, std::ofstream& s) {s.put(c);}
inline int GetWisdom(std::ifstream& s) {return s.get();}
// Base clase for fft routines
//
class fftw {
protected:
unsigned int size;
int sign;
double norm;
bool shift;
fftw_plan plan;
bool inplace;
static unsigned int effort;
static bool Wise;
static const char *WisdomName;
static std::ifstream ifWisdom;
static std::ofstream ofWisdom;
unsigned int Dist(unsigned int n, unsigned int stride, unsigned int dist) {
return dist ? dist : ((stride == 1) ? n : 1);
}
unsigned int realsize(unsigned int n, Complex *in, Complex *out) {
return n/2+(!out || in == out);
}
unsigned int realsize(unsigned int n, Complex *in, double *out) {
return realsize(n,in,(Complex *) out);
}
// Shift the Fourier origin to (nx/2,0)
void Shift(Complex *data, unsigned int nx, unsigned int ny) {
const unsigned int nyp=ny/2+1;
Complex *pstop=data+nx*nyp;
int pinc=2*nyp;
for(Complex *p=data+nyp; p < pstop; p += pinc) {
//#pragma ivdep
for(unsigned int j=0; j < nyp; j++) p[j]=-p[j];
}
}
// Shift the Fourier origin to (nx/2,ny/2,0)
void Shift(Complex *data, unsigned int nx, unsigned int ny,
unsigned int nz) {
const unsigned int nzp=nz/2+1;
const unsigned int nyzp=ny*nzp;
const unsigned int pinc=2*nzp;
Complex *p,*pstop;
p=pstop=data;
for(unsigned i=0; i < nx; i++) {
if(i % 2) p -= nzp;
else p += nzp;
pstop += nyzp;
for(; p < pstop; p += pinc) {
//#pragma ivdep
for(unsigned int k=0; k < nzp; k++) p[k]=-p[k];
}
}
}
public:
fftw(unsigned int size, int sign, unsigned int n=0) :
size(size), sign(sign), norm(1.0/(n ? n : size)), shift(false), plan(NULL)
{}
virtual ~fftw() {if(plan) fftw_destroy_plan(plan);}
virtual fftw_plan Plan(Complex *in, Complex *out)=0;
inline void CheckAlign(Complex *p, const char *s) {
if((size_t) p % sizeof(Complex) == 0) return;
std::cerr << "WARNING: " << s << " array is not " << sizeof(Complex)
<< "-byte aligned: address " << p << std::endl;
}
void Setup(Complex *in, Complex *out=NULL) {
if(!Wise) LoadWisdom();
bool alloc=!in;
if(alloc) in=FFTWComplex(size);
#ifndef NO_CHECK_ALIGN
CheckAlign(in,"constructor input");
if(out) CheckAlign(out,"constructor output");
else out=in;
#else
if(!out) out=in;
#endif
inplace=(out==in);
plan=Plan(in,out);
if(!plan) {
std::cerr << "Unable to construct FFTW plan" << std::endl;
exit(1);
}
if(alloc) FFTWdelete(in);
SaveWisdom();
}
void Setup(Complex *in, double *out) {Setup(in,(Complex *) out);}
void Setup(double *in, Complex *out) {Setup((Complex *) in,out);}
void LoadWisdom() {
ifWisdom.open(WisdomName);
fftwpp_import_wisdom(GetWisdom,ifWisdom);
ifWisdom.close();
Wise=true;
}
void SaveWisdom() {
ofWisdom.open(WisdomName);
fftwpp_export_wisdom(PutWisdom,ofWisdom);
ofWisdom.close();
}
virtual void Execute(Complex *in, Complex *out) {
fftw_execute_dft(plan,(fftw_complex *) in,(fftw_complex *) out);
}
void Setout(Complex *in, Complex *&out) {
#ifndef NO_CHECK_ALIGN
CheckAlign(in,"input");
if(out) CheckAlign(out,"output");
else out=in;
#else
if(!out) out=in;
#endif
if(inplace ^ (out == in)) {
std::cerr << "ERROR: fft constructor and call must be either both in place or out of place" << std::endl;
exit(1);
}
}
void fft(Complex *in, Complex *out=NULL) {
Setout(in,out);
Execute(in,out);
}
void fft(double *in, Complex *out) {
fft((Complex *) in,out);
}
void fft(Complex *in, double *out) {
fft(in,(Complex *) out);
}
void fft0(Complex *in, Complex *out=NULL) {
Setout(in,out);
shift=true;
Execute(in,out);
shift=false;
}
void fft0(double *in, Complex *out) {
fft0((Complex *) in,out);
}
void fft0(Complex *in, double *out) {
fft0(in,(Complex *) out);
}
void Normalize(Complex *out) {
for(unsigned int i=0; i < size; i++) out[i] *= norm;
}
virtual void fftNormalized(Complex *in, Complex *out=NULL) {
Setout(in,out);
Execute(in,out);
Normalize(out);
}
void fftNormalized(Complex *in, double *out) {
fftNormalized(in,(Complex *) out);
}
void fftNormalized(double *in, Complex *out) {
fftNormalized((Complex *) in,out);
}
void fft0Normalized(Complex *in, Complex *out=NULL) {
Setout(in,out);
shift=true;
Execute(in,out);
shift=false;
Normalize(out);
}
void fft0Normalized(Complex *in, double *out) {
fft0Normalized(in,(Complex *) out);
}
void fft0Normalized(double *in, Complex *out) {
fft0Normalized((Complex *) in,out);
}
void fftNormalized(Complex *in, Complex *out,
unsigned int nx, unsigned int m,
unsigned int stride, unsigned int dist) {
if(stride == 1 && dist == nx) fftw::fftNormalized(in,out);
else if(stride == nx && dist == 1) fftw::fftNormalized(in,out);
else {
Setout(in,out);
Execute(in,out);
for(unsigned int k=0; k < m; k++) {
for(unsigned int j=0; j < nx; j++) {
out[j*stride+k*dist] *= norm;
}
}
}
}
};
// Compute the complex Fourier transform of n complex values.
// Before calling fft(), the arrays in and out (which may coincide) must be
// allocated as Complex[n].
//
// Out-of-place usage:
//
// fft1d Forward(n,-1,in,out);
// Forward.fft(in,out);
//
// fft1d Backward(n,1,out,in);
// Backward.fft(out,in);
//
// fft1d Backward(n,1,out,in);
// Backward.fftNormalized(out,in); // True inverse of Forward.fft(in,out);
//
// In-place usage:
//
// fft1d Forward(n,-1);
// Forward.fft(in);
//
// fft1d Backward(n,1);
// Backward.fft(in);
//
class fft1d : public fftw {
unsigned int nx;
public:
fft1d(unsigned int nx, int sign, Complex *in=NULL, Complex *out=NULL)
: fftw(nx,sign), nx(nx) {Setup(in,out);}
#ifdef __Array_h__
fft1d(int sign, const array1<Complex>& in, const array1<Complex>& out=NULL1)
: fftw(in.Nx(),sign), nx(in.Nx()) {Setup(in,out);}
#endif
fftw_plan Plan(Complex *in, Complex *out) {
return fftw_plan_dft_1d(nx,(fftw_complex *) in,(fftw_complex *) out,
sign,effort);
}
};
// Compute the complex Fourier transform of m complex vectors, each of
// length n.
// Before calling fft(), the arrays in and out (which may coincide) must be
// allocated as Complex[m*n].
//
// Out-of-place usage:
//
// mfft1d Forward(n,-1,m,stride,dist,in,out);
// Forward.fft(in,out);
//
// In-place usage:
//
// mfft1d Forward(n,-1,m,stride,dist);
// Forward.fft(in);
//
// Notes:
// stride is the spacing between the elements of each Complex vector;
// dist is the spacing between the first elements of the vectors;
//
//
class mfft1d : public fftw {
unsigned int nx;
unsigned int m;
unsigned int stride;
unsigned int dist;
public:
mfft1d(unsigned int nx, int sign, unsigned int m=1, unsigned int stride=1,
unsigned int dist=0, Complex *in=NULL, Complex *out=NULL)
: fftw((nx-1)*stride+(m-1)*Dist(nx,stride,dist)+1,sign,nx),
nx(nx), m(m), stride(stride), dist(Dist(nx,stride,dist))
{Setup(in,out);}
fftw_plan Plan(Complex *in, Complex *out) {
int n[1]={nx};
return fftw_plan_many_dft(1,n,m,
(fftw_complex *) in,NULL,stride,dist,
(fftw_complex *) out,NULL,stride,dist,
sign,effort);
}
void fftNormalized(Complex *in, Complex *out=NULL) {
fftw::fftNormalized(in,out,nx,m,stride,dist);
}
};
// Compute the complex Fourier transform of n real values, using phase sign -1.
// Before calling fft(), the array in must be allocated as double[n] and
// the array out must be allocated as Complex[n/2+1]. The arrays in and out
// may coincide, in which case they must both be allocated as Complex[n/2+1].
//
// Out-of-place usage:
//
// rcfft1d Forward(n,in,out);
// Forward.fft(in,out);
//
// In-place usage:
//
// rcfft1d Forward(n);
// Forward.fft(out);
//
// Notes:
// in contains the n real values stored as a Complex array;
// out contains the first n/2+1 Complex Fourier values.
//
class rcfft1d : public fftw {
unsigned int nx;
public:
rcfft1d(unsigned int nx, Complex *out=NULL)
: fftw(nx/2+1,-1,nx), nx(nx) {Setup(out);}
rcfft1d(unsigned int nx, double *in, Complex *out=NULL)
: fftw(nx/2+1,-1,nx), nx(nx) {Setup(in,out);}
#ifdef __Array_h__
rcfft1d(const array1<Complex>& in)
: fftw(in.Size(),-1,2*(in.Nx()-1)), nx(2*(in.Nx()-1)) {Setup(in);}
rcfft1d(const array1<double>& in, const array1<Complex>& out)
: fftw(out.Size(),-1,in.Size()), nx(in.Nx()) {Setup(in,out);}
#endif
fftw_plan Plan(Complex *in, Complex *out) {
return fftw_plan_dft_r2c_1d(nx,(double *) in,(fftw_complex *) out, effort);
}
void Execute(Complex *in, Complex *out) {
fftw_execute_dft_r2c(plan,(double *) in,(fftw_complex *) out);
}
};
// Compute the real inverse Fourier transform of the n/2+1 Complex values
// corresponding to the non-negative part of the frequency spectrum, using
// phase sign +1.
// Before calling fft(), the array in must be allocated as Complex[n/2+1]
// and the array out must be allocated as double[n]. The arrays in and out
// may coincide, in which case they must both be allocated as Complex[n/2+1].
//
// Out-of-place usage:
//
// crfft1d Backward(n,in,out);
// Backward.fft(in,out);
//
// In-place usage:
//
// crfft1d Backward(n);
// Backward.fft(in);
//
// Notes:
// in contains the first n/2+1 Complex Fourier values.
// out contains the n real values stored as a Complex array;
//
class crfft1d : public fftw {
unsigned int nx;
public:
crfft1d(unsigned int nx, Complex *in=NULL)
: fftw(nx/2+1,1,nx), nx(nx) {Setup(in);}
crfft1d(unsigned int nx, Complex *in, double *out)
: fftw(realsize(nx,in,out),1,nx), nx(nx) {Setup(in,out);}
#ifdef __Array_h__
crfft1d(const array1<Complex>& in)
: fftw(in.Size(),1,2*(in.Nx()-1)), nx(2*(in.Nx()-1)) {Setup(in);}
crfft1d(const array1<Complex>& in, const array1<double>& out)
: fftw(out.Size()/2,1,out.Size()), nx(out.Nx()) {Setup(in,out);}
#endif
fftw_plan Plan(Complex *in, Complex *out) {
return fftw_plan_dft_c2r_1d(nx,(fftw_complex *) in,(double *) out,effort);
}
void Execute(Complex *in, Complex *out) {
fftw_execute_dft_c2r(plan,(fftw_complex *) in,(double *) out);
}
};
// Compute the real Fourier transform of m real vectors, each of length n,
// using phase sign -1. Before calling fft(), the array in must be
// allocated as double[m*n] and the array out must be allocated as
// Complex[m*(n/2+1)]. The arrays in and out may coincide, in which case
// they must both be allocated as Complex[m*(n/2+1)].
//
// Out-of-place usage:
//
// mrcfft1d Forward(n,m,stride,dist,in,out);
// Forward.fft(in,out);
//
// In-place usage:
//
// mrcfft1d Forward(n,m,stride,dist);
// Forward.fft(out);
//
// Notes:
// stride is the spacing between the elements of each Complex vector;
// dist is the spacing between the first elements of the vectors;
// in contains the n real values stored as a Complex array;
// out contains the first n/2+1 Complex Fourier values.
//
class mrcfft1d : public fftw {
unsigned int nx;
unsigned int m;
unsigned int stride;
unsigned int dist;
public:
mrcfft1d(unsigned int nx, unsigned int m=1, unsigned int stride=1,
unsigned int dist=0, Complex *out=NULL)
: fftw(nx/2*stride+(m-1)*Dist(nx,stride,dist)+1,-1,nx), nx(nx), m(m),
stride(stride), dist(Dist(nx,stride,dist)) {Setup(out);}
mrcfft1d(unsigned int nx, unsigned int m=1, unsigned int stride=1,
unsigned int dist=0, double *in=NULL, Complex *out=NULL)
: fftw(nx/2*stride+(m-1)*Dist(nx,stride,dist)+1,-1,nx), nx(nx), m(m),
stride(stride), dist(Dist(nx,stride,dist)) {Setup(in,out);}
fftw_plan Plan(Complex *in, Complex *out) {
const int n[1]={nx};
return fftw_plan_many_dft_r2c(1,n,m,
(double *) in,NULL,stride,2*dist,
(fftw_complex *) out,NULL,stride,dist,
effort);
}
void Execute(Complex *in, Complex *out) {
fftw_execute_dft_r2c(plan,(double *) in,(fftw_complex *) out);
}
void fftNormalized(Complex *in, Complex *out=NULL) {
fftw::fftNormalized(in,out,nx/2+1,m,stride,dist);
}
};
// Compute the real inverse Fourier transform of m complex vectors, each of
// length n/2+1, corresponding to the non-negative parts of the frequency
// spectra, using phase sign +1. Before calling fft(), the array in must be
// allocated as Complex[m*(n/2+1)] and the array out must be allocated as
// double[m*n]. The arrays in and out may coincide, in which case they
// must both be allocated as Complex[m*(n/2+1)].
//
// Out-of-place usage:
//
// mcrfft1d Backward(n,m,stride,dist,in,out);
// Backward.fft(in,out);
//
// In-place usage:
//
// mcrfft1d Backward(n,m,stride,dist);
// Backward.fft(out);
//
// Notes:
// stride is the spacing between the elements of each Complex vector;
// dist is the spacing between the first elements of the vectors;
// in contains the first n/2+1 Complex Fourier values.
// out contains the n real values stored as a Complex array;
//
class mcrfft1d : public fftw {
unsigned int nx;
unsigned int m;
unsigned int stride;
unsigned int dist;
public:
mcrfft1d(unsigned int nx, unsigned int m=1, unsigned int stride=1,
unsigned int dist=0, Complex *in=NULL)
: fftw(nx/2*stride+(m-1)*Dist(nx,stride,dist)+1,1,nx),
nx(nx), m(m), stride(stride), dist(Dist(nx,stride,dist)) {Setup(in);}
mcrfft1d(unsigned int nx, unsigned int m=1, unsigned int stride=1,
unsigned int dist=0, Complex *in=NULL, double *out=NULL)
: fftw((realsize(nx,in,out)-1)*stride+(m-1)*Dist(nx,stride,dist)+1,1,nx),
nx(nx), m(m), stride(stride), dist(Dist(nx,stride,dist)) {Setup(in,out);}
fftw_plan Plan(Complex *in, Complex *out) {
const int n[1]={nx};
return fftw_plan_many_dft_c2r(1,n,m,
(fftw_complex *) in,NULL,stride,dist,
(double *) out,NULL,stride,2*dist,
effort);
}
void Execute(Complex *in, Complex *out) {
fftw_execute_dft_c2r(plan,(fftw_complex *) in,(double *) out);
}
void fftNormalized(Complex *in, Complex *out=NULL) {
fftw::fftNormalized(in,out,(nx/2+1),m,stride,dist);
}
};
// Compute the complex two-dimensional Fourier transform of nx times ny
// complex values. Before calling fft(), the arrays in and out (which may
// coincide) must be allocated as Complex[nx*ny].
//
// Out-of-place usage:
//
// fft2d Forward(nx,ny,-1,in,out);
// Forward.fft(in,out);
//
// fft2d Backward(nx,ny,1,out,in);
// Backward.fft(out,in);
//
// fft2d Backward(nx,ny,1,out,in);
// Backward.fftNormalized(out,in); // True inverse of Forward.fft(in,out);
//
// In-place usage:
//
// fft2d Forward(nx,ny,-1);
// Forward.fft(in);
//
// fft2d Backward(nx,ny,1);
// Backward.fft(in);
//
// Note:
// in[ny*i+j] contains the ny Complex values for each i=0,...,nx-1.
//
class fft2d : public fftw {
unsigned int nx;
unsigned int ny;
public:
fft2d(unsigned int nx, unsigned int ny, int sign, Complex *in=NULL,
Complex *out=NULL)
: fftw(nx*ny,sign), nx(nx), ny(ny) {Setup(in,out);}
#ifdef __Array_h__
fft2d(int sign, const array2<Complex>& in, const array2<Complex>& out=NULL2)
: fftw(in.Size(),sign), nx(in.Nx()), ny(in.Ny()) {Setup(in,out);}
#endif
fftw_plan Plan(Complex *in, Complex *out) {
return fftw_plan_dft_2d(nx,ny,(fftw_complex *) in,(fftw_complex *) out,
sign,effort);
}
};
// Compute the complex two-dimensional Fourier transform of nx times ny real
// values, using phase sign -1.
// Before calling fft(), the array in must be allocated as double[nx*ny] and
// the array out must be allocated as Complex[nx*(ny/2+1)]. The arrays in
// and out may coincide, in which case they must both be allocated as
// Complex[nx*(ny/2+1)].
//
// Out-of-place usage:
//
// rcfft2d Forward(nx,ny,in,out);
// Forward.fft(in,out); // Origin of Fourier domain at (0,0)
// Forward.fft0(in,out); // Origin of Fourier domain at (nx/2,0)
//
//
// In-place usage:
//
// rcfft2d Forward(nx,ny);
// Forward.fft(in); // Origin of Fourier domain at (0,0)
// Forward.fft0(in); // Origin of Fourier domain at (nx/2,0)
//
// Notes:
// in contains the nx*ny real values stored as a Complex array;
// out contains the upper-half portion (ky >= 0) of the Complex transform.
//
class rcfft2d : public fftw {
unsigned int nx;
unsigned int ny;
public:
rcfft2d(unsigned int nx, unsigned int ny, Complex *out=NULL) :
fftw(nx*(ny/2+1),-1,nx*ny), nx(nx), ny(ny) {Setup(out);}
rcfft2d(unsigned int nx, unsigned int ny, double *in, Complex *out=NULL) :
fftw(nx*(ny/2+1),-1,nx*ny), nx(nx), ny(ny) {Setup(in,out);}
#ifdef __Array_h__
rcfft2d(const array2<Complex>& in) :
fftw(in.Size(),-1,in.Nx()*2*(in.Ny()-1)), nx(in.Nx()), ny(2*(in.Ny()-1))
{Setup(in);}
rcfft2d(const array2<double>& in, const array2<Complex>& out) :
fftw(out.Size(),-1,in.Size()), nx(in.Nx()), ny(in.Ny()) {Setup(in,out);}
#endif
fftw_plan Plan(Complex *in, Complex *out) {
return fftw_plan_dft_r2c_2d(nx,ny,(double *) in,(fftw_complex *) out,
effort);
}
void Execute(Complex *in, Complex *out) {
if(shift) Shift(in,nx,ny);
fftw_execute_dft_r2c(plan,(double *) in,(fftw_complex *) out);
}
};
// Compute the real two-dimensional inverse Fourier transform of the
// nx*(ny/2+1) Complex values corresponding to the spectral values in the
// half-plane ky >= 0, using phase sign +1.
// Before calling fft(), the array in must be allocated as
// Complex[nx*(ny+1)/2] and the array out must be allocated as
// double[nx*ny]. The arrays in and out may coincide, in which case they
// must both be allocated as Complex[nx*(ny/2+1)].
//
// Out-of-place usage:
//
// crfft2d Backward(nx,ny,in,out);
// Backward.fft(in,out); // Origin of Fourier domain at (0,0)
// Backward.fft0(in,out); // Origin of Fourier domain at (nx/2,0)
//
//
// In-place usage:
//
// crfft2d Backward(nx,ny);
// Backward.fft(in); // Origin of Fourier domain at (0,0)
// Backward.fft0(in); // Origin of Fourier domain at (nx/2,0)
//
// Notes:
// in contains the upper-half portion (ky >= 0) of the Complex transform;
// out contains the nx*ny real values stored as a Complex array.
//
class crfft2d : public fftw {
unsigned int nx;
unsigned int ny;
public:
crfft2d(unsigned int nx, unsigned int ny, Complex *in=NULL)
: fftw(nx*(ny/2+1),1,nx*ny), nx(nx), ny(ny) {Setup(in);}
crfft2d(unsigned int nx, unsigned int ny, Complex *in, double *out)
: fftw(nx*(realsize(ny,in,out)),1,nx*ny), nx(nx), ny(ny) {Setup(in,out);}
#ifdef __Array_h__
crfft2d(const array2<Complex>& in) :
fftw(in.Size(),1,in.Nx()*2*(in.Ny()-1)), nx(in.Nx()), ny(2*(in.Ny()-1))
{Setup(in);}
crfft2d(const array2<Complex>& in, const array2<double>& out) :
fftw(out.Size()/2,1,out.Size()), nx(out.Nx()), ny(out.Ny())
{Setup(in,out);}
#endif
fftw_plan Plan(Complex *in, Complex *out) {
return fftw_plan_dft_c2r_2d(nx,ny,(fftw_complex *) in,(double *) out,
effort);
}
void Execute(Complex *in, Complex *out) {
fftw_execute_dft_c2r(plan,(fftw_complex *) in,(double *) out);
if(shift) Shift(out,nx,ny);
}
};
// Compute the complex three-dimensional Fourier transform of
// nx times ny times nz complex values. Before calling fft(), the arrays in
// and out (which may coincide) must be allocated as Complex[nx*ny*nz].
//
// Out-of-place usage:
//
// fft3d Forward(nx,ny,nz,-1,in,out);
// Forward.fft(in,out);
//
// fft3d Backward(nx,ny,nz,1,out,in);
// Backward.fft(out,in);
//
// fft3d Backward(nx,ny,nz,1,out,in);
// Backward.fftNormalized(out,in); // True inverse of Forward.fft(in,out);
//
// In-place usage:
//
// fft3d Forward(nx,ny,nz,-1);
// Forward.fft(in);
//
// fft3d Backward(nx,ny,nz,1);
// Backward.fft(in);
//
// Note:
// in[nz*(ny*i+j)+k] contains the (i,j,k)th Complex value,
// indexed by i=0,...,nx-1, j=0,...,ny-1, and k=0,...,nz-1.
//
class fft3d : public fftw {
unsigned int nx;
unsigned int ny;
unsigned int nz;
public:
fft3d(unsigned int nx, unsigned int ny, unsigned int nz,
int sign, Complex *in=NULL, Complex *out=NULL)
: fftw(nx*ny*nz,sign), nx(nx), ny(ny), nz(nz) {Setup(in,out);}
#ifdef __Array_h__
fft3d(int sign, const array3<Complex>& in, const array3<Complex>& out=NULL3)
: fftw(in.Size(),sign), nx(in.Nx()), ny(in.Ny()), nz(in.Nz())
{Setup(in,out);}
#endif
fftw_plan Plan(Complex *in, Complex *out) {
return fftw_plan_dft_3d(nx,ny,nz,(fftw_complex *) in,
(fftw_complex *) out, sign, effort);
}
};
// Compute the complex two-dimensional Fourier transform of
// nx times ny times nz real values, using phase sign -1.
// Before calling fft(), the array in must be allocated as double[nx*ny*nz]
// and the array out must be allocated as Complex[nx*ny*(nz/2+1)]. The
// arrays in and out may coincide, in which case they must both be allocated as
// Complex[nx*ny*(nz/2+1)].
//
// Out-of-place usage:
//
// rcfft3d Forward(nx,ny,nz,in,out);
// Forward.fft(in,out); // Origin of Fourier domain at (0,0)
// Forward.fft0(in,out); // Origin of Fourier domain at (nx/2,ny/2,0)
//
//
// In-place usage:
//
// rcfft3d Forward(nx,ny,nz);
// Forward.fft(in); // Origin of Fourier domain at (0,0)
// Forward.fft0(in); // Origin of Fourier domain at (nx/2,ny/2,0)
//
// Notes:
// in contains the nx*ny*nz real values stored as a Complex array;
// out contains the upper-half portion (kz >= 0) of the Complex transform.
//
class rcfft3d : public fftw {
unsigned int nx;
unsigned int ny;
unsigned int nz;
public:
rcfft3d(unsigned int nx, unsigned int ny, unsigned int nz, Complex *out=NULL)
: fftw(nx*ny*(nz/2+1),-1,nx*ny*nz), nx(nx), ny(ny), nz(nz) {Setup(out);}
rcfft3d(unsigned int nx, unsigned int ny, unsigned int nz, double *in,
Complex *out=NULL) : fftw(nx*ny*(nz/2+1),-1,nx*ny*nz),
nx(nx), ny(ny), nz(nz) {Setup(in,out);}
#ifdef __Array_h__
rcfft3d(const array3<Complex>& in) :
fftw(in.Size(),-1,in.Nx()*in.Ny()*2*(in.Nz()-1)),
nx(in.Nx()), ny(in.Ny()), nz(2*(in.Nz()-1)) {Setup(in);}
rcfft3d(const array3<double>& in, const array3<Complex>& out) :
fftw(out.Size(),-1,in.Size()),
nx(in.Nx()), ny(in.Ny()), nz(in.Nz()) {Setup(in,out);}
#endif
fftw_plan Plan(Complex *in, Complex *out) {
return fftw_plan_dft_r2c_3d(nx,ny,nz,(double *) in,(fftw_complex *) out,
effort);
}
void Execute(Complex *in, Complex *out) {
if(shift) Shift(in,nx,ny,nz);
fftw_execute_dft_r2c(plan,(double *) in,(fftw_complex *) out);
}
};
// Compute the real two-dimensional inverse Fourier transform of the
// nx*ny*(nz/2+1) Complex values corresponding to the spectral values in the
// half-plane kz >= 0, using phase sign +1.
// Before calling fft(), the array in must be allocated as
// Complex[nx*ny*(nz+1)/2] and the array out must be allocated as
// double[nx*ny*nz]. The arrays in and out may coincide, in which case they
// must both be allocated as Complex[nx*ny*(nz/2+1)].
//
// Out-of-place usage:
//
// crfft3d Backward(nx,ny,nz,in,out);
// Backward.fft(in,out); // Origin of Fourier domain at (0,0)
// Backward.fft0(in,out); // Origin of Fourier domain at (nx/2,ny/2,0)
//
// In-place usage:
//
// crfft3d Backward(nx,ny,nz);
// Backward.fft(in); // Origin of Fourier domain at (0,0)
// Backward.fft0(in); // Origin of Fourier domain at (nx/2,ny/2,0)
//
// Notes:
// in contains the upper-half portion (kz >= 0) of the Complex transform;
// out contains the nx*ny*nz real values stored as a Complex array.
//
class crfft3d : public fftw {
unsigned int nx;
unsigned int ny;
unsigned int nz;
public:
crfft3d(unsigned int nx, unsigned int ny, unsigned int nz, Complex *in=NULL)
: fftw(nx*ny*(nz/2+1),1,nx*ny*nz), nx(nx), ny(ny), nz(nz)
{Setup(in);}
crfft3d(unsigned int nx, unsigned int ny, unsigned int nz, Complex *in,
double *out) : fftw(nx*ny*(realsize(nz,in,out)),1,nx*ny*nz),
nx(nx), ny(ny), nz(nz) {Setup(in,out);}
#ifdef __Array_h__
crfft3d(const array3<Complex>& in) :
fftw(in.Size(),1,in.Nx()*in.Ny()*2*(in.Nz()-1)),
nx(in.Nx()), ny(in.Ny()), nz(2*(in.Nz()-1)) {Setup(in);}
crfft3d(const array3<Complex>& in, const array3<double>& out) :
fftw(out.Size()/2,1,out.Size()),
nx(out.Nx()), ny(out.Ny()), nz(out.Nz()) {Setup(in,out);}
#endif
fftw_plan Plan(Complex *in, Complex *out) {
return fftw_plan_dft_c2r_3d(nx,ny,nz,(fftw_complex *) in,(double *) out,
effort);
}
void Execute(Complex *in, Complex *out) {
fftw_execute_dft_c2r(plan,(fftw_complex *) in,(double *) out);
if(shift) Shift(out,nx,ny,nz);
}
};
#endif
|