00001 #include <iostream>
00002 #include <iterator>
00003 #include <fstream>
00004 #include "TsSpectrum.h"
00005 #include "TimeSeriesData.h"
00006 #include "statutils.h"
00007 #include "MTStation.h"
00008 #include "types.h"
00009 #include "WFunc.h"
00010 #include "TimeFrequency.h"
00011 #include "VecMat.h"
00012 #include "MtuFilter.h"
00013 #include "Util.h"
00014
00015 using namespace std;
00016 using namespace gplib;
00017
00018 string version =
00019 "$Id: simple_processing.cpp 1816 2009-09-07 11:28:35Z mmoorkamp $";
00020 int main(int argc, char *argv[])
00021 {
00022 cout
00023 << "This is simpleproc: Simple most estimation of MT transfer function, spectral stacking"
00024 << endl << endl;
00025 cout << " Usage simpleproc inputfilename";
00026 cout
00027 << " Output will have the same name as MTU Input with '.mtt' appended "
00028 << endl << endl;
00029 cout << " This is Version: " << version << endl << endl;
00030
00031 TimeSeriesData MtuData;
00032 MTStation Output;
00033 string filename, basefilename;
00034 const unsigned int seglength = 2400;
00035
00036 if (argc == 2)
00037 {
00038 filename = argv[1];
00039 }
00040 else
00041 {
00042 filename = AskFilename("Time Series Filename: ");
00043 }
00044 string datafilename = filename;
00045 size_t dotpos = filename.find('.', 0);
00046 if (dotpos != string::npos)
00047 basefilename = filename.erase(dotpos);
00048 string exfiltername = basefilename + "_1.cts";
00049 string eyfiltername = basefilename + "_2.cts";
00050 string hxfiltername = basefilename + "_3.cts";
00051 string hyfiltername = basefilename + "_4.cts";
00052 MtuData.GetData(datafilename.c_str());
00053
00054 const unsigned int nfreqs = seglength / 2 + 1;
00055 tcompdata ExHxCorr(nfreqs, 0);
00056 tcompdata ExHyCorr(nfreqs, 0);
00057 tcompdata EyHxCorr(nfreqs, 0);
00058 tcompdata EyHyCorr(nfreqs, 0);
00059 tcompdata HxHxCorr(nfreqs, 0);
00060 tcompdata HxHyCorr(nfreqs, 0);
00061 tcompdata HyHyCorr(nfreqs, 0);
00062 tcompdata Hdet(nfreqs, 0);
00063 tcompdata Zxx(nfreqs, 0);
00064 tcompdata Zxy(nfreqs, 0);
00065 tcompdata Zyx(nfreqs, 0);
00066 tcompdata Zyy(nfreqs, 0);
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 double freqstep = MtuData.GetData().GetSamplerate() / (seglength);
00077 MtuFilter ExFilter(seglength, freqstep), EyFilter(seglength, freqstep),
00078 HxFilter(seglength, freqstep), HyFilter(seglength, freqstep);
00079
00080 ExFilter.GetData(exfiltername);
00081 EyFilter.GetData(eyfiltername);
00082 HxFilter.GetData(hxfiltername);
00083 HyFilter.GetData(hyfiltername);
00084
00085 gplib::cmat ExTimeFrequency, EyTimeFrequency, HxTimeFrequency,
00086 HyTimeFrequency;
00087
00088 ExTimeFrequency = TimeFrequency(
00089 MtuData.GetData().GetEx().GetData().begin(),
00090 MtuData.GetData().GetEx().GetData().end(), seglength, Hamming());
00091 EyTimeFrequency = TimeFrequency(
00092 MtuData.GetData().GetEy().GetData().begin(),
00093 MtuData.GetData().GetEy().GetData().end(), seglength, Hamming());
00094 HxTimeFrequency = TimeFrequency(
00095 MtuData.GetData().GetHx().GetData().begin(),
00096 MtuData.GetData().GetHx().GetData().end(), seglength, Hamming());
00097 HyTimeFrequency = TimeFrequency(
00098 MtuData.GetData().GetHy().GetData().begin(),
00099 MtuData.GetData().GetHy().GetData().end(), seglength, Hamming());
00100
00101 const unsigned int nsegs = ExTimeFrequency.size1();
00102 ofstream logfile((datafilename + ".log").c_str());
00103 for (size_t i = 0; i < Zxx.size(); ++i)
00104 {
00105 for (size_t j = 0; j < nsegs; ++j)
00106 {
00107 ExTimeFrequency(j, i) /= ExFilter.GetFilterCoeff().at(i);
00108 EyTimeFrequency(j, i) /= EyFilter.GetFilterCoeff().at(i);
00109 HxTimeFrequency(j, i) /= HxFilter.GetFilterCoeff().at(i);
00110 HyTimeFrequency(j, i) /= HyFilter.GetFilterCoeff().at(i);
00111 ExHxCorr.at(i) += (ExTimeFrequency(j, i) * conj(HxTimeFrequency(j,
00112 i)));
00113 ExHyCorr.at(i) += (ExTimeFrequency(j, i) * conj(HyTimeFrequency(j,
00114 i)));
00115 EyHxCorr.at(i) += (EyTimeFrequency(j, i) * conj(HxTimeFrequency(j,
00116 i)));
00117 EyHyCorr.at(i) += (EyTimeFrequency(j, i) * conj(HyTimeFrequency(j,
00118 i)));
00119 HxHyCorr.at(i) += (HxTimeFrequency(j, i) * conj(HyTimeFrequency(j,
00120 i)));
00121 HxHxCorr.at(i) += (HxTimeFrequency(j, i) * conj(HxTimeFrequency(j,
00122 i)));
00123 HyHyCorr.at(i) += (HyTimeFrequency(j, i) * conj(HyTimeFrequency(j,
00124 i)));
00125
00126 }
00127
00128 Hdet.at(i) = (HxHxCorr.at(i) * HyHyCorr.at(i) - HxHyCorr.at(i) * conj(
00129 HxHyCorr.at(i)));
00130 logfile << "Hdet: " << Hdet.at(i) << endl;
00131 Zxx.at(i) = ((ExHxCorr.at(i) * HyHyCorr.at(i) - ExHyCorr.at(i) * conj(
00132 HxHyCorr.at(i))) / Hdet.at(i));
00133 Zxy.at(i) = ((ExHyCorr.at(i) * HxHxCorr.at(i) - ExHxCorr.at(i)
00134 * HxHyCorr.at(i)) / Hdet.at(i));
00135 Zyx.at(i) = ((EyHxCorr.at(i) * HyHyCorr.at(i) - EyHyCorr.at(i) * conj(
00136 HxHyCorr.at(i))) / Hdet.at(i));
00137 Zyy.at(i) = ((EyHyCorr.at(i) * HxHxCorr.at(i) - EyHxCorr.at(i)
00138 * HxHyCorr.at(i)) / Hdet.at(i));
00139 }
00140 Output.AssignAll(Zxx.size());
00141 for (size_t i = 0; i < Zxx.size(); ++i)
00142 {
00143 MTTensor
00144 CurrZ(Zxx.at(i), Zxy.at(i), Zyx.at(i), Zyy.at(i), i * freqstep);
00145 Output.SetMTData().at(i) = CurrZ;
00146 }
00147 vector<double> transferfunc(Zxx.size() * 2 - 1, 0.0);
00148 TsSpectrum().CalcTimeSeries(Zxy.begin(), Zxy.end(), transferfunc.begin(),
00149 transferfunc.end());
00150 ofstream tffile((datafilename + ".tf").c_str());
00151 copy(transferfunc.begin(), transferfunc.end(), ostream_iterator<double> (
00152 tffile, "\n"));
00153
00154 Output.WriteAsMtt(datafilename.c_str());
00155 }