casacore
Loading...
Searching...
No Matches
FFTServer.h
Go to the documentation of this file.
1//# FFTServer.h: A class with methods for Fast Fourier Transforms
2//# Copyright (C) 1994,1995,1996,1997,1999,2003
3//# Associated Universities, Inc. Washington DC, USA.
4//#
5//# This library is free software; you can redistribute it and/or modify it
6//# under the terms of the GNU Library General Public License as published by
7//# the Free Software Foundation; either version 2 of the License, or (at your
8//# option) any later version.
9//#
10//# This library is distributed in the hope that it will be useful, but WITHOUT
11//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13//# License for more details.
14//#
15//# You should have received a copy of the GNU Library General Public License
16//# along with this library; if not, write to the Free Software Foundation,
17//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18//#
19//# Correspondence concerning AIPS++ should be addressed as follows:
20//# Internet email: casa-feedback@nrao.edu.
21//# Postal address: AIPS++ Project Office
22//# National Radio Astronomy Observatory
23//# 520 Edgemont Road
24//# Charlottesville, VA 22903-2475 USA
25
26#ifndef SCIMATH_FFTSERVER_H
27#define SCIMATH_FFTSERVER_H
28
29//# Includes
30#include <casacore/casa/aips.h>
31#include <casacore/scimath/Mathematics/FFTW.h>
32#include <casacore/casa/Arrays/IPosition.h>
33#include <casacore/casa/Arrays/ArrayFwd.h>
34#include <casacore/casa/Containers/Block.h>
35#include <vector>
36
37namespace casacore { //# NAMESPACE CASACORE - BEGIN
38
39// <summary>Lists the different types of FFT's that can be done</summary>
40// <synopsis>This enumerator is brought out as a separate class because g++
41// currently cannot handle enumerators in a templated class. When it can this
42// class will go away and this enumerator moved into the FFTServer
43// class</synopsis>
44class FFTEnums {
45public:
46 enum TransformType {
47 // Forward Complex to Complex transforms.
48 COMPLEX,
49 // Inverse Complex to Complex transforms.
51 // Real to Complex or Complex to Real transforms.
53 // Real to Complex or Complex to Real transforms.
55 // Real to Real transforms with symmetric Arrays (not used)
57 };
58};
59
60// <summary>A class with methods for Fast Fourier Transforms</summary>
61
62// <use visibility=export>
63
64// <reviewed reviewer="wbrouw" date="1997/10/29" tests="tFFTServer">
65// </reviewed>
66
67// <prerequisite>
68// <li> Basic concepts of Fast Fourier Transforms.
69// <li> <linkto module=Arrays>The Arrays module</linkto>
70// </prerequisite>
71
72// <etymology> The FFTServer class, can do Fast Fourier Transforms of
73// any length and dimensionality.
74// </etymology>
75
76
77// <synopsis>
78
79// The FFTServer class provides methods for performing n-dimensional Fast
80// Fourier Transforms with real and complex Array's of arbitrary size and
81// dimensionality. It can do either real to complex, complex to real, or
82// complex to complex transforms with the "origin" of the transform either at
83// the centre of the Array or at the first element.
84
85// Because the output from a real to complex transform is Hermitian only half
86// of the complex result is returned. Similarly with a complex to real
87// transform only half of the complex plane is required, the other half is
88// implicitly assumed to be the complex conjugate of the supplied half-plane.
89// <note role=warning> The complex to real transform does not check that the
90// imaginary component of the values where u=0 are zero</note>
91
92// This class can be initialised with a shape that indicates the length of the
93// transforms that will be performed, and whether they are going to be
94// real<->complex transforms or complex<->complex ones. This initialisation
95// sets up a variety of internal buffers and computes factorizations and
96// twiddle factors used during the transform. The initialised transform shape
97// is always compared with the shape of the supplied arguments when a transform
98// is done and the FFTServer class will automatically resize itself if
99// necessary. So the default constructor is perfectly safe to use.
100
101// With any transform the output Array must either be the correct shape for the
102// desired output or zero length (ie not contain any elements). If it is zero
103// length then it will be resized to the correct shape. For a complex->complex
104// transform the output Array will be the same shape as the input Array. For a
105// real->complex transform the output Array will be the same size as the input
106// Array except in the first dimension which will have a length of (nx+2)/2. So
107// if nx=7 the output length will be 4 and if nx=8 the output length will be 5,
108// on the first axis. nx is the length of the first axis on the <em>real</em>
109// input Array and cx (which is used later) is the length of the first axis on
110// the <em>complex</em> input Array.
111
112// <strong>For complex to real transforms the output length on the first axis
113// is not uniquely defined by the shape of the complex input
114// Array</strong>. This class uses the following algorithm to work out the
115// length of the first axis on the output Array.
116// <ul>
117// <li> If the size of the output Array is non-zero then its shape must match
118// the size of the input Array except for the first axis. The length of the
119// first axis must either be 2*cx-2 or 2*cx-1 and this determines the length of
120// the transform on the first axis.
121// <li> If the size of the output Array is zero then scan the imaginary
122// components of the values at the end of the first axis on the input Array (ie
123// at <src>[cx-1,....]</src> If any of these are non-zero the output Array
124// will have an odd length.
125// <li> Otherwise if all the imaginary components described above are zero then
126// look at the current size of the FFTServer object (either defined at
127// construction time or with the resize function). If it matches the size of
128// the input Array except for the first axis and if the length on this axis is
129// either 2*cx-2 or 2*cx-1 then use that to determine the size of the output
130// Array.
131// <li> Otherwise assume the output Array will an even length of 2*cx-2 on its
132// first axis.
133// </ul>
134
135// This class does transforms using
136// the highly optimized FFTW package.
137// <br>
138// <em> P.N. Swarztrauber, Vectorizing the FFTs, in Parallel Computations
139// (G. Rodrigue, ed.), Academic Press, 1982, pp. 51--83. </em><br>
140// <br>If at build time it is chosen to use FFTW in a multi-threaded way,
141// it will try to use as many cores as possible.
142
143// In this class a forward transform is defined as one that goes from the real
144// to the complex (or the time to frequency) domain. In a forward transform the
145// sign of the exponent is negative and no scaling is done on the output. The
146// backward transform goes from the complex to the real (or the frequency to
147// the time) domain. The sign of the exponent is positive and the result is
148// always scaled by 1/N were N is the total number of elements in the Array.
149
150// The origin of the transform is defined as the point where setting only that
151// element to one, and then doing a forward transform results in an Array that
152// is all one. The <src>fft</src> member functions in this class all assume
153// that the origin of the Transform is at the centre of the Array ie. at
154// <src>[nx/2,ny/2,...]</src> were the indexing begins at zero. Because the
155// fftpack software assumes the origin of the transform is at the first element
156// ie.,<src>[0,0,...]</src> this class flips the data in the Array around to
157// compensate. For fftpack this flipping takes about one 20% of the total
158// transform time, while for FFTW it can easily exceed the transform time.
159// Flipping can be avoided by using the <src>fft0</src> member
160// functions which do not flip the data.
161
162// Some of the member functions in this class scramble the input Array,
163// possibly by flipping the quandrants of the data although this is not
164// guaranteed. Modification of the input Array can be avoided, at the expense
165// of copying the data to temporary storage, by either:
166// <ul> <li> Ensuring the input Array is a const Array.
167// <li> Setting the constInput Flag to True.
168// </ul>
169// The latter option is provided to avoid users having to cast non-const
170// Arrays to const ones in order to prevent there input Array from being
171// scrambled.
172
173// <note role=warning> This class assumes that a Complex array is stored as
174// pairs of floating point numbers, with no intervening gaps, and with the real
175// component first ie., <src>[re0,im0,re1,im1, ...]</src>. This means that the
176// following type casts work,
177// <srcblock>
178// S * complexPtr;
179// T * realPtr = (T * ) complexPtr;
180// </srcblock>
181// and allow a Complex number to be accessed as a pair of real numbers. If this
182// assumption is bad then real Arrays will have to generated by copying the
183// complex ones. Ultimately this assumption about Complex<->Real Array
184// conversion should be put somewhere central like Array2Math.cc.
185// </note>
186// </synopsis>
187
188// <templating arg=T>
189// <li> The T argument must be of type Float or Double. These are the only
190// possible instantiations of this class.
191// </templating>
192
193// <templating arg=S>
194// <li> The S argument must be of type Complex, if T is Float, or DComplex, if T is
195// Double. These are the only possible instantiations of this class.
196// </templating>
197//
198// <example>
199// Do a real to complex Transform of a 1-Dimensional Vector. The following
200// example can trivially be extended to any number of dimensions.
201// <srcblock>
202// FFTServer<Float,Complex> server;
203// Vector<Float> input(32);
204// Vector<Complex> output(17);
205// input = 0.0f;
206// input(16) = 1.0f;
207// cout << "Input:" << input << endl;
208// server.fft(output, input);
209// cout << "Output:" << output << endl;
210// </srcblock>
211// </example>
212//
213// <thrown>
214// <li> AipsError: If the input and output Array have bad or incompatible
215// shapes. See the individual function descriptions for what Array shapes are
216// required.
217// </thrown>
218//
219// <todo asof="1997/10/22">
220// <li> The time taken to flip the Array can be reduced, if all the Array
221// dimensions are even, by pre-multiplying the every other element on the
222// input Array by -1. Then no flipping needs to be done on the output Array.
223// </todo>
224
225template<class T, class S> class FFTServer
226{
227public:
228
229 // The default constructor. The server will automatically resize to do
230 // transforms of the appropriate length when necessary.
232
233 // Initialise the server to do transforms on Arrays of the specified
234 // shape. The server will, however, resize to do transforms of other lengths
235 // if necessary. See the resize function for a description of the
236 // TransformType enumerator.
237 FFTServer(const IPosition & fftSize,
238 const FFTEnums::TransformType transformType
240
241 // copy constructor. The copied server is initialised to do transforms of the
242 // same length as the other server. Uses copy (and not reference) semantics
243 // so that changing the transform length of one server does not affect the
244 // other server.
245 FFTServer(const FFTServer<T,S> & other);
246
247 // destructor
249
250 // The assignment operator which does the same thing as the copy
251 // constructor.
253
254 // Modify the FFTServer object to do transforms of the supplied shape. The
255 // amount of internal storage, and the initialisation, depends on the type of
256 // transform that will be done. The transform type is specified with the
257 // TransformTypes enumerator. Currently there is no difference in
258 // initialisation for the COMPLEXTOREAL and REALTOCOMPLEX transforms. The
259 // shape argument is the shape of the real array (or complex one if complex
260 // to complex transforms are being done). In general it is not necessary to
261 // use this function as all the fft & fft0 functions will automatically
262 // resize the server, if necessary, to match their input arguments.
263 void resize(const IPosition & fftSize,
264 const FFTEnums::TransformType transformType
266
267 // Real to complex fft. The origin of the transform is in the centre of the
268 // Array. Because of the Hermitian property the output Array only contains
269 // half of the complex result. The output Array must either have no elements
270 // or be a size that is appropriate to the input Array size,
271 // ie. <src>shape = [(nx+2)/2, ny, nz,...]</src>. Otherwise an AipsError is
272 // thrown. See the synopsis for a description of the constInput flag.
273 // <group>
274 void fft(Array<S> & cResult, Array<T> & rData, const Bool constInput=False);
275 void fft(Array<S> & cResult, const Array<T> & rData);
276 // </group>
277
278 // Complex to real fft. The origin of the transform is in the centre of the
279 // Array. Because of the Hermitian property the input Array only contains
280 // half of the complex values. The output Array must either have no elements,
281 // or be a size that is appropriate to the input Array size ie.,<br>
282 // <src>shape = [2*cx-2, cy, cz,...]</src> or <br>
283 // <src>shape = [2*cx-1, cy, cz,...]</src>. <br>
284 // Otherwise an AipsError is thrown. See the description in the synopsis for
285 // the algorithm used to choose between the two possible output shapes and a
286 // description of the constInput Flag.
287 // <group>
288 void fft(Array<T> & rResult, Array<S> & cData, const Bool constInput=False);
289 void fft(Array<T> & rResult, const Array<S> & cData);
290 // </group>
291
292 // Complex to complex in-place fft. The origin of the transform is in the
293 // centre of the Array. The direction of the transform is controlled by the
294 // toFrequency variable. If True then a forward, or time to frequency,
295 // transform is performed. If False a backward or frequency to time transform
296 // is done. Scaling is always done on the backward transform.
297 void fft(Array<S> & cValues, const Bool toFrequency=True);
298
299 // Complex to complex fft. The origin of the transform is in the centre of
300 // the Array. The direction of the transform is controlled by the toFrequency
301 // variable. If True then a forward, or time to frequency, transform is
302 // performed. If False a backward or frequency to time transform is
303 // done. Scaling is always done on the backward transform. The output Array
304 // must either either contain no elements or be the same as the input Array,
305 // ie. <src>shape = [cx, cy, cz,...]</src>. Otherwise an AipsError is
306 // thrown.
307 void fft(Array<S> & cResult, const Array<S> & cData,
308 const Bool toFrequency=True);
309
310 // The <src>fft0</src> functions are equivalent to the <src>fft</src>
311 // functions described above except that the origin of the transform is the
312 // first element of the Array, ie. [0,0,0...], rather than the centre
313 // element, ie [nx/2, ny/2, nz/2, ...]. As the underlying functions
314 // assume that the origin of the transform is the first element these
315 // routines are in general faster than the equivalent ones with the origin
316 // at the centre of the Array.
317 // <group>
318 void fft0(Array<S> & cResult, Array<T> & rData, const Bool constInput=False);
319 void fft0(Array<S> & cResult, const Array<T> & rData);
320 void fft0(Array<T> & rResult, Array<S> & cData, const Bool constInput=False);
321 void fft0(Array<T> & rResult, const Array<S> & cData);
322 void fft0(Array<S> & cValues, const Bool toFrequency=True);
323 void fft0(Array<S> & cResult, const Array<S> & cData,
324 const Bool toFrequency=True);
325 //# void fft0(Array<T> & rValues, const Bool toFrequency=True);
326
327 // </group>
328 //# Flips the quadrants in a complex Array so that the point at
329 //# cData.shape()/2 moves to the origin. This moves, for example, the point
330 //# at [8,3] to the origin ([0,0]) in an array of shape [16,7]. Usually two
331 //# flips will restore an Array to its original state. But for Array's
332 //# where one or more dimension is an odd length two flips do NOT restore
333 //# the data to its original state. So the when toZero=False this routine
334 //# does an unflip operation (ie moves the data at [0,0] to the centre) and
335 //# restores the data to its original state for odd length arrays. When
336 //# passed a Hermitian Array where half the complex plane is implicit (eg as
337 //# produced by a real->complex Transform) it is not necessary to flip the
338 //# first dimension of the Array. In this case the isHermitian flag should
339 //# be set to True. For complex<->complex transforms this should be False.
340 // <group>
341 void flip(Array<T> & rData, const Bool toZero, const Bool isHermitian);
342 void flip(Array<S> & cData, const Bool toZero, const Bool isHermitian);
343 // </group>
344
345 // N-D in-place complex->complex FFT shift (FFT - phase-mult - inverse FFT)
346 // If toFrequency is true, the first FFT will be from time to frequency.
347 // relshift is the freq shift normalised to the bandwidth.
348 // Only transform over selected dimension. Iterate over the others.
349 void fftshift(Array<S> & cValues, const uInt& whichAxis,
350 const Double& relshift, const Bool toFrequency=True);
351
352 // N-D complex->complex FFT shift (FFT - phase-mult - inverse FFT)
353 // with flagging.
354 // If toFrequency is true, the first FFT will be from time to frequency.
355 // relshift is the freq shift normalised to the bandwidth.
356 // Only transform over selected dimension. Iterate over the others.
357 void fftshift(Array<S> & outValues, Array<Bool> & outFlags,
358 const Array<S> & cValues, const Array<Bool>& inFlags,
359 const uInt& whichAxis,
360 const Double& relshift,
361 const Bool goodIsTrue=False,
362 const Bool toFrequency=True);
363
364 // N-D real->real FFT shift (FFT to complex - phase-mult - inverse FFT)
365 // with flagging.
366 // relshift is the freq shift normalised to the bandwidth.
367 // Only transform over selected dimension. Iterate over the others.
368 void fftshift(Array<T> & outValues, Array<Bool> & outFlags,
369 const Array<T> & rValues, const Array<Bool>& inFlags,
370 const uInt& whichAxis,
371 const Double& relshift,
372 const Bool goodIsTrue=False);
373
374private:
375 //# finds the shape of the output array when doing complex->real transforms
376 IPosition determineShape(const IPosition & rShape, const Array<S> & cData);
377
378 //# Data members.
379 // The size of the last FFT done by this object
381 // Whether the last FFT was complex<->complex or not
383 // buffer for copying non-contigious arrays to contigious ones. This is done
384 // so that the FFT's have a better chance of fitting into cache and hence
385 // going faster.
386 // This buffer is also used as temporary storage when flipping the data.
388 // FFTW specific members.
390 std::vector<T> itsWorkIn;
391 std::vector<S> itsWorkOut;
392 std::vector<S> itsWorkC2C;
393};
394
395
396} //# NAMESPACE CASACORE - END
397
398//# Do NOT include the .tcc file here like done for other templated classes.
399//# The instantiations are done explicitly.
400//# In this way the HAVE_FFTW ifdef is only used in .cc files and does
401//# not appear in headers, so other packages using FFTServer do not need
402//# to (un)set HAVE_FFTW.
403
404#endif
simple 1-D array
Definition Block.h:198
@ REALTOCOMPLEX
Real to Complex or Complex to Real transforms.
Definition FFTServer.h:53
@ REALSYMMETRIC
Real to Real transforms with symmetric Arrays (not used)
Definition FFTServer.h:57
@ COMPLEXTOREAL
Real to Complex or Complex to Real transforms.
Definition FFTServer.h:55
@ COMPLEX
Forward Complex to Complex transforms.
Definition FFTServer.h:49
@ INVCOMPLEX
Inverse Complex to Complex transforms.
Definition FFTServer.h:51
A class with methods for Fast Fourier Transforms.
Definition FFTServer.h:226
void flip(Array< S > &cData, const Bool toZero, const Bool isHermitian)
void fft(Array< S > &cValues, const Bool toFrequency=True)
Complex to complex in-place fft.
std::vector< S > itsWorkOut
Definition FFTServer.h:391
IPosition determineShape(const IPosition &rShape, const Array< S > &cData)
void fftshift(Array< T > &outValues, Array< Bool > &outFlags, const Array< T > &rValues, const Array< Bool > &inFlags, const uInt &whichAxis, const Double &relshift, const Bool goodIsTrue=False)
N-D real->real FFT shift (FFT to complex - phase-mult - inverse FFT) with flagging.
Block< S > itsBuffer
buffer for copying non-contigious arrays to contigious ones.
Definition FFTServer.h:387
void fft(Array< T > &rResult, const Array< S > &cData)
FFTServer(const IPosition &fftSize, const FFTEnums::TransformType transformType=FFTEnums::REALTOCOMPLEX)
Initialise the server to do transforms on Arrays of the specified shape.
void fft(Array< T > &rResult, Array< S > &cData, const Bool constInput=False)
Complex to real fft.
std::vector< T > itsWorkIn
Definition FFTServer.h:390
IPosition itsSize
The size of the last FFT done by this object.
Definition FFTServer.h:380
void fft0(Array< S > &cResult, Array< T > &rData, const Bool constInput=False)
The fft0 functions are equivalent to the fft functions described above except that the origin of the ...
FFTEnums::TransformType itsTransformType
Whether the last FFT was complex<->complex or not.
Definition FFTServer.h:382
void fft(Array< S > &cResult, const Array< T > &rData)
void resize(const IPosition &fftSize, const FFTEnums::TransformType transformType=FFTEnums::REALTOCOMPLEX)
Modify the FFTServer object to do transforms of the supplied shape.
void fft0(Array< S > &cResult, const Array< S > &cData, const Bool toFrequency=True)
FFTServer< T, S > & operator=(const FFTServer< T, S > &other)
The assignment operator which does the same thing as the copy constructor.
void flip(Array< T > &rData, const Bool toZero, const Bool isHermitian)
void fft(Array< S > &cResult, const Array< S > &cData, const Bool toFrequency=True)
Complex to complex fft.
void fft0(Array< T > &rResult, const Array< S > &cData)
void fft0(Array< T > &rResult, Array< S > &cData, const Bool constInput=False)
void fftshift(Array< S > &outValues, Array< Bool > &outFlags, const Array< S > &cValues, const Array< Bool > &inFlags, const uInt &whichAxis, const Double &relshift, const Bool goodIsTrue=False, const Bool toFrequency=True)
N-D complex->complex FFT shift (FFT - phase-mult - inverse FFT) with flagging.
void fftshift(Array< S > &cValues, const uInt &whichAxis, const Double &relshift, const Bool toFrequency=True)
N-D in-place complex->complex FFT shift (FFT - phase-mult - inverse FFT) If toFrequency is true,...
FFTServer(const FFTServer< T, S > &other)
copy constructor.
std::vector< S > itsWorkC2C
Definition FFTServer.h:392
FFTServer()
The default constructor.
void fft0(Array< S > &cResult, const Array< T > &rData)
void fft0(Array< S > &cValues, const Bool toFrequency=True)
void fft(Array< S > &cResult, Array< T > &rData, const Bool constInput=False)
Real to complex fft.
~FFTServer()
destructor
FFTW itsFFTW
FFTW specific members.
Definition FFTServer.h:389
this file contains all the compiler specific defines
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
unsigned int uInt
Definition aipstype.h:49
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
const Bool True
Definition aipstype.h:41
double Double
Definition aipstype.h:53