⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 public.cpp

📁 EVC 下 USB 通信测试程序
💻 CPP
字号:
#include "stdafx.h"
#include "Public.h"


void ConvToIntFFT(const short *inWaveData,int DataLength,short *outWaveData,float *yMax,float *yMin)
{
	int i;
	int PowerNum;
	float meanVal;
	float PPVal;
	short *ReArray=new short[DataLength];
	short *ImArray=new short[DataLength];
	short *TempArray=new short[DataLength];
	short *indexArray =new short[DataLength];

	CFile bitRevFile;
	int startAddress;
//	iPublicDll BitRev = (iPublicDll)GetProcAddress(g_hPublicDll,_T("BitRev"));
	//计算最大值、最小值、峰峰值、均值
	*yMax=*(inWaveData);
	*yMin=*(inWaveData);
	meanVal=(float)*(inWaveData);
	*(ReArray)=*(inWaveData);
	for(i=1;i<DataLength;i++)
	{
		*(ReArray+i)=*(inWaveData+i);
		if(*(ReArray+i)>*yMax)
		{
			*yMax=*(ReArray+i);
		}
		if(ReArray[i]<*yMin)
		{
			*yMin=*(ReArray+i);
		}
		meanVal+=*(ReArray+i);
	}
	meanVal=meanVal/DataLength; //均值
	PPVal=*yMax-*yMin;   //峰峰值

	//数据计算
	switch(DataLength)
	{
	case 1024:
		PowerNum=10;
		startAddress = 0;
		break;
	case 2048:
		PowerNum=11;
		startAddress = 2048;
		break;
	case 4096:
		PowerNum=12;
		startAddress = 6144;
		break;
	case 8192:
		PowerNum=13;
		startAddress = 14336;
		break;
	case 16384:
		PowerNum=14;
		break;
	case 32768:
		PowerNum=15;
		break;
	}
	
	for(i=0;i<DataLength;i++)
	{
		TempArray[i]=(short)((ReArray[i]-meanVal)*(pow(2,BITNUM-1)-1)/PPVal);
	}
	if(bitRevFile.Open(BITREVPATH,CFile::modeRead))
	{
		bitRevFile.Seek(startAddress,CFile::begin);
		bitRevFile.Read(indexArray,DataLength*sizeof(short));
		bitRevFile.Close();
	}
	else
	{
		for(i=0; i<DataLength; i++)
			*(indexArray+i)=BitRev(i,PowerNum);   //到位序算法
	}
	for(i=0; i<DataLength; i++)
	{
		ReArray[i]=TempArray[*(indexArray+i)];
	}
	
	//INTFFT计算
	INTFFTCalc(ReArray,ImArray,DataLength,NULL);

	//输出
	DataLength=(int)(DataLength/2.56f);
	for(i=0;i<DataLength;i++)
	{
		*(outWaveData+i)= static_cast<short>((float)((PPVal * *(ReArray+i))/(pow(2,BITNUM-1)-1)));
	}
	
	*yMin=*outWaveData;
	*yMax=*outWaveData;
	for(i=1;i<DataLength;i++)
	{		
		if(*(outWaveData+i)>*yMax)
		{
			*yMax= *(outWaveData+i);
		}
		if(*(outWaveData+i)<*yMin)
		{
			*yMin= *(outWaveData+i);
		}
	}

	delete [] ReArray;
	delete [] ImArray;
	delete []TempArray;
	delete []indexArray;
	//return;
}

