source: Sophya/trunk/SophyaLib/NTools/fftservintf.cc@ 1652

Last change on this file since 1652 was 1652, checked in by cmv, 24 years ago

bug ecrit a=() ? a=blabla: a=blibli; cmv 21/9/01

File size: 12.0 KB
Line 
1#include "fftservintf.h"
2
3
4/*!
5 \class SOPHYA::FFTServerInterface
6 \ingroup NTools
7 Defines the interface for FFT (Fast Fourier Transform) operations.
8 Definitions :
9 - Sampling period \b T
10 - Sampling frequency \b fs=1/T
11 - Total number of samples \b N
12 - Frequency step in Fourier space \b =fs/N=1/(N*T)
13 - Component frequencies
14 - k=0 -> 0
15 - k=1 -> 1/(N*T)
16 - k -> k/(N*T)
17 - k=N/2 -> 1/(2*T) (Nyquist frequency)
18 - k>N/2 -> k/(N*T) (or negative frequency -(N-k)/(N*T))
19
20 For a sampling period T=1, the computed Fourier components correspond to :
21 \verbatim
22 0 1/N 2/N ... 1/2 1/2+1/N 1/2+2/N ... 1-2/N 1-1/N
23 0 1/N 2/N ... 1/2 ... -2/N -1/N
24 \endverbatim
25
26 For complex one-dimensional transforms:
27 \f[
28 out(i) = F_{norm} \Sigma_{j} \ e^{-2 \pi \sqrt{-1} \ i \ j} \ {\rm (forward)}
29 \f]
30 \f[
31 out(i) = F_{norm} \Sigma_{j} \ e^{2 \pi \sqrt{-1} \ i \ j} \ {\rm (backward)}
32 \f]
33 i,j= 0..N-1 , where N is the input or the output array size.
34
35 For complex multi-dimensional transforms:
36 \f[
37 out(i1,i2,...,id) = F_{norm} \Sigma_{j1} \Sigma_{j2} ... \Sigma_{jd} \
38 e^{-2 \pi \sqrt{-1} \ i1 \ j1} ... e^{-2 \pi \sqrt{-1} \ id \ jd} \ {\rm (forward)}
39 \f]
40 \f[
41 out(i1,i2,...,id) = F_{norm} \Sigma_{j1} \Sigma_{j2} ... \Sigma_{jd} \
42 e^{2 \pi \sqrt{-1} \ i1 \ j1} ... e^{2 \pi \sqrt{-1} \ id \ jd} \ {\rm (backward)}
43 \f]
44
45 For real forward transforms, the input array is real, and
46 the output array complex, with Fourier components up to k=N/2.
47 For real backward transforms, the input array is complex and
48 the output array is real.
49*/
50
51/* --Methode-- */
52FFTServerInterface::FFTServerInterface(string info)
53{
54 _info = info;
55 _fgnorm = true;
56}
57
58/* --Methode-- */
59FFTServerInterface::~FFTServerInterface()
60{
61}
62
63// ----------------- Transforme pour les double -------------------
64
65/* --Methode-- */
66//! Forward Fourier transform for double precision complex data
67/*!
68 \param in : Input complex array
69 \param out : Output complex array
70 */
71void FFTServerInterface::FFTForward(TArray< complex<r_8> > const &, TArray< complex<r_8> > &)
72{
73 throw NotAvailableOperation("FFTServer::FFTForward(TArray...) Unsupported operation !");
74}
75
76/* --Methode-- */
77//! Backward (inverse) Fourier transform for double precision complex data
78/*!
79 \param in : Input complex array
80 \param out : Output complex array
81 */
82void FFTServerInterface::FFTBackward(TArray< complex<r_8> > const &, TArray< complex<r_8> > &)
83{
84 throw NotAvailableOperation("FFTServer::FFTBackward(TArray...) Unsupported operation !");
85}
86
87/* --Methode-- */
88//! Forward Fourier transform for double precision real input data
89/*!
90 \param in : Input real array
91 \param out : Output complex array
92 */
93void FFTServerInterface::FFTForward(TArray< r_8 > const &, TArray< complex<r_8> > &)
94{
95 throw NotAvailableOperation("FFTServer::FFTForward(TArray...) Unsupported operation !");
96}
97
98/* --Methode-- */
99//! Backward (inverse) Fourier transform for double precision real output data
100/*!
101 \param in : Input complex array
102 \param out : Output real array
103 \param usoutsz : if true, use the output array size for computing the inverse FFT.
104 */
105void FFTServerInterface::FFTBackward(TArray< complex<r_8> > const &, TArray< r_8 > &, bool)
106{
107 throw NotAvailableOperation("FFTServer::FFTBackward(TArray...) Unsupported operation !");
108}
109
110
111// ----------------- Transforme pour les float -------------------
112
113/* --Methode-- */
114//! Forward Fourier transform for complex data
115/*!
116 \param in : Input complex array
117 \param out : Output complex array
118 */
119void FFTServerInterface::FFTForward(TArray< complex<r_4> > const &, TArray< complex<r_4> > &)
120{
121 throw NotAvailableOperation("FFTServer::FFTForward(TArray r_4 ... ) Unsupported operation !");
122}
123
124/* --Methode-- */
125//! Backward (inverse) Fourier transform for complex data
126/*!
127 \param in : Input complex array
128 \param out : Output complex array
129 */
130void FFTServerInterface::FFTBackward(TArray< complex<r_4> > const &, TArray< complex<r_4> > &)
131{
132 throw NotAvailableOperation("FFTServer::FFTBackward(TArray r_4 ... ) Unsupported operation !");
133}
134
135/* --Methode-- */
136//! Forward Fourier transform for real input data
137/*!
138 \param in : Input real array
139 \param out : Output complex array
140 */
141void FFTServerInterface::FFTForward(TArray< r_4 > const &, TArray< complex<r_4> > &)
142{
143 throw NotAvailableOperation("FFTServer::FFTForward(TArray r_4 ... ) Unsupported operation !");
144}
145
146/* --Methode-- */
147//! Backward (inverse) Fourier transform for real output data
148/*!
149 \param in : Input complex array
150 \param out : Output real array
151 \param usoutsz : if true, use the output array size for computing the inverse FFT.
152 */
153void FFTServerInterface::FFTBackward(TArray< complex<r_4> > const &, TArray< r_4 > &, bool)
154{
155 throw NotAvailableOperation("FFTServer::FFTBackward(TArray r_4 ... ) Unsupported operation !");
156}
157
158
159// ----------------- Transformation de normalisation pour les energies -------------------
160
161/* --Methode-- */
162//! Compute the transform to be applied to "fftx=FFT(x)" so that "x" and "FFT(x)" have the same energy
163/*!
164\return The factor to be applied to the FFT energy such that we get the same energy as for the x.
165\verbatim
166 fftx is computed by: FFTForward(x,fftx)
167 Energy of x : Ex = sum{|x(i)|^2} i=0,x.Size()-1
168 Energy of fftx : Efftx = sum{|fftx(i)^2|} i=0,fftx.Size()-1
169 ( usually x.Size() != fftx.Size() )
170 -------------------------------------------------------------------
171 | TransfEnergyFFT return A and B such that : Ex = A * Efftx + B |
172 | and Norm such that : Ex = Norm * Efftx |
173 -------------------------------------------------------------------
174 To normalize the fftx vector apply : "fftx *= sqrt(Norm)"
175\endverbatim
176*/
177r_8 FFTServerInterface::TransfEnergyFFT
178 (TVector< complex<r_8> > const& x, TVector< complex<r_8> > const& fftx, r_8& A, r_8& B)
179{
180 B=0.;
181 if(getNormalize()) A = x.Size(); else A = 1./x.Size();
182 r_8 norm = A;
183 return norm;
184}
185
186//! Compute the transform to be applied to "fftx=FFT(x)" so that "x" and "FFT(x)" have the same energy
187r_8 FFTServerInterface::TransfEnergyFFT
188 (TVector< complex<r_4> > const & x, TVector< complex<r_4> > const & fftx, r_8& A, r_8& B)
189{
190 B=0.;
191 if(getNormalize()) A = x.Size(); else A = 1./x.Size();
192 r_8 norm = A;
193 return norm;
194}
195
196//! Compute the transform to be applied to "fftx=FFT(x)" so that "x" and "FFT(x)" have the same energy
197r_8 FFTServerInterface::TransfEnergyFFT
198 (TVector< r_8 > const & x, TVector< complex<r_8> > const & fftx, r_8& A, r_8& B)
199{
200 A= 2.;
201 B= - abs(fftx(0)*fftx(0));
202 if(x.Size()%2 == 0) B -= abs(fftx(fftx.Size()-1)*fftx(fftx.Size()-1));
203 if(getNormalize()) {A *= x.Size(); B *= x.Size();}
204 else {A /= x.Size(); B /= x.Size();}
205 r_8 norm = 0.;
206 for(int_4 i=0;i<fftx.Size();i++) norm += abs(fftx(i)*fftx(i));
207 if(norm>0.) norm = (A*norm+B)/norm;
208 return norm;
209}
210
211//! Compute the transform to be applied to "fftx=FFT(x)" so that "x" and "FFT(x)" have the same energy
212r_8 FFTServerInterface::TransfEnergyFFT
213 (TVector< r_4 > const & x, TVector< complex<r_4> > const & fftx, r_8& A, r_8& B)
214{
215 A= 2.;
216 B= - abs(fftx(0)*fftx(0));
217 if(x.Size()%2 == 0) B -= abs(fftx(fftx.Size()-1)*fftx(fftx.Size()-1));
218 if(getNormalize()) {A *= x.Size(); B *= x.Size();}
219 else {A /= x.Size(); B /= x.Size();}
220 r_8 norm = 0.;
221 for(int_4 i=0;i<fftx.Size();i++) norm += abs(fftx(i)*fftx(i));
222 if(norm>0.) norm = (A*norm+B)/norm;
223 return norm;
224}
225
226
227/* --Methode-- */
228/*!
229 \class SOPHYA::FFTArrayChecker
230 \ingroup NTools
231 Service class for checking array size and resizing output arrays,
232 to be used by FFTServer classes
233*/
234
235template <class T>
236FFTArrayChecker<T>::FFTArrayChecker(string msg, bool checkpack, bool onedonly)
237{
238 _msg = msg + " FFTArrayChecker::";
239 _checkpack = checkpack;
240 _onedonly = onedonly;
241}
242
243/* --Methode-- */
244template <class T>
245FFTArrayChecker<T>::~FFTArrayChecker()
246{
247}
248
249template <class T>
250T FFTArrayChecker<T>::ZeroThreshold()
251{
252 return(0);
253}
254
255r_8 FFTArrayChecker< r_8 >::ZeroThreshold()
256{
257 return(1.e-18);
258}
259
260r_4 FFTArrayChecker< r_4 >::ZeroThreshold()
261{
262 return(1.e-9);
263}
264
265/* --Methode-- */
266template <class T>
267int FFTArrayChecker<T>::CheckResize(TArray< complex<T> > const & in, TArray< complex<T> > & out)
268{
269 int k;
270 string msg;
271 if (in.Size() < 1) {
272 msg = _msg + "CheckResize(complex in, complex out) - Unallocated input array !";
273 throw(SzMismatchError(msg));
274 }
275 if (_checkpack)
276 if ( !in.IsPacked() ) {
277 msg = _msg + "CheckResize(complex in, complex out) - Not packed input array !";
278 throw(SzMismatchError(msg));
279 }
280 int ndg1 = 0;
281 for(k=0; k<in.NbDimensions(); k++)
282 if (in.Size(k) > 1) ndg1++;
283 if (_onedonly)
284 if (ndg1 > 1) {
285 msg = _msg + "CheckResize(complex in, complex out) - Only 1-D array accepted !";
286 throw(SzMismatchError(msg));
287 }
288 out.ReSize(in);
289 // sa_size_t sz[BASEARRAY_MAXNDIMS];
290 // for(k=0; k<in.NbDimensions(); k++)
291 // sz[k] = in.Size(k);
292 // out.ReSize(in.NbDimensions(), sz);
293
294 return(ndg1);
295}
296
297/* --Methode-- */
298template <class T>
299int FFTArrayChecker<T>::CheckResize(TArray< T > const & in, TArray< complex<T> > & out)
300{
301 int k;
302 string msg;
303 if (in.Size() < 1) {
304 msg = _msg + "CheckResize(real in, complex out) - Unallocated input array !";
305 throw(SzMismatchError(msg));
306 }
307 if (_checkpack)
308 if ( !in.IsPacked() ) {
309 msg = _msg + "CheckResize(real in, complex out) - Not packed input array !";
310 throw(SzMismatchError(msg));
311 }
312 int ndg1 = 0;
313 for(k=0; k<in.NbDimensions(); k++)
314 if (in.Size(k) > 1) ndg1++;
315 if (_onedonly)
316 if (ndg1 > 1) {
317 msg = _msg + "CheckResize(real in, complex out) - Only 1-D array accepted !";
318 throw(SzMismatchError(msg));
319 }
320 sa_size_t sz[BASEARRAY_MAXNDIMS];
321 //
322 if (ndg1 > 1) {
323 sz[0] = in.Size(0)/2+1;
324 for(k=1; k<in.NbDimensions(); k++)
325 sz[k] = in.Size(k);
326 }
327 else {
328 for(k=0; k<BASEARRAY_MAXNDIMS; k++) sz[k] = 1;
329 sz[in.MaxSizeKA()] = in.Size(in.MaxSizeKA())/2+1;
330 // sz[k] = in.Size(k)/2+1;
331 // sz[k] = (in.Size(k)%2 != 0) ? in.Size(k)/2+1 : in.Size(k)/2;
332 }
333 out.ReSize(in.NbDimensions(), sz);
334
335 return(ndg1);
336}
337
338/* --Methode-- */
339template <class T>
340int FFTArrayChecker<T>::CheckResize(TArray< complex<T> > const & in, TArray< T > & out,
341 bool usoutsz)
342{
343 int k;
344 string msg;
345 if (in.Size() < 1) {
346 msg = _msg + "CheckResize(complex in, real out) - Unallocated input array !";
347 throw(SzMismatchError(msg));
348 }
349 if (_checkpack)
350 if ( !in.IsPacked() ) {
351 msg = _msg + "CheckResize(complex in, real out) - Not packed input array !";
352 throw(SzMismatchError(msg));
353 }
354 int ndg1 = 0;
355 for(k=0; k<in.NbDimensions(); k++)
356 if (in.Size(k) > 1) ndg1++;
357 if (_onedonly)
358 if (ndg1 > 1) {
359 msg = _msg + "CheckResize(complex in, real out) - Only 1-D array accepted !";
360 throw(SzMismatchError(msg));
361 }
362 if (usoutsz) { // We have to use output array size
363 bool fgerr = false;
364 if (ndg1 > 1) {
365 if (in.Size(0) != out.Size(0)/2+1) fgerr = true;
366 }
367 else {
368 if (in.Size(in.MaxSizeKA()) != out.Size(in.MaxSizeKA())/2+1) fgerr = true;
369 }
370 if (fgerr) {
371 msg = _msg + "CheckResize(complex in, real out) - Incompatible in-out sizes !";
372 throw(SzMismatchError(msg));
373 }
374 }
375 else { // We have to resize the output array
376 sa_size_t sz[BASEARRAY_MAXNDIMS];
377 if (ndg1 > 1) {
378 sz[0] = 2*in.Size(0)-1;
379 for(k=1; k<in.NbDimensions(); k++)
380 sz[k] = in.Size(k);
381 // sz[k] = in.Size(k)*2-1;
382 }
383 else {
384 for(k=0; k<BASEARRAY_MAXNDIMS; k++) sz[k] = 1;
385 T thr = ZeroThreshold();
386 sa_size_t n = in.Size(in.MaxSizeKA());
387 sa_size_t ncs = ( (in[n-1].imag() < -thr) || (in[n-1].imag() > thr) )
388 ? 2*n-1 : 2*n-2;
389 sz[in.MaxSizeKA()] = ncs;
390 }
391 out.ReSize(in.NbDimensions(), sz);
392 }
393
394 return(ndg1);
395
396}
397
398
399#ifdef __CXX_PRAGMA_TEMPLATES__
400#pragma define_template FFTArrayChecker<r_4>
401#pragma define_template FFTArrayChecker<r_8>
402#endif
403
404#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
405template class FFTArrayChecker<r_4>;
406template class FFTArrayChecker<r_8>;
407#endif
Note: See TracBrowser for help on using the repository browser.