My Project
 All Classes Files Functions Variables Enumerations Enumerator Friends Macros Pages
Dft.h
Go to the documentation of this file.
1 // $Id: Dft.h,v 1.2 2010/02/23 17:19:57 eml Exp $
2 
3 #ifndef GCP_UTIL_DFT_H
4 #define GCP_UTIL_DFT_H
5 
16 #define COMPLEX_AV
17 
18 #include <iostream>
19 #include <vector>
20 
21 #include "gcp/util/common/CircBuf.h"
22 #include "gcp/util/common/Frequency.h"
23 #include "gcp/util/common/TimeVal.h"
24 
25 #include <fftw3.h>
26 
27 #define APOD_FN(fn) double (fn)(unsigned iSamp, unsigned n)
28 
29 namespace gcp {
30  namespace util {
31 
32  class Dft {
33  public:
34 
35  enum Apodization {
36  APOD_RECTANGLE = 0, // Rectangular (Dirichlet) window
37  APOD_TRIANGLE = 1, // Triangle apodization (goes to zero at edges)
38  APOD_HAMMING = 2, // Hamming window
39  APOD_HANN = 3, // Hann window
40  APOD_COS = 4, // Cosine window
41  APOD_SINC = 5, // Sinc window
42  };
43 
47  Dft(int n, bool optimize=true, Apodization apod=APOD_RECTANGLE);
48 
52  Dft(const Dft& objToBeCopied);
53 
57  Dft(Dft& objToBeCopied);
58 
62  void operator=(const Dft& objToBeAssigned);
63 
67  void operator=(Dft& objToBeAssigned);
68 
72  friend std::ostream& operator<<(std::ostream& os, Dft& obj);
73 
77  virtual ~Dft();
78 
79  //-----------------------------------------------------------------------
80  // Set methods
81  //-----------------------------------------------------------------------
82 
89  void resize(unsigned int);
90 
95  void setTimeRes(TimeVal tVal);
96 
100  void setAverage(bool doAv);
101 
105  void setVectorAverage(bool vecAv);
106 
110  void setApodizationType(Apodization apod);
111 
112  //-----------------------------------------------------------------------
113  // Query methods
114  //-----------------------------------------------------------------------
115 
119  unsigned inputSize();
120 
124  unsigned transformSize();
125 
129  static unsigned transformSize(unsigned n);
130 
135 
136  // Static method to return the same
137 
138  static Frequency getFrequencyResolution(unsigned npt, TimeVal timeRes);
139 
143  std::vector<double> powerSpectrum(double* data);
144 
148  std::vector<double> abs();
149 
154  void abs(float* outputArray,
155  bool& first, double& min, double& max, bool doLog=false);
156 
160  void updateMinMax(unsigned i, double& val, bool doLog,
161  bool& first, double& min, double& max);
162 
163  //-----------------------------------------------------------------------
164  // Manual methods for using this class
165  //-----------------------------------------------------------------------
166 
170  void fillInputArray(double* data);
171 
175  void computeTransform();
176 
180  double* getInputData();
181 
185  fftw_complex* getTransform();
186 
187  //-----------------------------------------------------------------------
188  // Automatic methods for computing transforms
189  //-----------------------------------------------------------------------
190 
196  void pushSample(double sample);
197 
202  bool transformIsReady();
203 
204  //-----------------------------------------------------------------------
205  // Apodization functions
206  //-----------------------------------------------------------------------
207 
208  static APOD_FN(apodRectangle);
209  static APOD_FN(apodTriangle);
210  static APOD_FN(apodHamming);
211  static APOD_FN(apodHann);
212  static APOD_FN(apodCos);
213  static APOD_FN(apodSinc);
214 
215  private:
216 
220  void computePlan(unsigned flag);
221 
225  void addToAverage();
226 
227  // Remove the mean from the input array
228 
229  void removeMean();
230 
231  // Assert a pending apodization type
232 
233  void assertApodizationType(Apodization apod);
234 
235  // Return the apodization coefficient for the current sample
236 
237  double apodizationCoefficient(unsigned iSamp);
238 
239  void copySamples();
240 
241  private:
242 
243  // Fundamental variables
244 
245  int n_; // The size transform we will compute
246  TimeVal tRes_; // The time resolution of the samples
247  bool haveRes_; // True once the resolution is set
248  Apodization currentApodType_; // The current apodization type
249  Apodization pendingApodType_; // The pending apodization type
250  bool apodTypePending_; // An apodization command was received,
251  // that should be put into effect the next
252  // time the sample counter rolls over
253  APOD_FN(*apodFn_); // The current apodization function
254 
255  double mean_;
256  bool subtractMean_;
257 
258  // FFTW specific variables
259 
260  double* in_; // The input array to be transformed
261  fftw_complex* out_; // The output of the transformed
262  fftw_plan plan_; // Instructions to fftw for the best method
263  // to FFT
264  bool optimize_; // True if we want fftw to perform expensive
265  // tests to determine the optimal FFT
266  // method. Note that these tests will only
267  // be done on calls to resize()
268 
269  // Averaging variables
270 
271  unsigned nAv_; // The number of transforms currently in
272  // the running average
273  bool doAv_; // If true, average the transforms
274 
275  bool vecAv_; // If true, vector-average the transforms.
276  // If false, rms-average the power spectra
277 
278  bool linear_; // If true, output data in linear (rather
279  // than log) units
280 
281  fftw_complex* outAv_; // The averaged output of the transform
282  std::valarray<double> absBuf_; // The average output of the
283  // power spectra
284 
285  // Auto-transform variables
286 
287  std::valarray<double> sampBuf_; // Buffer into which we store
288  // samples
289  std::valarray<double> apodBuf_; // Buffer into which we store
290  // apodization coefficients
291 #if 0
292  CircBuf<double> buf_; // A circular buffer for storing
293  // on-the-fly samples
294  CircBuf<double> apodBuf_; // A circular buffer for storing
295  // on-the-fly samples
296 #endif
297  unsigned nSinceLastTransform_;
298  bool transformIsReady_;
299 
300 
301  }; // End class Dft
302 
303  } // End namespace util
304 } // End namespace gcp
305 
306 
307 #endif // End #ifndef GCP_UTIL_DFT_H
std::vector< double > abs()
Definition: Dft.cc:312
void pushSample(double sample)
Definition: Dft.cc:156
Definition: Dft.h:32
void setTimeRes(TimeVal tVal)
Definition: Dft.cc:481
fftw_complex * getTransform()
Definition: Dft.cc:280
Frequency getFrequencyResolution()
Definition: Dft.cc:490
void setAverage(bool doAv)
Definition: Dft.cc:447
void computeTransform()
Definition: Dft.cc:227
void setVectorAverage(bool vecAv)
Definition: Dft.cc:464
Definition: CircBuf.h:27
void updateMinMax(unsigned i, double &val, bool doLog, bool &first, double &min, double &max)
Definition: Dft.cc:348
bool transformIsReady()
Definition: Dft.cc:439
std::vector< double > powerSpectrum(double *data)
Definition: Dft.cc:429
void setApodizationType(Apodization apod)
Definition: Dft.cc:519
Definition: TimeVal.h:55
Dft(int n, bool optimize=true, Apodization apod=APOD_RECTANGLE)
Definition: Dft.cc:16
void resize(unsigned int)
Definition: Dft.cc:103
void fillInputArray(double *data)
Definition: Dft.cc:199
double * getInputData()
Definition: Dft.cc:272
friend std::ostream & operator<<(std::ostream &os, Dft &obj)
unsigned inputSize()
Definition: Dft.cc:288
virtual ~Dft()
Definition: Dft.cc:79
void operator=(const Dft &objToBeAssigned)
Definition: Dft.cc:54
unsigned transformSize()
Definition: Dft.cc:296
Definition: Frequency.h:21