miscfunc.h

Go to the documentation of this file.
00001 #ifndef MISCFUNC_H_INCLUDED
00002 #define MISCFUNC_H_INCLUDED
00003 #include "types.h"
00004 #include <vector>
00005 #include "CFatalException.h"
00006 #include "TsSpectrum.h"
00007 #include <boost/numeric/ublas/vector.hpp>
00008 #include <iostream>
00009 #include <functional>
00010 
00011 class CMTStation; //forward declaration, we include miscfunc.h from CMTStation
00012 namespace ublas = boost::numeric::ublas;
00013 void NormEnvelope(std::vector<double>::iterator Inputbegin,std::vector<double>::iterator Inputend , std::vector<double> &Output);
00014 void Hilbert(std::vector<double>::iterator Inputbegin,std::vector<double>::iterator Inputend , std::vector<double> &Output);
00015 double Cross(ttsdata Master, ttsdata Corr, const int startpoint, const int endpoint);
00016 
00017 
00018 
00019 inline double Commute(dcomp A, dcomp B) //calculates Commutator of two complex numbers
00020 {
00021     return(A.real() * B.imag() - B.real() * A.imag());
00022     
00023 }
00024 
00025 inline int FuzzComp(double par1, double par2, double tolerance)
00026 { 
00027     double lower;
00028     double higher;
00029     double target;
00030     
00031     target = par2;
00032     lower = par1 - tolerance;
00033     higher = par1 + tolerance;
00034     if ( (lower <= par2) && ( higher >= par2) )
00035         return(1);
00036         else return(0);
00037     //return (((par1-tolerance) <= par2) && ((par1+tolerance) >= par2)); 
00038 }
00039 
00040 //! perform spectral multipliaction on two vector style container and write the result to output
00041 
00042 /*! Do_Spec_Mul takes two input vectors, Master and Corr of the same length, writes either the convolution or correlation
00043  * into the vector output, depending on docorrel
00044  */
00045 template<typename ts_type>
00046 void Do_Spec_Mul(const ts_type &Master,const ts_type &Corr, ts_type &Output,const bool docorrel)
00047 {
00048         if (Corr.size() != Master.size()) //if input sizes are different
00049                 throw CFatalException("In Correl: Master.size != Corr.size !"); //throw exception
00050         if (Output.size() != Master.size()) //if ouput vector doesn't have the right length
00051             Output.resize(Master.size()); //adjust
00052         
00053         TsSpectrum SpecCalc(false); //Create an object to calculate spectra
00054         tcompdata MasterSpec(Master.size()/2+1), CorrSpec(Corr.size()/2+1); //some temporary objects
00055         SpecCalc.CalcSpectrum(Master.begin(),Master.end(), MasterSpec.begin(),MasterSpec.end()); // calc spectrum for master
00056         SpecCalc.CalcSpectrum(Corr.begin(),Corr.end(), CorrSpec.begin(),CorrSpec.end()); // and for corr
00057         tcompdata CrossPower;
00058         if (docorrel) //if we want to do a correlation
00059         {
00060                 std::transform(MasterSpec.begin(),MasterSpec.end(),CorrSpec.begin(),back_inserter(CrossPower), //we have to multiply
00061                                 boost::bind<std::complex<double> >( //Master elements with the conjugate of Corr elements and write
00062                                                         std::multiplies<std::complex<double> >(), //into CrossPower
00063                                                         _1,
00064                                                         boost::bind<std::complex<double> >(&std::conj<double>,_2)));
00065         }
00066         else //if convolution
00067         {
00068                 std::transform(MasterSpec.begin(),MasterSpec.end(),CorrSpec.begin(),back_inserter(CrossPower),
00069                                                                 std::multiplies<std::complex<double> >()); //just multiply elements in Master with elements in Corr
00070         }
00071         SpecCalc.CalcTimeSeries(CrossPower.begin(),CrossPower.end(),Output.begin(),Output.end()); //Calculate the timeseries from CrossPower
00072 }
00073 
00074 //! A convenience wrapper to calculate correlations
00075 template<typename ts_type>
00076 void Correl(const ts_type &Master,const ts_type &Corr, ts_type &Output)
00077 {
00078         Do_Spec_Mul(Master,Corr,Output,true);
00079 }
00080 //! A convenience wrapper to calculate convolution
00081 template<typename ts_type>
00082 void Convolve(const ts_type &Master,const ts_type &Corr, ts_type &Output)
00083 {
00084         Do_Spec_Mul(Master,Corr,Output,false);
00085 }
00086 #endif
00087 
00088 
00089 

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