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