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