source: Sophya/trunk/SophyaProg/Examples/apcxx.icc@ 4057

Last change on this file since 4057 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/* ----------------- 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
32int NL = 1024;
33int NC = 4096;
34cout << " apc_xx : NL= " << NL << " NC= " << NC << endl;
35PrtTim("apcxx_Start");
36
37// BaseArray::SetDefaultMemoryMapping(BaseArray::CMemoryMapping);
38// BaseArray::SetMaxPrint(10, 3);
39
40// Creation of the initial matrix
41TMatrix< FTYP > mtx(NL, NC), mtxs;
42// Filling matrix with gaussian random values
43mtx = RandomSequence(RandomSequence::Gaussian, 15., 3.);
44// Making a copy of the original matrix
45mtxs = mtx;
46
47// Creation and initialization of the Fourier filter filt(nu) = 1/(1+0.3*nu)
48int LFFT = NC/2+1;
49TVector< complex< FTYP > > filt(LFFT, BaseArray::RowVector);
50filt(0) = 1.;
51for(int i=1; i<filt.Size(); i++)
52 filt(i) = 1./(1+0.3*(double)i);
53
54
55// Creation of the FFTServer
56FFTPackServer ffts;
57ffts.setNormalize(true);
58
59PrtTim("apcxx_AfterInit");
60
61// Vectors for FFT operations
62TVector< FTYP > fline(NC, BaseArray::RowVector);
63TVector< complex< FTYP > > vfft;
64
65// Loop over matrix rows
66for(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
75PrtTim("apcxx_AfterFFTLoop");
76
77// Macro KeepObj can be used with runcxx or within (s)piapp
78// KeepObj(mtx);
79// KeepObj(mtxs);
80
Note: See TracBrowser for help on using the repository browser.