GPLIB++
seispca.cpp
Go to the documentation of this file.
1 #include "PCA.h"
2 #include "statutils.h"
3 #include "CUniformRNG.h"
4 #include "VecMat.h"
5 #include "SeismicDataComp.h"
6 #include <iostream>
7 #include <fstream>
8 #include <vector>
9 
10 using namespace gplib;
11 
12 int main()
13  {
14  SeismicDataComp Radial, Tangential, Vertical;
15  std::string radname, tangname, vername;
16 
17  std::cout << "Radial Component: ";
18  std::cin >> radname;
19  std::cout << "Tangential Component: ";
20  std::cin >> tangname;
21  std::cout << "Vertical Component: ";
22  std::cin >> vername;
23 
24  Radial.ReadData(radname);
25  Tangential.ReadData(tangname);
26  Vertical.ReadData(vername);
27 
28  const size_t nobs = Radial.GetData().size();
29  const size_t nchan = 3;
30  gplib::rmat input(nchan, nobs);
31  gplib::cmat evec(nchan, nchan);
32  gplib::cvec eval(nchan);
33 
34  for (int i = 0; i < nobs; ++i)
35  {
36  input(0, i) = Radial.GetData().at(i);
37  input(1, i) = Tangential.GetData().at(i);
38  input(2, i) = Vertical.GetData().at(i);
39  }
40 
41  PCA(input, evec, eval);
42 
43  gplib::cmat wmat(WhiteMat(evec, eval));
44  std::cout << "pca evec: " << evec << std::endl;
45  std::cout << "pca eval: " << eval << std::endl;
46  std::cout << "pca WhM: " << wmat << std::endl;
47  std::cout << "pca DeWhM: " << DeWhiteMat(evec, eval) << std::endl;
48 
49  gplib::cmat output(nchan, nobs);
50  axpy_prod(wmat, input, output);
51  SeismicDataComp RadOut(Radial), TangOut(Tangential), VerOut(Vertical);
52  for (int i = 0; i < nobs; ++i)
53  {
54  RadOut.GetData().at(i) = output(0, i).real();
55  TangOut.GetData().at(i) = output(1, i).real();
56  VerOut.GetData().at(i) = output(2, i).real();
57  }
58  RadOut.WriteAsSac("pc1.out");
59  TangOut.WriteAsSac("pc2.out");
60  VerOut.WriteAsSac("pc3.out");
61 
62  }
gplib::cmat WhiteMat(gplib::cmat &evectors, gplib::cvec &evalues)
Calculate the Whitening Matrix.
Definition: PCA.h:46
int ReadData(const std::string &filename, tseismicdataformat format=sac)
Read in data from a file, as we cannot determine the type from the ending we have to provide it...
std::vector< double > & GetData()
Access for data vector, for ease of use and efficiency we return a reference.
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
gplib::cmat DeWhiteMat(gplib::cmat &evectors, gplib::cvec &evalues)
Calculate the Dewhitening Matrix.
Definition: PCA.h:59
int main()
Definition: seispca.cpp:12
int WriteAsSac(const std::string &filename) const
Write the data in sac binary format.