| 1 | #include "sopnamsp.h"
|
|---|
| 2 | #include "machdefs.h"
|
|---|
| 3 |
|
|---|
| 4 | #include <math.h>
|
|---|
| 5 | #include <iostream>
|
|---|
| 6 |
|
|---|
| 7 | #include "nbrandom.h"
|
|---|
| 8 | #include "matharr.h"
|
|---|
| 9 | #include "fftpserver.h"
|
|---|
| 10 | #include "fftmserver.h"
|
|---|
| 11 | #include "fftwserver.h"
|
|---|
| 12 | #include "ntoolsinit.h"
|
|---|
| 13 |
|
|---|
| 14 | #include "timing.h"
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 | static bool inp_typ_random = false ; // true -> random input
|
|---|
| 18 |
|
|---|
| 19 | template <class T>
|
|---|
| 20 | inline T module(complex<T> c)
|
|---|
| 21 | {
|
|---|
| 22 | return (sqrt(c.real()*c.real()+c.imag()*c.imag()));
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | // Max Matrix print elts
|
|---|
| 26 | static int nprt = 2;
|
|---|
| 27 | static int nprtfc = 8;
|
|---|
| 28 |
|
|---|
| 29 | template <class T>
|
|---|
| 30 | void TestFFTPack(T seuil, int num)
|
|---|
| 31 | {
|
|---|
| 32 | int i;
|
|---|
| 33 | T fact = 1./num;
|
|---|
| 34 |
|
|---|
| 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);
|
|---|
| 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 |
|
|---|
| 47 | if (inp_typ_random)
|
|---|
| 48 | for (i=0; i<num ; i++){
|
|---|
| 49 | ino[i] = in[i] = GauRnd(0., 1.);
|
|---|
| 50 | inc[i] = complex<T> (in[i], 0.);
|
|---|
| 51 | }
|
|---|
| 52 | else for (i=0; i<num ; i++){
|
|---|
| 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);
|
|---|
| 55 | inc[i] = complex<T> (in[i], 0.);
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 | cout << "Input / L = " << num << in;
|
|---|
| 60 | cout << endl;
|
|---|
| 61 |
|
|---|
| 62 | cout << " >>>> Testing FFTPackServer " << endl;
|
|---|
| 63 | FFTPackServer fftp;
|
|---|
| 64 | cout << " Testing FFTPackServer " << endl;
|
|---|
| 65 | fftp.fftf(in.NElts(), in.Data());
|
|---|
| 66 | // in /= (num/2.);
|
|---|
| 67 | cout << " fftp.fftf(in.NElts(), in.Data()) FORWARD: " << in << endl;
|
|---|
| 68 | cout << endl;
|
|---|
| 69 | fftp.fftb(in.NElts(), in.Data());
|
|---|
| 70 | cout << " fftp.fftb(in.NElts(), in.Data()) BACKWARD: " << in <<endl;
|
|---|
| 71 | cout << endl;
|
|---|
| 72 | dif = ino-in;
|
|---|
| 73 | cout << " dif , NElts= " << dif.NElts() << dif << endl;
|
|---|
| 74 |
|
|---|
| 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;
|
|---|
| 84 |
|
|---|
| 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 |
|
|---|
| 100 | for (i=0; i<num ; i++){
|
|---|
| 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 |
|
|---|
| 109 | cout << "Input / L = " << num << in << endl;
|
|---|
| 110 | cout << endl;
|
|---|
| 111 |
|
|---|
| 112 | int ndiff = 0;
|
|---|
| 113 |
|
|---|
| 114 | cout << "\n ---- Testing FFT(T, complex<T>) ---- " << endl;
|
|---|
| 115 | ffts.FFTForward(in, outc);
|
|---|
| 116 | cout << " FourierCoefs , NElts= " << outc.NElts() << outc << endl;
|
|---|
| 117 |
|
|---|
| 118 | ffts.FFTBackward(outc, bk);
|
|---|
| 119 | cout << " Backward , NElts= " << bk.NElts() << bk << endl;
|
|---|
| 120 |
|
|---|
| 121 | dif = bk*fact - in;
|
|---|
| 122 | cout << " Difference , NElts= " << dif.NElts() << dif << endl;
|
|---|
| 123 |
|
|---|
| 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;
|
|---|
| 133 |
|
|---|
| 134 | cout << "\n ---- Testing FFT(complex<T>, complex<T>) ---- " << endl;
|
|---|
| 135 | ffts.FFTForward(inc, outc);
|
|---|
| 136 | cout << " FourierCoef , NElts= " << outc.NElts() << outc << endl;
|
|---|
| 137 |
|
|---|
| 138 | ffts.FFTBackward(outc, bkc);
|
|---|
| 139 | cout << " Backward , NElts= " << bkc.NElts() << bkc << endl;
|
|---|
| 140 |
|
|---|
| 141 | difc = bkc*complex<T>(fact,0.) - inc;
|
|---|
| 142 | cout << " Difference , NElts= " << difc.NElts() << difc << endl;
|
|---|
| 143 |
|
|---|
| 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;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 | int main(int narg, char* arg[])
|
|---|
| 160 | {
|
|---|
| 161 |
|
|---|
| 162 | SophyaInit();
|
|---|
| 163 | InitTim(); // Initializing the CPU timer
|
|---|
| 164 |
|
|---|
| 165 | if (narg < 4) {
|
|---|
| 166 | cout << "tfft/ args error - \n Usage tfft size px/Px/Mx/Wx f/d [NPrt=50 PrtLev=0] \n"
|
|---|
| 167 | << " p=FFTPackTest P=FFTPack, M=FFTMayer, W= FFTWServer"
|
|---|
| 168 | << " x=0 -> Random input - f=float, d=double " << endl;
|
|---|
| 169 | exit(0);
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | FFTPackServer fftp;
|
|---|
| 173 | FFTMayerServer fftm;
|
|---|
| 174 |
|
|---|
| 175 | FFTWServer fftw;
|
|---|
| 176 |
|
|---|
| 177 | inp_typ_random = false;
|
|---|
| 178 | if (arg[2][1] == '0') inp_typ_random = true;
|
|---|
| 179 |
|
|---|
| 180 | int sz = atoi(arg[1]);
|
|---|
| 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);
|
|---|
| 186 |
|
|---|
| 187 | if (sz < 2) sz = 2;
|
|---|
| 188 | FFTServerInterface * ffts;
|
|---|
| 189 | if (*arg[2] == 'M') ffts = fftm.Clone();
|
|---|
| 190 | else if (*arg[2] == 'W') ffts = fftw.Clone();
|
|---|
| 191 | else ffts = fftp.Clone();
|
|---|
| 192 |
|
|---|
| 193 | float fs = 1.e-4;
|
|---|
| 194 | double ds = 1.e-6;
|
|---|
| 195 |
|
|---|
| 196 | try {
|
|---|
| 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 | }
|
|---|
| 211 | }
|
|---|
| 212 | catch(PThrowable exc ) {
|
|---|
| 213 | cerr << "TestFFT-main() , Catched exception: \n" << exc.Msg() << endl;
|
|---|
| 214 | }
|
|---|
| 215 | catch(std::exception ex) {
|
|---|
| 216 | cerr << "TestFFT-main() , Catched exception ! " << (string)(ex.what()) << endl;
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | /*
|
|---|
| 220 | catch(...) {
|
|---|
| 221 | cerr << "TestFFT-main() , Catched ... exception ! " << endl;
|
|---|
| 222 | }
|
|---|
| 223 | */
|
|---|
| 224 | PrtTim("End of tfft ");
|
|---|
| 225 | delete ffts;
|
|---|
| 226 | }
|
|---|