Public Types | Public Member Functions | Private Attributes | List of all members
FwFFT Class Reference

#include <FwFFT.h>

Public Types

using Index = unsigned int
 
using Float = double
 
using Complex = fftw_complex
 
using DFT = CompactRealDftData< float >
 
using FloatVector = std::vector< float >
 
using Plan = fftw_plan
 
using PlanMap = std::map< Index, Plan >
 

Public Member Functions

 FwFFT (Index nsamMax, Index opt)
 
 ~FwFFT ()
 
PlanforwardPlan (Index nsam)
 
PlanbackwardPlan (Index nsam)
 
int fftForward (Index nsam, const float *psam, DFT &dft, Index logLevel=0)
 
int fftForward (const FloatVector &sams, DFT &dft, Index logLevel=0)
 
int fftInverse (const DFT &dft, FloatVector &sams, Index logLevel=0)
 

Private Attributes

Index m_nsamMax
 
Index m_flag
 
double * m_inData
 
Complexm_outData
 
PlanMap m_forwardPlans
 
PlanMap m_backwardPlans
 

Detailed Description

Definition at line 24 of file FwFFT.h.

Member Typedef Documentation

using FwFFT::Complex = fftw_complex

Definition at line 30 of file FwFFT.h.

Definition at line 31 of file FwFFT.h.

using FwFFT::Float = double

Definition at line 29 of file FwFFT.h.

Definition at line 32 of file FwFFT.h.

using FwFFT::Index = unsigned int

Definition at line 28 of file FwFFT.h.

using FwFFT::Plan = fftw_plan

Definition at line 33 of file FwFFT.h.

using FwFFT::PlanMap = std::map<Index, Plan>

Definition at line 34 of file FwFFT.h.

Constructor & Destructor Documentation

FwFFT::FwFFT ( Index  nsamMax,
Index  opt 
)

Definition at line 22 of file FwFFT.cxx.

23 : m_nsamMax(nsamMax),
24  m_flag(opt==2 ? FFTW_PATIENT : opt==1 ? FFTW_MEASURE : FFTW_ESTIMATE),
25  m_inData((Float*) fftw_malloc(m_nsamMax*sizeof(Float))),
26  m_outData((Complex*) fftw_malloc(m_nsamMax*sizeof(Complex))) { }
Fw2dFFT::Complex Complex
Index m_nsamMax
Definition: FwFFT.h:67
opt
Definition: train.py:196
double * m_inData
Definition: FwFFT.h:69
Index m_flag
Definition: FwFFT.h:68
Complex * m_outData
Definition: FwFFT.h:70
DftData::Float Float
FwFFT::~FwFFT ( )

Definition at line 30 of file FwFFT.cxx.

30  {
31  for ( auto& iplan : m_forwardPlans ) fftw_destroy_plan(iplan.second);
32  for ( auto& iplan : m_backwardPlans ) fftw_destroy_plan(iplan.second);
33  fftw_free(m_inData);
34  fftw_free(m_outData);
35 }
double * m_inData
Definition: FwFFT.h:69
PlanMap m_forwardPlans
Definition: FwFFT.h:71
Complex * m_outData
Definition: FwFFT.h:70
PlanMap m_backwardPlans
Definition: FwFFT.h:72

Member Function Documentation

FwFFT::Plan & FwFFT::backwardPlan ( Index  nsam)

Definition at line 54 of file FwFFT.cxx.

54  {
55  const string myname = "FwFFT::backwardPlan";
56  static Plan badplan;
57  if ( nsam > m_nsamMax ) {
58  cout << myname << "Sample count is too large. Maximum is " << m_nsamMax << endl;
59  return badplan;
60  }
61  if ( m_backwardPlans.count(nsam) == 0 ) {
62  m_backwardPlans[nsam] = fftw_plan_dft_c2r_1d(nsam, m_outData, m_inData, m_flag);
63  }
64  return m_backwardPlans[nsam];
65 }
fftw_plan Plan
Definition: FwFFT.h:33
Index m_nsamMax
Definition: FwFFT.h:67
double * m_inData
Definition: FwFFT.h:69
Index m_flag
Definition: FwFFT.h:68
Complex * m_outData
Definition: FwFFT.h:70
PlanMap m_backwardPlans
Definition: FwFFT.h:72
QTextStream & endl(QTextStream &s)
int FwFFT::fftForward ( Index  nsam,
const float *  psam,
DFT dft,
Index  logLevel = 0 
)

Definition at line 70 of file FwFFT.cxx.

