[459] | 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;
|
---|
[467] | 25 | sz_cdfft = 0;
|
---|
| 26 | ws_cdfft = NULL;
|
---|
[459] | 27 | }
|
---|
| 28 |
|
---|
| 29 | FFTServer::~FFTServer()
|
---|
| 30 | {
|
---|
| 31 | if (ws_rfft) delete[] ws_rfft;
|
---|
| 32 | if (ws_cfft) delete[] ws_cfft;
|
---|
[467] | 33 | if (ws_cdfft) delete[] ws_cdfft;
|
---|
[459] | 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);
|
---|
[467] | 80 | for (int k= 2;k<=(l+1)/2;k++) inout[2*k-2]=-inout[2*k-2];
|
---|
[459] | 81 | }
|
---|
| 82 |
|
---|
| 83 | void 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 |
|
---|
| 90 | void FFTServer::fftf(int l, complex<float>* inout)
|
---|
| 91 | {
|
---|
| 92 | checkint_cfft(l);
|
---|
| 93 | float* foo = new float[2*l];
|
---|
[515] | 94 | int i;
|
---|
| 95 | for (i=0;i<l;i++){
|
---|
[459] | 96 | foo[2*i]=inout[i].real();
|
---|
| 97 | foo[2*i+1]=inout[i].imag();
|
---|
| 98 | }
|
---|
| 99 | cfftf_(&l, foo, ws_cfft);
|
---|
[467] | 100 | inout[0]=complex<float> (foo[0],foo[1]);
|
---|
[515] | 101 | for (i=1;i<l;i++) inout[l-i]= complex<float> (foo[2*i], foo[2*i+1]);
|
---|
[459] | 102 | delete[] foo;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | void FFTServer::fftf(int l, complex<double>* inout)
|
---|
| 106 | {
|
---|
| 107 | checkint_cdfft(l);
|
---|
[467] | 108 | double* foo=new double[2*l];
|
---|
[515] | 109 | int i;
|
---|
| 110 | for (i=0;i<l;i++){
|
---|
[459] | 111 | foo[2*i]=inout[i].real();
|
---|
| 112 | foo[2*i+1]=inout[i].imag();
|
---|
| 113 | }
|
---|
| 114 | cdfftf_(&l, foo, ws_cdfft);
|
---|
[467] | 115 | inout[0]=complex<double> (foo[0],foo[1]);
|
---|
[515] | 116 | for (i=1;i<l;i++) {
|
---|
[467] | 117 | inout[l-i]= complex<double> (foo[2*i],foo[2*i+1]);
|
---|
| 118 | }
|
---|
[459] | 119 | delete[] foo;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | void FFTServer::fftb(int l, float* inout)
|
---|
| 123 | {
|
---|
| 124 | checkint_rfft(l);
|
---|
[467] | 125 | rfftf_(&l, inout, ws_rfft);
|
---|
[459] | 126 | }
|
---|
| 127 |
|
---|
| 128 | void FFTServer::fftb(int l, double* inout)
|
---|
| 129 | {
|
---|
| 130 | checkint_dfft(l);
|
---|
[467] | 131 | dfftf_(&l, inout, ws_dfft);
|
---|
[459] | 132 | }
|
---|
| 133 |
|
---|
| 134 | void FFTServer::fftb(int l, complex<float>* inout)
|
---|
| 135 | {
|
---|
| 136 | checkint_cfft(l);
|
---|
| 137 | float* foo = new float[2*l];
|
---|
[515] | 138 | int i;
|
---|
| 139 | for (i=0;i<l;i++){
|
---|
[459] | 140 | foo[2*i]=inout[i].real();
|
---|
| 141 | foo[2*i+1]=inout[i].imag();
|
---|
| 142 | }
|
---|
[467] | 143 | cfftf_(&l, foo, ws_cfft);
|
---|
[515] | 144 | for (i=0;i<l;i++) inout[i]=complex<float> (foo[2*i],foo[2*i+1]);
|
---|
[459] | 145 | delete[] foo;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | void FFTServer::fftb(int l, complex<double>* inout)
|
---|
| 149 | {
|
---|
| 150 | checkint_cdfft(l);
|
---|
| 151 | double* foo = new double[2*l];
|
---|
[515] | 152 | int i;
|
---|
| 153 | for (i=0;i<l;i++){
|
---|
[459] | 154 | foo[2*i]=inout[i].real();
|
---|
| 155 | foo[2*i+1]=inout[i].imag();
|
---|
| 156 | }
|
---|
[467] | 157 | cdfftf_(&l, foo, ws_cdfft);
|
---|
[515] | 158 | for (i=0;i<l;i++) inout[i]=complex<double> (foo[2*i],foo[2*i+1]);
|
---|
[459] | 159 | delete[] foo;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[514] | 162 | void FFTServer::fftf(Vector& in, Vector& out)
|
---|
[459] | 163 | {
|
---|
| 164 | int l = in.NElts();
|
---|
[514] | 165 | /* ----- Si c'etait un Vector<float> on aurait ecrit comme ca
|
---|
| 166 | Pour le moment Vector est double, on passe donc par un tableau
|
---|
[459] | 167 | intermediare
|
---|
| 168 | // La transformee sur le tableau de flaot se fait en place,
|
---|
| 169 | // on utilise donc out comme in-out
|
---|
| 170 | out = in;
|
---|
| 171 | fftf_float(l, out.Data() );
|
---|
| 172 | ------------------------------------------------------------- */
|
---|
| 173 | float * inout = new float[l];
|
---|
| 174 | int i;
|
---|
| 175 | for(i=0; i<l; i++) inout[i] = in(i);
|
---|
| 176 | fftf(l, inout);
|
---|
| 177 | out.Realloc(l);
|
---|
| 178 | for(i=0; i<l; i++) out(i) = inout[i];
|
---|
| 179 | }
|
---|
| 180 |
|
---|
[514] | 181 | void FFTServer::fftb(Vector& in, Vector& out)
|
---|
[459] | 182 | {
|
---|
| 183 | int l = in.NElts();
|
---|
[514] | 184 | /* ----- Si c'etait un Vector<float> on aurait ecrit comme ca
|
---|
| 185 | Pour le moment Vector est double, on passe donc par un tableau
|
---|
[459] | 186 | intermediare
|
---|
| 187 | // La transformee sur le tableau de flaot se fait en place,
|
---|
| 188 | // on utilise donc out comme in-out
|
---|
| 189 | out = in;
|
---|
| 190 | fftf_float(l, out.Data() );
|
---|
| 191 | ------------------------------------------------------------- */
|
---|
| 192 | float * inout = new float[l];
|
---|
| 193 | int i;
|
---|
| 194 | for(i=0; i<l; i++) inout[i] = in(i);
|
---|
| 195 | fftb(l, inout);
|
---|
| 196 | out.Realloc(l);
|
---|
| 197 | for(i=0; i<l; i++) out(i) = inout[i];
|
---|
| 198 | }
|
---|
| 199 |
|
---|