GPLIB++
WienerFilter.cpp
Go to the documentation of this file.
1 #include "WienerFilter.h"
2 #include "miscfunc.h"
3 #include <boost/numeric/ublas/matrix.hpp>
4 #include <boost/numeric/ublas/operation.hpp>
5  #include <boost/numeric/ublas/triangular.hpp>
6  #include <boost/numeric/ublas/lu.hpp>
7  #include <boost/numeric/ublas/io.hpp>
8 
9 using namespace boost::numeric::ublas;
10 
11 namespace gplib
12  {
13 
14  template<class InMatrix, class OutMatrix>
15  bool InvertMatrix(InMatrix& input, OutMatrix& inverse)
16  {
17  using namespace boost::numeric::ublas;
18  typedef permutation_matrix<std::size_t> pmatrix;
19 
20  // create a permutation matrix for the LU-factorization
21  pmatrix pm(input.size1());
22 
23  // perform LU-factorization
24  int res = lu_factorize(input, pm);
25  if (res != 0)
26  return false;
27 
28  // create identity matrix of "inverse"
29  inverse.assign(boost::numeric::ublas::identity_matrix<double>(
30  input.size1()));
31 
32  // backsubstitute to get the inverse
33  lu_substitute(input, pm, inverse);
34 
35  return true;
36  }
37 
38  WienerFilter::WienerFilter(const int inputsize) :
39  AdaptiveFilter(inputsize, inputsize), CorrMatrix(inputsize, inputsize),
40  Weights(inputsize), lambda(0)
41  {
42  }
43 
45  {
46  }
47 
48  void WienerFilter::PrintWeights(std::ostream &output)
49  {
50  std::copy(Weights.begin(), Weights.end(),
51  std::ostream_iterator<double>(output, "\n"));
52  }
53 
54  void WienerFilter::AdaptFilter(const gplib::rvec &Input,
55  const gplib::rvec &Desired)
56  {
57  const int inputsize = Input.size();
58  vector<double> Cross(Input.size());
59  vector<double> Auto(Input.size());
60  Correl(Desired, Desired, Auto);
61  Correl(Input, Desired, Cross);
62  for (int i = 0; i < inputsize; ++i)
63  for (int j = i; j < inputsize; ++j)
64  {
65  CorrMatrix(i, j) = Auto(j - i);
66  CorrMatrix(j, i) = Auto(j - i);
67  }
68  CorrMatrix += lambda * identity_matrix<double> (inputsize);
69  matrix<double> Inverse(inputsize, inputsize);
70 
71  InvertMatrix(CorrMatrix,Inverse);
72 
73 
74  axpy_prod(Inverse, Cross, Weights, true);
75 
76  }
77 
78  void WienerFilter::CalcOutput(const gplib::rvec &Input, gplib::rvec &Output)
79  {
80  vector<double> output(Input.size());
81  Convolve(Input, Weights, Output);
82  SetOutput(Output);
83  }
84  }
virtual void PrintWeights(std::ostream &output)
Print the current set of weights to the output stream, has to be implemented in derived class...
virtual void AdaptFilter(const gplib::rvec &Input, const gplib::rvec &Desired)
Adapt the filter weights given the Input and Desired vectors.
bool InvertMatrix(InMatrix &input, OutMatrix &inverse)
A generic base class for all types of adaptive filters.
void SetOutput(const gplib::rvec &Out)
Possibility for derived classes to set output.
virtual void CalcOutput(const gplib::rvec &Input, gplib::rvec &Output)
Calculate the filter output given Input.