//
//
//   File: altmath.h       Created 12-MAY-2007      Richard B. Johnson
//                                                  RichardBJohnson@comcast.net
//
//   The data, text, and source-code within these files are presumed to
//   be accurate and free of encumbrances. Use at your own risk.
//

#ifndef _ALTMATH_H
#define _ALTMATH_H
#ifdef linux
#define ALIGN __asm__ __volatile__(".align 0x10\n");
#else
#define ALIGN _asm{align 10H}
#endif


#if !defined(PI)
# define PI 3.1415926535897932384626433832795029  // c/d
#endif

#define ALPHA 9.675         // Alpha for Kaiser kernel
#define BADPI 3.10          // Used for noisey non-sync PI
#define HIGPI 3.20          // Used for noisey non-sync PI
#define PAD   0.4900        // For floating-point rounding
#define DYN   1e-12         // Min dynamic range number
#define ZERO  1e-50         // Save us from div/0

class SIG {

public:
enum WINDOW_TYPE {
     WINDOW_TYPE_RCOS,      // Raised cosine
     WINDOW_TYPE_HAM,       // Hamming
     WINDOW_TYPE_HAN,       // Hanning
     WINDOW_TYPE_NONE,      // Nothing
     WINDOW_TYPE_KAIS,      // Kaiser
     WINDOW_TYPE_ALOG,      // Johnson-Analogic
     WINDOW_TYPE_WEED,      // Weedon-Gauss
     WINDOW_TYPE_BKHA,      // Blackman-Harris
	 WINDOW_TYPE_BKMN,      // Blackman
	 WINDOW_TYPE_NUTA,      // Nutall
	 WINDOW_TYPE_BKNT,      // Blackman-Nuttall
	 WINDOW_TYPE_LANC,      // Lanczos
	 WINDOW_TYPE_RECT		// Rectangular
     };

    double ArrayRMS(double *buf, int len);
    double IndexToFirstPeak(double *array, int len);
    double MeasMean(double *array, int len);
    double MeasStdDev(double *array, int len);
    double MeasVariance(double *array, int len);
    double do_FFT(double *out, double *input, enum WINDOW_TYPE typ, int len);
    double get_bins(double *array, int ctr, int nr_bins);
    double get_db(double ref, double val);
	double BesselI0(double x);
    int NrCrossRefLevel(double *array, double ref, int len);
    int SettlingTime(double *array, double ref, int len);
    int TimeMaxSlope(double *array, int len);
    int TimeToNegRef(double *array, double ref, int len);
    int TimeToPosRef(double *array, double ref, int len);
    int TimeToReference(double *array, double ref, int len);
    int WithinLimits(double *array, double upper, double lower, int len);
    int isNaN(double x);
    void AbsValue(double *array, int len);
    void AddConst(double *array, double val, int len);
    void PutConst(double *array, double val, int len);
    void CvtInt2Double(double *array, int *input, int len);
    void CvtShort2Double(double *array, short *input, int len);
    void DivConst(double *array, double val, int len);
    void filter(double *buf, int box, int len);
    void Log10(double *array, int len);
    void LogN(double *array, int len);
    void MakeAnalogicWIN(double *array, int len);
    void MakeBlackmanHarris(double *array, int len);
    void MakeBlackman(double *array, int len);
    void MakeHammingWIN(double *array, int len);
    void MakeHanningWIN(double *array, int len);
    void MakeKaiserWIN(double *array, int len);
    void MakeRaisedCOS(double *array, int len);
    void MakeWeedonGauss(double *array, int len);
    void MakeNutall(double *array, int len);
    void MakeBlackNutall(double *array, int len);
    void MakeLanczos(double *array, int len);
    void MakeRectWIN(double *array, int len);
    void MulConst(double *array, double val, int len);
    void MultArray(double *array, double *kern, int len);
	void MultpArray(double *out, double *in1, double *in2, int len);
	void AddArray(double *out, double *in1, double *in2, int len);
	void SubArray(double *out, double *in1, double *in2, int len);
	void SqurArray(double *out, double *in1, int len);
	void SqrtArray(double *out, double *in1, int len);
    void SubConst(double *array, double val, int len);
    void Window(double *array, enum WINDOW_TYPE typ, int len);
    void get_phase(double *out, double *a, double *b, int len);
    void get_mag(double *out, double *a, double *b, int len);
	void parse_fft(double *a, double *b, int m, int iopt);

protected:
	static const double turn;
	static const double one;
	static const double two;

};

#endif



