GPLIB++
simplenoisecancel.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <vector>
3 #include <fstream>
4 #include "RLSCanceller.h"
5 #include "LMSCanceller.h"
6 
7 using namespace std;
8 using namespace gplib;
9 
10 int main()
11  {
12  const int filterlength = 100; // set the length of the filter
13  const double mu = 1e-14; // the stepsize for adaptation
14  const double secfactor = 5; // the factor between the value calculated and the value used
15  const int shift = filterlength / 4; // the shift in index between the first point in the segment and the value used as reference
16 
17  vector<double> input, desired;//declare vectors that hold the input data, the reference data
18  // the filter output and the difference to the original time series
19  string inputfilename, reffilename, outputfilename; // the names of the input and output files
20  ifstream inputfile, reffile; // the file objects for reading
21  ofstream outfile, weightfile, epsfile; // and writingy
22  double currentnumber; // we have to read in a bunch of numbers and use this a temporary storage
23 
24  cout << "Inputfile: ";
25  cin >> inputfilename; // read in inputfilename
26  cout << "Reference Filename: ";
27  cin >> reffilename; // and reference filename
28 
29  inputfile.open(inputfilename.c_str()); //open inputfile stream for reading
30  reffile.open(reffilename.c_str()); // same for reference file stream
31  outfile.open((reffilename + ".clean").c_str()); //open outputfile with an automatically generated name
32  weightfile.open((reffilename + ".weight").c_str());
33  epsfile.open((reffilename + ".eps").c_str());
34  while (inputfile.good()) // while no errors occur
35  {
36  inputfile >> currentnumber; // read in a number from the input file
37  input.push_back(currentnumber); // add it to the end of the input vector
38  }
39 
40  while (reffile.good()) // while no errors occur
41  {
42  reffile >> currentnumber; // read in a number from the reference file
43  desired.push_back(currentnumber);// add it to the end of the reference vector
44  }
45  inputfile.close(); //close both files
46  reffile.close();
47  vector<double> output(input.size()), epsilon(input.size());
48  LMSCanceller Canceller(filterlength); // declare a new object of type LMSCanceller
49  Canceller.SetMu(mu);
50 
51  gplib::rvec currentinput(filterlength), currdesired(1), currout(1);
52  copy(input.begin(), input.begin() + filterlength, currentinput.begin());
53  //for (int i = filterlength-1; i >= 0; --i)
54  // currentinput(i) = input[filterlength-1-i];
55  //currdesired(0) = desired.at(filterlength/2);
56 
57  //Canceller.CalcOutput(currentinput,currout);
58  //output.at(0) = currout(0);
59  //epsilon.at(0) = Canceller.GetEpsilon()(0);
60  for (int i = 0; i < input.size() - filterlength - 1; ++i)
61  {
62  Canceller.CalcOutput(currentinput, currout);
63  Canceller.PrintWeights(weightfile);
64  currdesired(0) = desired.at(i + shift);
65  // Canceller.GetFilterOutput().front();
66 
67  Canceller.AdaptFilter(currentinput, currdesired);
68  output.at(i + filterlength - 1) = currout(0);
69  epsilon.at(i + filterlength - 1) = Canceller.GetEpsilon()(0);
70  rotate(currentinput.begin(), currentinput.begin() + 1,
71  currentinput.end());
72  currentinput(filterlength - 1) = input.at(i + filterlength);
73  }
74 
75  for (int i = shift; i < output.size(); ++i) //output the output data
76  outfile << output.at(i) << endl; //to outputfile
77  for (int i = shift; i < epsilon.size(); ++i) //output the difference data
78  epsfile << epsilon.at(i) << endl; //to difference file
79  //for (int i = 0; i < Canceller.Weights.size(); ++i)
80  // weightfile << Canceller.Weights.at(i) << endl;
81  outfile.close(); // close file
82  weightfile.close();
83  epsfile.close();
84  }
int main()
Implements a LMS adaptive filter.
Definition: LMSCanceller.h:19