11 #include "TApplication.h" 18 #include "FancyFFTs.h" 19 #include "RootTools.h" 21 double sum(
int n,
double* x){
23 for(
int i=0; i<n; i++){
29 int main(
int argc,
char *argv[]){
31 TApplication* theApp =
new TApplication(
"App", &argc, argv);
39 double phi = TMath::Pi()/2;
43 for(
int i=0; i<n; i++){
46 sineWave1[i] = 3.3*TMath::Sin(omega1*t + phi);
47 sineWave2[i] = 1.6*TMath::Sin(omega2*t);
50 TGraph* gr1 =
new TGraph(n, ts, sineWave1);
51 gr1->SetName(
"grSineWave");
52 gr1->SetTitle(
"A sine wave; t (s); Amplitude (V)");
53 TGraph* gr2 =
new TGraph(n, ts, sineWave2);
54 gr2->SetLineColor(kRed);
56 TCanvas* c1 =
new TCanvas(
"c1",
"Fancy FFT Frivolities", 1200, 1600);
64 std::cout <<
"Before doing anything: ";
70 std::cout <<
"After makeing two power spectrums of length " << n<<
": ";
78 gr4->SetLineColor(kRed);
79 gr3->SetTitle(
"Power spectrum of a sine wave; Frequency (Hz); Power (V^{2})");
94 std::cout << std::endl << std::endl << std::endl;
95 std::cout <<
"Doing some tests of normalization a la Parsevals Theorem." << std::endl;
96 std::cout <<
"If the program doesn't creash unceremoniously then we're all good :)" << std::endl;
100 for(
int i=0; i<gr1->GetN(); i++){
101 powTime += gr1->GetY()[i]*gr1->GetY()[i];
103 std::cout <<
"powTime = " << powTime << std::endl;
107 std::cout <<
"powFreq = " << powFreq << std::endl;
108 std::cout <<
"powFreq/powTime = " << powFreq/powTime << std::endl;
109 std::cout <<
"powFreq-powTime = " << powFreq-powTime << std::endl;
110 std::cout << std::endl << std::endl << std::endl;
113 assert(TMath::Abs(powFreq-powTime) < 1e-10);
122 std::cout <<
"powFreq_ave = " << powFreq_ave << std::endl;
123 std::cout <<
"powFreq_ave/powTime_ave = " << powFreq_ave/(powTime/n) << std::endl;
124 std::cout <<
"powFreq_ave-powTime_ave = " << powFreq_ave-(powTime/n) << std::endl;
125 std::cout << std::endl << std::endl << std::endl;
128 assert(TMath::Abs(powFreq_ave-(powTime/n)) < 1e-10);
135 std::cout <<
"powFreq_timeIntegral = " << powFreq_timeIntegral << std::endl;
136 std::cout <<
"powFreq_timeIntegral/powTime_ave = " << powFreq_timeIntegral/(dt*powTime) << std::endl;
137 std::cout <<
"powFreq_timeIntegral-powTime_ave = " << powFreq_timeIntegral-(dt*powTime) << std::endl;
138 std::cout << std::endl << std::endl << std::endl;
141 assert(TMath::Abs(powFreq_timeIntegral-(dt*powTime)) < 1e-10);
143 delete [] ps1_timeIntegral;
154 std::complex<double>* testFFT =
FancyFFTs::doFFT(gr1->GetN(), gr1->GetY(),
true);
156 TGraph* grTestInv =
new TGraph(gr1->GetN(), ts, testInvFFT);
157 grTestInv->SetTitle(
"Test of inverse fourier transform; Time (s); Amplitude (V)");
160 delete [] testInvFFT;
164 std::cout <<
"Checking conversion between fftw_complex and std::complex<double>... " << std::endl;
165 std::cout <<
"sizeof(std::complex<double>) = " <<
sizeof(std::complex<double>) << std::endl;
166 std::cout <<
"sizeof(fftw_complex) = " <<
sizeof(fftw_complex) << std::endl;
167 std::cout <<
"sizeof(double) = " <<
sizeof(double) << std::endl;
169 std::complex<double> x(1, 1);
170 std::cout <<
"std::complex<double> x(1, 1) = " << x << std::endl;
172 std::cout <<
"Doing memcpy(&y, &x, sizeof(fftw_complex))... " << std::endl;
173 memcpy(&y, &x,
sizeof(fftw_complex));
174 std::cout <<
"fftw_complex y has y[0] = " << y[0] <<
" and y[1] = " << y[1] << std::endl;
175 std::cout << std::endl << std::endl;
177 std::cout <<
"Now copying array of fftw_complex to std::complex<double> with mempcy..." << std::endl;
183 std::cout <<
"y2[2] = {{" << y2[0][0] <<
", " << y2[0][1] <<
"}, {" << y2[1][0] <<
", " << y2[1][1] <<
"}}" 185 std::complex<double> x2[2];
186 std::cout <<
"Doing memcpy(x2, y2, 2*sizeof(fftw_complex))" << std::endl;
187 memcpy(x2, y2, 2*
sizeof(fftw_complex));
188 std::cout <<
"x[0] = " << x2[0] <<
", x[1] = " << x2[1] << std::endl;
189 std::cout << std::endl << std::endl;
199 TGraph* grCor1 =
new TGraph(gr1->GetN(), ts, crossCorr1);
200 TGraph* grCor2 =
new TGraph(gr1->GetN(), ts, crossCorr2);
201 grCor1->SetTitle(
"Cross correlation");
203 grCor2->SetLineColor(kRed);
204 grCor2->Draw(
"l same");
218 gSystem->ProcessEvents();
219 std::cerr <<
"Select File->Quit to quit." << std::endl;
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).
double * crossCorrelate(int len, double *v1, double *v2, int threadInd=0)
Cross correlates the two input arrays.
complex< double > * doFFT(int len, double *input, bool copyOutputToNewArray, int threadInd=0)
Does a forward fast fourier transform on a real input array.
int printListOfKeys()
Prints the list of keys of plans to stdout.
double * getFreqArray(int len, double dt)
Get the array of frequencies to go with FFT output, determined by the tranform length and dt...
int getNumFreqs(int len)
Get the number of frequencies from the number of samples.
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.