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