Fw2dFFT.h
Go to the documentation of this file.
1 // Fw2dFFT.h
2 
3 // David Adams
4 // January 2021
5 //
6 // This utility provides wrappers for performing forward and backward DFT (discrete
7 // 2D Fourier transform) of real (time-domain) data using FFTW.
8 //
9 // Separate forward and backward FFTW plans are created for each array of data dimensions.
10 // These may be created in advance by calling fowardPlan or backwardPlan or are created
11 // in the first call to perform an FFT with that set of dimentsions.
12 //
13 // Space to hold the transformation data is allocated in the constructor. Use
14 // checkDataSize to check if the space is sufficient for given data dimensions.
15 //
16 // The real data is held in a vector of floats. The DFT is a vector of complex values
17 // of the same length and so has a factor of two redundancy. The DFT data is returned
18 // in an object with the Real2dDftData interface.
19 //
20 // The concrete type for the returned data is
21 // FftwReal2dDftData<double>
22 
23 #ifndef Fw2dFFT_H
24 #define Fw2dFFT_H
25 
28 #include <map>
29 
30 class Fw2dFFT {
31 
32 public:
33 
34  using DataFloat = float;
35  using DftFloat = double; // Type used in FFTW
38  using Index = DFT::Index;
41  using Plan = fftw_plan;
42  using PlanMap = std::map<IndexArray, Plan>;
43  typedef double FftwComplex[2];
44 
45  // Ctor from maximum data size and optimization.
46  // For n1xn2 data, ndat must be at least as big as n1*n2 and 2*n1*(n2/2+1)
47  // opt = 0-2 (FFTW_ESTIMATE, FFTW_PLAN, FFTW_PATIENT)
48  // Larger numbers take longer to build plans but are faster for each transform.
49  Fw2dFFT(Index ndatMax, Index opt);
50 
51  // Dtor. Frees the data caches and destroys the plans.
52  ~Fw2dFFT();
53 
54  // Check an array of sample sizes.
55  // 0 - Ok for processing here.
56  // 1 - Insufficent size for input data.
57  // 2 - Insufficient size for output (DFT) data.
58  // 3 - One or more dimensions has size zero.
59  Index checkDataSize(const IndexArray& nsams) const;
60 
61  // Return if we have plan for given data sizes.
62  bool haveForwardPlan(const IndexArray& nsams) const;
63  bool haveBackwardPlan(const IndexArray& nsams) const;
64 
65  // Return the plan for a data size.
66  // The plan is created if not already existing.
67  Plan& forwardPlan(const IndexArray& nsams);
68  Plan& backwardPlan(const IndexArray& nsams);
69 
70  // Forward transform: real data (ntick starting at psam[0] --> complex freqs).
71  // psam - Address of the first element in input the data array
72  // dft - Output DFT data. Dimension sizes are obtained from this.
73  // logLevel - 0=silent, 1=init, 2=each event, >2=more
74  // Returns 0 for success.
75  int fftForward(const Data& dat, DFT& dft, Index logLevel =0);
76 
77  // Inverse transform: complex freqs mags, phases --> real data sams
78  // The real and imag freq component are also recorded in xres, xims
79  // Returns 0 for success.
80  int fftBackward(const DFT& dft, Data& dat, Index logLevel =0);
81 
82  // Return the DFT data as FFTW complex.
83  FftwComplex* fftwOutData() { return reinterpret_cast<FftwComplex*>(m_outData); }
84 
85  // Return the DFT data as floating point: real0, imag0, real1, ...
86  DftFloat* floatOutData() { return reinterpret_cast<DftFloat*>(m_outData); }
87 
88 private:
89 
96 
97 };
98 
99 #endif
std::array< Index, 2 > IndexArray
Definition: Real2dDftData.h:27
double FftwComplex[2]
Definition: Fw2dFFT.h:43
Index checkDataSize(const IndexArray &nsams) const
Definition: Fw2dFFT.cxx:39
int fftForward(const Data &dat, DFT &dft, Index logLevel=0)
Definition: Fw2dFFT.cxx:93
DFT::IndexArray IndexArray
Definition: Fw2dFFT.h:39
Index m_flag
Definition: Fw2dFFT.h:91
opt
Definition: train.py:196
Plan & backwardPlan(const IndexArray &nsams)
Definition: Fw2dFFT.cxx:77
PlanMap m_forwardPlans
Definition: Fw2dFFT.h:94
FftwComplex * fftwOutData()
Definition: Fw2dFFT.h:83
int fftBackward(const DFT &dft, Data &dat, Index logLevel=0)
Definition: Fw2dFFT.cxx:123
DftFloat * m_inData
Definition: Fw2dFFT.h:92
Complex * m_outData
Definition: Fw2dFFT.h:93
Plan & forwardPlan(const IndexArray &nsams)
Definition: Fw2dFFT.cxx:62
DFT::Index Index
Definition: Fw2dFFT.h:38
double DftFloat
Definition: Fw2dFFT.h:35
unsigned int Index
Definition: Real2dDftData.h:26
bool haveForwardPlan(const IndexArray &nsams) const
Definition: Fw2dFFT.cxx:50
std::complex< Float > Complex
Definition: Real2dDftData.h:29
std::map< IndexArray, Plan > PlanMap
Definition: Fw2dFFT.h:42
PlanMap m_backwardPlans
Definition: Fw2dFFT.h:95
Index m_ndatMax
Definition: Fw2dFFT.h:90
float DataFloat
Definition: Fw2dFFT.h:34
bool haveBackwardPlan(const IndexArray &nsams) const
Definition: Fw2dFFT.cxx:56
Fw2dFFT(Index ndatMax, Index opt)
Definition: Fw2dFFT.cxx:22
DFT::Complex Complex
Definition: Fw2dFFT.h:40
fftw_plan Plan
Definition: Fw2dFFT.h:41
DftFloat * floatOutData()
Definition: Fw2dFFT.h:86
~Fw2dFFT()
Definition: Fw2dFFT.cxx:30