#ifndef _NEWMATH_H_
#define _NEWMATH_H_


#define DF_FILTER_NULL      0
#define DF_FILTER_LOWPASS   1
#define DF_FILTER_HIGHPASS  2
#define DF_FILTER_BELDPASS  3
#define DF_FILTER_BELDSTOP  4

#define DF_WINDOW_RECTANGLE  0 //矩形窗
#define DF_WINDOW_BARTLETT   1 //三角形窗
#define DF_WINDOW_HANNING    2 //汉宁窗
#define DF_WINDOW_HAMMING    3 //海明窗
#define DF_WINDOW_BLACKMAN   4 //布莱克窗
#define DF_WINDOW_SINXX      5 //
#define DF_WINDOW_COS        6 //余弦边窗
#define DF_WINDOW_KAISER     7 //凯塞窗
#define DF_WINDOW_KAISER_EX  8 //修正的凯塞窗

//快速傅里叶变换
typedef struct
{
	double real;
	double image;
}COMPLEX_FFT, *LPCOMPLEX_FFT;

struct FILTER_PARAMETER
{
	BOOL   bEmulation;
	BOOL   bFilter;

	int    nFilter;
	int    nWindow;
	int    nChannel;

	double dbSampleFreq;	
	int    nDelay;
	double dbFreqLow;
	double dbFreqHigh;
};

struct WAVE_ATTRIB
{
	BOOL bWave;
	int  nWave;
	
	double dbAmp;
	double dbFreq;
	double dbRatio;
	double dbPhase;
};

COMPLEX_FFT* ComplexFFT(COMPLEX_FFT* pX, unsigned int n, COMPLEX_FFT* pY);
COMPLEX_FFT* ComplexIFFT(COMPLEX_FFT* pX, unsigned int wCount, COMPLEX_FFT* pY);

int AfxGetMaxLocation();
int AfxGetMinLocation();
int AfxGetMaxPeriod();

double GetMin(double *pdbData,int nCount);
double GetMax(double *pdbData,int nCount);

float GetMin(float *pfData,int nCount);
float GetMax(float *pfData,int nCount);
WORD GetMin_Word(WORD *pwData,int nCount);
WORD GetMax_Word(WORD *pwData,int nCount);
WORD GetAverage_Word(WORD *pwData,int nCount);

float Modify_AverageDC_AbsFloat(float *pfData,int nCount);
WORD Modify_AverageDC_AbsWord(WORD *pwData,int nCount, WORD wData);

float GetAverage(float *pfData,int nCount);
float GetDelta(float *pfData,int nCount);
long  GetSum(BYTE *pByte,int nCount);


BOOL  GetSinWave(WORD *pwData, int nCount, WORD wAmp, double dbSignFreq,
    double dbPhase, WORD wDC, double dbSampleFreq, BOOL bAdd);
BOOL  GetSinWave(float *pfData, int nCount, double dbAmp, double dbSignFreq, double dbPhase, double dbDC, double dbSampleFreq, BOOL bAdd);
BOOL GetSinWave(double *pdbData, int nCount, double dbAmp, double dbSignFreq, double dbPhase, double dbDC, double dbSampleFreq, BOOL bAdd);
BOOL  GetRectangleWave(float* pfData,int nCount,double dbAmp,double dbSignFreq,double dbPhase,double dbDC,double dbRatio,double dbSampleFreq,BOOL bAdd);
BOOL  GetTriangleWave(float* pfData,int nCount,double dbAmp,double dbSignFreq,double dbPhase,double dbDC,double dbRatio,double dbSampleFreq,BOOL bAdd);
BOOL  GetPurseWave(float* pfData,int nCount,double dbAmp,double dbSignFreq,double dbPhase,double dbDC,double dbSampleFreq,BOOL bAdd);

//BOOL FAR EmulationSurge9225(WORD* pwData,int nCount,int nBase,int nOscillatory,int nDivideFreq,BOOL bInternalTrigger);
BOOL FAR EmulationSurge(WORD* pwData, int nCount, int nPrecision);
BOOL FAR EmulationSurgeL(WORD* pwData, int nCount, int nPrecision,
     double dbU, double dbSample, double dbE, double dbTf, double dbTt);

void  Kernel_Fft_InternalSingle(float *pfReal,float *pfImage,int nCount, int nType);


void  Dft_InternalDouble(float* pfReal,float *pfImage,int nCount);
void  Dft_InternalSingle(float* pfReal,float *pfImage,int nCount);

void  Fft_InternalDouble(float *pfReal,float *pfImage,int nCount);
void  Fft_InternalSingle(float *pfReal,float *pfImage,int nCount);

