[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")
|
---|
[1394] | 30 | , ckR4("FFTPackServer: ", true, true) , ckR8("FFTPackServer: ", true, true)
|
---|
[710] | 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 |
|
---|
[1390] | 58 | void FFTPackServer::FFTForward(TArray< complex<r_8> > const & in, TArray< complex<r_8> > & out)
|
---|
[710] | 59 | {
|
---|
[1390] | 60 | ckR8.CheckResize(in, out);
|
---|
[710] | 61 | out = in;
|
---|
[1390] | 62 | fftf(out.Size(), out.Data());
|
---|
| 63 | if (getNormalize()) out *= (1./(r_8)(in.Size()));
|
---|
[710] | 64 | }
|
---|
| 65 |
|
---|
[1390] | 66 | void FFTPackServer::FFTBackward(TArray< complex<r_8> > const & in, TArray< complex<r_8> > & out)
|
---|
[710] | 67 | {
|
---|
[1390] | 68 | ckR8.CheckResize(in, out);
|
---|
[710] | 69 | out = in;
|
---|
[1390] | 70 | fftb(out.Size(), out.Data());
|
---|
[710] | 71 | }
|
---|
| 72 |
|
---|
[717] | 73 |
|
---|
[1390] | 74 | void FFTPackServer::FFTForward(TArray< complex<r_4> > const & in, TArray< complex<r_4> > & out)
|
---|
[710] | 75 | {
|
---|
[1390] | 76 | ckR4.CheckResize(in, out);
|
---|
[710] | 77 | out = in;
|
---|
[1394] | 78 | cout << out << endl;
|
---|
[1390] | 79 | fftf(out.Size(), out.Data());
|
---|
| 80 | if (getNormalize()) out *= (1./(r_4)(in.Size()));
|
---|
[710] | 81 | }
|
---|
| 82 |
|
---|
[1390] | 83 | void FFTPackServer::FFTBackward(TArray< complex<r_4> > const & in, TArray< complex<r_4> > & out)
|
---|
[710] | 84 | {
|
---|
[1390] | 85 | ckR4.CheckResize(in, out);
|
---|
[710] | 86 | out = in;
|
---|
[1390] | 87 | fftb(out.Size(), out.Data());
|
---|
[710] | 88 | }
|
---|
| 89 |
|
---|
[1390] | 90 | void FFTPackServer::FFTForward(TArray< r_4 > const & in, TArray< complex<r_4> > & out)
|
---|
[710] | 91 | {
|
---|
[1390] | 92 | ckR4.CheckResize(in, out);
|
---|
| 93 | TArray< r_4 > inout(in, false);
|
---|
| 94 | fftf(inout.Size(), inout.Data());
|
---|
[1394] | 95 | ReShapetoCompl(inout, out);
|
---|
[1390] | 96 | if (getNormalize()) out *= complex<r_4>((1./(r_4)(in.Size())), 0.);
|
---|
[710] | 97 | }
|
---|
| 98 |
|
---|
[1402] | 99 | void FFTPackServer::FFTBackward(TArray< complex<r_4> > const & in, TArray< r_4 > & out,
|
---|
| 100 | bool usoutsz)
|
---|
[710] | 101 | {
|
---|
[1402] | 102 | ckR4.CheckResize(in, out, usoutsz);
|
---|
[1394] | 103 | ReShapetoReal(in, out);
|
---|
[1390] | 104 | fftb(out.Size(), out.Data());
|
---|
[710] | 105 | }
|
---|
| 106 |
|
---|
[717] | 107 |
|
---|
[1390] | 108 | void FFTPackServer::FFTForward(TArray< r_8 > const & in, TArray< complex<r_8> > & out)
|
---|
[710] | 109 | {
|
---|
[1390] | 110 | ckR8.CheckResize(in, out);
|
---|
| 111 | TArray< r_8 > inout(in, false);
|
---|
| 112 | fftf(inout.Size(), inout.Data());
|
---|
[1394] | 113 | ReShapetoCompl(inout, out);
|
---|
[1390] | 114 | if (getNormalize()) out *= complex<r_8>((1./(r_8)(in.Size())), 0.);
|
---|
[710] | 115 | }
|
---|
| 116 |
|
---|
[1402] | 117 | void FFTPackServer::FFTBackward(TArray< complex<r_8> > const & in, TArray< r_8 > & out,
|
---|
| 118 | bool usoutsz)
|
---|
[710] | 119 | {
|
---|
[1402] | 120 | ckR8.CheckResize(in, out, usoutsz);
|
---|
[1394] | 121 | ReShapetoReal(in, out);
|
---|
[1390] | 122 | fftb(out.Size(), out.Data());
|
---|
[710] | 123 | }
|
---|
| 124 |
|
---|
| 125 |
|
---|
[1394] | 126 | template <class T>
|
---|
| 127 | void FFTPack_ReShapetoReal(TArray< complex<T> > const & ina, TArray< T > & outa)
|
---|
| 128 | {
|
---|
| 129 | TVector< complex<T> > in(ina);
|
---|
| 130 | TVector< T > out(outa);
|
---|
| 131 | sa_size_t n = in.NElts();
|
---|
| 132 | T thr = FFTArrayChecker<T>::ZeroThreshold();
|
---|
| 133 | sa_size_t ncs = ( (in(n-1).imag() < -thr) || (in(n-1).imag() > thr) ) ?
|
---|
| 134 | ncs = 2*n-1 : ncs = 2*n-2;
|
---|
| 135 |
|
---|
[1400] | 136 | if (out.NElts() != ncs) {
|
---|
| 137 | cerr << "DEBUG-FFTPack_ReShapetoReal() ncs = " << ncs
|
---|
| 138 | << " out.NElts()= " << out.NElts() << endl;
|
---|
[1394] | 139 | throw SzMismatchError("FFTPack_ReShapetoReal() - Wrong output array size !");
|
---|
[1400] | 140 | }
|
---|
[1394] | 141 |
|
---|
| 142 | sa_size_t k;
|
---|
| 143 |
|
---|
| 144 | out(0) = in(0).real();
|
---|
| 145 | for(k=1;k<n-1;k++) {
|
---|
| 146 | out(2*k-1) = in(k).real();
|
---|
| 147 | out(2*k) = in(k).imag();
|
---|
| 148 | }
|
---|
| 149 | if (ncs == n*2-2) out(ncs-1) = in(n-1).real();
|
---|
| 150 | else { out(ncs-2) = in(n-1).real(); out(ncs-1) = in(n-1).imag(); }
|
---|
| 151 |
|
---|
| 152 | return;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | template <class T>
|
---|
| 156 | void FFTPack_ReShapetoCompl(TArray< T > const & ina, TArray< complex<T> > & outa)
|
---|
| 157 | {
|
---|
| 158 | TVector< T > in(ina);
|
---|
| 159 | TVector< complex<T> > out(outa);
|
---|
| 160 | sa_size_t n = in.NElts();
|
---|
| 161 | sa_size_t ncs = n/2+1;
|
---|
| 162 | sa_size_t nc = (n%2 != 0) ? n/2+1 : n/2;
|
---|
[1400] | 163 | if (out.NElts() != ncs) {
|
---|
| 164 | cerr << "DBG-ReShapetoCompl() ncs=" << ncs
|
---|
| 165 | << " out.NElts()= " << out.NElts() << endl;
|
---|
[1394] | 166 | throw SzMismatchError("FFTPack_ReShapetoCompl() - Wrong output array size !");
|
---|
[1400] | 167 | }
|
---|
[1394] | 168 | out(0) = complex<T> (in(0),0.);
|
---|
| 169 | for(int k=1;k<nc;k++)
|
---|
| 170 | out(k) = complex<r_4> (in(2*k-1), in(2*k));
|
---|
| 171 | if (n%2 == 0) out(ncs-1) = complex<T>(in(n-1), 0.);
|
---|
| 172 |
|
---|
| 173 | return;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | void FFTPackServer::ReShapetoReal(TArray< complex<r_8> > const & in, TArray< r_8 > & out)
|
---|
| 177 | {
|
---|
| 178 | FFTPack_ReShapetoReal<r_8>(in, out);
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | void FFTPackServer::ReShapetoCompl(TArray< r_8 > const & in, TArray< complex<r_8> > & out)
|
---|
| 182 | {
|
---|
| 183 | FFTPack_ReShapetoCompl<r_8>(in, out);
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | void FFTPackServer::ReShapetoReal(TArray< complex<r_4> > const & in, TArray< r_4 > & out)
|
---|
| 187 | {
|
---|
| 188 | FFTPack_ReShapetoReal<r_4>(in, out);
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | void FFTPackServer::ReShapetoCompl(TArray< r_4 > const & in, TArray< complex<r_4> > & out)
|
---|
| 192 | {
|
---|
| 193 | FFTPack_ReShapetoCompl<r_4>(in, out);
|
---|
| 194 | }
|
---|
| 195 |
|
---|
[791] | 196 | void FFTPackServer::checkint_rfft(int_4 l)
|
---|
[710] | 197 | {
|
---|
| 198 | if (sz_rfft == l) return; //checkint functions check and reallocate
|
---|
| 199 | //memory for the work arrays when performing
|
---|
| 200 | if (ws_rfft) delete[] ws_rfft; //a transform
|
---|
| 201 | sz_rfft = l;
|
---|
[717] | 202 | ws_rfft = new r_4[2*l+15];
|
---|
[710] | 203 | rffti_(&l, ws_rfft);
|
---|
| 204 | }
|
---|
| 205 |
|
---|
[791] | 206 | void FFTPackServer::checkint_cfft(int_4 l)
|
---|
[710] | 207 | {
|
---|
| 208 | if (sz_cfft == l) return;
|
---|
| 209 |
|
---|
| 210 | if (ws_cfft) delete[] ws_cfft;
|
---|
| 211 | sz_cfft = l;
|
---|
[717] | 212 | ws_cfft = new r_4[4*l+15];
|
---|
[710] | 213 | cffti_(&l, ws_cfft);
|
---|
| 214 | }
|
---|
| 215 |
|
---|
[791] | 216 | void FFTPackServer::checkint_dfft(int_4 l)
|
---|
[710] | 217 | {
|
---|
| 218 | if (sz_dfft == l) return;
|
---|
| 219 |
|
---|
| 220 | if (ws_dfft) delete[] ws_dfft;
|
---|
| 221 | sz_dfft = l;
|
---|
[717] | 222 | ws_dfft = new r_8[2*l+15];
|
---|
[710] | 223 | dffti_(&l, ws_dfft);
|
---|
| 224 | }
|
---|
| 225 |
|
---|
[791] | 226 | void FFTPackServer::checkint_cdfft(int_4 l)
|
---|
[710] | 227 | {
|
---|
| 228 | if (sz_cdfft == l) return;
|
---|
| 229 |
|
---|
| 230 | if (ws_cdfft) delete[] ws_cdfft;
|
---|
| 231 | sz_cdfft = l;
|
---|
[717] | 232 | ws_cdfft = new r_8[4*l+15];
|
---|
[710] | 233 | cdffti_(&l, ws_cdfft);
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | /* In general forward transformations are resorted since fftpack functions
|
---|
| 237 | return inverse transformations */
|
---|
| 238 |
|
---|
[791] | 239 | void FFTPackServer::fftf(int_4 l, r_4* inout)
|
---|
[710] | 240 | {
|
---|
| 241 | checkint_rfft(l);
|
---|
| 242 | rfftf_(&l, inout, ws_rfft);
|
---|
[717] | 243 | // for (int k= 2;k<=(l+1)/2;k++) inout[2*k-2]=-inout[2*k-2];
|
---|
[710] | 244 | }
|
---|
| 245 |
|
---|
[791] | 246 | void FFTPackServer::fftf(int_4 l, r_8* inout)
|
---|
[710] | 247 | {
|
---|
| 248 | checkint_dfft(l);
|
---|
| 249 | dfftf_(&l, inout, ws_dfft);
|
---|
[717] | 250 | // for (int k= 2;k<=(l+1)/2;k++) inout[2*k-2]=-inout[2*k-2];
|
---|
[710] | 251 | }
|
---|
| 252 |
|
---|
[791] | 253 | void FFTPackServer::fftf(int_4 l, complex<r_4>* inout)
|
---|
[710] | 254 | {
|
---|
| 255 | checkint_cfft(l);
|
---|
[717] | 256 | cfftf_(&l, (r_4 *)(inout), ws_cfft);
|
---|
[710] | 257 | }
|
---|
| 258 |
|
---|
[791] | 259 | void FFTPackServer::fftf(int_4 l, complex<r_8>* inout)
|
---|
[710] | 260 | {
|
---|
| 261 | checkint_cdfft(l);
|
---|
[717] | 262 | cdfftf_(&l, (r_8*)(inout), ws_cdfft);
|
---|
[710] | 263 | }
|
---|
| 264 |
|
---|
[791] | 265 | void FFTPackServer::fftb(int_4 l, r_4* inout)
|
---|
[710] | 266 | {
|
---|
| 267 | checkint_rfft(l);
|
---|
[717] | 268 | rfftb_(&l, inout, ws_rfft);
|
---|
[710] | 269 | }
|
---|
| 270 |
|
---|
[791] | 271 | void FFTPackServer::fftb(int_4 l, r_8* inout)
|
---|
[710] | 272 | {
|
---|
| 273 | checkint_dfft(l);
|
---|
[717] | 274 | dfftb_(&l, inout, ws_dfft);
|
---|
[710] | 275 | }
|
---|
| 276 |
|
---|
[791] | 277 | void FFTPackServer::fftb(int_4 l, complex<r_4>* inout)
|
---|
[710] | 278 | {
|
---|
| 279 | checkint_cfft(l);
|
---|
[717] | 280 | cfftb_(&l, (r_4 *)(inout), ws_cfft);
|
---|
[710] | 281 | }
|
---|
| 282 |
|
---|
[791] | 283 | void FFTPackServer::fftb(int_4 l, complex<r_8>* inout)
|
---|
[710] | 284 | {
|
---|
| 285 | checkint_cdfft(l);
|
---|
[717] | 286 | cdfftb_(&l, (r_8 *)(inout), ws_cdfft);
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 |
|
---|