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 {
45 public:
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// <synopsis>
77
78// The FFTServer class provides methods for performing n-dimensional Fast
79// Fourier Transforms with real and complex Array's of arbitrary size and
80// dimensionality. It can do either real to complex, complex to real, or
81// complex to complex transforms with the "origin" of the transform either at
82// the centre of the Array or at the first element.
83
84// Because the output from a real to complex transform is Hermitian only half
85// of the complex result is returned. Similarly with a complex to real
86// transform only half of the complex plane is required, the other half is
87// implicitly assumed to be the complex conjugate of the supplied half-plane.
88// <note role=warning> The complex to real transform does not check that the
89// imaginary component of the values where u=0 are zero</note>
90
91// This class can be initialised with a shape that indicates the length of the
92// transforms that will be performed, and whether they are going to be
93// real<->complex transforms or complex<->complex ones. This initialisation
94// sets up a variety of internal buffers and computes factorizations and
95// twiddle factors used during the transform. The initialised transform shape
96// is always compared with the shape of the supplied arguments when a transform
97// is done and the FFTServer class will automatically resize itself if
98// necessary. So the default constructor is perfectly safe to use.
99
100// With any transform the output Array must either be the correct shape for the
101// desired output or zero length (ie not contain any elements). If it is zero
102// length then it will be resized to the correct shape. For a complex->complex
103// transform the output Array will be the same shape as the input Array. For a
104// real->complex transform the output Array will be the same size as the input
105// Array except in the first dimension which will have a length of (nx+2)/2. So
106// if nx=7 the output length will be 4 and if nx=8 the output length will be 5,
107// on the first axis. nx is the length of the first axis on the <em>real</em>
108// input Array and cx (which is used later) is the length of the first axis on
109// the <em>complex</em> input Array.
110
111// <strong>For complex to real transforms the output length on the first axis
112// is not uniquely defined by the shape of the complex input
113// Array</strong>. This class uses the following algorithm to work out the
114// length of the first axis on the output Array.
115// <ul>
116// <li> If the size of the output Array is non-zero then its shape must match
117// the size of the input Array except for the first axis. The length of the
118// first axis must either be 2*cx-2 or 2*cx-1 and this determines the length of
119// the transform on the first axis.
120// <li> If the size of the output Array is zero then scan the imaginary
121// components of the values at the end of the first axis on the input Array (ie
122// at <src>[cx-1,....]</src> If any of these are non-zero the output Array
123// will have an odd length.
124// <li> Otherwise if all the imaginary components described above are zero then
125// look at the current size of the FFTServer object (either defined at
126// construction time or with the resize function). If it matches the size of
127// the input Array except for the first axis and if the length on this axis is
128// either 2*cx-2 or 2*cx-1 then use that to determine the size of the output
129// Array.
130// <li> Otherwise assume the output Array will an even length of 2*cx-2 on its
131// first axis.
132// </ul>
133
134// This class does transforms using
135// the highly optimized FFTW package.
136// <br>
137// <em> P.N. Swarztrauber, Vectorizing the FFTs, in Parallel Computations
138// (G. Rodrigue, ed.), Academic Press, 1982, pp. 51--83. </em><br>
139// <br>If at build time it is chosen to use FFTW in a multi-threaded way,
140// it will try to use as many cores as possible.
141
142// In this class a forward transform is defined as one that goes from the real
143// to the complex (or the time to frequency) domain. In a forward transform the
144// sign of the exponent is negative and no scaling is done on the output. The
145// backward transform goes from the complex to the real (or the frequency to
146// the time) domain. The sign of the exponent is positive and the result is
147// always scaled by 1/N were N is the total number of elements in the Array.
148
149// The origin of the transform is defined as the point where setting only that
150// element to one, and then doing a forward transform results in an Array that
151// is all one. The <src>fft</src> member functions in this class all assume
152// that the origin of the Transform is at the centre of the Array ie. at
153// <src>[nx/2,ny/2,...]</src> were the indexing begins at zero. Because the
154// fftpack software assumes the origin of the transform is at the first element
155// ie.,<src>[0,0,...]</src> this class flips the data in the Array around to
156// compensate. For fftpack this flipping takes about one 20% of the total
157// transform time, while for FFTW it can easily exceed the transform time.
158// Flipping can be avoided by using the <src>fft0</src> member
159// functions which do not flip the data.
160
161// Some of the member functions in this class scramble the input Array,
162// possibly by flipping the quandrants of the data although this is not
163// guaranteed. Modification of the input Array can be avoided, at the expense
164// of copying the data to temporary storage, by either:
165// <ul> <li> Ensuring the input Array is a const Array.
166// <li> Setting the constInput Flag to True.
167// </ul>
168// The latter option is provided to avoid users having to cast non-const
169// Arrays to const ones in order to prevent there input Array from being
170// scrambled.
171
172// <note role=warning> This class assumes that a Complex array is stored as
173// pairs of floating point numbers, with no intervening gaps, and with the real
174// component first ie., <src>[re0,im0,re1,im1, ...]</src>. This means that the
175// following type casts work,
176// <srcblock>
177// S * complexPtr;
178// T * realPtr = (T * ) complexPtr;
179// </srcblock>
180// and allow a Complex number to be accessed as a pair of real numbers. If this
181// assumption is bad then real Arrays will have to generated by copying the
182// complex ones. Ultimately this assumption about Complex<->Real Array
183// conversion should be put somewhere central like Array2Math.cc.
184// </note>
185// </synopsis>
186
187// <templating arg=T>
188// <li> The T argument must be of type Float or Double. These are the only
189// possible instantiations of this class.
190// </templating>
191
192// <templating arg=S>
193// <li> The S argument must be of type Complex, if T is Float, or DComplex, if T is
194// Double. These are the only possible instantiations of this class.
195// </templating>
196//
197// <example>
198// Do a real to complex Transform of a 1-Dimensional Vector. The following
199// example can trivially be extended to any number of dimensions.
200// <srcblock>
201// FFTServer<Float,Complex> server;
202// Vector<Float> input(32);
203// Vector<Complex> output(17);
204// input = 0.0f;
205// input(16) = 1.0f;
206// cout << "Input:" << input << endl;
207// server.fft(output, input);
208// cout << "Output:" << output << endl;
209// </srcblock>
210// </example>
211//
212// <thrown>
213// <li> AipsError: If the input and output Array have bad or incompatible
214// shapes. See the individual function descriptions for what Array shapes are
215// required.
216// </thrown>
217//
218// <todo asof="1997/10/22">
219// <li> The time taken to flip the Array can be reduced, if all the Array
220// dimensions are even, by pre-multiplying the every other element on the
221// input Array by -1. Then no flipping needs to be done on the output Array.
222// </todo>
223
224template <class T, class S>
226 public:
227 // The default constructor. The server will automatically resize to do
228 // transforms of the appropriate length when necessary.
230
231 // Initialise the server to do transforms on Arrays of the specified
232 // shape. The server will, however, resize to do transforms of other lengths
233 // if necessary. See the resize function for a description of the
234 // TransformType enumerator.
235 FFTServer(const IPosition& fftSize,
237
238 // copy constructor. The copied server is initialised to do transforms of the
239 // same length as the other server. Uses copy (and not reference) semantics
240 // so that changing the transform length of one server does not affect the
241 // other server.
243
244 // destructor
246
247 // The assignment operator which does the same thing as the copy
248 // constructor.
250
251 // Modify the FFTServer object to do transforms of the supplied shape. The
252 // amount of internal storage, and the initialisation, depends on the type of
253 // transform that will be done. The transform type is specified with the
254 // TransformTypes enumerator. Currently there is no difference in
255 // initialisation for the COMPLEXTOREAL and REALTOCOMPLEX transforms. The
256 // shape argument is the shape of the real array (or complex one if complex
257 // to complex transforms are being done). In general it is not necessary to
258 // use this function as all the fft & fft0 functions will automatically
259 // resize the server, if necessary, to match their input arguments.
260 void resize(const IPosition& fftSize,
262
263 // Real to complex fft. The origin of the transform is in the centre of the
264 // Array. Because of the Hermitian property the output Array only contains
265 // half of the complex result. The output Array must either have no elements
266 // or be a size that is appropriate to the input Array size,
267 // ie. <src>shape = [(nx+2)/2, ny, nz,...]</src>. Otherwise an AipsError is
268 // thrown. See the synopsis for a description of the constInput flag.
269 // <group>
270 void fft(Array<S>& cResult, Array<T>& rData, const Bool constInput = False);
271 void fft(Array<S>& cResult, const Array<T>& rData);
272 // </group>
273
274 // Complex to real fft. The origin of the transform is in the centre of the
275 // Array. Because of the Hermitian property the input Array only contains
276 // half of the complex values. The output Array must either have no elements,
277 // or be a size that is appropriate to the input Array size ie.,<br>
278 // <src>shape = [2*cx-2, cy, cz,...]</src> or <br>
279 // <src>shape = [2*cx-1, cy, cz,...]</src>. <br>
280 // Otherwise an AipsError is thrown. See the description in the synopsis for
281 // the algorithm used to choose between the two possible output shapes and a
282 // description of the constInput Flag.
283 // <group>
284 void fft(Array<T>& rResult, Array<S>& cData, const Bool constInput = False);
285 void fft(Array<T>& rResult, const Array<S>& cData);
286 // </group>
287
288 // Complex to complex in-place fft. The origin of the transform is in the
289 // centre of the Array. The direction of the transform is controlled by the
290 // toFrequency variable. If True then a forward, or time to frequency,
291 // transform is performed. If False a backward or frequency to time transform
292 // is done. Scaling is always done on the backward transform.
293 void fft(Array<S>& cValues, const Bool toFrequency = True);
294
295 // Complex to complex fft. The origin of the transform is in the centre of
296 // the Array. The direction of the transform is controlled by the toFrequency
297 // variable. If True then a forward, or time to frequency, transform is
298 // performed. If False a backward or frequency to time transform is
299 // done. Scaling is always done on the backward transform. The output Array
300 // must either either contain no elements or be the same as the input Array,
301 // ie. <src>shape = [cx, cy, cz,...]</src>. Otherwise an AipsError is
302 // thrown.
303 void fft(Array<S>& cResult, const Array<S>& cData, const Bool toFrequency = True);
304
305 // The <src>fft0</src> functions are equivalent to the <src>fft</src>
306 // functions described above except that the origin of the transform is the
307 // first element of the Array, ie. [0,0,0...], rather than the centre
308 // element, ie [nx/2, ny/2, nz/2, ...]. As the underlying functions
309 // assume that the origin of the transform is the first element these
310 // routines are in general faster than the equivalent ones with the origin
311 // at the centre of the Array.
312 // <group>
313 void fft0(Array<S>& cResult, Array<T>& rData, const Bool constInput = False);
314 void fft0(Array<S>& cResult, const Array<T>& rData);
315 void fft0(Array<T>& rResult, Array<S>& cData, const Bool constInput = False);
316 void fft0(Array<T>& rResult, const Array<S>& cData);
317 void fft0(Array<S>& cValues, const Bool toFrequency = True);
318 void fft0(Array<S>& cResult, const Array<S>& cData, const Bool toFrequency = True);
319 // # void fft0(Array<T> & rValues, const Bool toFrequency=True);
320
321 // </group>
322 // # Flips the quadrants in a complex Array so that the point at
323 // # cData.shape()/2 moves to the origin. This moves, for example, the point
324 // # at [8,3] to the origin ([0,0]) in an array of shape [16,7]. Usually two
325 // # flips will restore an Array to its original state. But for Array's
326 // # where one or more dimension is an odd length two flips do NOT restore
327 // # the data to its original state. So the when toZero=False this routine
328 // # does an unflip operation (ie moves the data at [0,0] to the centre) and
329 // # restores the data to its original state for odd length arrays. When
330 // # passed a Hermitian Array where half the complex plane is implicit (eg as
331 // # produced by a real->complex Transform) it is not necessary to flip the
332 // # first dimension of the Array. In this case the isHermitian flag should
333 // # be set to True. For complex<->complex transforms this should be False.
334 // <group>
335 void flip(Array<T>& rData, const Bool toZero, const Bool isHermitian);
336 void flip(Array<S>& cData, const Bool toZero, const Bool isHermitian);
337 // </group>
338
339 // N-D in-place complex->complex FFT shift (FFT - phase-mult - inverse FFT)
340 // If toFrequency is true, the first FFT will be from time to frequency.
341 // relshift is the freq shift normalised to the bandwidth.
342 // Only transform over selected dimension. Iterate over the others.
343 void fftshift(Array<S>& cValues, const uInt& whichAxis, const Double& relshift,
344 const Bool toFrequency = True);
345
346 // N-D complex->complex FFT shift (FFT - phase-mult - inverse FFT)
347 // with flagging.
348 // If toFrequency is true, the first FFT will be from time to frequency.
349 // relshift is the freq shift normalised to the bandwidth.
350 // Only transform over selected dimension. Iterate over the others.
351 void fftshift(Array<S>& outValues, Array<Bool>& outFlags, const Array<S>& cValues,
352 const Array<Bool>& inFlags, const uInt& whichAxis, const Double& relshift,
353 const Bool goodIsTrue = False, const Bool toFrequency = True);
354
355 // N-D real->real FFT shift (FFT to complex - phase-mult - inverse FFT)
356 // with flagging.
357 // relshift is the freq shift normalised to the bandwidth.
358 // Only transform over selected dimension. Iterate over the others.
359 void fftshift(Array<T>& outValues, Array<Bool>& outFlags, const Array<T>& rValues,
360 const Array<Bool>& inFlags, const uInt& whichAxis, const Double& relshift,
361 const Bool goodIsTrue = False);
362
363 private:
364 // # finds the shape of the output array when doing complex->real transforms
365 IPosition determineShape(const IPosition& rShape, const Array<S>& cData);
366
367 // # Data members.
368 // The size of the last FFT done by this object
370 // Whether the last FFT was complex<->complex or not
372 // buffer for copying non-contigious arrays to contigious ones. This is done
373 // so that the FFT's have a better chance of fitting into cache and hence
374 // going faster.
375 // This buffer is also used as temporary storage when flipping the data.
377 // FFTW specific members.
379 std::vector<T> itsWorkIn;
380 std::vector<S> itsWorkOut;
381 std::vector<S> itsWorkC2C;
382};
383
384} // namespace casacore
385
386// # Do NOT include the .tcc file here like done for other templated classes.
387// # The instantiations are done explicitly.
388// # In this way the HAVE_FFTW ifdef is only used in .cc files and does
389// # not appear in headers, so other packages using FFTServer do not need
390// # to (un)set HAVE_FFTW.
391
392#endif
@ 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
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:380
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:376
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:379
IPosition itsSize
The size of the last FFT done by this object.
Definition FFTServer.h:369
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:371
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:381
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:378
For temporary backward namespace compatibility, use casa as alias for casacore.
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