double Dft_RealAndImageDouble(double* pdbWave, int nCount, double dbSampleFreq, double dbFreq, double* pdbReal, double* pdbImage);
double Dft_RealAndImageSingle(float* pfWave, int nCount, double dbSampleFreq, double dbFreq, double* pdbReal, double* pdbImage);

double AfxGetSmallFreqAmp();
double Dft_SmallFreq(float* pfWave, int nCount,
	double dbSampleFreq, double dbCurFreq, double dbPrecision);
double Dft_SmallFreq(double* pfWave, int nCount,
	double dbSampleFreq, double dbCurFreq, double dbPrecision);

float WaveDistortion(float *pfReal,float *pfImage,int nCount,double dbWaveFreq,double dbSampleFreq);

double GetWindowCoefficient(int nLocation ,int nDelay,int nWindowStyle);
double GetLowPassCoefficient_Window(int nLocation ,int nDelay,double dbFc,double dbS,int nWindowStyle);
double GetHighPassCoefficient_Window(int nLocation ,int nDelay,double dbFc,double dbS,int nWindowStyle);
double GetBeldPassCoefficient_Window(int nLocation ,int nDelay,double dbFc1,double dbFc2,double dbS,int nWindowStyle);
double GetBeldStopCoefficient_Window(int nLocation ,int nDelay,double dbFc1,double dbFc2,double dbS,int nWindowStyle);
BOOL GetFilterCoefficient_Window(double* pdbCoefficient ,int nDelay,double dbFc1,double dbFc2,
	double dbS,int nWindowStyle, int nFilterStyle);

void Fc_Filter_D21(double* pdbData,int nCount,double* pdbFilterCoefficient,int nDelay);
void Fc_Filter_D21(float* pfData,int nCount,double* pdbFilterCoefficient,int nDelay);
void Fc_Filter_D21(WORD* pwData,int nCount,double* pdbFilterCoefficient,int nDelay);




void FAR GetMin2Coordinate(float* pfDataX, float* pfDataY, int nCount, float* pfK, float* pfB, float* pfR2);

BOOL   ExploreFormature(double* pdbA, double* pdbY, int nCount, double* pdbX);
BOOL   CurveFittingFormature(float* pfX, float* pfY,int nCount,int nRank,double* pdbA);
BOOL   CurveFittingFormature(double* pdbX,double* pdbY,int nCount,int nRank,double* pdbA);
double GetCurveFittingFormatureValue(double* pdbNewA, int nRank,double dbValue);

BOOL PolynomialInterpolation(double* pdbX, double* pdbY, int nCount, int nRank, double* pdbA);
BOOL PolynomialInterpolation(float* pfX, float* pfY, int nCount, int nRank, double* pdbA);

double GetPolynomialValue(double* pdbNewA, int nRank,double dbValue);
BOOL   LagrangeInterpolation(double* pdbX, double* pdbY, int nCount, int nRank, double* pdbA);
BOOL   LagrangeInterpolation(float* pfX, float* pfY, int nCount, int nRank, double* pdbA);

void    GetSinPeriodEx_Kernel(WORD *pwWave,int nCount,int nIndex,int *pnStart,int *pnEnd,int *pnMiddle,int nValidLen, BOOL bFilterDC, BOOL bRect, float fRectLimitVolX);
void    GetSinPeriodEx_Kernel(float *pfWave,int nCount,int nIndex,int *pnStart,int *pnEnd,int *pnMiddle,int nValidLen, BOOL bFilterDC, BOOL bRect, float fRectLimitVolX);
void    GetMaxSinPeriodEx_Kernel(WORD *pwWave,int nCount,int nMax,int *pnStart,int *pnEnd,int *pnMiddle,int nValidLen, BOOL bFilterDC, BOOL bRect, float fRectLimitVolX);
void    GetMaxSinPeriodEx_Kernel(float *pfWave,int nCount,int nMax,int *pnStart,int *pnEnd,int *pnMiddle,int nValidLen, BOOL bFilterDC, BOOL bRect, float fRectLimitVolX);

void    GetSinPeriodEx(WORD *pwWave,int nCount,int nIndex,int *pnStart,int *pnEnd,int *pnMiddle,int nValidLen, BOOL bFilterDC);
void    GetSinPeriodEx(float *pfWave,int nCount,int nIndex,int *pnStart,int *pnEnd,int *pnMiddle,int nValidLen, BOOL bFilterDC);
void    GetMaxSinPeriodEx(WORD *pwWave,int nCount,int nMax,int *pnStart,int *pnEnd,int *pnMiddle,int nValidLen, BOOL bFilterDC);
void    GetMaxSinPeriodEx(float *pfWave,int nCount,int nMax,int *pnStart,int *pnEnd,int *pnMiddle,int nValidLen, BOOL bFilterDC);

