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