source: Sophya/trunk/SophyaProg/Examples/apc.icc@ 4003

Last change on this file since 4003 was 1634, checked in by ansari, 24 years ago

Ajout exemples de filtrage de Fourier sur tableaux + ecriture equivalente en C , Reza 13/8/2001

File size: 2.4 KB
Line 
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
34int NL = 1024;
35int NC = 4096;
36int i,k;
37cout << " apc : NL= " << NL << " NC= " << NC << endl;
38PrtTim("apc_Start");
39
40// Creation of the initial matrix
41FTYP * mtx = new FTYP [NL*NC];
42// Filling matrix with gaussian random values
43for(i=0; i<NL*NC; i++)
44 mtx[i] = 15.+NorRand()*3.;
45
46// Allocation of the copy matrix (mtxs)
47FTYP * mtxs = new FTYP [NL*NC];
48// copying mtxs to mtx
49for(i=0; i<NL*NC; i++)
50 mtxs[i] = mtx[i];
51
52// Creation and initialization of the Fourier filter filt(nu)
53FTYP * filt = new FTYP [NC];
54for(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.
62FFTPackServer ffts;
63ffts.setNormalize(true);
64
65PrtTim("apc_AfterInit");
66
67// Allocation of the matrix row memory
68FTYP * fline = new FTYP [NC];
69
70// Loop over matrix rows
71for(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
84PrtTim("apc_AfterFFTLoop");
85
86
87delete[] mtx;
88delete[] mtxs;
89delete[] fline;
90delete[] filt;
91
Note: See TracBrowser for help on using the repository browser.