PCA.h
Go to the documentation of this file.00001 #ifndef PCA_H_
00002 #define PCA_H_
00003
00004 #include "VecMat.h"
00005 #include "statutils.h"
00006 #include "Cov.h"
00007
00008 namespace ublas = boost::numeric::ublas;
00009
00010 namespace gplib
00011 {
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 template<typename UblasMatrix>
00025 void PCA(const UblasMatrix &observations, gplib::cmat &evectors,
00026 gplib::cvec &evalues)
00027 {
00028 UblasMatrix loc_obs(observations);
00029 for (unsigned int i = 0; i < loc_obs.size1(); ++i)
00030 SubMean(row(loc_obs, i).begin(), row(loc_obs, i).end());
00031 UblasMatrix covariance = Cov(loc_obs);
00032
00033 const int nchannels = covariance.size1();
00034
00035 gplib::rvec s(nchannels);
00036 gplib::cmat vl(nchannels, nchannels), vr(nchannels, nchannels), in(
00037 nchannels, nchannels);
00038 boost::numeric::bindings::lapack::geev(covariance, evalues, &vl,
00039 &evectors, boost::numeric::bindings::lapack::optimal_workspace());
00040 }
00041
00042
00043
00044
00045
00046 gplib::cmat WhiteMat(gplib::cmat &evectors, gplib::cvec &evalues)
00047 {
00048 gplib::cmat result(evectors);
00049 for (unsigned int i = 0; i < evectors.size2(); ++i)
00050 ublas::column(result, i) /= std::sqrt(evalues(i));
00051 return trans(result);
00052 }
00053
00054
00055
00056
00057
00058
00059 gplib::cmat DeWhiteMat(gplib::cmat &evectors, gplib::cvec &evalues)
00060 {
00061 gplib::cmat result(evectors);
00062 for (unsigned int i = 0; i < evectors.size2(); ++i)
00063 ublas::column(result, i) *= std::sqrt(evalues(i));
00064 return result;
00065 }
00066
00067 }
00068 #endif