source: Sophya/trunk/SophyaLib/NTools/fftserver.cc@ 852

Last change on this file since 852 was 679, checked in by ansari, 26 years ago

Ajout de delete tableau temp - Reza 9/12/99

File size: 4.9 KB
RevLine 
[459]1#include "fftserver.h"
2#include <iostream.h>
3
4extern "C" {
[538]5 int rffti_(int *n, float *wsave); //fftpack functions
[459]6 int rfftf_(int *n, float *r__, float *wsave);
7 int rfftb_(int *n, float *r__, float *wsave);
8 int dffti_(int *n, double *wsave);
9 int dfftf_(int *n, double *r__, double *wsave);
10 int dfftb_(int *n, double *r__, double *wsave);
11 int cffti_(int *n, float *wsave);
12 int cfftf_(int *n, float *c__, float *wsave);
13 int cfftb_(int *n, float *c__, float *wsave);
14 int cdffti_(int *n, double *wsave);
15 int cdfftf_(int *n, double *c__, double *wsave);
16 int cdfftb_(int *n, double *c__, double *wsave);
17 }
18
19FFTServer::FFTServer()
20{
[538]21 sz_rfft = 0; //the working array and its size for the different
22 ws_rfft = NULL; //possible numerical types
23 sz_cfft = 0;
24 ws_cfft = NULL;
25 sz_cdfft = 0;
26 ws_cdfft = NULL;
[459]27}
28
29FFTServer::~FFTServer()
30{
31if (ws_rfft) delete[] ws_rfft;
32if (ws_cfft) delete[] ws_cfft;
[467]33if (ws_cdfft) delete[] ws_cdfft;
[459]34}
35
36void FFTServer::checkint_rfft(int l)
37{
[538]38 if (sz_rfft == l) return; //checkint functions check and reallocate
39 //memory for the work arrays when performing
40 if (ws_rfft) delete[] ws_rfft; //a transform
[459]41 sz_rfft = l;
42 ws_rfft = new float[2*l+15];
43 rffti_(&l, ws_rfft);
44}
45
46void FFTServer::checkint_cfft(int l)
47{
48 if (sz_cfft == l) return;
49
50 if (ws_cfft) delete[] ws_cfft;
51 sz_cfft = l;
52 ws_cfft = new float[4*l+15];
53 cffti_(&l, ws_cfft);
54}
55
56void FFTServer::checkint_dfft(int l)
57{
58 if (sz_dfft == l) return;
59
60 if (ws_dfft) delete[] ws_dfft;
61 sz_dfft = l;
62 ws_dfft = new double[2*l+15];
63 dffti_(&l, ws_dfft);
64}
65
66void FFTServer::checkint_cdfft(int l)
67{
68 if (sz_cdfft == l) return;
69
70 if (ws_cdfft) delete[] ws_cdfft;
71 sz_cdfft = l;
72 ws_cdfft = new double[4*l+15];
73 cdffti_(&l, ws_cdfft);
74}
75
[538]76/* In general forward transformations are resorted since fftpack functions
77 return inverse transformations */
78
[459]79void FFTServer::fftf(int l, float* inout)
80{
81 checkint_rfft(l);
82 rfftf_(&l, inout, ws_rfft);
[467]83 for (int k= 2;k<=(l+1)/2;k++) inout[2*k-2]=-inout[2*k-2];
[459]84}
85
86void FFTServer::fftf(int l, double* inout)
87{
88 checkint_dfft(l);
89 dfftf_(&l, inout, ws_dfft);
[467]90 for (int k= 2;k<=(l+1)/2;k++) inout[2*k-2]=-inout[2*k-2];
[459]91}
92
93void FFTServer::fftf(int l, complex<float>* inout)
94{
95 checkint_cfft(l);
96 float* foo = new float[2*l];
[515]97 int i;
98 for (i=0;i<l;i++){
[459]99 foo[2*i]=inout[i].real();
100 foo[2*i+1]=inout[i].imag();
101 }
102 cfftf_(&l, foo, ws_cfft);
[467]103 inout[0]=complex<float> (foo[0],foo[1]);
[515]104 for (i=1;i<l;i++) inout[l-i]= complex<float> (foo[2*i], foo[2*i+1]);
[459]105 delete[] foo;
106}
107
108void FFTServer::fftf(int l, complex<double>* inout)
109{
110 checkint_cdfft(l);
[467]111 double* foo=new double[2*l];
[515]112 int i;
113 for (i=0;i<l;i++){
[459]114 foo[2*i]=inout[i].real();
115 foo[2*i+1]=inout[i].imag();
116 }
117 cdfftf_(&l, foo, ws_cdfft);
[467]118 inout[0]=complex<double> (foo[0],foo[1]);
[515]119 for (i=1;i<l;i++) {
[467]120 inout[l-i]= complex<double> (foo[2*i],foo[2*i+1]);
121 }
[459]122 delete[] foo;
123}
124
125void FFTServer::fftb(int l, float* inout)
126{
127 checkint_rfft(l);
[467]128 rfftf_(&l, inout, ws_rfft);
[459]129}
130
131void FFTServer::fftb(int l, double* inout)
132{
133 checkint_dfft(l);
[467]134 dfftf_(&l, inout, ws_dfft);
[459]135}
136
137void FFTServer::fftb(int l, complex<float>* inout)
138{
139 checkint_cfft(l);
140 float* foo = new float[2*l];
[515]141 int i;
142 for (i=0;i<l;i++){
[459]143 foo[2*i]=inout[i].real();
144 foo[2*i+1]=inout[i].imag();
145 }
[467]146 cfftf_(&l, foo, ws_cfft);
[515]147 for (i=0;i<l;i++) inout[i]=complex<float> (foo[2*i],foo[2*i+1]);
[459]148 delete[] foo;
149}
150
151void FFTServer::fftb(int l, complex<double>* inout)
152{
153 checkint_cdfft(l);
154 double* foo = new double[2*l];
[515]155 int i;
156 for (i=0;i<l;i++){
[459]157 foo[2*i]=inout[i].real();
158 foo[2*i+1]=inout[i].imag();
159 }
[467]160 cdfftf_(&l, foo, ws_cdfft);
[515]161 for (i=0;i<l;i++) inout[i]=complex<double> (foo[2*i],foo[2*i+1]);
[459]162 delete[] foo;
163}
164
[514]165void FFTServer::fftf(Vector& in, Vector& out)
[459]166{
167 int l = in.NElts();
[514]168/* ----- Si c'etait un Vector<float> on aurait ecrit comme ca
169 Pour le moment Vector est double, on passe donc par un tableau
[459]170 intermediare
171// La transformee sur le tableau de flaot se fait en place,
172// on utilise donc out comme in-out
173 out = in;
174 fftf_float(l, out.Data() );
175 ------------------------------------------------------------- */
176 float * inout = new float[l];
177 int i;
178 for(i=0; i<l; i++) inout[i] = in(i);
179 fftf(l, inout);
180 out.Realloc(l);
181 for(i=0; i<l; i++) out(i) = inout[i];
[679]182 delete[] inout;
[459]183}
184
[514]185void FFTServer::fftb(Vector& in, Vector& out)
[459]186{
187 int l = in.NElts();
[514]188/* ----- Si c'etait un Vector<float> on aurait ecrit comme ca
189 Pour le moment Vector est double, on passe donc par un tableau
[459]190 intermediare
191// La transformee sur le tableau de flaot se fait en place,
192// on utilise donc out comme in-out
193 out = in;
194 fftf_float(l, out.Data() );
195 ------------------------------------------------------------- */
196 float * inout = new float[l];
197 int i;
198 for(i=0; i<l; i++) inout[i] = in(i);
199 fftb(l, inout);
200 out.Realloc(l);
201 for(i=0; i<l; i++) out(i) = inout[i];
[679]202 delete[] inout;
[459]203}
204
Note: See TracBrowser for help on using the repository browser.