8 namespace ublas = boost::numeric::ublas;
24 template<
typename UblasMatrix>
25 void PCA(
const UblasMatrix &observations, gplib::cmat &evectors,
28 UblasMatrix loc_obs(observations);
29 for (
unsigned int i = 0; i < loc_obs.size1(); ++i)
30 SubMean(row(loc_obs, i).begin(), row(loc_obs, i).end());
31 UblasMatrix covariance =
Cov(loc_obs);
33 const int nchannels = covariance.size1();
35 gplib::rvec s(nchannels);
36 gplib::cmat vl(nchannels, nchannels), vr(nchannels, nchannels), in(
37 nchannels, nchannels);
38 boost::numeric::bindings::lapack::geev(covariance, evalues, &vl,
39 &evectors, boost::numeric::bindings::lapack::optimal_workspace());
46 gplib::cmat
WhiteMat(gplib::cmat &evectors, gplib::cvec &evalues)
48 gplib::cmat result(evectors);
49 for (
unsigned int i = 0; i < evectors.size2(); ++i)
50 ublas::column(result, i) /= std::sqrt(evalues(i));
59 gplib::cmat
DeWhiteMat(gplib::cmat &evectors, gplib::cvec &evalues)
61 gplib::cmat result(evectors);
62 for (
unsigned int i = 0; i < evectors.size2(); ++i)
63 ublas::column(result, i) *= std::sqrt(evalues(i));
gplib::cmat WhiteMat(gplib::cmat &evectors, gplib::cvec &evalues)
Calculate the Whitening Matrix.
UblasMatrix Cov(const UblasMatrix &observations)
Calculate the NxN covariance matrix for a NxM matrix of observations with 0 mean. ...
void SubMean(InputIterator begin, InputIterator end, typename std::iterator_traits< InputIterator >::value_type mean)
Substract the mean from each element in the container, mean is passed as a parameter.
void PCA(const UblasMatrix &observations, gplib::cmat &evectors, gplib::cvec &evalues)
This template function calculates the principal component rotation matrix from a matrix of observatio...
gplib::cmat DeWhiteMat(gplib::cmat &evectors, gplib::cvec &evalues)
Calculate the Dewhitening Matrix.