void    GetSinPeriod(WORD *pwWave,int nCount,int nIndex,int *pnStart,int *pnEnd,int *pnMiddle,int nValidLen = 10);
void    GetSinPeriod(float *pfWave,int nCount,int nIndex,int *pnStart,int *pnEnd,int *pnMiddle,int nValidLen = 10);
void    GetMaxSinPeriod(WORD *pwWave,int nCount,int nMax,int *pnStart,int *pnEnd,int *pnMiddle,int nValidLen = 10);
void    GetMaxSinPeriod(float *pfWave,int nCount,int nMax,int *pnStart,int *pnEnd,int *pnMiddle,int nValidLen = 10);


void    GetStaffSinPeriod(WORD *pwWave,int nCount,int nMax,int *pnStart,int *pnEnd,int *pnMiddle,int nValidLen = 10);
void    GetStaffSinPeriod(float *pfWave,int nCount,int nMax,int *pnStart,int *pnEnd,int *pnMiddle,int nValidLen = 10);


float  GetAntiLineCoordinateXY(float x1, float y1, float x2, float y2, float fR);
float  GetLineCoordinateXY(float fX1,float fY1,float fX2,float fY2,float fValue);
void   TransrateWave(float* pfWave, int nCount, float fX1, float fY1, float fX2, float fY2);
float  Modify_AverageDC(WORD *pwData,int nCount,float fX1,float fY1,float fX2,float fY2);
float  Modify_AverageAC(WORD *pwData,int nCount,float fX1,float fY1,float fX2,float fY2);
float  Modify_AverageDC(float *pfData,int nCount,float fX1,float fY1,float fX2,float fY2);
float  Modify_AverageAC(float *pfData,int nCount,float fX1,float fY1,float fX2,float fY2);

void GetAdd_Word(WORD* pwData, int nCount, WORD wDC);
void GetSub_Word(WORD* pwData, int nCount, WORD wDC);

void   FilterDC(WORD *pwData,int nCount);

//校准波形
void CalibrationWave(float *pfWave, int nCount, 
		float fX1, float fY1, float fX2, float fY2);
//微分，内部采用多项式拟合
void Differentiation(float *pfWave, int nCount, int nBank, float* pfOut);
//添加直流
void AddDC(float *pfWave, int nCount, float fDC);
//添加比例系数
void MultiScale(float *pfWave, int nCount, float fScale);

void SetCalibrationGain(int nGain);
int GetCalibrationGain();
float GetCalibrationRangeVoltage();

int  JudgeAround(BOOL bActive1, BOOL bActive2,
	int nTimes1, int nTime2, BOOL bWaitLogic,
	BOOL* pbOldActive1, DWORD* pdwCounter, BOOL* pbStart);
void FAR Afx80x87Init();
WORD AfxGetCRC16(BYTE *pStr,int nCount);
WORD AfxGetCRC16_Ex (BYTE* pStr, int nCount);

void Fumu(double dbA, double dbB, double dbC, float* pfRoot1, float* pfRoot2);

void FAR AfxSetADPricisionLow(int nPricision);
int FAR AfxGetADPricisionLow();
float AfxGetADMaxLow();
float AfxGetADHalfLow();

void FAR AfxSetADPricisionHigh(int nPricision);
int FAR AfxGetADPricisionHigh();
float AfxGetADMaxHigh();
float AfxGetADHalfHigh();


int GetZhanKongRatio_Rect(WORD* pwData, int nCount, int nCircles, int* pnCircles,//nIndexPeriod,
      int nDotLen, float fLimitPhysis,
      float fX1, float fY1, float fX2, float fY2,
      float* pfVolH, float* pfVolL, int* pnDotH, int* pnDotL, float* pfRatio,
      float fRectLimitVolY);

float GetPhaseDelta_Rect(WORD* pwDataS, WORD* pwDataT, WORD* pwDataL, int nCount, int nCircles, int* pnCircles,
      float fLimitPhy, WORD wDC, float fX1, float fY1, float fX2, float fY2,
      float* pfVolH, float* pfVolL, int* pnDotH, int* pnDotL, float* pfRatio,
      float fRectLimitVol, BOOL bSin = FALSE);

void  FAR Afx_IO_Delay();
void  FAR Afx_IO_Delay2(WORD wTimes);
BOOL AfxJudgeInputAgain(int nPort, int nBit, int nAgain, BOOL bHigh);

#endif