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

Last change on this file since 483 was 467, checked in by ansari, 26 years ago

Fixing of bugs, output formats, inverse transforms.

File size: 4.5 KB
RevLine 
[459]1#include "fftserver.h"
2#include <iostream.h>
3
4extern "C" {
5 int rffti_(int *n, float *wsave);
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{
21sz_rfft = 0;
22ws_rfft = NULL;
23sz_cfft = 0;
24ws_cfft = NULL;
[467]25sz_cdfft = 0;
26ws_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{
38 if (sz_rfft == l) return;
39
40 if (ws_rfft) delete[] ws_rfft;
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
76void FFTServer::fftf(int l, float* inout)
77{
78 checkint_rfft(l);
79 rfftf_(&l, inout, ws_rfft);
[467]80 for (int k= 2;k<=(l+1)/2;k++) inout[2*k-2]=-inout[2*k-2];
[459]81}
82
83void FFTServer::fftf(int l, double* inout)
84{
85 checkint_dfft(l);
86 dfftf_(&l, inout, ws_dfft);
[467]87 for (int k= 2;k<=(l+1)/2;k++) inout[2*k-2]=-inout[2*k-2];
[459]88}
89
90void FFTServer::fftf(int l, complex<float>* inout)
91{
92 checkint_cfft(l);
93 float* foo = new float[2*l];
94 for (int i=0;i<l;i++){
95 foo[2*i]=inout[i].real();
96 foo[2*i+1]=inout[i].imag();
97 }
98 cfftf_(&l, foo, ws_cfft);
[467]99 inout[0]=complex<float> (foo[0],foo[1]);
100 for (int i=1;i<l;i++) inout[l-i]= complex<float> (foo[2*i], foo[2*i+1]);
[459]101 delete[] foo;
102}
103
104void FFTServer::fftf(int l, complex<double>* inout)
105{
106 checkint_cdfft(l);
[467]107 double* foo=new double[2*l];
[459]108 for (int i=0;i<l;i++){
109 foo[2*i]=inout[i].real();
110 foo[2*i+1]=inout[i].imag();
111 }
112 cdfftf_(&l, foo, ws_cdfft);
[467]113 inout[0]=complex<double> (foo[0],foo[1]);
114 for (int i=1;i<l;i++) {
115 inout[l-i]= complex<double> (foo[2*i],foo[2*i+1]);
116 }
[459]117 delete[] foo;
118}
119
120void FFTServer::fftb(int l, float* inout)
121{
122 checkint_rfft(l);
[467]123 rfftf_(&l, inout, ws_rfft);
[459]124}
125
126void FFTServer::fftb(int l, double* inout)
127{
128 checkint_dfft(l);
[467]129 dfftf_(&l, inout, ws_dfft);
[459]130}
131
132void FFTServer::fftb(int l, complex<float>* inout)
133{
134 checkint_cfft(l);
135 float* foo = new float[2*l];
136 for (int i=0;i<l;i++){
137 foo[2*i]=inout[i].real();
138 foo[2*i+1]=inout[i].imag();
139 }
[467]140 cfftf_(&l, foo, ws_cfft);
[459]141 for (int i=0;i<l;i++) inout[i]=complex<float> (foo[2*i],foo[2*i+1]);
142 delete[] foo;
143}
144
145void FFTServer::fftb(int l, complex<double>* inout)
146{
147 checkint_cdfft(l);
148 double* foo = new double[2*l];
149 for (int i=0;i<l;i++){
150 foo[2*i]=inout[i].real();
151 foo[2*i+1]=inout[i].imag();
152 }
[467]153 cdfftf_(&l, foo, ws_cdfft);
[459]154 for (int i=0;i<l;i++) inout[i]=complex<double> (foo[2*i],foo[2*i+1]);
155 delete[] foo;
156}
157
158void FFTServer::fftf(Vector& in, Vector& out)
159{
160 int l = in.NElts();
161/* ----- Si c'etait un Vector<float> on aurait ecrit comme ca
162 Pour le moment Vector est double, on passe donc par un tableau
163 intermediare
164// La transformee sur le tableau de flaot se fait en place,
165// on utilise donc out comme in-out
166 out = in;
167 fftf_float(l, out.Data() );
168 ------------------------------------------------------------- */
169 float * inout = new float[l];
170 int i;
171 for(i=0; i<l; i++) inout[i] = in(i);
172 fftf(l, inout);
173 out.Realloc(l);
174 for(i=0; i<l; i++) out(i) = inout[i];
175}
176
177void FFTServer::fftb(Vector& in, Vector& out)
178{
179 int l = in.NElts();
180/* ----- Si c'etait un Vector<float> on aurait ecrit comme ca
181 Pour le moment Vector est double, on passe donc par un tableau
182 intermediare
183// La transformee sur le tableau de flaot se fait en place,
184// on utilise donc out comme in-out
185 out = in;
186 fftf_float(l, out.Data() );
187 ------------------------------------------------------------- */
188 float * inout = new float[l];
189 int i;
190 for(i=0; i<l; i++) inout[i] = in(i);
191 fftb(l, inout);
192 out.Realloc(l);
193 for(i=0; i<l; i++) out(i) = inout[i];
194}
195
Note: See TracBrowser for help on using the repository browser.