FancyFFTs.h
1 /* -*- C++ -*-.***************************************************************************************************
2  Author: Ben Strutt
3  Email: b.strutt.12@ucl.ac.uk
4 
5  Description:
6  C++ ROOT friendly namespace to do FFTs faster than Ryan.
7  Will probably be pretty bare bones intially.
8  I only really want this for doing Cross Correlations.
9 *************************************************************************************************************** */
10 
11 #ifndef FANCYFFTS_H
12 #define FANCYFFTS_H
13 
14 #include <iostream>
15 #include <map>
16 #include <algorithm>
17 #include "TObject.h"
18 #include "TSystem.h"
19 #include "TMath.h"
20 #include "TGraph.h"
21 #include "TThread.h"
22 
23 
24 // Needs to be in front of including fftw3.h!
25 #ifdef __CINT__
26 #ifdef FFTW_64_BIT // Hack for Hawaii install of FFTW. Is there ever a reason to now have this defed?
27 typedef struct {char a[16];} __float128; /* 16 chars have the same size as one __float128 */
28 #endif
29 #endif
30 
31 
32 /*
33  Will use std::complex<double> for i/o as should be bit-to-bit identical to typdef fftw_complex double[2].
34  So long as <complex> is included in front of fftw3.h.
35 */
36 #include <complex>
37 #include <fftw3.h>
38 
39 
40 
41 using std::complex;
42 
49 namespace FancyFFTs {
50 
55  kSum = 0,
56  kAverage = 1,
57  kTimeIntegral = 2,
58  kPowSpecDensity = 3,
59  kPowSpecDensity_dBm = 4
60  };
61 
62  complex<double>* doFFT(int len, double* input, bool copyOutputToNewArray, int threadInd=0);
63  complex<double>* doFFT(int len, double* input, complex<double>* output, int threadInd=0);
64  complex<double>* doFFT(int len, double* input, complex<double>* output,
65  bool copyOutputToNewArray, int threadInd=0);
66  double* doInvFFT(int len, complex<double>* input, bool copyOutputToNewArray, int threadInd=0);
67  double* doInvFFT(int len, complex<double>* input, double* output, int threadInd=0);
68  double* doInvFFT(int len, complex<double>* input, double* output,
69  bool copyOutputToNewArray, int threadInd=0);
70 
71  double* getPowerSpectrum(int len, double* input, double dt,
72  conventionFlag normFlag,
73  int threadInd=0);
74 
75  double* getPowerSpectrum(int len, double* input, double dt,
76  conventionFlag normFlag,
77  double* outputPtr, int threadInd=0);
78 
79  TGraph* getPowerSpectrumTGraph(int len, double* input, double dt,
80  conventionFlag normFlag,
81  bool dBScale, int threadInd=0);
82  double* getFreqArray(int len, double dt);
83  int getNumFreqs(int len);
84  int printListOfKeys();
85  double* crossCorrelate(int len, double* v1, double* v2, int threadInd=0);
86  double* crossCorrelate(int len, complex<double>* fft1, complex<double>* fft2, int threadInd=0);
87  double* crossCorrelate(int len, complex<double>* fft1, complex<double>* fft2,
88  double* output, int threadInd=0);
89  int extendToPowerOfTwo(int len);
90  complex<double>* zeroPadFFT(complex<double>* fft, int numSamples, int numSamplesUpsampled);
91  complex<double>* zeroPadFFT(complex<double>* fft, complex<double>* output,
92  int numSamples, int numSamplesUpsampled);
93 
94 
95  /* Takes care of checking whether a plan exists or not */
96  bool makeNewPlanIfNeeded(int len,int threadInd=0);
97 
98  double* getRealArray(std::pair<Int_t, Int_t> key);
99 
100 
101 };
102 
103 
104 #endif //FANCYFFTS_H
int extendToPowerOfTwo(int len)
Find the next highest power of two.
Definition: FancyFFTs.cxx:377
My implementation of a wrapper for FFTW for use with ROOT thing.
TGraph * getPowerSpectrumTGraph(int len, double *input, double dt, conventionFlag normFlag, bool dBScale, int threadInd=0)
Creates a power spectrum TGraph from an array of doubles.
Definition: FancyFFTs.cxx:52
double * getPowerSpectrum(int len, double *input, double dt, conventionFlag normFlag, int threadInd=0)
Creates the power spectrum in an array of doubles of length (len/2 + 1).
Definition: FancyFFTs.cxx:77
complex< double > * zeroPadFFT(complex< double > *fft, int numSamples, int numSamplesUpsampled)
Zero padds FFTts,.
Definition: FancyFFTs.cxx:436
double * crossCorrelate(int len, double *v1, double *v2, int threadInd=0)
Cross correlates the two input arrays.
Definition: FancyFFTs.cxx:541
complex< double > * doFFT(int len, double *input, bool copyOutputToNewArray, int threadInd=0)
Does a forward fast fourier transform on a real input array.
Definition: FancyFFTs.cxx:212
int printListOfKeys()
Prints the list of keys of plans to stdout.
Definition: FancyFFTs.cxx:501
double * getFreqArray(int len, double dt)
Get the array of frequencies to go with FFT output, determined by the tranform length and dt...
Definition: FancyFFTs.cxx:394
int getNumFreqs(int len)
Get the number of frequencies from the number of samples.
Definition: FancyFFTs.cxx:416
bool makeNewPlanIfNeeded(int len, int threadInd=0)
Creates new fftw plan to handle 1D transforms of length len if they haven&#39;t been allocated already...
Definition: FancyFFTs.cxx:19
double * getRealArray(std::pair< Int_t, Int_t > key)
Get the real array of the internal fftw memory. Do not delete this!
Definition: FancyFFTs.cxx:673
conventionFlag
Flag to pass to FancyFFT functions specifying normalization of power spectra.
Definition: FancyFFTs.h:54
double * doInvFFT(int len, complex< double > *input, bool copyOutputToNewArray, int threadInd=0)
Does an inverse fast fourier transform on an array of complex<double>s.
Definition: FancyFFTs.cxx:288