From 1b35506339c2bb8ba858e5679c318d122a9122c9 Mon Sep 17 00:00:00 2001 From: nemu Date: Tue, 30 Sep 2008 05:37:02 +0000 Subject: [PATCH] some more testing towards fourier --- src/tests/fourier/Makefile.fourierPhaseTest | 94 ++++++++++++ src/tests/fourier/PMusrFourier.cpp | 62 ++++---- src/tests/fourier/fourierPhaseTest.cpp | 153 ++++++++++++++++++++ 3 files changed, 278 insertions(+), 31 deletions(-) create mode 100644 src/tests/fourier/Makefile.fourierPhaseTest create mode 100644 src/tests/fourier/fourierPhaseTest.cpp diff --git a/src/tests/fourier/Makefile.fourierPhaseTest b/src/tests/fourier/Makefile.fourierPhaseTest new file mode 100644 index 000000000..a352a7488 --- /dev/null +++ b/src/tests/fourier/Makefile.fourierPhaseTest @@ -0,0 +1,94 @@ +#--------------------------------------------------- +# Makefile.fourierPhaseTest +# +# Author: Andreas Suter +# e-mail: andreas.suter@psi.ch +# +# $Id$ +#--------------------------------------------------- + +#--------------------------------------------------- +# get compilation and library flags from root-config + +ROOTCFLAGS = $(shell $(ROOTSYS)/bin/root-config --cflags) +ROOTLIBS = $(shell $(ROOTSYS)/bin/root-config --libs) +ROOTGLIBS = $(shell $(ROOTSYS)/bin/root-config --glibs) + +#--------------------------------------------------- +# depending on the architecture, choose the compiler, +# linker, and the flags to use +# + +OSTYPE = linux + +ifeq ($(OSTYPE),linux) +OS = LINUX +endif +ifeq ($(OSTYPE),linux-gnu) +OS = LINUX +endif +ifeq ($(OSTYPE),darwin) +OS = DARWIN +endif + +# -- Linux +ifeq ($(OS),LINUX) +CXX = g++ +CXXFLAGS = -g -Wall -fPIC +PMUSRPATH = ./include +MNPATH = $(ROOTSYS)/include +INCLUDES = -I $(PMUSRPATH) -I $(MNPATH) +LD = g++ +LDFLAGS = -g +endif + +# -- Darwin +ifeq ($(OS),DARWIN) +CXX = g++ +CXXFLAGS = -g -Wall -fPIC +INCLUDES = -I./ +LD = g++ +LDFLAGS = -g +endif + +# the output from the root-config script: +CXXFLAGS += $(ROOTCFLAGS) +LDFLAGS += + +# the ROOT libraries (G = graphic) +LIBS = $(ROOTLIBS) -lfftw3 +GLIBS = $(ROOTGLIBS) -lfftw3 + +INSTALLPATH = $(HOME)/analysis/bin + +EXEC = fourierPhaseTest + +# some definitions: headers, sources, objects,... +OBJS = +OBJS += $(EXEC).o + +# make the executable: +# +all: $(EXEC) + +$(EXEC): $(OBJS) + @echo "---> Building $(EXEC) ..." + /bin/rm -f $(SHLIB) + $(LD) $(OBJS) -o $(EXEC) $(GLIBS) + @echo "done" + +# clean up: remove all object file (and core files) +# semicolon needed to tell make there is no source +# for this target! +# +clean:; @rm -f $(OBJS) + @echo "---> removing $(OBJS)" + +# +$(OBJS): %.o: %.cpp + $(CXX) $(INCLUDES) $(CXXFLAGS) -c $< + +install: all + @echo "copy $(EXEC) to $(INSTALLPATH)" + cp -p $(EXEC) $(INSTALLPATH) + diff --git a/src/tests/fourier/PMusrFourier.cpp b/src/tests/fourier/PMusrFourier.cpp index 896cf0c7e..2c317cfbb 100644 --- a/src/tests/fourier/PMusrFourier.cpp +++ b/src/tests/fourier/PMusrFourier.cpp @@ -107,6 +107,10 @@ PMusrFourier::PMusrFourier(int dataType, PDoubleVector &data, double timeResolut rebinCounter = 0; } } + } else { // no rebinning, just copy data + for (unsigned int i=0; i> sum = " << sum << endl; +cout << endl << "-> sum = " << fabs(sum) << ", min = " << min << ", min_phase = " << min_phase/PI*180.0; + if (p==-90) { + min = fabs(sum); + min_phase = corr_phase; +cout << endl << "!> min = " << min << ", min_phase = " << min_phase/PI*180.0; } - sumHist.SetBinContent(p+181, fabs(sum)); + if (fabs(sum) < min) { + min = fabs(sum); + min_phase = corr_phase; +cout << endl << "-> min = " << min << ", min_phase = " << min_phase/PI*180.0; + } + sumHist.SetBinContent(p+91, fabs(sum)); } +cout << endl << ">> min = " << min << ", min_phase = " << min_phase/PI*180.0; + +for (unsigned int i=0; i +using namespace std; + +#include "fftw3.h" + +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) +{ + if (argc != 2) { + cout << endl << "usage: fourierPhaseTest "; + cout << endl; + return -1; + } + + UInt_t noOfBins = (UInt_t)pow(2, 11); + Double_t freq = TMath::TwoPi() * 10.0 / (Double_t)noOfBins; + Double_t phase = 23.7 * TMath::Pi() / 180.0; + cout << endl << ">> noOfBins = " << noOfBins; + cout << endl << ">> freq = " << freq; + cout << endl << ">> phase = " << phase; + + fftw_plan fftwPlan; + fftw_complex *in; + fftw_complex *out; + + // allocate necessary memory + in = (fftw_complex *)fftw_malloc(sizeof(fftw_complex)*noOfBins); + out = (fftw_complex *)fftw_malloc(sizeof(fftw_complex)*noOfBins); + + // generate plan + fftwPlan = fftw_plan_dft_1d(noOfBins, in, out, FFTW_FORWARD, FFTW_ESTIMATE); + cout << endl << ">> fftwPlan = " << fftwPlan; + if (!fftwPlan) { + cout << endl << "**ERROR** Problems to establish a FFTW plan ..."; + cout << endl; + return -1; + } + + // fill data + cout << endl << ">> fill data ..."; + for (UInt_t i=0; iSetPoint(i, i, in[i][0]); + } + gin->Draw("ap"); + + fftw_execute(fftwPlan); + + // find peak + Double_t dval, peak = 0.0; + UInt_t peak_pos = 0; + for (UInt_t i=0; i> peak at peak_pos = " << peak_pos; + if (2*peak_pos > noOfBins/2) + peak_pos = noOfBins/4; + + TCanvas *canv_out = new TCanvas("canv_out", "phase test .."); + + TGraph *goutRe = new TGraph(noOfBins/2); + TGraph *goutIm = new TGraph(noOfBins/2); + + // estimate phase + Double_t sum; + Double_t corr_phase; + Double_t min, min_phase; + for (Int_t k=-90; k<90; k++) { + sum = 0.0; + corr_phase = (Double_t)k/180.0*TMath::Pi(); + for (UInt_t i=0; i<2*peak_pos; i++) { + sum += out[i][0]*sin(corr_phase) + out[i][1]*cos(corr_phase); + } + if (k == -90) { + min = fabs(sum); + min_phase = 0.0; + } + if (fabs(sum) < fabs(min)) { + min = fabs(sum); + min_phase = (Double_t)k; + } + cout << endl << ">> phase = " << k << ", sum = " << fabs(sum); + } + + cout << endl << ">> min = " << min << ", min phase = " << min_phase; + cout << endl; + + min_phase = -23.7*TMath::Pi()/180.0; + for (UInt_t i=0; iSetPoint(i, i, out[i][0]*cos(min_phase) - out[i][1]*sin(min_phase)); + goutIm->SetPoint(i, i, out[i][0]*sin(min_phase) + out[i][1]*cos(min_phase)); + } + goutRe->SetMarkerStyle(20); // bullet + goutRe->SetMarkerColor(TColor::GetColor(0,0,0)); // black + goutRe->SetLineColor(TColor::GetColor(0,0,0)); // black + goutRe->SetFillColor(TColor::GetColor(255,255,255)); // white + goutRe->SetTitle("Re"); + goutIm->SetMarkerStyle(29); // filled star + goutIm->SetMarkerColor(TColor::GetColor(255,0,0)); // red + goutIm->SetLineColor(TColor::GetColor(255,0,0)); // red + goutIm->SetFillColor(TColor::GetColor(255,255,255)); // white + goutIm->SetTitle("Im"); + goutRe->Draw("apl"); + goutIm->Draw("plsame"); + + // write data to file + TFile f(argv[1], "RECREATE"); + canv_in->Write(); + canv_out->Write(); + f.Close(); + + // clean up + delete canv_in; + delete gin; + delete canv_out; + delete goutRe; + delete goutIm; + + fftw_destroy_plan(fftwPlan); + fftw_free(in); + fftw_free(out); + + cout << endl; + + return 0; +}