30 #define MATLAB "-MATLAB" 61 void WriteExtremaToFile(
char* filename, vector<TPairedExtrema> pairs,
const int idxGlobalMin);
66 bool ParseCmdLine(
int argc,
char* argv[],
float &threshold,
bool & matlabIndexing);
76 int main(
int argc,
char* argv[])
81 vector <TPairedExtrema> pairs;
87 cout <<
"No filename" << endl;
88 cout <<
"Usage: " << argv[0] <<
" <filename> [threshold] [-MATLAB]" << endl;
93 char * filename = argv[1];
94 char * outfilename =
new char[strlen(filename) + strlen(
"_res.txt")];
95 strcpy(outfilename, filename);
97 outfilename[strlen(filename)-4] =
'\0';
98 strcat(outfilename,
"_res.txt");
100 if (!
ParseCmdLine(argc, argv, threshold, matlabIndexing))
102 cout <<
"Usage: " << argv[0] <<
" <filename> [threshold] [-MATLAB]" << endl;
108 cout <<
"Error reading data to file." << endl;
127 datafile.open(filename, ifstream::in);
132 cout <<
"Cannot open file " << filename <<
" for reading" << endl;
138 while(datafile >> currdata)
140 data.push_back(currdata);
149 datafile.open(filename);
153 cout <<
"Cannot open file " << filename <<
" for writing." << endl;
157 for (vector<TPairedExtrema>::iterator p = pairs.begin(); p != pairs.end(); p++)
159 datafile << to_string((
long long)(*p).MinIndex) << endl;
160 datafile << to_string((
long long)(*p).MaxIndex) << endl;
163 datafile << to_string((
long long)idxGlobalMin) << endl;
167 bool ParseCmdLine(
int argc,
char* argv[],
float &threshold,
bool & matlabIndexing)
169 bool noErrors =
true;
172 matlabIndexing =
false;
175 for (
int counter = 2; counter < argc ; counter ++)
177 if (argv[counter][0]==
'-' && matlabIndexing ==
false)
179 if (strcmp(argv[counter],
"-MATLAB") == 0 ||
180 strcmp(argv[counter],
"-Matlab") == 0 ||
181 strcmp(argv[counter],
"-matlab") == 0 )
184 matlabIndexing =
true;
188 cout <<
"Possibly misspelled Matlab flag or negative values for threshold." << endl;
192 else if ( argv[counter][0] ==
'0')
195 threshold = (float)atof(argv[counter]);
204 cout <<
"Error. Threshold value should be >= 0. Rerun with valid threshold value or leave out to get all features.\n";
211 threshold = (float)atof(argv[counter]);
215 if (threshold == 0.0)
217 cout <<
"Cannot convert threshold value to number.\n" << endl;
220 else if (threshold < 0)
222 cout <<
"Error. Threshold value should be >= 0. Rerun with valid threshold value or leave out to get all features.\n";
bool ReadFileToVector(char *filename, vector< float > &data)
Tries to open the input file and read its contents to a float vector.
bool ParseCmdLine(int argc, char *argv[], float &threshold, bool &matlabIndexing)
Parses user command line.
void WriteExtremaToFile(char *filename, vector< TPairedExtrema > pairs, const int idxGlobalMin)
Writes indices of extrema features to file, sorted according to their persistence.
int GetGlobalMinimumIndex(const bool matlabIndexing=false) const
Returns the index of the global minimum.
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 GetPairedExtrema(std::vector< TPairedExtrema > &pairs, const float threshold=0, const bool matlabIndexing=false) const
Use this method to get the results of RunPersistence.
int main(int argc, char *argv[])
Main function - reads a file specified as a command line argument.