GPLIB++
mtupca.cpp
Go to the documentation of this file.
1 #include "PCA.h"
2 #include "statutils.h"
3 #include "VecMat.h"
4 #include "TimeSeriesData.h"
5 #include <iostream>
6 #include <fstream>
7 #include <vector>
8 
9 using namespace gplib;
10 
11 int main()
12  {
13  TimeSeriesData TsData;
14  std::string mtuname;
15 
16  std::cout << "Data file name: ";
17  std::cin >> mtuname;
18 
19  TsData.GetData(mtuname);
20 
21  const size_t nobs = TsData.GetData().Size();
22  const size_t nchan = 4;
23  gplib::rmat input(nchan, nobs);
24  gplib::cmat evec(nchan, nchan);
25  gplib::cvec eval(nchan);
26 
27  for (unsigned int i = 0; i < nobs; ++i)
28  {
29  input(0, i) = TsData.GetData().GetEx().GetData().at(i);
30  input(1, i) = TsData.GetData().GetEy().GetData().at(i);
31  input(2, i) = TsData.GetData().GetHx().GetData().at(i);
32  input(3, i) = TsData.GetData().GetHy().GetData().at(i);
33  }
34 
35  PCA(input, evec, eval);
36 
37  gplib::cmat wmat(WhiteMat(evec, eval));
38  std::cout << "pca evec: " << evec << std::endl;
39  std::cout << "pca eval: " << eval << std::endl;
40  std::cout << "pca WhM: " << wmat << std::endl;
41  std::cout << "pca DeWhM: " << DeWhiteMat(evec, eval) << std::endl;
42 
43  gplib::cmat output(nchan, nobs);
44  noalias(output) = prec_prod(wmat, input);
45  for (unsigned int i = 0; i < nobs; ++i)
46  {
47  TsData.GetData().GetEx().GetData().at(i) = output(0, i).real();
48  TsData.GetData().GetEy().GetData().at(i) = output(1, i).real();
49  TsData.GetData().GetHx().GetData().at(i) = output(2, i).real();
50  TsData.GetData().GetHy().GetData().at(i) = output(3, i).real();
51  }
52  TsData.WriteAsBirrp(mtuname + ".pca");
53  }
gplib::cmat WhiteMat(gplib::cmat &evectors, gplib::cvec &evalues)
Calculate the Whitening Matrix.
Definition: PCA.h:46
std::vector< double > & GetData()
Access for data vector, for ease of use and efficiency we return a reference.
TimeSeries & GetData()
return a reference to the actual object stored in the pointer
TimeSeriesComponent & GetEx()
Definition: TimeSeries.h:47
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...
Definition: PCA.h:25
TimeSeriesComponent & GetHy()
Definition: TimeSeries.h:39
gplib::cmat DeWhiteMat(gplib::cmat &evectors, gplib::cvec &evalues)
Calculate the Dewhitening Matrix.
Definition: PCA.h:59
void WriteAsBirrp(std::string filename_base)
Write data to file in ascii format for birrp processing.
TimeSeriesData stores a pointer to the different components of magnetotelluric data and provides func...
int main()
Definition: mtupca.cpp:11
size_t Size()
Return the size of the time series, throws if one of the components has a different size...
Definition: TimeSeries.cpp:74
TimeSeriesComponent & GetEy()
Definition: TimeSeries.h:51
TimeSeriesComponent & GetHx()
Access function for Hx, returns reference for efficiency.
Definition: TimeSeries.h:35