[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")
|
---|
[1390] | 30 | , ckR4(true, true) , ckR8(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 |
|
---|
| 74 |
|
---|
[1390] | 75 | void FFTPackServer::FFTForward(TArray< complex<r_4> > const & in, TArray< complex<r_4> > & out)
|
---|
[710] | 76 | {
|
---|
[1390] | 77 | ckR4.CheckResize(in, out);
|
---|
[710] | 78 | out = in;
|
---|
[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());
|
---|
| 95 | ckR4.ReShapetoCompl(inout, out);
|
---|
| 96 | if (getNormalize()) out *= complex<r_4>((1./(r_4)(in.Size())), 0.);
|
---|
[710] | 97 | }
|
---|
| 98 |
|
---|
[1390] | 99 | void FFTPackServer::FFTBackward(TArray< complex<r_4> > const & in, TArray< r_4 > & out)
|
---|
[710] | 100 | {
|
---|
[1390] | 101 | ckR4.CheckResize(in, out);
|
---|
| 102 | ckR4.ReShapetoReal(in, out);
|
---|
| 103 | fftb(out.Size(), out.Data());
|
---|
[710] | 104 | }
|
---|
| 105 |
|
---|
[717] | 106 |
|
---|
[1390] | 107 | void FFTPackServer::FFTForward(TArray< r_8 > const & in, TArray< complex<r_8> > & out)
|
---|
[710] | 108 | {
|
---|
[1390] | 109 | ckR8.CheckResize(in, out);
|
---|
| 110 | TArray< r_8 > inout(in, false);
|
---|
| 111 | fftf(inout.Size(), inout.Data());
|
---|
| 112 | ckR8.ReShapetoCompl(inout, out);
|
---|
| 113 | if (getNormalize()) out *= complex<r_8>((1./(r_8)(in.Size())), 0.);
|
---|
[710] | 114 | }
|
---|
| 115 |
|
---|
[1390] | 116 | void FFTPackServer::FFTBackward(TArray< complex<r_8> > const & in, TArray< r_8 > & out)
|
---|
[710] | 117 | {
|
---|
[1390] | 118 | ckR8.CheckResize(in, out);
|
---|
| 119 | ckR8.ReShapetoReal(in, out);
|
---|
| 120 | fftb(out.Size(), out.Data());
|
---|
[710] | 121 | }
|
---|
| 122 |
|
---|
[717] | 123 |
|
---|
[710] | 124 |
|
---|
[791] | 125 | void FFTPackServer::checkint_rfft(int_4 l)
|
---|
[710] | 126 | {
|
---|
| 127 | if (sz_rfft == l) return; //checkint functions check and reallocate
|
---|
| 128 | //memory for the work arrays when performing
|
---|
| 129 | if (ws_rfft) delete[] ws_rfft; //a transform
|
---|
| 130 | sz_rfft = l;
|
---|
[717] | 131 | ws_rfft = new r_4[2*l+15];
|
---|
[710] | 132 | rffti_(&l, ws_rfft);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[791] | 135 | void FFTPackServer::checkint_cfft(int_4 l)
|
---|
[710] | 136 | {
|
---|
| 137 | if (sz_cfft == l) return;
|
---|
| 138 |
|
---|
| 139 | if (ws_cfft) delete[] ws_cfft;
|
---|
| 140 | sz_cfft = l;
|
---|
[717] | 141 | ws_cfft = new r_4[4*l+15];
|
---|
[710] | 142 | cffti_(&l, ws_cfft);
|
---|
| 143 | }
|
---|
| 144 |
|
---|
[791] | 145 | void FFTPackServer::checkint_dfft(int_4 l)
|
---|
[710] | 146 | {
|
---|
| 147 | if (sz_dfft == l) return;
|
---|
| 148 |
|
---|
| 149 | if (ws_dfft) delete[] ws_dfft;
|
---|
| 150 | sz_dfft = l;
|
---|
[717] | 151 | ws_dfft = new r_8[2*l+15];
|
---|
[710] | 152 | dffti_(&l, ws_dfft);
|
---|
| 153 | }
|
---|
| 154 |
|
---|
[791] | 155 | void FFTPackServer::checkint_cdfft(int_4 l)
|
---|
[710] | 156 | {
|
---|
| 157 | if (sz_cdfft == l) return;
|
---|
| 158 |
|
---|
| 159 | if (ws_cdfft) delete[] ws_cdfft;
|
---|
| 160 | sz_cdfft = l;
|
---|
[717] | 161 | ws_cdfft = new r_8[4*l+15];
|
---|
[710] | 162 | cdffti_(&l, ws_cdfft);
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | /* In general forward transformations are resorted since fftpack functions
|
---|
| 166 | return inverse transformations */
|
---|
| 167 |
|
---|
[791] | 168 | void FFTPackServer::fftf(int_4 l, r_4* inout)
|
---|
[710] | 169 | {
|
---|
| 170 | checkint_rfft(l);
|
---|
| 171 | rfftf_(&l, inout, ws_rfft);
|
---|
[717] | 172 | // for (int k= 2;k<=(l+1)/2;k++) inout[2*k-2]=-inout[2*k-2];
|
---|
[710] | 173 | }
|
---|
| 174 |
|
---|
[791] | 175 | void FFTPackServer::fftf(int_4 l, r_8* inout)
|
---|
[710] | 176 | {
|
---|
| 177 | checkint_dfft(l);
|
---|
| 178 | dfftf_(&l, inout, ws_dfft);
|
---|
[717] | 179 | // for (int k= 2;k<=(l+1)/2;k++) inout[2*k-2]=-inout[2*k-2];
|
---|
[710] | 180 | }
|
---|
| 181 |
|
---|
[791] | 182 | void FFTPackServer::fftf(int_4 l, complex<r_4>* inout)
|
---|
[710] | 183 | {
|
---|
| 184 | checkint_cfft(l);
|
---|
[717] | 185 | cfftf_(&l, (r_4 *)(inout), ws_cfft);
|
---|
[710] | 186 | }
|
---|
| 187 |
|
---|
[791] | 188 | void FFTPackServer::fftf(int_4 l, complex<r_8>* inout)
|
---|
[710] | 189 | {
|
---|
| 190 | checkint_cdfft(l);
|
---|
[717] | 191 | cdfftf_(&l, (r_8*)(inout), ws_cdfft);
|
---|
[710] | 192 | }
|
---|
| 193 |
|
---|
[791] | 194 | void FFTPackServer::fftb(int_4 l, r_4* inout)
|
---|
[710] | 195 | {
|
---|
| 196 | checkint_rfft(l);
|
---|
[717] | 197 | rfftb_(&l, inout, ws_rfft);
|
---|
[710] | 198 | }
|
---|
| 199 |
|
---|
[791] | 200 | void FFTPackServer::fftb(int_4 l, r_8* inout)
|
---|
[710] | 201 | {
|
---|
| 202 | checkint_dfft(l);
|
---|
[717] | 203 | dfftb_(&l, inout, ws_dfft);
|
---|
[710] | 204 | }
|
---|
| 205 |
|
---|
[791] | 206 | void FFTPackServer::fftb(int_4 l, complex<r_4>* inout)
|
---|
[710] | 207 | {
|
---|
| 208 | checkint_cfft(l);
|
---|
[717] | 209 | cfftb_(&l, (r_4 *)(inout), ws_cfft);
|
---|
[710] | 210 | }
|
---|
| 211 |
|
---|
[791] | 212 | void FFTPackServer::fftb(int_4 l, complex<r_8>* inout)
|
---|
[710] | 213 | {
|
---|
| 214 | checkint_cdfft(l);
|
---|
[717] | 215 | cdfftb_(&l, (r_8 *)(inout), ws_cdfft);
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 |
|
---|