mtulmscancel.cpp

Go to the documentation of this file.
00001 #include <fstream>
00002 #include <iostream>
00003 #include <vector>
00004 #include <string>
00005 #include <algorithm>
00006 #include "LMSCanceller.h"
00007 #include "RLSCanceller.h"
00008 #include "TimeSeriesData.h"
00009 #include "CLmsConfig.h"
00010 #include "ApplyFilter.h"
00011 #include "netcdfcpp.h"
00012 
00013 using namespace std;
00014 string version = "$Id: mtulmscancel.cpp 865 2007-03-02 17:47:39Z max $";
00015 
00016 
00017 void WriteNetCDF(const string name,const ublas::matrix<double,boost::numeric::ublas::column_major> &Matrix)
00018 {
00019         NcFile mtrescdf(name.c_str(),NcFile::Replace);
00020         NcDim* xd = mtrescdf.add_dim("x",Matrix.size1());
00021         NcDim* yd = mtrescdf.add_dim("y",Matrix.size2());
00022         NcVar* x = mtrescdf.add_var("x",ncFloat,xd);
00023         NcVar* y = mtrescdf.add_var("y",ncFloat,yd);
00024         NcVar* z = mtrescdf.add_var("z",ncFloat,xd,yd);
00025         float *xvals = new float[xd->size()];
00026         float *yvals = new float[yd->size()];
00027         float *zvals = new float[xd->size()*yd->size()];
00028         for (size_t i = 0; i < Matrix.size1(); ++i)
00029         {
00030                 xvals[i] = i;
00031                 for (size_t j = 0; j < Matrix.size2(); ++j)
00032                 {
00033                         yvals[j] = j;
00034                         zvals[(Matrix.size1()-1-i)*Matrix.size2() +j] = Matrix(i,j);
00035                 }
00036         }       
00037         x->put(xvals,xd->size());
00038         y->put(yvals,yd->size());
00039         z->put(zvals,z->edges());
00040 }
00041 
00042 
00043 int main()
00044 {
00045         cout << "This is lmcancel: Use an lms-adaptive filter to cancel noise in MT time series" << endl<< endl;
00046         cout  << " The program will ask for reference and input filename, further settings are read from 'lms.conf' " << endl;
00047         cout << " Output will be 2 Phoenix format files with ending '.clean' and '.eps'" << endl<< endl;
00048         cout << " This is Version: " << version << endl << endl;
00049         
00050         CLmsConfig Configuration;
00051         TimeSeriesData InputData,ReferenceData;
00052         
00053         int index;
00054         string tsfilename, noisefilename;
00055         
00056         int ntimeseries;
00057         Configuration.GetData("lms.conf");
00058         LMSCanceller Filter(Configuration.filterlength); 
00059         Filter.SetMu(Configuration.mu);
00060         
00061         ApplyFilter Canceller(Filter,true);
00062         
00063         
00064         cout << "Reference Data: ";
00065         cin >> noisefilename;
00066         ReferenceData.GetData(noisefilename);
00067         cout << "Component Index (Hx,Hy,Ex,Ey): ";
00068         cin >> index;
00069         TimeSeriesComponent *RefComp;
00070         switch (index)
00071         {
00072                 case 1:
00073                         RefComp = &ReferenceData.GetData().GetHx();
00074                 break;
00075                 case 2:
00076                         RefComp = &ReferenceData.GetData().GetHy();
00077                 break;
00078                 case 3:
00079                         RefComp = &ReferenceData.GetData().GetEx();
00080                 break;
00081                 case 4:
00082                         RefComp = &ReferenceData.GetData().GetEy();
00083                 break;
00084                 default:
00085                         cerr << "Component index not valid !";
00086                         return 100;
00087                 break;
00088         }
00089         Canceller.AddReferenceChannel(*RefComp);
00090         cout << "Time Series Filename: ";
00091         cin >> tsfilename;
00092         InputData.GetData(tsfilename);
00093         cout << "Number of InputData: ";
00094         cin >> ntimeseries;
00095         
00096         for (int i=0; i < ntimeseries; ++i)
00097         {
00098                 cout << "Component Index (Hx,Hy,Ex,Ey): ";
00099                 cin >> index;
00100                 switch (index)
00101                 {
00102                         case 1:
00103                                 Canceller.AddInputChannel(InputData.GetData().GetHx());
00104                         break;
00105                         case 2:
00106                                 Canceller.AddInputChannel(InputData.GetData().GetHy());
00107                         break;
00108                         case 3:
00109                                 Canceller.AddInputChannel(InputData.GetData().GetEx());
00110                         break;
00111                         case 4:
00112                                 Canceller.AddInputChannel(InputData.GetData().GetEy());
00113                         break;
00114                         default:
00115                                 cerr << "Component index not valid !";
00116                                 return 100;
00117                         break;
00118                 }
00119         }
00120         Canceller.SetWeightSaveIntervall(1000);
00121         Canceller.SetShift(Configuration.shift);
00122         Canceller.FilterData();
00123         Canceller.FilterData();
00124         
00125         //*RefComp = *Canceller.GetOutChannels().front();
00126         copy(Canceller.GetOutChannels().front()->GetData().begin(),Canceller.GetOutChannels().front()->GetData().end(),RefComp->GetData().begin());
00127         ReferenceData.WriteAsMtu(noisefilename+".clean");
00128         ofstream cleanfile((noisefilename+".clean").c_str());
00129         copy(Canceller.GetOutChannels().front()->GetData().begin(),Canceller.GetOutChannels().front()->GetData().end(),ostream_iterator<double>(cleanfile,"\n"));
00130         copy(Canceller.GetEpsValues().front().begin(),Canceller.GetEpsValues().front().end(),RefComp->GetData().begin());
00131         
00132         ReferenceData.WriteAsMtu(noisefilename+".eps");
00133         ofstream epsfile((noisefilename+".eps").c_str());
00134         copy(Canceller.GetEpsValues().front().begin(),Canceller.GetEpsValues().front().end(),ostream_iterator<double>(epsfile,"\n"));
00135         
00136         //cout << "Last Weights: " << endl;
00137         //Filter.PrintWeights(cout);
00138         //cout << endl;
00139         ofstream weightfile((noisefilename+".weights").c_str());
00140         copy(Filter.GetWeights().begin(),Filter.GetWeights().end(),ostream_iterator<double>(weightfile,"\n"));
00141         WriteNetCDF(noisefilename+".weights.nc",Canceller.GetWeightHistory());
00142 }

Generated on Tue Aug 4 16:04:06 2009 for GPLIB++ by  doxygen 1.5.8