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;
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)
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
00038 }
00039
00040
00041
00042
00043
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())
00049 throw CFatalException("In Correl: Master.size != Corr.size !");
00050 if (Output.size() != Master.size())
00051 Output.resize(Master.size());
00052
00053 TsSpectrum SpecCalc(false);
00054 tcompdata MasterSpec(Master.size()/2+1), CorrSpec(Corr.size()/2+1);
00055 SpecCalc.CalcSpectrum(Master.begin(),Master.end(), MasterSpec.begin(),MasterSpec.end());
00056 SpecCalc.CalcSpectrum(Corr.begin(),Corr.end(), CorrSpec.begin(),CorrSpec.end());
00057 tcompdata CrossPower;
00058 if (docorrel)
00059 {
00060 std::transform(MasterSpec.begin(),MasterSpec.end(),CorrSpec.begin(),back_inserter(CrossPower),
00061 boost::bind<std::complex<double> >(
00062 std::multiplies<std::complex<double> >(),
00063 _1,
00064 boost::bind<std::complex<double> >(&std::conj<double>,_2)));
00065 }
00066 else
00067 {
00068 std::transform(MasterSpec.begin(),MasterSpec.end(),CorrSpec.begin(),back_inserter(CrossPower),
00069 std::multiplies<std::complex<double> >());
00070 }
00071 SpecCalc.CalcTimeSeries(CrossPower.begin(),CrossPower.end(),Output.begin(),Output.end());
00072 }
00073
00074
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
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