| 1 | /*  ------------------ apc.icc ------------------- | 
|---|
| 2 |  | 
|---|
| 3 | Example illustrating C programming for performing | 
|---|
| 4 | operations comparable to those performed in apcxx.icc | 
|---|
| 5 |  | 
|---|
| 6 | R. Ansari  08/2001 | 
|---|
| 7 |  | 
|---|
| 8 | ---- Computation steps : | 
|---|
| 9 | > Allocate an array representing a matrix (NL x NC )  (mtx) | 
|---|
| 10 | > fill it with a gaussian distributed random values | 
|---|
| 11 | > make a copy of the matrix   (mtxs) | 
|---|
| 12 | > allocate and fill 1D filter in Fourier space | 
|---|
| 13 | > Loop over matrix rows k | 
|---|
| 14 | >> Extract row k (fline) | 
|---|
| 15 | >> compute 1D Fourier transform FFTForward (fline) | 
|---|
| 16 | >> apply filter in Fourier space | 
|---|
| 17 | >> compute backward 1D FFT | 
|---|
| 18 | >> Replace matrix row with the filtered values | 
|---|
| 19 |  | 
|---|
| 20 | > free the allocated memory | 
|---|
| 21 |  | 
|---|
| 22 | this example code can be | 
|---|
| 23 | - included in a main program | 
|---|
| 24 | - executed using runcxx | 
|---|
| 25 | csh> runcxx -tmpdir /tmp -f apc.icc | 
|---|
| 26 | - executed within spiapp | 
|---|
| 27 | Cmd> c++execfrf apc.icc | 
|---|
| 28 | */ | 
|---|
| 29 |  | 
|---|
| 30 | // Select computation on float or double (r_4 r_8) | 
|---|
| 31 | #define FTYP r_4 | 
|---|
| 32 |  | 
|---|
| 33 | // Number of matrix lines and colums | 
|---|
| 34 | int NL = 1024; | 
|---|
| 35 | int NC = 4096; | 
|---|
| 36 | int i,k; | 
|---|
| 37 | cout << " apc : NL= " << NL << " NC= " << NC << endl; | 
|---|
| 38 | PrtTim("apc_Start"); | 
|---|
| 39 |  | 
|---|
| 40 | // Creation of the initial matrix | 
|---|
| 41 | FTYP * mtx = new FTYP [NL*NC]; | 
|---|
| 42 | // Filling matrix with gaussian random values | 
|---|
| 43 | for(i=0; i<NL*NC; i++) | 
|---|
| 44 | mtx[i] = 15.+NorRand()*3.; | 
|---|
| 45 |  | 
|---|
| 46 | // Allocation of the copy matrix (mtxs) | 
|---|
| 47 | FTYP * mtxs = new FTYP [NL*NC]; | 
|---|
| 48 | // copying mtxs to mtx | 
|---|
| 49 | for(i=0; i<NL*NC; i++) | 
|---|
| 50 | mtxs[i] = mtx[i]; | 
|---|
| 51 |  | 
|---|
| 52 | // Creation and initialization of the Fourier filter filt(nu) | 
|---|
| 53 | FTYP * filt = new FTYP [NC]; | 
|---|
| 54 | for(i=0; i<NC; i++) { | 
|---|
| 55 | filt[i] =  1.-i/(double)NC; | 
|---|
| 56 | filt[i] *= filt[i]; | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 |  | 
|---|
| 60 | // Creation of the FFTPackServer | 
|---|
| 61 | // We use this class to avoid to do the initialization steps needed to use fftpack. | 
|---|
| 62 | FFTPackServer ffts; | 
|---|
| 63 | ffts.setNormalize(true); | 
|---|
| 64 |  | 
|---|
| 65 | PrtTim("apc_AfterInit"); | 
|---|
| 66 |  | 
|---|
| 67 | // Allocation of the matrix row memory | 
|---|
| 68 | FTYP * fline = new FTYP [NC]; | 
|---|
| 69 |  | 
|---|
| 70 | //  Loop over matrix rows | 
|---|
| 71 | for(k=0; k<NL; k++) { | 
|---|
| 72 | // Matrix row extraction | 
|---|
| 73 | for(i=0; i<NC; i++) fline[i] = mtx[k*NC+i]; | 
|---|
| 74 | // Compute 1D forward FFT | 
|---|
| 75 | ffts.fftf(NC, fline); | 
|---|
| 76 | // Applying filter in Fourier space f(nu) =  f(nu)*filter(nu) | 
|---|
| 77 | for(i=0; i<NC; i++) fline[i] *= filt[i]; | 
|---|
| 78 | // backward FFT | 
|---|
| 79 | ffts.fftb(NC, fline); | 
|---|
| 80 | // replace matrix row with filtered values | 
|---|
| 81 | for(i=0; i<NC; i++) mtx[k*NC+i] = fline[i]; | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | PrtTim("apc_AfterFFTLoop"); | 
|---|
| 85 |  | 
|---|
| 86 |  | 
|---|
| 87 | delete[] mtx; | 
|---|
| 88 | delete[] mtxs; | 
|---|
| 89 | delete[] fline; | 
|---|
| 90 | delete[] filt; | 
|---|
| 91 |  | 
|---|