miscfunc.cpp

Go to the documentation of this file.
00001 #include "miscfunc.h"
00002 #include "CFatalException.h"
00003 #include <numeric>
00004 #include <algorithm>
00005 #include <fftw3.h>
00006 #include <gsl/gsl_math.h>
00007 #include <fstream>
00008 #include <iomanip>
00009 #include <boost/bind.hpp>
00010 #include <complex>
00011 using namespace std;
00012 
00013 void Hilbert(std::vector<double>::iterator Inputbegin,std::vector<double>::iterator Inputend, std::vector<double> &Output)
00014 {
00015         fftw_complex *timedomain;
00016         fftw_complex *freqdomain;
00017         fftw_plan p;
00018         double swapstore;
00019         const unsigned int size = distance(Inputbegin,Inputend);
00020         if (Output.size() != size)
00021                 Output.assign(size,0);
00022                 
00023         timedomain = (fftw_complex *)fftw_malloc(sizeof(fftw_complex) * size);
00024         freqdomain = (fftw_complex *)fftw_malloc(sizeof(fftw_complex) * size);
00025         p = fftw_plan_dft_1d(size,timedomain,freqdomain,-1,FFTW_ESTIMATE);      
00026         for (unsigned int i=0; i < size; ++i)
00027         {
00028                 timedomain[i][0] = *(Inputbegin+i) * ( 0.5 - 0.5 * cos(2 * PI *i /size));
00029                 timedomain[i][1] = 0;
00030         }
00031         fftw_execute(p);
00032         for (unsigned int i=0; i < size; ++i)
00033         {
00034                 swapstore = freqdomain[i][0];
00035                 if (i < size/2)
00036                 {
00037                         freqdomain[i][0] = -freqdomain[i][1];
00038                         freqdomain[i][1] = swapstore;
00039                 }
00040                 else
00041                 {
00042                         freqdomain[i][0] = freqdomain[i][1];
00043                         freqdomain[i][1] = -swapstore;
00044                 }
00045         }
00046         p = fftw_plan_dft_1d(size,freqdomain,timedomain,1,FFTW_ESTIMATE);
00047         fftw_execute(p);        
00048         for (unsigned int i = 0; i < size; ++i)
00049                 Output.at(i) = 1./double(size) * timedomain[i][0];
00050         fftw_destroy_plan(p);
00051         fftw_free(timedomain);
00052         fftw_free(freqdomain);
00053 }
00054 
00055 
00056 
00057 void NormEnvelope(std::vector<double>::iterator Inputbegin,std::vector<double>::iterator Inputend , std::vector<double> &Output)
00058 {
00059         const int size = distance(Inputbegin,Inputend);
00060         vector<double> Transformed(size);
00061         double maximum;
00062         Output.assign(size,0);
00063         Hilbert(Inputbegin,Inputend,Transformed);
00064         for (int i = 0; i < size; ++i)
00065                 Output.at(i) = sqrt(pow(*(Inputbegin+i),2)+pow(Transformed.at(i),2));
00066         maximum = *max_element(Output.begin(),Output.end());
00067         for (int i = 0; i < size; ++i)
00068                 Output.at(i) /= maximum;
00069 }
00070 
00071 
00072 double Cross(ttsdata Master, ttsdata Corr, const int startpoint, const int endpoint)
00073 {
00074         const int size = endpoint - startpoint;
00075         double cross = 0;
00076         double auto1 = 0;
00077         double auto2 = 0;
00078         const double mean1 = accumulate(Master.begin()+startpoint,Master.begin()+endpoint,0.)/size;
00079         const double mean2 = accumulate(Corr.begin()+startpoint,Corr.begin()+endpoint,0.)/size;
00080         for (int i = startpoint; i < endpoint; ++i)
00081         {
00082                 cross += (Master.at(i)-mean1)*(Corr.at(i)-mean2);
00083                 auto1 += gsl_pow_2(Master.at(i)-mean1);
00084                 auto2 += gsl_pow_2(Corr.at(i)-mean2);
00085         }
00086         if (gsl_fcmp(0.0,auto1,GSL_SQRT_DBL_EPSILON) || gsl_fcmp(0.0,auto2,GSL_SQRT_DBL_EPSILON) )
00087                 return(cross / (sqrt (auto1) * sqrt (auto2)));
00088         else 
00089                 return 0.0;
00090 }
00091 
00092 void index(const trealdata data, vector<int> &result)
00093 {
00094     int i,j;
00095     int a;
00096         const int freqnum = result.size();
00097     for (i= 0; i < freqnum; ++i)
00098                 result.at(i) = i;
00099     for (j = 1; j < freqnum; ++j)
00100     {
00101                 a = result.at(j);
00102                 i = j - 1;
00103                 while (i >= 0 && data.at(result.at(i)) > data.at(a))
00104                 {
00105                 result.at(i+1) = result.at(i);
00106                 --i;
00107                 }
00108                 result.at(i+1) = a;
00109     }
00110 }
00111 
00112 
00113 
00114 
00115 
00116 
00117 
00118 
00119 
00120 
00121 
00122 
00123 
00124 
00125 
00126 
00127 
00128 
00129 

Generated on Fri Jun 27 13:05:10 2008 for GPLIB++ by  doxygen 1.5.5