70  {
71  const string myname = "FwFFT::fftForward: ";
72  if ( nsam > m_nsamMax ) {
73  cout << myname << "Sample count is too large. Maximum is " << m_nsamMax << endl;
74  return 2;
75  }
76  dft.reset(nsam);
77  if ( ! dft.isValid() ) return 1;
78  if ( nsam == 0 ) return 0;
79  for ( Index isam=0; isam<nsam; ++isam ) m_inData[isam] = psam[isam];
80  Plan& plan = forwardPlan(nsam);
81  fftw_execute(plan);
82  double xre = 0.0;
83  double xim = 0.0;
84  Index namp = dft.nAmplitude();
85  Index npha = dft.nPhase();
86  FloatVector amps(namp);
87  FloatVector phas(npha);
88  // Loop over the compact samples.
89  float nfac = 1.0;
90  if ( dft.normalization().isConsistent() ) nfac = 1.0/sqrt(nsam);
91  if ( dft.normalization().isBin() ) nfac = 1.0/nsam;
92  for ( Index ifrq=0; ifrq<namp; ++ifrq ) {
93  xre = m_outData[ifrq][0];
94  xim = m_outData[ifrq][1];
95  // For an even # samples (namp = npha + 1), the Nyquist term is real
96  // and we store the sign with the amplitude.
97  double xam = ifrq < npha ? sqrt(xre*xre + xim*xim) : xre;
98  double xph = atan2(xim, xre);
99  float fac = nfac;
100  if ( dft.normalization().isPower() && dft.isAliased(ifrq) ) fac *= sqrt(2.0);
101  xam *= fac;
102  dft.setAmplitude(ifrq, xam);
103  if ( ifrq < npha ) {
104  dft.setPhase(ifrq, xph);
105  }
106  if ( logLevel >= 3 ) {
107  cout << myname << setw(4) << ifrq << ": ("
108  << setw(10) << fixed << xre << ", "
109  << setw(10) << fixed << xim << "): "
110  << setw(10) << fixed << xam;
111  if ( ifrq < npha ) cout << " @ " << setw(10) << fixed << xph;
112  cout << endl;
113  }
114  }
115  return 0;
116 }
fftw_plan Plan
Definition: FwFFT.h:33
Index m_nsamMax
Definition: FwFFT.h:67
int setPhase(Index ifrq, F val)
double * m_inData
Definition: FwFFT.h:69
unsigned int Index
void reset(Index nsam) override
virtual bool isValid() const
Definition: RealDftData.h:42
Complex * m_outData
Definition: FwFFT.h:70
Index nAmplitude() const
Plan & forwardPlan(Index nsam)
Definition: FwFFT.cxx:39
int setAmplitude(Index ifrq, F val)
bool isAliased(Index ifrq) const
Definition: RealDftData.h:56
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
const Norm & normalization() const override
Dft::FloatVector FloatVector
QTextStream & endl(QTextStream &s)
int FwFFT::fftForward ( const FloatVector sams,
DFT dft,
Index  logLevel = 0 
)

Definition at line 121 of file FwFFT.cxx.

121  {
122  return fftForward(sams.size(), &sams[0], dft, logLevel);
123 }
int fftForward(Index nsam, const float *psam, DFT &dft, Index logLevel=0)
Definition: FwFFT.cxx:70
int FwFFT::fftInverse ( const DFT dft,
FloatVector sams,
Index  logLevel = 0 
)

Definition at line 128 of file FwFFT.cxx.

128  {
129  const string myname = "FwFFT::fftInverse: ";
130  if ( ! dft.isValid() ) return 1;
131  Index namp = dft.nAmplitude();
132  Index npha = dft.nPhase();
133  if ( namp == 0 || npha == 0 ) return 1;
134  if ( namp < npha ) return 2;
135  if ( namp - npha > 1 ) return 3;
136  Index nsam = namp + npha - 1;
137  if ( nsam > m_nsamMax ) {
138  cout << myname << "Sample count is too large. Maximum is " << m_nsamMax << endl;
139  return 4;
140  }
141  for ( Index ifrq=0; ifrq<nsam; ++ifrq ) {
142  double amp = dft.convAmplitude(ifrq);
143  double pha = dft.phase(ifrq);
144  double xre = amp*cos(pha);
145  double xim = amp*sin(pha);
146  m_outData[ifrq][0] = xre;
147  m_outData[ifrq][1] = xim;
148  }
149  Plan& plan = backwardPlan(nsam);
150  fftw_execute(plan);
151  float nfac = 1.0/nsam;
152  sams.resize(nsam);
153  for ( Index isam=0; isam<nsam; ++isam ) sams[isam] = nfac*m_inData[isam];
154  return 0;
155 }
fftw_plan Plan
Definition: FwFFT.h:33
Index m_nsamMax
Definition: FwFFT.h:67
double * m_inData
Definition: FwFFT.h:69
unsigned int Index
virtual bool isValid() const
Definition: RealDftData.h:42
Complex * m_outData
Definition: FwFFT.h:70
F phase(Index ifrq) const override
Index nAmplitude() const
F convAmplitude(Index ifrq) const
Definition: RealDftData.h:70
Plan & backwardPlan(Index nsam)
Definition: FwFFT.cxx:54
QTextStream & endl(QTextStream &s)
FwFFT::Plan & FwFFT::forwardPlan ( Index  nsam)

Definition at line 39 of file FwFFT.cxx.

39  {
40  const string myname = "FwFFT::forwardPlan";
41  static Plan badplan;
42  if ( nsam > m_nsamMax ) {
43  cout << myname << "Sample count is too large. Maximum is " << m_nsamMax << endl;
44  return badplan;
45  }
46  if ( m_forwardPlans.count(nsam) == 0 ) {
47  m_forwardPlans[nsam] = fftw_plan_dft_r2c_1d(nsam, m_inData, m_outData, m_flag);
48  }
49  return m_forwardPlans[nsam];
50 }
fftw_plan Plan
Definition: FwFFT.h:33
Index m_nsamMax
Definition: FwFFT.h:67
double * m_inData
Definition: FwFFT.h:69
PlanMap m_forwardPlans
Definition: FwFFT.h:71
Index m_flag
Definition: FwFFT.h:68
Complex * m_outData
Definition: FwFFT.h:70
QTextStream & endl(QTextStream &s)

Member Data Documentation

PlanMap FwFFT::m_backwardPlans
private

Definition at line 72 of file FwFFT.h.

Index FwFFT::m_flag
private

Definition at line 68 of file FwFFT.h.

PlanMap FwFFT::m_forwardPlans
private

Definition at line 71 of file FwFFT.h.

double* FwFFT::m_inData
private

Definition at line 69 of file FwFFT.h.

Index FwFFT::m_nsamMax
private

Definition at line 67 of file FwFFT.h.

Complex* FwFFT::m_outData
private

Definition at line 70 of file FwFFT.h.


The documentation for this class was generated from the following files: