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