/***************************************************************************
                          Misc.h  -  description
                             -------------------
    begin                : Sam Feb 8 2003
    copyright            : (C) 2003 by klaus frieler
    email                : kgf@omniversum.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
#ifndef _MISC_H
#define _MISC_H

#include "Types.h"

double      getSum(const data& vec);
double      getSquareSum(const data& vec);
double      getMinimum(const data& vec);
double      getMinimum(const data& vec, uint& pos);
double      getMaximum(const data& vec);
double      getMaximum(const data& vec, uint& pos);
uint        getMinimumN(double est, double mean, double thres, uint max);
double      calcSquareErr(double est, double thres, const data& vec, uint max, double& N );
void        dumpData(const data& data, const char* delim = " ");
void        dumpData(const vector<uint>& data, const char* delim = " ");
void        dumpData(const data& data, const vector<uint>& points, const char* delim = " ");

double      calcMinimumDistance(const data &vec, uint &i, uint &j);
char       *diffString(const char *str1, const char *str2);

double      match(const double *vec1,  const double *vec2,  const uint len);
double      match2(const double *vec1, const double *vec2, const uint len);
double      match2(const double *vec1, const double  val,  const uint len);
double      mismatch2(const double *vec, const double val, const uint len);
double      mismatch3(const double *vec1, const double *vec2, const uint len);
double      norm(const double *vec,  const uint len);
void        normalize(double *vec,  const uint len);

void        calcTimePoints(const data& iois, data& timePoints);
void        calcIOIs(const data& timePoints, data& iois);
void        shiftData(data & vec, double offset);

void        getAutoCorrMaxima(const data &autoCorr, data& dMaxPoints, data& maxima, double autoCorrStep, const bool verbose, const bool correct);
void        evalAutoCorrMaxima(const data &max, const data& dMaxPoints, uint &tactus, uint &period);

bool        writeDataToFile(string filename, const data& vec,  double step = 1, double start = 0.0);
bool        writeDataToFile(string filename, const data& vec,  const data& step);

void        scale(data& vec, double scale);
data        scale(const vector<uint>& d, double scale);

data        getMaxima(const data& vec, vector<uint>& points, data& values);
data        getMinima(const data& vec, vector<uint>& points, data& values);
void        getMaxPair(const data& values, const vector<uint>& points, double &val, uint &pos );


inline
double      msToBPM(const double t)  { return 60000/t;  }

inline
double      BPMToMs(const double bpm){ return 60000/bpm;}

bool        isNumber(const char *str);

void        setAccentPattern(uint period, data &accPatt);

string      toLower(const string& str);
string      stripWhiteSpace(const string& str, bool left, bool right);
string      normalizeString(const string &str);
string      getToken(const char *str, uint which, const char delim);
string      stripPath(const char* fullPath);
string      stripExtension(const char* name);
char        *subchr(char *str, char from, char to);
string      substituteChar(const string& str, const char which, const char what);
uint        ggT(uint m, uint n);
uint        kgV(uint m, uint n);
vector<uint>   getPrimeFactorDecomposition(const uint n);
vector<char>   stringToVector(const string &str);
#endif
/*
-f 66jahr2.dat --window-size 3000 --window-offset 500 -a 4 --reduce-cut 0.8 --categorize --quant-time 3000
*/