//INT16FFT计算
void INTFFTCalc(short *ReArray,short *ImArray,int DataLength,short *temArray)
{
	int CosVal;
	int SinVal;
	int TempImA;
	int TempImB;
	int TempReA;
	int TempReB;
	int TempReA2;
	int TempReB2;

	int TempL;
	long ReTwid;
	long ImTwid;
	short *SineTable=new short[DataLength];

	int i;
	int Gcnt;
	int Scnt;
	int Group;
	int Stage;
	int IndexA;
	int IndexB;
	int SinIndex;
	CFile sinFile;
	int startAddress;
	float fTemp;
	
	if(sinFile.Open(SINDATAPATH,CFile::modeRead))
	{
		switch(DataLength)
		{
		case 1024:
			startAddress = 0;
			break;
		case 2048:
			startAddress = 2048;
			break;
		case 4096:
			startAddress = 6144;
			break;
		case 8192:
			startAddress = 14336;
			break;
		default:
			startAddress = 0;
			break;
		}
		sinFile.Seek(startAddress,CFile::begin);
		sinFile.Read(SineTable,DataLength*sizeof(short));
		sinFile.Close();
	}
	else
	{
		for(i=0;i<DataLength;i++)
		{
			*(SineTable+i)=(short)(32767*sin(6.2832f*i/DataLength));
		}
	}
	

	Stage=2;
	Group=DataLength/4;
	IndexA=0;

	for(Gcnt=0;Gcnt<DataLength/2;Gcnt++)
	{
		IndexB=IndexA+1;
		TempReA=ReArray[IndexA];
		TempReB=ReArray[IndexB];

		//Calculate new value for ReArray[IndexA]
		TempL=TempReA+TempReB;
		ReArray[IndexA]=TempL/2;

		//Calculate new value for ReArray[IndexB]
		TempL=TempReA-TempReB;
		ReArray[IndexB]=TempL/2;

		ImArray[IndexA]=0;
		ImArray[IndexB]=0;

		IndexA=IndexB+1;
	}

	while(Stage<=DataLength/2)
	{
		IndexA=0;
		SinIndex=0;
		for(Gcnt=0;Gcnt<Group;Gcnt++)
		{
			for(Scnt=0;Scnt<Stage;Scnt++)
			{
				IndexB=IndexA+Stage;

				TempReA=ReArray[IndexA];
				TempReB=ReArray[IndexB];
				TempImA=ImArray[IndexA];
				TempImB=ImArray[IndexB];

				if(SinIndex==0)
				{
					TempL=TempReA+TempReB;
					TempReA2=TempL/2;

					TempL=TempReA-TempReB;
					TempReB2=TempL/2;

					TempL=TempImA-TempImB;
					TempImB=TempL/2;

					TempL=TempImA+TempImB;
					TempImA=TempL/2;
				}
				else if(SinIndex==DataLength/4)
				{
					TempL=TempReA-TempImB;
					TempReB2=TempL/2;

					TempL=TempReA+TempImB;
					TempReA2=TempL/2;

					TempL=TempImA+TempReB;
					TempImB=TempL/2;

					TempL=TempImA-TempReB;
					TempImA=TempL/2;
				}
				else
				{
					if(SinIndex>DataLength/4)
					{
						SinVal=SineTable[DataLength/2-SinIndex];
						CosVal=-1*SineTable[SinIndex-DataLength/4];
					}
					else
					{
						SinVal=SineTable[SinIndex];
						CosVal=SineTable[DataLength/4-SinIndex];
					}

					ReTwid=TempReB*CosVal+TempImB*SinVal;
					ImTwid=TempImB*CosVal-TempReB*SinVal;

					ReTwid = ReTwid / 32767;
					ImTwid = ImTwid / 32767;

					TempL=TempReA;
					TempL=TempL+ReTwid;
					TempReA2=TempL/2;

					TempL=TempReA;
					TempL=TempL-ReTwid;
					TempReB2=TempL/2;

					TempL=TempImA;
					TempL=TempL-ImTwid;
					TempImB=TempL/2;

					TempL=TempImA;
					TempL=TempL+ImTwid;
					TempImA=TempL/2;
				}

				ReArray[IndexA]=TempReA2;
				ReArray[IndexB]=TempReB2;
				ImArray[IndexA]=TempImA;
				ImArray[IndexB]=TempImB;

				IndexA=IndexA+1;
				SinIndex=SinIndex+Group;
			}//end for(Scnt=0;Scnt<Stage;Scnt++)
			IndexA=IndexB+1;
			SinIndex=0;
		}//end for(Gcnt=0;Gcnt<Group;Gcnt++)
		Group=Group/2;
		Stage=Stage*2;
	}// end while(Stage<=DataLength/2)

	for(i=0;i<DataLength;i++)
	{
		if (temArray!=NULL)
		{
			temArray[i]=ReArray[i];
		}

		fTemp=(float)(2*sqrt(pow(ReArray[i],2)+pow(ImArray[i],2)));
		if(fTemp>=32767)
		{
			ReArray[i]=32767;
		}
		else
		{
			ReArray[i]=(short)fTemp;
		}
	}

	if(*SineTable!=NULL)
	{
		delete SineTable;
	}
	return ;

	
}

int BitRev(int index,int bitNum)
{
	int *iBool=new int[bitNum];
	int i;
	int result;

	for(i=bitNum-1;i>=0;i--)
	{
		if(index>=pow(2,i))
		{
			index=(int)(index-pow(2,i));
			iBool[i]=1;
		}
		else
		{
			iBool[i]=0;
		}
	}

	result=0;
	for(i=0;i<bitNum;i++)
	{
		result=(int)(result+pow(2,(bitNum-1-i))*iBool[i]);
	}

	delete iBool;
	return result;
}
void CalcIndexValue(const float *waveData,int dataLength,DataIndexVal *tDataIndex)
{
	//计算指标值,目前包括有效值,峰峰值,峭度
	int i;
	float temp1;
	float temp2;
	float minVal;
	float maxVal;
	
	temp1=0;
	temp2=0;
	minVal=*waveData;
	maxVal=*waveData;
	for(i=0;i<dataLength;i++)
	{
		temp1=(float)(temp1+pow(*(waveData+i),2));
		temp2=(float)(temp2+pow(*(waveData+i),4));
		if(*(waveData+i)<minVal)
		{
			minVal=*(waveData+i);
		}
		if(*(waveData+i)>maxVal)
		{
			maxVal=*(waveData+i);
		}
	}
	//有效值
	temp1=temp1/dataLength;
	tDataIndex->RMSValue=(float)sqrt(temp1);
	//峭度
	temp2=temp2/dataLength;
	tDataIndex->KurValue=(float)(temp2/pow(tDataIndex->RMSValue,4));
	//峰峰值
	tDataIndex->PPValue=maxVal-minVal;
	return;
}
BOOL DirectoryCheck(CString strPath,BOOL bCreate)
{
	HANDLE hSearch;
	WIN32_FIND_DATA FileData;
	BOOL bRet = FALSE;   
	CString strSearchPath = strPath;
	if(_T('\\') == strSearchPath[strSearchPath.GetLength()-1])
	{
		strSearchPath.SetAt(strSearchPath.GetLength()-1,_T('\0'));  
	}
	hSearch = FindFirstFile(strSearchPath,&FileData);   
	if((INVALID_HANDLE_VALUE!=hSearch) && (FileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
	{   
		bRet = TRUE;   
	}
	else
	{
		if (bCreate)
		{
			CreateDirectory(strSearchPath,NULL);
		}
	}
	if(INVALID_HANDLE_VALUE != hSearch)
	{
		FindClose(hSearch);
	}
	return   bRet;      
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -