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