1 | #include "fftserver.h"
|
---|
2 | #include <iostream.h>
|
---|
3 |
|
---|
4 | extern "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 |
|
---|
19 | FFTServer::FFTServer()
|
---|
20 | {
|
---|
21 | sz_rfft = 0;
|
---|
22 | ws_rfft = NULL;
|
---|
23 | sz_cfft = 0;
|
---|
24 | ws_cfft = NULL;
|
---|
25 | sz_cdfft = 0;
|
---|
26 | ws_cdfft = NULL;
|
---|
27 | }
|
---|
28 |
|
---|
29 | FFTServer::~FFTServer()
|
---|
30 | {
|
---|
31 | if (ws_rfft) delete[] ws_rfft;
|
---|
32 | if (ws_cfft) delete[] ws_cfft;
|
---|
33 | if (ws_cdfft) delete[] ws_cdfft;
|
---|
34 | }
|
---|
35 |
|
---|
36 | void 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 |
|
---|
46 | void 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 |
|
---|
56 | void 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 |
|
---|
66 | void 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 |
|
---|
76 | void FFTServer::fftf(int l, float* inout)
|
---|
77 | {
|
---|
78 | checkint_rfft(l);
|
---|
79 | rfftf_(&l, inout, ws_rfft);
|
---|
80 | for (int k= 2;k<=(l+1)/2;k++) inout[2*k-2]=-inout[2*k-2];
|
---|
81 | }
|
---|
82 |
|
---|
83 | void FFTServer::fftf(int l, double* inout)
|
---|
84 | {
|
---|
85 | checkint_dfft(l);
|
---|
86 | dfftf_(&l, inout, ws_dfft);
|
---|
87 | for (int k= 2;k<=(l+1)/2;k++) inout[2*k-2]=-inout[2*k-2];
|
---|
88 | }
|
---|
89 |
|
---|
90 | void 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);
|
---|
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]);
|
---|
101 | delete[] foo;
|
---|
102 | }
|
---|
103 |
|
---|
104 | void FFTServer::fftf(int l, complex<double>* inout)
|
---|
105 | {
|
---|
106 | checkint_cdfft(l);
|
---|
107 | double* foo=new double[2*l];
|
---|
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);
|
---|
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 | }
|
---|
117 | delete[] foo;
|
---|
118 | }
|
---|
119 |
|
---|
120 | void FFTServer::fftb(int l, float* inout)
|
---|
121 | {
|
---|
122 | checkint_rfft(l);
|
---|
123 | rfftf_(&l, inout, ws_rfft);
|
---|
124 | }
|
---|
125 |
|
---|
126 | void FFTServer::fftb(int l, double* inout)
|
---|
127 | {
|
---|
128 | checkint_dfft(l);
|
---|
129 | dfftf_(&l, inout, ws_dfft);
|
---|
130 | }
|
---|
131 |
|
---|
132 | void 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 | }
|
---|
140 | cfftf_(&l, foo, ws_cfft);
|
---|
141 | for (int i=0;i<l;i++) inout[i]=complex<float> (foo[2*i],foo[2*i+1]);
|
---|
142 | delete[] foo;
|
---|
143 | }
|
---|
144 |
|
---|
145 | void 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 | }
|
---|
153 | cdfftf_(&l, foo, ws_cdfft);
|
---|
154 | for (int i=0;i<l;i++) inout[i]=complex<double> (foo[2*i],foo[2*i+1]);
|
---|
155 | delete[] foo;
|
---|
156 | }
|
---|
157 |
|
---|
158 | void FFTServer::fftf(OVector& in, OVector& out)
|
---|
159 | {
|
---|
160 | int l = in.NElts();
|
---|
161 | /* ----- Si c'etait un OVector<float> on aurait ecrit comme ca
|
---|
162 | Pour le moment OVector 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 |
|
---|
177 | void FFTServer::fftb(OVector& in, OVector& out)
|
---|
178 | {
|
---|
179 | int l = in.NElts();
|
---|
180 | /* ----- Si c'etait un OVector<float> on aurait ecrit comme ca
|
---|
181 | Pour le moment OVector 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 |
|
---|