GPLIB++
IterDecon.cpp
Go to the documentation of this file.
1 #include "IterDecon.h"
2 #include "miscfunc.h"
3 #include <boost/algorithm/minmax_element.hpp>
4 #include <iostream>
5 #include <cmath>
6 
7 namespace gplib
8  {
9 
10  IterDecon::IterDecon(const int inputsize, TsSpectrum &Spec) :
11  AdaptiveFilter(inputsize, inputsize), Weights(inputsize), Spectrum(Spec)
12  {
13  for (int i = 0; i < inputsize; ++i)
14  Weights(i) = 0;
15  }
16 
18  {
19  }
20 
21  void IterDecon::PrintWeights(std::ostream &output)
22  {
23  std::copy(Weights.begin(), Weights.end(),
24  std::ostream_iterator<double>(output, "\n"));
25  }
26 
27  void IterDecon::AdaptFilter(const gplib::rvec &Input,
28  const gplib::rvec &Desired)
29  {
30  typedef gplib::rvec::const_iterator iterator;
31  gplib::rvec Corr(Desired.size());
32  Correl(Desired, Input, Corr, Spectrum); //calculate the correlation between desired and input
33  const double power = ublas::prec_inner_prod(Input, Input);
34  std::pair<iterator, iterator> MinMaxPos(boost::minmax_element(
35  Corr.begin(), Corr.begin() + Corr.size() / 2)); //find minimum and maximum
36  int MaxIndex = MinMaxPos.second - Corr.begin(); //index of maximum
37  int MinIndex = MinMaxPos.first - Corr.begin(); //index of minimum
38 
39  if (std::abs(*MinMaxPos.second) > std::abs(*MinMaxPos.first)) //if max has higher absolute value than min
40  Weights(MaxIndex) += *MinMaxPos.second / power; //update weight for max
41  else
42  Weights(MinIndex) += *MinMaxPos.first / power; //update weight for min
43  }
44 
45  void IterDecon::CalcOutput(const gplib::rvec &Input, gplib::rvec &Output)
46  {
47  Convolve(Input, Weights, Output, Spectrum);//multiplication with a toeplitz matrix is the same as cyclic convolution
48  SetOutput(Output);
49  }
50  }
IterDecon(const int inputsize, TsSpectrum &Spec)
Input and output length have to be the same, so only one parameter for the constructor.
Definition: IterDecon.cpp:10
virtual void AdaptFilter(const gplib::rvec &Input, const gplib::rvec &Desired)
Adapt the filter weights given the Input and Desired vectors.
Definition: IterDecon.cpp:27
A generic base class for all types of adaptive filters.
virtual void PrintWeights(std::ostream &output)
print weights to a file
Definition: IterDecon.cpp:21
virtual void CalcOutput(const gplib::rvec &Input, gplib::rvec &Output)
Calculate the filter output given Input.
Definition: IterDecon.cpp:45
The class CTsSpectrum is used to calculate spectra from (real) time series data.
Definition: TsSpectrum.h:21
void SetOutput(const gplib::rvec &Out)
Possibility for derived classes to set output.
virtual ~IterDecon()
Definition: IterDecon.cpp:17