Persistence 1D inc. Reconstruct1D  1.1
Finding extrema in one dimensional data, filtering them by persistence and reconstructing smooth functions
 All Classes Namespaces Files Functions Variables Macros Pages
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
71  p.RunPersistence(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 }