ApplyFilter.cpp

Go to the documentation of this file.
00001 #include "ApplyFilter.h"
00002 #include "FatalException.h"
00003 #include <boost/progress.hpp>
00004 
00005 namespace gplib
00006   {
00007 
00008     ApplyFilter::ApplyFilter(AdaptiveFilter &TheFilter, bool keephistory) :
00009       showprogress(false), wanthistory(keephistory), shift(0),
00010           Filter(TheFilter), weightsaveintervall(TheFilter.GetOutputLength())
00011       {
00012       }
00013 
00014     ApplyFilter::~ApplyFilter()
00015       {
00016       }
00017 
00018     /*! Return a matrix with the values of the weights at iterations specified by weightsaveintervall.
00019      * Internally we use a std::vector of ublas::vector and we construct the matrix in this routine.
00020      * Therefore we return a copy and the access is computationally reasonably expensive. Mostly
00021      * needed for final display of weights.
00022      */
00023     gplib::rmat ApplyFilter::GetWeightHistory()
00024       {
00025         if (WeightHistory.empty())
00026           throw FatalException("Trying to access empty Weight History");
00027         const size_t xsize = WeightHistory.size();
00028         const size_t ysize = WeightHistory.front().size();
00029         gplib::rmat temp(xsize, ysize);
00030         for (size_t i = 0; i < xsize; ++i)
00031           for (size_t j = 0; j < ysize; ++j)
00032             temp(i, j) = WeightHistory.at(i)(j);
00033         return temp;
00034       }
00035 
00036     void ApplyFilter::AddInputChannel(TimeSeriesComponent &Channel)
00037       {
00038         if (!InputChannels.empty() && Channel.GetData().size()
00039             != InputChannels.front()->GetData().size())
00040           throw FatalException(
00041               "Input channel has incompatible size to first channel");
00042         InputChannels.push_back(&Channel);
00043       }
00044 
00045     void ApplyFilter::AddReferenceChannel(TimeSeriesComponent &Channel)
00046       {
00047         if (RefChannels.size() >= Filter.GetOutputLength()) // if we already have enough reference channels
00048           throw FatalException(
00049               "Trying to add more reference channels than  permitted by Adaptive Filter class");
00050         if (!RefChannels.empty() && Channel.GetData().size()
00051             != RefChannels.front()->GetData().size())
00052           throw FatalException(
00053               "Ref channel has incompatible size to first channel");
00054         RefChannels.push_back(&Channel);
00055       }
00056 
00057     void ApplyFilter::FilterData()
00058       {
00059         const int ninputchannels = InputChannels.size();
00060         const int datalength = InputChannels.front()->GetData().size();
00061         const int inputsize = Filter.GetInputLength();
00062         const int outputsize = Filter.GetOutputLength();
00063         const int pointsperchannel = inputsize / ninputchannels;
00064 
00065         gplib::rvec currentinput(inputsize), currdesired(outputsize), currout(
00066             outputsize);
00067         //std::vector<double> eps;
00068         //EpsValues.assign(outputsize,eps);
00069 
00070         if (!EpsValues.empty()) // if we have run this routine before
00071           {
00072             EpsValues.clear(); //clear errors
00073             OutChannels.clear(); // and output channels, but not weights
00074           }
00075 
00076         for (int i = 0; i < outputsize; ++i)
00077           {
00078             std::vector<double> temp;
00079             EpsValues.push_back(temp);
00080             OutChannels.push_back(boost::shared_ptr<TimeSeriesComponent>(
00081                 new TimeSeriesComponent));
00082             OutChannels.at(i)->GetData().reserve(
00083                 RefChannels.at(i)->GetData().size());
00084             EpsValues.at(i).reserve(RefChannels.at(i)->GetData().size());
00085             //if (shift > 2)
00086             OutChannels.at(i)->GetData().insert(
00087                 OutChannels.at(i)->GetData().begin(), shift, 0.0);
00088             EpsValues.at(i).insert(EpsValues.at(i).begin(), shift, 0.0);
00089           }
00090         const int maxindex = datalength - pointsperchannel - shift;
00091         boost::progress_display *progressbar = NULL;
00092         if (showprogress) // the constructor already displays something, so we use a pointer and only construct when needed
00093           {
00094             progressbar = new boost::progress_display(maxindex); // init progress bar
00095           }
00096         for (int i = 0; i < maxindex; ++i)
00097           {
00098 
00099             for (int j = 0; j < ninputchannels; ++j)
00100               {
00101                 copy(InputChannels.at(j)->GetData().begin() + i,
00102                     InputChannels.at(j)->GetData().begin() + pointsperchannel
00103                         + i, currentinput.begin() + j * pointsperchannel);
00104               }
00105             for (int j = 0; j < outputsize; ++j)
00106               currdesired(j) = RefChannels.at(j)->GetData().at(i + shift);
00107 
00108             Filter.CalcOutput(currentinput, currout);
00109             Filter.AdaptFilter(currentinput, currdesired);
00110             if (showprogress)
00111               {
00112                 ++(*progressbar); // make sure progress bar progresses
00113               }
00114             for (int j = 0; j < outputsize; ++j)
00115               {
00116                 OutChannels.at(j)->GetData().push_back(currout(j));
00117               }
00118             if (wanthistory && ((i - shift) % weightsaveintervall == 0))
00119               {
00120                 WeightHistory.push_back(Filter.GetWeightsAsVector());
00121               }
00122             for (int j = 0; j < outputsize; ++j)
00123               {
00124                 EpsValues.at(j).push_back(Filter.GetEpsilon()(j));
00125               }
00126           } // end of main loop
00127         for (int i = 0; i < outputsize; ++i)
00128           {
00129             OutChannels.at(i)->GetData().insert(
00130                 OutChannels.at(i)->GetData().end(), pointsperchannel, 0.0);
00131             EpsValues.at(i).insert(EpsValues.at(i).end(), pointsperchannel, 0.0);
00132           }
00133         delete progressbar;
00134       }
00135   }

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