00001 #include "ApplyFilter.h"
00002 #include <boost/progress.hpp>
00003
00004 ApplyFilter::ApplyFilter(AdaptiveFilter &TheFilter, bool keephistory):
00005 showprogress(false),
00006 wanthistory(keephistory),
00007 shift(0),
00008 Filter(TheFilter),
00009 weightsaveintervall(TheFilter.GetOutputLength())
00010 {
00011 }
00012
00013 ApplyFilter::~ApplyFilter()
00014 {
00015 }
00016
00017
00018
00019
00020
00021
00022 gplib::rmat ApplyFilter::GetWeightHistory()
00023 {
00024 if (WeightHistory.empty())
00025 throw CFatalException("Trying to access empty Weight History");
00026 const size_t xsize =WeightHistory.size();
00027 const size_t ysize = WeightHistory.front().size();
00028 gplib::rmat temp(xsize,ysize);
00029 for (size_t i = 0; i < xsize; ++i)
00030 for (size_t j = 0; j < ysize; ++j)
00031 temp(i,j) = WeightHistory.at(i)(j);
00032 return temp;
00033 }
00034
00035 void ApplyFilter::AddInputChannel(TimeSeriesComponent &Channel)
00036 {
00037 if (!InputChannels.empty() && Channel.GetData().size() != InputChannels.front()->GetData().size())
00038 throw CFatalException("Input channel has incompatible size to first channel");
00039 InputChannels.push_back(&Channel);
00040 }
00041
00042 void ApplyFilter::AddReferenceChannel(TimeSeriesComponent &Channel)
00043 {
00044 if (RefChannels.size() >= Filter.GetOutputLength())
00045 throw CFatalException("Trying to add more reference channels than permitted by Adaptive Filter class");
00046 if (!RefChannels.empty() && Channel.GetData().size() != RefChannels.front()->GetData().size())
00047 throw CFatalException("Ref channel has incompatible size to first channel");
00048 RefChannels.push_back(&Channel);
00049 }
00050
00051 void ApplyFilter::FilterData()
00052 {
00053 const int ninputchannels = InputChannels.size();
00054 const int datalength = InputChannels.front()->GetData().size();
00055 const int inputsize = Filter.GetInputLength();
00056 const int outputsize = Filter.GetOutputLength();
00057 const int pointsperchannel = inputsize/ninputchannels;
00058
00059 gplib::rvec currentinput(inputsize), currdesired(outputsize), currout(outputsize);
00060
00061
00062
00063 if (!EpsValues.empty())
00064 {
00065 EpsValues.clear();
00066 OutChannels.clear();
00067 }
00068
00069 for (int i = 0; i < outputsize; ++i)
00070 {
00071 std::vector<double> temp;
00072 EpsValues.push_back(temp);
00073 OutChannels.push_back(boost::shared_ptr<TimeSeriesComponent>(new TimeSeriesComponent));
00074 OutChannels.at(i)->GetData().reserve(RefChannels.at(i)->GetData().size());
00075 EpsValues.at(i).reserve(RefChannels.at(i)->GetData().size());
00076
00077 OutChannels.at(i)->GetData().insert(OutChannels.at(i)->GetData().begin(),shift,0.0);
00078 EpsValues.at(i).insert(EpsValues.at(i).begin(),shift,0.0);
00079 }
00080 const int maxindex = datalength-pointsperchannel-shift;
00081 boost::progress_display *progressbar = NULL;
00082 if (showprogress)
00083 {
00084 progressbar = new boost::progress_display(maxindex);
00085 }
00086 for (int i = 0; i < maxindex; ++i)
00087 {
00088
00089 for (int j = 0; j < ninputchannels; ++j)
00090 {
00091 copy(InputChannels.at(j)->GetData().begin()+i,InputChannels.at(j)->GetData().begin()+pointsperchannel+i,
00092 currentinput.begin()+j*pointsperchannel);
00093 }
00094 for (int j = 0; j < outputsize;++j)
00095 currdesired(j) = RefChannels.at(j)->GetData().at(i+shift);
00096
00097 Filter.CalcOutput(currentinput,currout);
00098 Filter.AdaptFilter(currentinput,currdesired);
00099 if (showprogress)
00100 {
00101 ++(*progressbar);
00102 }
00103 for (int j = 0; j < outputsize; ++j)
00104 {
00105 OutChannels.at(j)->GetData().push_back(currout(j));
00106 }
00107 if (wanthistory && ((i-shift) % weightsaveintervall== 0))
00108 {
00109 WeightHistory.push_back(Filter.GetWeightsAsVector());
00110 }
00111 for (int j = 0; j < outputsize; ++j)
00112 {
00113 EpsValues.at(j).push_back(Filter.GetEpsilon()(j));
00114 }
00115 }
00116 for (int i = 0; i < outputsize; ++i)
00117 {
00118 OutChannels.at(i)->GetData().insert(OutChannels.at(i)->GetData().end(),pointsperchannel,0.0);
00119 EpsValues.at(i).insert(EpsValues.at(i).end(),pointsperchannel,0.0);
00120 }
00121 delete progressbar;
00122 }