| [2615] | 1 | #include "sopnamsp.h" | 
|---|
| [768] | 2 | #include "machdefs.h" | 
|---|
|  | 3 |  | 
|---|
| [460] | 4 | #include <math.h> | 
|---|
| [2322] | 5 | #include <iostream> | 
|---|
| [460] | 6 |  | 
|---|
| [768] | 7 | #include "nbrandom.h" | 
|---|
| [1519] | 8 | #include "matharr.h" | 
|---|
| [705] | 9 | #include "fftpserver.h" | 
|---|
| [715] | 10 | #include "fftmserver.h" | 
|---|
| [768] | 11 | #include "fftwserver.h" | 
|---|
|  | 12 | #include "ntoolsinit.h" | 
|---|
| [460] | 13 |  | 
|---|
| [705] | 14 | #include "timing.h" | 
|---|
| [460] | 15 |  | 
|---|
| [705] | 16 |  | 
|---|
| [768] | 17 | static bool inp_typ_random = false ;  // true -> random input | 
|---|
|  | 18 |  | 
|---|
| [705] | 19 | template <class T> | 
|---|
|  | 20 | inline T module(complex<T> c) | 
|---|
| [460] | 21 | { | 
|---|
| [705] | 22 | return (sqrt(c.real()*c.real()+c.imag()*c.imag())); | 
|---|
| [460] | 23 | } | 
|---|
|  | 24 |  | 
|---|
| [715] | 25 | // Max Matrix print elts | 
|---|
|  | 26 | static int nprt = 2; | 
|---|
|  | 27 | static int nprtfc = 8; | 
|---|
|  | 28 |  | 
|---|
| [705] | 29 | template <class T> | 
|---|
| [715] | 30 | void TestFFTPack(T seuil, int num) | 
|---|
| [460] | 31 | { | 
|---|
| [705] | 32 | int i; | 
|---|
|  | 33 | T fact = 1./num; | 
|---|
| [460] | 34 |  | 
|---|
| [705] | 35 | TVector< complex<T> > inc(num), bkc(num), difc(num); | 
|---|
|  | 36 | TVector< T > in(num), ino(num), bk(num),dif(num); | 
|---|
|  | 37 | TVector< complex<T> > outc(num); | 
|---|
| [715] | 38 |  | 
|---|
|  | 39 | cout << " DBG/1 outc " << outc.NElts()  << endl; | 
|---|
|  | 40 | outc.ReSize(32); | 
|---|
|  | 41 | cout << " DBG/2 outc " << outc.NElts()  << endl; | 
|---|
|  | 42 | outc.ReSize(10); | 
|---|
|  | 43 | cout << " DBG/3 outc " << outc.NElts()  << endl; | 
|---|
|  | 44 | outc.ReSize(48); | 
|---|
|  | 45 | cout << " DBG/4 outc " << outc.NElts()  << endl; | 
|---|
|  | 46 |  | 
|---|
| [768] | 47 | if (inp_typ_random) | 
|---|
| [1408] | 48 | for (i=0; i<num ; i++){ | 
|---|
| [768] | 49 | ino[i] = in[i] = GauRnd(0., 1.); | 
|---|
|  | 50 | inc[i] = complex<T> (in[i], 0.); | 
|---|
|  | 51 | } | 
|---|
| [1408] | 52 | else for (i=0; i<num ; i++){ | 
|---|
| [715] | 53 | ino[i] = in[i] = 0.5 + cos(2*M_PI*(double)i/(double)num) | 
|---|
|  | 54 | + 2*sin(4*M_PI*(double)i/(double)num); | 
|---|
| [705] | 55 | inc[i] = complex<T> (in[i], 0.); | 
|---|
|  | 56 | } | 
|---|
|  | 57 |  | 
|---|
| [460] | 58 |  | 
|---|
| [836] | 59 | cout << "Input / L = " << num << in; | 
|---|
| [460] | 60 | cout << endl; | 
|---|
|  | 61 |  | 
|---|
| [705] | 62 | cout << " >>>> Testing FFTPackServer "  << endl; | 
|---|
|  | 63 | FFTPackServer fftp; | 
|---|
|  | 64 | cout << " Testing FFTPackServer "  << endl; | 
|---|
|  | 65 | fftp.fftf(in.NElts(), in.Data()); | 
|---|
| [715] | 66 | //  in /= (num/2.); | 
|---|
| [836] | 67 | cout << " fftp.fftf(in.NElts(), in.Data()) FORWARD: " << in << endl; | 
|---|
| [705] | 68 | cout << endl; | 
|---|
|  | 69 | fftp.fftb(in.NElts(), in.Data()); | 
|---|
| [836] | 70 | cout << " fftp.fftb(in.NElts(), in.Data()) BACKWARD: " << in <<endl; | 
|---|
| [705] | 71 | cout << endl; | 
|---|
| [715] | 72 | dif = ino-in; | 
|---|
| [836] | 73 | cout << " dif , NElts= " << dif.NElts() << dif << endl; | 
|---|
| [460] | 74 |  | 
|---|
| [715] | 75 | int ndiff = 0; | 
|---|
|  | 76 | T maxdif=0., vdif; | 
|---|
|  | 77 | for(i=0; i<num; i++) { | 
|---|
|  | 78 | vdif = fabs(dif(i)); | 
|---|
|  | 79 | if (vdif > seuil)  ndiff++; | 
|---|
|  | 80 | if (vdif > maxdif) maxdif = vdif; | 
|---|
|  | 81 | } | 
|---|
|  | 82 | cout << " Difference, Seuil= " << seuil << " NDiff= " << ndiff | 
|---|
|  | 83 | << " MaxDiff= " << maxdif << endl; | 
|---|
| [705] | 84 |  | 
|---|
| [715] | 85 | } | 
|---|
|  | 86 |  | 
|---|
|  | 87 | template <class T> | 
|---|
|  | 88 | void TestFFTS(T seuil, FFTServerInterface & ffts, int num) | 
|---|
|  | 89 | { | 
|---|
|  | 90 |  | 
|---|
|  | 91 | cout <<" ===>  TestFFTS " << ffts.getInfo() << " ArrSz= " << num << endl; | 
|---|
|  | 92 | int i; | 
|---|
|  | 93 |  | 
|---|
|  | 94 | T fact = 1.; | 
|---|
|  | 95 |  | 
|---|
|  | 96 | TVector< complex<T> > inc(num), bkc(num), difc(num); | 
|---|
|  | 97 | TVector< T > in(num), ino(num), bk(num),dif(num); | 
|---|
|  | 98 | TVector< complex<T> > outc(num); | 
|---|
|  | 99 |  | 
|---|
| [1408] | 100 | for (i=0; i<num ; i++){ | 
|---|
| [715] | 101 | ino[i] = in[i] = 0.5 + cos(2*M_PI*(double)i/(double)num) | 
|---|
|  | 102 | + 2*sin(4*M_PI*(double)i/(double)num); | 
|---|
|  | 103 | inc[i] = complex<T> (in[i], 0.); | 
|---|
|  | 104 | } | 
|---|
|  | 105 |  | 
|---|
|  | 106 |  | 
|---|
|  | 107 | cout << " Testing FFTServer " << ffts.getInfo() << endl; | 
|---|
|  | 108 |  | 
|---|
| [836] | 109 | cout << "Input / L = " << num << in << endl; | 
|---|
| [715] | 110 | cout << endl; | 
|---|
|  | 111 |  | 
|---|
|  | 112 | int ndiff = 0; | 
|---|
|  | 113 |  | 
|---|
| [705] | 114 | cout << "\n ----  Testing FFT(T, complex<T>) ---- " << endl; | 
|---|
|  | 115 | ffts.FFTForward(in, outc); | 
|---|
| [836] | 116 | cout << " FourierCoefs , NElts= " << outc.NElts() << outc << endl; | 
|---|
| [705] | 117 |  | 
|---|
|  | 118 | ffts.FFTBackward(outc, bk); | 
|---|
| [836] | 119 | cout << " Backward , NElts= " << bk.NElts() << bk << endl; | 
|---|
| [705] | 120 |  | 
|---|
|  | 121 | dif = bk*fact - in; | 
|---|
| [836] | 122 | cout << " Difference , NElts= " << dif.NElts() << dif << endl; | 
|---|
| [705] | 123 |  | 
|---|
| [715] | 124 | ndiff = 0; | 
|---|
|  | 125 | T maxdif=0., vdif; | 
|---|
|  | 126 | for(i=0; i<num; i++) { | 
|---|
|  | 127 | vdif = fabs(dif(i)); | 
|---|
|  | 128 | if (vdif > seuil)  ndiff++; | 
|---|
|  | 129 | if (vdif > maxdif) maxdif = vdif; | 
|---|
|  | 130 | } | 
|---|
|  | 131 | cout << " Difference, Seuil= " << seuil << " NDiff= " << ndiff | 
|---|
|  | 132 | << " MaxDiff= " << maxdif << endl; | 
|---|
| [705] | 133 |  | 
|---|
|  | 134 | cout << "\n ----  Testing FFT(complex<T>, complex<T>) ---- " << endl; | 
|---|
|  | 135 | ffts.FFTForward(inc, outc); | 
|---|
| [836] | 136 | cout << " FourierCoef , NElts= " << outc.NElts() << outc << endl; | 
|---|
| [705] | 137 |  | 
|---|
|  | 138 | ffts.FFTBackward(outc, bkc); | 
|---|
| [836] | 139 | cout << " Backward , NElts= " << bkc.NElts() << bkc << endl; | 
|---|
| [705] | 140 |  | 
|---|
|  | 141 | difc = bkc*complex<T>(fact,0.) - inc; | 
|---|
| [836] | 142 | cout << " Difference , NElts= " << difc.NElts() << difc << endl; | 
|---|
| [705] | 143 |  | 
|---|
| [715] | 144 | ndiff = 0; | 
|---|
|  | 145 | maxdif=0., vdif; | 
|---|
|  | 146 | for(i=0; i<num; i++) { | 
|---|
|  | 147 | vdif = fabs(module(difc(i))); | 
|---|
|  | 148 | if (vdif > seuil)  ndiff++; | 
|---|
|  | 149 | if (vdif > maxdif) maxdif = vdif; | 
|---|
|  | 150 | } | 
|---|
|  | 151 | cout << " Difference, Seuil= " << seuil << " NDiff= " << ndiff | 
|---|
|  | 152 | << " MaxDiff= " << maxdif << endl; | 
|---|
| [460] | 153 | } | 
|---|
| [705] | 154 |  | 
|---|
|  | 155 |  | 
|---|
| [715] | 156 |  | 
|---|
|  | 157 |  | 
|---|
|  | 158 |  | 
|---|
|  | 159 | int main(int narg, char* arg[]) | 
|---|
| [705] | 160 | { | 
|---|
|  | 161 |  | 
|---|
| [768] | 162 | SophyaInit(); | 
|---|
| [705] | 163 | InitTim();   // Initializing the CPU timer | 
|---|
|  | 164 |  | 
|---|
| [715] | 165 | if (narg < 4) { | 
|---|
| [836] | 166 | cout << "tfft/ args error - \n  Usage tfft size px/Px/Mx/Wx f/d [NPrt=50 PrtLev=0] \n" | 
|---|
| [768] | 167 | << " p=FFTPackTest  P=FFTPack, M=FFTMayer, W= FFTWServer" | 
|---|
|  | 168 | << " x=0 -> Random input - f=float, d=double " << endl; | 
|---|
| [715] | 169 | exit(0); | 
|---|
|  | 170 | } | 
|---|
| [705] | 171 |  | 
|---|
| [715] | 172 | FFTPackServer fftp; | 
|---|
|  | 173 | FFTMayerServer fftm; | 
|---|
|  | 174 |  | 
|---|
| [768] | 175 | FFTWServer fftw; | 
|---|
|  | 176 |  | 
|---|
|  | 177 | inp_typ_random = false; | 
|---|
|  | 178 | if (arg[2][1] == '0')  inp_typ_random = true; | 
|---|
|  | 179 |  | 
|---|
| [715] | 180 | int sz = atoi(arg[1]); | 
|---|
| [836] | 181 | int nprt = 50; | 
|---|
|  | 182 | int prtlev = 0; | 
|---|
|  | 183 | if (narg > 4) nprt = atoi(arg[4]); | 
|---|
|  | 184 | if (narg > 5) prtlev = atoi(arg[5]); | 
|---|
|  | 185 | BaseArray::SetMaxPrint(nprt, prtlev); | 
|---|
| [715] | 186 |  | 
|---|
|  | 187 | if (sz < 2) sz = 2; | 
|---|
|  | 188 | FFTServerInterface * ffts; | 
|---|
|  | 189 | if (*arg[2] == 'M')  ffts = fftm.Clone(); | 
|---|
| [768] | 190 | else if (*arg[2] == 'W')  ffts = fftw.Clone(); | 
|---|
| [715] | 191 | else ffts = fftp.Clone(); | 
|---|
|  | 192 |  | 
|---|
|  | 193 | float fs = 1.e-4; | 
|---|
|  | 194 | double ds = 1.e-6; | 
|---|
|  | 195 |  | 
|---|
| [705] | 196 | try { | 
|---|
| [715] | 197 | if (*arg[3] == 'd') { | 
|---|
|  | 198 | cout << "\n ========================================== \n" | 
|---|
|  | 199 | << "    ------ Testing FFTServer for double ----- \n" | 
|---|
|  | 200 | << " ============================================ " << endl; | 
|---|
|  | 201 | if (*arg[2] == 'p') TestFFTPack(ds, sz); | 
|---|
|  | 202 | else TestFFTS(ds, *ffts, sz); | 
|---|
|  | 203 | } | 
|---|
|  | 204 | else { | 
|---|
|  | 205 | cout << "\n ========================================= \n" | 
|---|
|  | 206 | << "    ------ Testing FFTServer for float ----- \n" | 
|---|
|  | 207 | << " =========================================== " << endl; | 
|---|
|  | 208 | if (*arg[2] == 'p') TestFFTPack(fs, sz); | 
|---|
|  | 209 | else TestFFTS(fs, *ffts, sz); | 
|---|
|  | 210 | } | 
|---|
| [705] | 211 | } | 
|---|
|  | 212 | catch(PThrowable exc ) { | 
|---|
| [715] | 213 | cerr << "TestFFT-main() , Catched exception: \n" << exc.Msg() << endl; | 
|---|
| [705] | 214 | } | 
|---|
|  | 215 | catch(std::exception ex) { | 
|---|
|  | 216 | cerr << "TestFFT-main() , Catched exception ! " << (string)(ex.what()) << endl; | 
|---|
|  | 217 | } | 
|---|
| [715] | 218 |  | 
|---|
| [705] | 219 | /* | 
|---|
|  | 220 | catch(...) { | 
|---|
|  | 221 | cerr << "TestFFT-main() , Catched ... exception ! " << endl; | 
|---|
|  | 222 | } | 
|---|
|  | 223 | */ | 
|---|
|  | 224 | PrtTim("End of tfft "); | 
|---|
| [715] | 225 | delete ffts; | 
|---|
| [705] | 226 | } | 
|---|