[710] | 1 | #include "fftpserver.h"
|
---|
| 2 | #include "fftpackc.h"
|
---|
| 3 |
|
---|
| 4 | #include <iostream.h>
|
---|
| 5 |
|
---|
| 6 |
|
---|
| 7 | /*! \class FFTPackServer
|
---|
| 8 | \brief An FFT server based on fftpack
|
---|
| 9 |
|
---|
| 10 | A class that calculates Fourier transforms forwards and backwards.
|
---|
| 11 |
|
---|
| 12 | The class calls the c library ``fftpack'', which is accessible and documented
|
---|
| 13 | at http://www.netlib.org/fftpack/. However, the class functions do not
|
---|
| 14 | necessarily correspond with the equivalent fftpack function. For example,
|
---|
| 15 | fftpack "forward" transformations are in fact inverse fourier transformations.
|
---|
| 16 | Otherwise, the output is in the fftpack format.
|
---|
| 17 |
|
---|
| 18 | Complex input must be entered using the <complex> template. Otherwise,
|
---|
| 19 | all input is assumed to be real.
|
---|
| 20 |
|
---|
| 21 | Due to the way that fftpack manages
|
---|
| 22 | its work arrays, an object can run faster if the length of the input arrays
|
---|
| 23 | does not change. For example, if you need to do a series of FFT's
|
---|
| 24 | of differing length, it may be more efficient to create an fftserver object
|
---|
| 25 | for each length.
|
---|
| 26 | */
|
---|
| 27 |
|
---|
[717] | 28 | /*! \fn virtual void FFTServer::fftf(int l, r_4* inout)
|
---|
[710] | 29 | \param l length of array
|
---|
| 30 | \param inout input array /output forward FFT (original array destroyed)
|
---|
| 31 | */
|
---|
[717] | 32 | /*! \fn virtual void FFTServer::fftb(int l, r_4* inout)
|
---|
[710] | 33 | \param l length of array
|
---|
| 34 | \param inout input array /output backward FFT (original array destroyed)
|
---|
| 35 | */
|
---|
[717] | 36 | /*! \fn virtual void FFTServer::fftf(int l, r_8* inout)
|
---|
[710] | 37 | \param l length of array
|
---|
| 38 | \param inout input array /output forward FFT (original array destroyed)
|
---|
| 39 | \param inout input/output array (original array destroyed)
|
---|
| 40 | */
|
---|
[717] | 41 | /*! \fn virtual void FFTServer::fftb(int l, r_8* inout)
|
---|
[710] | 42 | \param l length of array
|
---|
| 43 | \param inout input array /output backward FFT(original array destroyed)
|
---|
| 44 | */
|
---|
[717] | 45 | /*!\fn virtual void FFTServer::fftf(int l, complex<r_4>* inout)
|
---|
[710] | 46 | \param l length of array
|
---|
| 47 | \param inout input array /output forward FFT (original array destroyed)
|
---|
| 48 | */
|
---|
[717] | 49 | /*! \fn virtual void FFTServer::fftb(int l, complex<r_4>* inout)
|
---|
[710] | 50 | \param l length of array
|
---|
| 51 | \param inout input array /output backward FFT (original array destroyed)
|
---|
| 52 | */
|
---|
[717] | 53 | /*! \fn virtual void FFTServer::fftf(int l, complex<r_8>* inout)
|
---|
[710] | 54 | \param l length of array
|
---|
| 55 | \param inout input array /output forward FFT (original array destroyed)
|
---|
| 56 | */
|
---|
[717] | 57 | /*! \fn virtual void FFTServer::fftb(int l, complex<r_8>* inout)
|
---|
[710] | 58 | \param l length of array
|
---|
| 59 | \param inout input array /output backward FFT(original array destroyed)
|
---|
| 60 | */
|
---|
| 61 | /*!\fn virtual void FFTServer::fftf(Vector& in, Vector& out)
|
---|
| 62 | \param in input array
|
---|
| 63 | \param out forward FFT
|
---|
| 64 | */
|
---|
| 65 | /*! \fn virtual void FFTServer::fftb(Vector& in, Vector& out)
|
---|
| 66 | \param in input array
|
---|
| 67 | \param out backward FFT
|
---|
| 68 | */
|
---|
| 69 |
|
---|
| 70 | FFTPackServer::FFTPackServer()
|
---|
[717] | 71 | : FFTServerInterface("FFTPackServer using extended FFTPack (C-version) package")
|
---|
[710] | 72 |
|
---|
| 73 | {
|
---|
| 74 | sz_rfft = 0; //the working array and its size for the different
|
---|
| 75 | ws_rfft = NULL; //possible numerical types
|
---|
| 76 | sz_cfft = 0;
|
---|
| 77 | ws_cfft = NULL;
|
---|
| 78 | sz_cdfft = 0;
|
---|
| 79 | ws_cdfft = NULL;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | FFTPackServer::~FFTPackServer()
|
---|
| 83 | {
|
---|
| 84 | if (ws_rfft) delete[] ws_rfft;
|
---|
| 85 | if (ws_cfft) delete[] ws_cfft;
|
---|
| 86 | if (ws_cdfft) delete[] ws_cdfft;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | FFTServerInterface * FFTPackServer::Clone()
|
---|
| 90 | {
|
---|
| 91 | return (new FFTPackServer);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[717] | 94 |
|
---|
[710] | 95 | void FFTPackServer::FFTForward(TVector< complex<r_8> > const & in, TVector< complex<r_8> > & out)
|
---|
| 96 | {
|
---|
| 97 | out = in;
|
---|
| 98 | fftf(out.NElts(), out.Data());
|
---|
[717] | 99 | if (getNormalize()) out *= (1./(r_8)(in.NElts()));
|
---|
[710] | 100 | }
|
---|
| 101 |
|
---|
| 102 | void FFTPackServer::FFTBackward(TVector< complex<r_8> > const & in, TVector< complex<r_8> > & out)
|
---|
| 103 | {
|
---|
| 104 | out = in;
|
---|
| 105 | fftb(out.NElts(), out.Data());
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[717] | 108 |
|
---|
| 109 |
|
---|
[710] | 110 | void FFTPackServer::FFTForward(TVector< complex<r_4> > const & in, TVector< complex<r_4> > & out)
|
---|
| 111 | {
|
---|
| 112 | out = in;
|
---|
| 113 | fftf(out.NElts(), out.Data());
|
---|
[717] | 114 | if (getNormalize()) out *= (1./(r_4)(in.NElts()));
|
---|
[710] | 115 | }
|
---|
| 116 |
|
---|
| 117 | void FFTPackServer::FFTBackward(TVector< complex<r_4> > const & in, TVector< complex<r_4> > & out)
|
---|
| 118 | {
|
---|
| 119 | out = in;
|
---|
| 120 | fftb(out.NElts(), out.Data());
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | void FFTPackServer::FFTForward(TVector< r_4 > const & in, TVector< complex<r_4> > & out)
|
---|
| 124 | {
|
---|
| 125 | TVector< r_4 > inout(in);
|
---|
| 126 | fftf(inout.NElts(), inout.Data());
|
---|
| 127 | ReShapetoCompl(inout, out);
|
---|
[717] | 128 | if (getNormalize()) out *= (1./(r_4)(in.NElts()));
|
---|
[710] | 129 | }
|
---|
| 130 |
|
---|
| 131 | void FFTPackServer::FFTBackward(TVector< complex<r_4> > const & in, TVector< r_4 > & out)
|
---|
| 132 | {
|
---|
| 133 | ReShapetoReal(in, out);
|
---|
| 134 | fftb(out.NElts(), out.Data());
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[717] | 137 |
|
---|
[710] | 138 | void FFTPackServer::FFTForward(TVector< r_8 > const & in, TVector< complex<r_8> > & out)
|
---|
| 139 | {
|
---|
| 140 | TVector< r_8 > inout(in);
|
---|
| 141 | fftf(inout.NElts(), inout.Data());
|
---|
| 142 | ReShapetoCompl(inout, out);
|
---|
[717] | 143 | if (getNormalize()) out *= (1./(r_8)(in.NElts()));
|
---|
[710] | 144 | }
|
---|
| 145 |
|
---|
| 146 | void FFTPackServer::FFTBackward(TVector< complex<r_8> > const & in, TVector< r_8 > & out)
|
---|
| 147 | {
|
---|
| 148 | ReShapetoReal(in, out);
|
---|
| 149 | fftb(out.NElts(), out.Data());
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[717] | 152 |
|
---|
[710] | 153 |
|
---|
| 154 | void FFTPackServer::checkint_rfft(int l)
|
---|
| 155 | {
|
---|
| 156 | if (sz_rfft == l) return; //checkint functions check and reallocate
|
---|
| 157 | //memory for the work arrays when performing
|
---|
| 158 | if (ws_rfft) delete[] ws_rfft; //a transform
|
---|
| 159 | sz_rfft = l;
|
---|
[717] | 160 | ws_rfft = new r_4[2*l+15];
|
---|
[710] | 161 | rffti_(&l, ws_rfft);
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | void FFTPackServer::checkint_cfft(int l)
|
---|
| 165 | {
|
---|
| 166 | if (sz_cfft == l) return;
|
---|
| 167 |
|
---|
| 168 | if (ws_cfft) delete[] ws_cfft;
|
---|
| 169 | sz_cfft = l;
|
---|
[717] | 170 | ws_cfft = new r_4[4*l+15];
|
---|
[710] | 171 | cffti_(&l, ws_cfft);
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | void FFTPackServer::checkint_dfft(int l)
|
---|
| 175 | {
|
---|
| 176 | if (sz_dfft == l) return;
|
---|
| 177 |
|
---|
| 178 | if (ws_dfft) delete[] ws_dfft;
|
---|
| 179 | sz_dfft = l;
|
---|
[717] | 180 | ws_dfft = new r_8[2*l+15];
|
---|
[710] | 181 | dffti_(&l, ws_dfft);
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | void FFTPackServer::checkint_cdfft(int l)
|
---|
| 185 | {
|
---|
| 186 | if (sz_cdfft == l) return;
|
---|
| 187 |
|
---|
| 188 | if (ws_cdfft) delete[] ws_cdfft;
|
---|
| 189 | sz_cdfft = l;
|
---|
[717] | 190 | ws_cdfft = new r_8[4*l+15];
|
---|
[710] | 191 | cdffti_(&l, ws_cdfft);
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | /* In general forward transformations are resorted since fftpack functions
|
---|
| 195 | return inverse transformations */
|
---|
| 196 |
|
---|
[717] | 197 | void FFTPackServer::fftf(int l, r_4* inout)
|
---|
[710] | 198 | {
|
---|
| 199 | checkint_rfft(l);
|
---|
| 200 | rfftf_(&l, inout, ws_rfft);
|
---|
[717] | 201 | // for (int k= 2;k<=(l+1)/2;k++) inout[2*k-2]=-inout[2*k-2];
|
---|
[710] | 202 | }
|
---|
| 203 |
|
---|
[717] | 204 | void FFTPackServer::fftf(int l, r_8* inout)
|
---|
[710] | 205 | {
|
---|
| 206 | checkint_dfft(l);
|
---|
| 207 | dfftf_(&l, inout, ws_dfft);
|
---|
[717] | 208 | // for (int k= 2;k<=(l+1)/2;k++) inout[2*k-2]=-inout[2*k-2];
|
---|
[710] | 209 | }
|
---|
| 210 |
|
---|
[717] | 211 | void FFTPackServer::fftf(int l, complex<r_4>* inout)
|
---|
[710] | 212 | {
|
---|
| 213 | checkint_cfft(l);
|
---|
[717] | 214 | cfftf_(&l, (r_4 *)(inout), ws_cfft);
|
---|
[710] | 215 | }
|
---|
| 216 |
|
---|
[717] | 217 | void FFTPackServer::fftf(int l, complex<r_8>* inout)
|
---|
[710] | 218 | {
|
---|
| 219 | checkint_cdfft(l);
|
---|
[717] | 220 | cdfftf_(&l, (r_8*)(inout), ws_cdfft);
|
---|
[710] | 221 | }
|
---|
| 222 |
|
---|
[717] | 223 | void FFTPackServer::fftb(int l, r_4* inout)
|
---|
[710] | 224 | {
|
---|
| 225 | checkint_rfft(l);
|
---|
[717] | 226 | rfftb_(&l, inout, ws_rfft);
|
---|
[710] | 227 | }
|
---|
| 228 |
|
---|
[717] | 229 | void FFTPackServer::fftb(int l, r_8* inout)
|
---|
[710] | 230 | {
|
---|
| 231 | checkint_dfft(l);
|
---|
[717] | 232 | dfftb_(&l, inout, ws_dfft);
|
---|
[710] | 233 | }
|
---|
| 234 |
|
---|
[717] | 235 | void FFTPackServer::fftb(int l, complex<r_4>* inout)
|
---|
[710] | 236 | {
|
---|
| 237 | checkint_cfft(l);
|
---|
[717] | 238 | cfftb_(&l, (r_4 *)(inout), ws_cfft);
|
---|
[710] | 239 | }
|
---|
| 240 |
|
---|
[717] | 241 | void FFTPackServer::fftb(int l, complex<r_8>* inout)
|
---|
[710] | 242 | {
|
---|
| 243 | checkint_cdfft(l);
|
---|
[717] | 244 | cdfftb_(&l, (r_8 *)(inout), ws_cdfft);
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | // Methodes pour reordonner les donnees
|
---|
| 248 |
|
---|
| 249 | /* --Methode-- */
|
---|
| 250 | void FFTPackServer::ReShapetoReal( TVector< complex<r_8> > const & in, TVector< r_8 > & out)
|
---|
| 251 | {
|
---|
| 252 | int n = in.NElts();
|
---|
| 253 | int ncs = (fabs(in(n-1).imag()) > 1.e-12) ? ncs = 2*n-1 : ncs = n*2-2;
|
---|
| 254 | out.ReSize(ncs);
|
---|
| 255 | int k;
|
---|
| 256 | out(0) = in(0).real();
|
---|
| 257 | for(k=1;k<n-1;k++) {
|
---|
| 258 | out(2*k-1) = in(k).real();
|
---|
| 259 | out(2*k) = in(k).imag();
|
---|
[710] | 260 | }
|
---|
[717] | 261 | if (ncs == n*2-2) out(ncs-1) = in(n-1).real();
|
---|
| 262 | else { out(ncs-2) = in(n-1).real(); out(ncs-1) = in(n-1).imag(); }
|
---|
[710] | 263 | }
|
---|
| 264 |
|
---|
[717] | 265 | /* --Methode-- */
|
---|
| 266 | void FFTPackServer::ReShapetoReal( TVector< complex<r_4> > const & in, TVector< r_4 > & out)
|
---|
| 267 | {
|
---|
| 268 | int n = in.NElts();
|
---|
| 269 | int ncs = (fabs(in(n-1).imag()) > 1.e-12) ? ncs = 2*n-1 : ncs = n*2-2;
|
---|
| 270 | out.ReSize(ncs);
|
---|
| 271 | int k;
|
---|
| 272 | out(0) = in(0).real();
|
---|
| 273 | for(k=1;k<n-1;k++) {
|
---|
| 274 | out(2*k-1) = in(k).real();
|
---|
| 275 | out(2*k) = in(k).imag();
|
---|
| 276 | }
|
---|
| 277 | if (ncs == n*2-2) out(ncs-1) = in(n-1).real();
|
---|
| 278 | else { out(ncs-2) = in(n-1).real(); out(ncs-1) = in(n-1).imag(); }
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 |
|
---|
| 282 | /* --Methode-- */
|
---|
| 283 | void FFTPackServer::ReShapetoCompl(TVector< r_8 > const & in, TVector< complex<r_8> > & out)
|
---|
| 284 | {
|
---|
| 285 | uint_4 n = in.NElts();
|
---|
| 286 | uint_4 ncs = n/2+1;
|
---|
| 287 | uint_4 nc = (n%2 != 0) ? n/2+1 : n/2;
|
---|
| 288 | out.ReSize(ncs);
|
---|
| 289 | out(0) = complex<r_8> (in(0),0.);
|
---|
| 290 | int k;
|
---|
| 291 | for(int k=1;k<nc;k++)
|
---|
| 292 | out(k) = complex<r_4> (in(2*k-1), in(2*k));
|
---|
| 293 | if (n%2 == 0) out(ncs-1) = complex<r_8>(in(n-1), 0.);
|
---|
| 294 |
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | /* --Methode-- */
|
---|
| 298 | void FFTPackServer::ReShapetoCompl(TVector< r_4 > const & in, TVector< complex<r_4> > & out)
|
---|
| 299 | {
|
---|
| 300 | uint_4 n = in.NElts();
|
---|
| 301 | uint_4 ncs = n/2+1;
|
---|
| 302 | uint_4 nc = (n%2 != 0) ? n/2+1 : n/2;
|
---|
| 303 | out.ReSize(ncs);
|
---|
| 304 | out(0) = complex<r_4> (in(0),0.);
|
---|
| 305 | int k;
|
---|
| 306 | for(int k=1;k<nc;k++)
|
---|
| 307 | out(k) = complex<r_4> (in(2*k-1), in(2*k));
|
---|
| 308 | if (n%2 == 0) out(ncs-1) = complex<r_4>(in(n-1), 0.);
|
---|
| 309 | }
|
---|