1 | #include <math.h>
|
---|
2 | #include <iostream.h>
|
---|
3 |
|
---|
4 | #include "fftpserver.h"
|
---|
5 | #include "sambainit.h"
|
---|
6 |
|
---|
7 | #include "timing.h"
|
---|
8 |
|
---|
9 |
|
---|
10 | template <class T>
|
---|
11 | inline T module(complex<T> c)
|
---|
12 | {
|
---|
13 | return (sqrt(c.real()*c.real()+c.imag()*c.imag()));
|
---|
14 | }
|
---|
15 |
|
---|
16 | template <class T>
|
---|
17 | void TestFFTS(T seuil, FFTServerInterface & ffts)
|
---|
18 | {
|
---|
19 |
|
---|
20 | int num=16;
|
---|
21 | int i;
|
---|
22 |
|
---|
23 | T fact = 1./num;
|
---|
24 |
|
---|
25 | TVector< complex<T> > inc(num), bkc(num), difc(num);
|
---|
26 | TVector< T > in(num), ino(num), bk(num),dif(num);
|
---|
27 | TVector< complex<T> > outc(num);
|
---|
28 |
|
---|
29 |
|
---|
30 | for (int i=0; i<num ; i++){
|
---|
31 | ino[i] = in[i] = cos(2*M_PI*i/8.) + sin(2*M_PI*i/4.);
|
---|
32 | inc[i] = complex<T> (in[i], 0.);
|
---|
33 | }
|
---|
34 |
|
---|
35 |
|
---|
36 | cout << " Testing FFTServer " << ffts.getInfo() << endl;
|
---|
37 |
|
---|
38 | cout << "Input / L = " << num << endl;
|
---|
39 | in.Print(0,0,num,0,num);
|
---|
40 | cout << endl;
|
---|
41 |
|
---|
42 | cout << " >>>> Testing FFTPackServer " << endl;
|
---|
43 | FFTPackServer fftp;
|
---|
44 | cout << " Testing FFTPackServer " << endl;
|
---|
45 | fftp.fftf(in.NElts(), in.Data());
|
---|
46 | in /= (num/2.);
|
---|
47 | cout << " fftp.fftf(in.NElts(), in.Data()) FORWARD: " << endl;
|
---|
48 | in.Print(0,0,num,0,num);
|
---|
49 | cout << endl;
|
---|
50 | fftp.fftb(in.NElts(), in.Data());
|
---|
51 | cout << " fftp.fftb(in.NElts(), in.Data()) BACKWARD: " << endl;
|
---|
52 | in.Print(0,0,num,0,num);
|
---|
53 | cout << endl;
|
---|
54 |
|
---|
55 | in = ino;
|
---|
56 |
|
---|
57 | cout << "\n ---- Testing FFT(T, complex<T>) ---- " << endl;
|
---|
58 |
|
---|
59 | ffts.FFTForward(in, outc);
|
---|
60 | cout << " outc , NElts= " << outc.NElts() << endl;
|
---|
61 | outc.Print(0,0,num,0,num);
|
---|
62 |
|
---|
63 | ffts.FFTBackward(outc, bk);
|
---|
64 | cout << " bk , NElts= " << bk.NElts() << endl;
|
---|
65 | bk.Print(0,0,num,0,num);
|
---|
66 |
|
---|
67 | dif = bk*fact - in;
|
---|
68 | cout << " dif , NElts= " << dif.NElts() << endl;
|
---|
69 | dif.Print(0,0,num,0,num);
|
---|
70 |
|
---|
71 | int ndiff = 0;
|
---|
72 | for(i=0; i<num; i++)
|
---|
73 | if (fabs(dif(i)) > seuil) ndiff++;
|
---|
74 |
|
---|
75 | cout << " Difference, Seuil= " << seuil << " NDiff= " << ndiff << endl;
|
---|
76 |
|
---|
77 | cout << "\n ---- Testing FFT(complex<T>, complex<T>) ---- " << endl;
|
---|
78 | ffts.FFTForward(inc, outc);
|
---|
79 | cout << " outc , NElts= " << outc.NElts() << endl;
|
---|
80 | outc.Print(0,0,num,0,num);
|
---|
81 |
|
---|
82 | ffts.FFTBackward(outc, bkc);
|
---|
83 | cout << " bkc , NElts= " << bkc.NElts() << endl;
|
---|
84 | bkc.Print(0,0,num,0,num);
|
---|
85 |
|
---|
86 | difc = bkc*complex<T>(fact,0.) - inc;
|
---|
87 | cout << " difc , NElts= " << difc.NElts() << endl;
|
---|
88 | difc.Print(0,0,num,0,num);
|
---|
89 |
|
---|
90 | int ndiffc = 0;
|
---|
91 | for(i=0; i<num; i++)
|
---|
92 | if (fabs(module(difc(i))) > seuil) ndiffc++;
|
---|
93 | cout << " Difference, Seuil= " << seuil << " NDiffC= " << ndiffc << endl;
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | int main(int narg, char* arg)
|
---|
98 | {
|
---|
99 |
|
---|
100 | PeidaInit();
|
---|
101 | InitTim(); // Initializing the CPU timer
|
---|
102 | FFTPackServer ffts;
|
---|
103 |
|
---|
104 |
|
---|
105 | try {
|
---|
106 | float fs = 1.e-6;
|
---|
107 | double ds = 1.e-12;
|
---|
108 | cout << "\n ======================================== \n"
|
---|
109 | << " ------ Testing FFTS for float ----- \n"
|
---|
110 | << " ======================================== " << endl;
|
---|
111 | TestFFTS(fs, ffts);
|
---|
112 | cout << "\n ======================================== \n"
|
---|
113 | << " ------ Testing FFTS for double ----- \n"
|
---|
114 | << " ======================================== " << endl;
|
---|
115 | TestFFTS(ds, ffts);
|
---|
116 | }
|
---|
117 | catch(PThrowable exc ) {
|
---|
118 | cerr << "TestFFT-main() , Catched exception: " << exc.Msg() << endl;
|
---|
119 | }
|
---|
120 | catch(std::exception ex) {
|
---|
121 | cerr << "TestFFT-main() , Catched exception ! " << (string)(ex.what()) << endl;
|
---|
122 | }
|
---|
123 | /*
|
---|
124 | catch(...) {
|
---|
125 | cerr << "TestFFT-main() , Catched ... exception ! " << endl;
|
---|
126 | }
|
---|
127 | */
|
---|
128 | PrtTim("End of tfft ");
|
---|
129 | }
|
---|