Persistence 1D incl. Reconstruct1D  1.3
Finding extrema in one dimensional data, filtering them by persistence and reconstructing smooth functions
MatlabVisualization.cpp
Go to the documentation of this file.
1 /*! \file MatlabVisualization.cpp
2  Demonstrates how to run the C++ code and visualize the results using Matlab.
3 */
4 /*! \example MatlabVisualization.cpp
5 
6  Demonstrates how to run the C++ code and visualize the results using Matlab.
7 
8  # Contents
9  This example demonstrates how to:
10  -# Read data from a file,
11  -# Use Persistence1D class to extract extrema
12  -# Filter results according to persistence values.
13  -# Get results in Matlab output format
14  -# Visualize results using Matlab scripts.
15 
16  # Expected Results
17  Reference output file: [persistence_base_dir]\\src\\examples\\MatlabVisualization\\MatlabVisualizationRes.ref
18  with contents:
19  \include MatlabVisualizationRes.ref
20 
21  # Results Visualization
22  -# Start Matlab
23  -# Change the directory to [persistence_base_dir]\\matlab
24  -# Run: visualize_features('..\\data.txt', '..\\res.txt')
25 
26  ## Matlab Plot of Results
27  \image html MatlabVisRes.png "Matlab Plot of Results"
28  \image latex MatlabVisRes.png "Matlab Plot of Results"
29 
30  # Code Documentation
31 */
32 
33 #include "..\..\persistence1d\persistence1d.hpp"
34 #include <fstream>
35 
36 using namespace std;
37 using namespace p1d;
38 
39 int main()
40 {
41  //Input and class variables declaration
42  Persistence1D p;
43  vector<float> data;
44  float currdata;
45  ifstream datafile;
46  ofstream outfile;
47  char * filename = "data.txt";
48  char * outfilename = "res.txt";
49  bool enableMatlabIndexing = true;
50 
51  //Output variables declaration
52  vector<int> min, max;
53  int globalMinIndex;
54 
55  //Open and read the data file
56  datafile.open(filename);
57  if (!datafile)
58  {
59  cout << "Cannot open data file for reading: " << filename << endl;
60  return -1;
61  }
62 
63  while(datafile >> currdata)
64  {
65  data.push_back(currdata);
66  }
67 
68  datafile.close();
69 
70  //To start data processing, run persistence on data
72 
73  //It is possible to verify the correctness of the results via sanity tests.
74  //Check the function documentation for further information.
75  if (!p.VerifyResults())
76  {
77  cout << "ERROR" << endl;
78  }
79 
80  //Now, set a threshold for features filtering
81  float filterThreshold = 1.0;
82 
83  //Retrieve the filtered results from the class.
84  //Since results are intended to be used within Matlab, set MatlabIndexing in all Get function calls
85  //to get 1-based indexing results.
86  //Results retrieved without MatlabIndexing set to true are 0-based.
87  p.GetExtremaIndices(min, max, filterThreshold, enableMatlabIndexing);
88  globalMinIndex = p.GetGlobalMinimumIndex(enableMatlabIndexing);
89 
90  //Print all found indices to a file
91  outfile.open(outfilename);
92  if (!outfile)
93  return -2;
94 
95  for (unsigned int i = 0; i < min.size() && i < max.size(); i++)
96  {
97  outfile << min[i] << endl;
98  outfile << max[i] << endl;
99  }
100 
101  //Add the global minimum to the file as well:
102  //The global minimum does is not returned via GetExtremaIndices, as it is not paired.
103  outfile << globalMinIndex << endl;
104 
105  outfile.close();
106 
107  //To visualize the results:
108  //Start Matlab
109  //Change the directory to [persistence_base_dir]\matlab
110  //Run: visualize_features('..\src\examples\MatlabVisualization\data.txt', '..\src\examples\MatlabVisualization\res.txt')
111 
112  return 0;
113 }
int GetGlobalMinimumIndex(const bool matlabIndexing=false) const
Returns the index of the global minimum.
bool GetExtremaIndices(std::vector< int > &min, std::vector< int > &max, const float threshold=0, const bool matlabIndexing=false) const
Use this method to get two vectors with all indices of PairedExterma.
int main()
bool RunPersistence(const std::vector< float > &InputData)
Call this function with a vector of one dimensional data to find extrema features in the data.
Finds extrema and their persistence in one-dimensional data.
bool VerifyResults()
Runs basic sanity checks on results of RunPersistence: