GPLIB++
ApplyFilter.cpp
Go to the documentation of this file.
1 #include "ApplyFilter.h"
2 #include "FatalException.h"
3 #include <boost/progress.hpp>
4 
5 namespace gplib
6  {
7 
8  ApplyFilter::ApplyFilter(AdaptiveFilter &TheFilter, bool keephistory) :
9  showprogress(false), wanthistory(keephistory), shift(0),
10  Filter(TheFilter), weightsaveintervall(TheFilter.GetOutputLength())
11  {
12  }
13 
15  {
16  }
17 
18  /*! Return a matrix with the values of the weights at iterations specified by weightsaveintervall.
19  * Internally we use a std::vector of ublas::vector and we construct the matrix in this routine.
20  * Therefore we return a copy and the access is computationally reasonably expensive. Mostly
21  * needed for final display of weights.
22  */
24  {
25  if (WeightHistory.empty())
26  throw FatalException("Trying to access empty Weight History");
27  const size_t xsize = WeightHistory.size();
28  const size_t ysize = WeightHistory.front().size();
29  gplib::rmat temp(xsize, ysize);
30  for (size_t i = 0; i < xsize; ++i)
31  for (size_t j = 0; j < ysize; ++j)
32  temp(i, j) = WeightHistory.at(i)(j);
33  return temp;
34  }
35 
37  {
38  if (!InputChannels.empty() && Channel.GetData().size()
39  != InputChannels.front()->GetData().size())
40  throw FatalException(
41  "Input channel has incompatible size to first channel");
42  InputChannels.push_back(&Channel);
43  }
44 
46  {
47  if (RefChannels.size() >= Filter.GetOutputLength()) // if we already have enough reference channels
48  throw FatalException(
49  "Trying to add more reference channels than permitted by Adaptive Filter class");
50  if (!RefChannels.empty() && Channel.GetData().size()
51  != RefChannels.front()->GetData().size())
52  throw FatalException(
53  "Ref channel has incompatible size to first channel");
54  RefChannels.push_back(&Channel);
55  }
56 
58  {
59  const int ninputchannels = InputChannels.size();
60  const int datalength = InputChannels.front()->GetData().size();
61  const int inputsize = Filter.GetInputLength();
62  const int outputsize = Filter.GetOutputLength();
63  const int pointsperchannel = inputsize / ninputchannels;
64 
65  gplib::rvec currentinput(inputsize), currdesired(outputsize), currout(
66  outputsize);
67  //std::vector<double> eps;
68  //EpsValues.assign(outputsize,eps);
69 
70  if (!EpsValues.empty()) // if we have run this routine before
71  {
72  EpsValues.clear(); //clear errors
73  OutChannels.clear(); // and output channels, but not weights
74  }
75 
76  for (int i = 0; i < outputsize; ++i)
77  {
78  std::vector<double> temp;
79  EpsValues.push_back(temp);
80  OutChannels.push_back(boost::shared_ptr<TimeSeriesComponent>(
81  new TimeSeriesComponent));
82  OutChannels.at(i)->GetData().reserve(
83  RefChannels.at(i)->GetData().size());
84  EpsValues.at(i).reserve(RefChannels.at(i)->GetData().size());
85  //if (shift > 2)
86  OutChannels.at(i)->GetData().insert(
87  OutChannels.at(i)->GetData().begin(), shift, 0.0);
88  EpsValues.at(i).insert(EpsValues.at(i).begin(), shift, 0.0);
89  }
90  const int maxindex = datalength - pointsperchannel - shift;
91  boost::progress_display *progressbar = NULL;
92  if (showprogress) // the constructor already displays something, so we use a pointer and only construct when needed
93  {
94  progressbar = new boost::progress_display(maxindex); // init progress bar
95  }
96  for (int i = 0; i < maxindex; ++i)
97  {
98 
99  for (int j = 0; j < ninputchannels; ++j)
100  {
101  copy(InputChannels.at(j)->GetData().begin() + i,
102  InputChannels.at(j)->GetData().begin() + pointsperchannel
103  + i, currentinput.begin() + j * pointsperchannel);
104  }
105  for (int j = 0; j < outputsize; ++j)
106  currdesired(j) = RefChannels.at(j)->GetData().at(i + shift);
107 
108  Filter.CalcOutput(currentinput, currout);
109  Filter.AdaptFilter(currentinput, currdesired);
110  if (showprogress)
111  {
112  ++(*progressbar); // make sure progress bar progresses
113  }
114  for (int j = 0; j < outputsize; ++j)
115  {
116  OutChannels.at(j)->GetData().push_back(currout(j));
117  }
118  if (wanthistory && ((i - shift) % weightsaveintervall == 0))
119  {
120  WeightHistory.push_back(Filter.GetWeightsAsVector());
121  }
122  for (int j = 0; j < outputsize; ++j)
123  {
124  EpsValues.at(j).push_back(Filter.GetEpsilon()(j));
125  }
126  } // end of main loop
127  for (int i = 0; i < outputsize; ++i)
128  {
129  OutChannels.at(i)->GetData().insert(
130  OutChannels.at(i)->GetData().end(), pointsperchannel, 0.0);
131  EpsValues.at(i).insert(EpsValues.at(i).end(), pointsperchannel, 0.0);
132  }
133  delete progressbar;
134  }
135  }
const gplib::rvec & GetEpsilon() const
Return the last estimation error.
std::vector< double > & GetData()
Access for data vector, for ease of use and efficiency we return a reference.
void AddReferenceChannel(TimeSeriesComponent &Channel)
Add a reference channel to the filter, some AdaptiveFilter objects require only one reference...
Definition: ApplyFilter.cpp:45
gplib::rmat GetWeightHistory()
Return a matrix with the values of the weights at iterations specified by weightsaveintervall.
Definition: ApplyFilter.cpp:23
virtual const gplib::rvec & GetWeightsAsVector()=0
We can always convert the weights to some sort of vector, the details have to be implemented in the d...
void AddInputChannel(TimeSeriesComponent &Channel)
Add an input channel to the filter.
Definition: ApplyFilter.cpp:36
ApplyFilter(AdaptiveFilter &TheFilter, bool keephistory=false)
The constructor takes the AdaptiveFilter object that determines how the filtering is done...
Definition: ApplyFilter.cpp:8
unsigned int GetInputLength()
Access function for derived classes for the inputlength.
A generic base class for all types of adaptive filters.
TimeSeriesComponent is the base storage class for all types of time series data.
virtual void CalcOutput(const gplib::rvec &Input, gplib::rvec &Output)=0
Calculate the filter output given Input.
virtual void AdaptFilter(const gplib::rvec &Input, const gplib::rvec &Desired)=0
Adapt the filter weights given the Input and Desired vectors.
void FilterData()
Filter the input channels with the current settings.
Definition: ApplyFilter.cpp:57
unsigned int GetOutputLength()
Access function for derived classes for the outputlength.
virtual ~ApplyFilter()
Definition: ApplyFilter.cpp:14
The basic exception class for all errors that arise in gplib.