RealDftData.h
Go to the documentation of this file.
1 // RealDftData.h
2 //
3 // David Adams
4 // April 2019
5 //
6 // This class provides an interface and partial implementation for the data describing
7 // a 1D DFT (discrete Fourier transform) of real data. Subclasses add the
8 // normalization and DFT data.
9 //
10 // It is templated so DFT elements can be stored at different levels of
11 // floating point precision.
12 
13 #ifndef RealDftData_H
14 #define RealDftData_H
15 
17 
18 template<typename F>
19 class RealDftData {
20 
21 public:
22 
23  using Float = F;
24 
25  using Index = unsigned int;
26 
27  // Return the normalization.
28  virtual const RealDftNormalization& normalization() const =0;
29 
30  // Check and return dimension information.
31  // nSample = size = # samples
32  // nCompact = # amplitudes in the compact representation
33  // Subclass must override nSample and may override nCompact.
34  virtual Index nSample() const =0;
35  Index size() const { return nSample(); }
36  virtual Index nCompact() const { return nSample() ? nSample()/2 + 1 : 0; }
37  bool isEven() const { return (nSample() + 1) % 2; }
38  bool isOdd() const { return nSample() % 2; }
39 
40  // Check if this object is valid.
41  // Object must have an assigned size so that the frequency status methods work.
42  virtual bool isValid() const { return normalization().isValid() && size() > 0; }
43 
44  // Clear the DFT, i.e. zero the # samples.
45  // The normalization is retained.
46  virtual void clear() =0;
47 
48  // Reset to nsam samples and zero the DFT.
49  virtual void reset(Index nsam) =0;
50 
51  // Return the status for a frequency index.
52  bool inRange(Index ifrq) const { return ifrq < size(); }
53  bool isZero(Index ifrq) const { return ifrq == 0; }
54  bool isNyquist(Index ifrq) const { return 2*ifrq == size(); }
55  bool isNotAliased(Index ifrq) const { return isZero(ifrq) || isNyquist(ifrq); }
56  bool isAliased(Index ifrq) const { return ifrq < size() && !isZero(ifrq) && !isNyquist(ifrq); }
57 
58  // Value to return if caller requests a frequency out of range.
59  virtual F badValue() const { return 0.0; }
60 
61  // Return any term, i.e. ifrq < nSample.
62  // Values depend on global and term normalizations.
63  virtual F amplitude(Index ifrq) const =0;
64  virtual F phase(Index ifrq) const =0;
65  virtual F real(Index ifrq) const =0;
66  virtual F imag(Index ifrq) const =0;
67 
68  // Return the amplitude with convolution normalization, i.e.
69  // standard global and simple term normalizations.
70  F convAmplitude(Index ifrq) const {
71  if ( ifrq >= size() ) return badValue();
72  if ( ! isValid() ) return badValue();
73  F amp = amplitude(ifrq);
74  F len = size();
75  const F osq2 = 1.0/sqrt(2.0);
76  if ( normalization().isConsistent() ) amp *= sqrt(len);
77  if ( normalization().isBin() ) amp *= len;
78  if ( normalization().isPower() && isAliased(ifrq) ) amp *= osq2;
79  return amp;
80  }
81 
82  // Return the compact power for any compact term, i.e. adding that from its alias.
83  // The sum over the range [0, nCompact) gives the total power.
84  F compactPower(Index ifrq) const {
85  if ( ifrq >= nCompact() ) return 0.0;
86  F amp = amplitude(ifrq);
87  F pwr = amp*amp;
88  if ( normalization().isUnit() && isAliased(ifrq) ) pwr *= 2.0;
89  if ( normalization().isStandard() ) pwr /= size();
90  if ( normalization().isBin() ) pwr *= size();
91  return pwr;
92  }
93 
94  // Return the total power.
95  F power() const {
96  F pwr = 0.0;
97  for ( Index ifrq=0; ifrq<nCompact(); ++ifrq ) {
98  pwr += compactPower(ifrq);
99  }
100  return pwr;
101  }
102 
103 };
104 
105 //**********************************************************************
106 
107 #endif
bool inRange(Index ifrq) const
Definition: RealDftData.h:52
bool isNyquist(Index ifrq) const
Definition: RealDftData.h:54
virtual F phase(Index ifrq) const =0
virtual F amplitude(Index ifrq) const =0
bool isNotAliased(Index ifrq) const
Definition: RealDftData.h:55
virtual void clear()=0
virtual const RealDftNormalization & normalization() const =0
bool isEven() const
Definition: RealDftData.h:37
virtual bool isValid() const
Definition: RealDftData.h:42
Index size() const
Definition: RealDftData.h:35
virtual Index nSample() const =0
F compactPower(Index ifrq) const
Definition: RealDftData.h:84
unsigned int Index
Definition: RealDftData.h:25
F convAmplitude(Index ifrq) const
Definition: RealDftData.h:70
virtual F real(Index ifrq) const =0
virtual F badValue() const
Definition: RealDftData.h:59
F power() const
Definition: RealDftData.h:95
bool isAliased(Index ifrq) const
Definition: RealDftData.h:56
bool isZero(Index ifrq) const
Definition: RealDftData.h:53
virtual Index nCompact() const
Definition: RealDftData.h:36
virtual F imag(Index ifrq) const =0
bool isOdd() const
Definition: RealDftData.h:38
virtual void reset(Index nsam)=0