| 1 | /*  ----------------- apcxx.icc -------------------
 | 
|---|
| 2 | 
 | 
|---|
| 3 |    Example illustrating C++ scientific programming 
 | 
|---|
| 4 |    using SOPHYA library arrays and FFT computation 
 | 
|---|
| 5 |    through FFTServerInterface.                         
 | 
|---|
| 6 |                                    R. Ansari  08/2001 
 | 
|---|
| 7 |    ---- Computation steps :
 | 
|---|
| 8 |     > Create a matrix (NL x NC )  (mtx)
 | 
|---|
| 9 |     > fill it with a gaussian distributed random values
 | 
|---|
| 10 |     > make a copy of the matrix   (mtxs)
 | 
|---|
| 11 |     > create a 1D filter in Fourier space 
 | 
|---|
| 12 |     > Loop over matrix rows k
 | 
|---|
| 13 |      >> Extract row k (fline)
 | 
|---|
| 14 |      >> compute 1D Fourier transform FFTForward (fline) 
 | 
|---|
| 15 |      >> apply filter in Fourier space
 | 
|---|
| 16 |      >> compute backward 1D FFT 
 | 
|---|
| 17 |      >> Replace matrix row with the filtered values
 | 
|---|
| 18 |    
 | 
|---|
| 19 | 
 | 
|---|
| 20 |    this example code can be 
 | 
|---|
| 21 |      - included in a main program
 | 
|---|
| 22 |      - executed using runcxx 
 | 
|---|
| 23 |        csh> runcxx -tmpdir /tmp -f apcxx.icc
 | 
|---|
| 24 |      - executed within spiapp 
 | 
|---|
| 25 |        Cmd> c++execfrf apcxx.icc
 | 
|---|
| 26 | */
 | 
|---|
| 27 | 
 | 
|---|
| 28 | // Select computation on float or double (r_4 r_8)
 | 
|---|
| 29 | #define FTYP r_4
 | 
|---|
| 30 | 
 | 
|---|
| 31 | // Number of matrix lines and colums
 | 
|---|
| 32 | int NL = 1024;
 | 
|---|
| 33 | int NC = 4096;
 | 
|---|
| 34 | cout << " apc_xx : NL= " << NL << " NC= " << NC << endl;
 | 
|---|
| 35 | PrtTim("apcxx_Start");
 | 
|---|
| 36 | 
 | 
|---|
| 37 | // BaseArray::SetDefaultMemoryMapping(BaseArray::CMemoryMapping);
 | 
|---|
| 38 | // BaseArray::SetMaxPrint(10, 3);
 | 
|---|
| 39 | 
 | 
|---|
| 40 | // Creation of the initial matrix
 | 
|---|
| 41 | TMatrix< FTYP > mtx(NL, NC), mtxs;
 | 
|---|
| 42 | // Filling matrix with gaussian random values
 | 
|---|
| 43 | mtx = RandomSequence(RandomSequence::Gaussian, 15., 3.);
 | 
|---|
| 44 | // Making a copy of the original matrix
 | 
|---|
| 45 | mtxs = mtx;
 | 
|---|
| 46 | 
 | 
|---|
| 47 | // Creation and initialization of the Fourier filter filt(nu) = 1/(1+0.3*nu)
 | 
|---|
| 48 | int LFFT = NC/2+1;
 | 
|---|
| 49 | TVector< complex< FTYP > > filt(LFFT, BaseArray::RowVector);
 | 
|---|
| 50 | filt(0) = 1.;
 | 
|---|
| 51 | for(int i=1; i<filt.Size(); i++) 
 | 
|---|
| 52 |   filt(i) = 1./(1+0.3*(double)i);
 | 
|---|
| 53 | 
 | 
|---|
| 54 | 
 | 
|---|
| 55 | // Creation of the FFTServer
 | 
|---|
| 56 | FFTPackServer ffts;
 | 
|---|
| 57 | ffts.setNormalize(true);
 | 
|---|
| 58 | 
 | 
|---|
| 59 | PrtTim("apcxx_AfterInit");
 | 
|---|
| 60 | 
 | 
|---|
| 61 | // Vectors for FFT operations 
 | 
|---|
| 62 | TVector< FTYP > fline(NC, BaseArray::RowVector);
 | 
|---|
| 63 | TVector< complex< FTYP > > vfft;
 | 
|---|
| 64 | 
 | 
|---|
| 65 | //  Loop over matrix rows
 | 
|---|
| 66 | for(int k=0; k<NL; k++) {
 | 
|---|
| 67 |   fline = mtx.Row(k);      // Matrix row extraction 
 | 
|---|
| 68 |   ffts.FFTForward(fline, vfft);    // Compute 1D forward FFT
 | 
|---|
| 69 |   // Applying filter in Fourier space f(nu) =  f(nu)*filter(nu)
 | 
|---|
| 70 |   vfft.MulElt(filt);             
 | 
|---|
| 71 |   ffts.FFTBackward(vfft, fline);   // backward FFT
 | 
|---|
| 72 |   mtx.Row(k) = fline;      // replace matrix row with filtered values
 | 
|---|
| 73 |  }
 | 
|---|
| 74 | 
 | 
|---|
| 75 | PrtTim("apcxx_AfterFFTLoop");
 | 
|---|
| 76 | 
 | 
|---|
| 77 | // Macro KeepObj can be used with runcxx or within (s)piapp 
 | 
|---|
| 78 | // KeepObj(mtx);
 | 
|---|
| 79 | // KeepObj(mtxs);
 | 
|---|
| 80 | 
 | 
|---|