simplenoisecancel.cpp

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <vector>
00003 #include <fstream>
00004 #include "RLSCanceller.h"
00005 #include "LMSCanceller.h"
00006 
00007 using namespace std;
00008 using namespace gplib;
00009 
00010 int main()
00011   {
00012     const int filterlength = 100; // set the length of the filter
00013     const double mu = 1e-14; // the stepsize for adaptation
00014     const double secfactor = 5; // the factor between the value calculated and the value used
00015     const int shift = filterlength / 4; // the shift in index between the first point in the segment and the value used as reference
00016 
00017     vector<double> input, desired;//declare vectors that hold the input data, the reference data
00018     // the filter output and the difference to the original time series
00019     string inputfilename, reffilename, outputfilename; // the names of the input and output files
00020     ifstream inputfile, reffile; // the file objects for reading
00021     ofstream outfile, weightfile, epsfile; // and writingy
00022     double currentnumber; // we have to read in a bunch of numbers and use this a temporary storage
00023 
00024     cout << "Inputfile: ";
00025     cin >> inputfilename; // read in inputfilename
00026     cout << "Reference Filename: ";
00027     cin >> reffilename; // and reference filename
00028 
00029     inputfile.open(inputfilename.c_str()); //open inputfile stream for reading
00030     reffile.open(reffilename.c_str()); // same for reference file stream
00031     outfile.open((reffilename + ".clean").c_str()); //open outputfile with an automatically generated name
00032     weightfile.open((reffilename + ".weight").c_str());
00033     epsfile.open((reffilename + ".eps").c_str());
00034     while (inputfile.good()) // while no errors occur
00035       {
00036         inputfile >> currentnumber; // read in a number from the input file
00037         input.push_back(currentnumber); // add it to the end of the input vector
00038       }
00039 
00040     while (reffile.good()) // while no errors occur
00041       {
00042         reffile >> currentnumber; // read in a number from the reference file
00043         desired.push_back(currentnumber);// add it to the end of the reference vector
00044       }
00045     inputfile.close(); //close both files
00046     reffile.close();
00047     vector<double> output(input.size()), epsilon(input.size());
00048     LMSCanceller Canceller(filterlength); // declare a new object of type LMSCanceller
00049     Canceller.SetMu(mu);
00050 
00051     gplib::rvec currentinput(filterlength), currdesired(1), currout(1);
00052     copy(input.begin(), input.begin() + filterlength, currentinput.begin());
00053     //for (int i = filterlength-1; i >= 0; --i)
00054     //  currentinput(i) = input[filterlength-1-i];
00055     //currdesired(0) = desired.at(filterlength/2);
00056 
00057     //Canceller.CalcOutput(currentinput,currout);
00058     //output.at(0) = currout(0);
00059     //epsilon.at(0) = Canceller.GetEpsilon()(0);
00060     for (int i = 0; i < input.size() - filterlength - 1; ++i)
00061       {
00062         Canceller.CalcOutput(currentinput, currout);
00063         Canceller.PrintWeights(weightfile);
00064         currdesired(0) = desired.at(i + shift);
00065         // Canceller.GetFilterOutput().front();
00066 
00067         Canceller.AdaptFilter(currentinput, currdesired);
00068         output.at(i + filterlength - 1) = currout(0);
00069         epsilon.at(i + filterlength - 1) = Canceller.GetEpsilon()(0);
00070         rotate(currentinput.begin(), currentinput.begin() + 1,
00071             currentinput.end());
00072         currentinput(filterlength - 1) = input.at(i + filterlength);
00073       }
00074 
00075     for (int i = shift; i < output.size(); ++i) //output the output data
00076       outfile << output.at(i) << endl; //to outputfile
00077     for (int i = shift; i < epsilon.size(); ++i) //output the difference data
00078       epsfile << epsilon.at(i) << endl; //to difference file
00079     //for (int i = 0; i < Canceller.Weights.size(); ++i)
00080     //  weightfile << Canceller.Weights.at(i) << endl;
00081     outfile.close(); // close file
00082     weightfile.close();
00083     epsfile.close();
00084   }

Generated on Tue May 4 16:52:15 2010 for GPLIB++ by  doxygen 1.5.8