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

📄 dsp_ecg.cpp

📁 一个开源的心电图测量仪驱动和应用软件,可记录
💻 CPP
字号:
// DSP_ECG.cpp: implementation of the DSP_ECG class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ECG_1.h"
#include "DSP_ECG.h"
#include "DSP_Filter.h"
#include "math.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

DSP_ECG::DSP_ECG()
{

}

DSP_ECG::~DSP_ECG()
{

}

void DSP_ECG::SetECG(double* ARECG,int lenght,double* destMINMAX,int* qrs_count,int* qrs_ms,int* pr_ms,int* qt_qtc_ms,int select_segment)
{
		//clear the arrays
	ZeroAll();
	//Move ARECG in to ecg
	MoveArr(arECG,ARECG,lenght);
	//calculate D1 of the ECg signal
	dsp_flf.Delta1(arD1,arECG,lenght);
	//calculate the D2 of the ECg sygnal
	dsp_flf.Delta2(arD2,arECG,lenght);
	//count QRS complex
	//the result will be set at the address that points
	//*qrs_count so to use in after this
	dsp_flf.RFilter_Low(arECG,lenght,250,0.1);
	*qrs_count = QRS_COUNT(arECG,lenght,destMINMAX);
		
	*pr_ms = ms_PR(arECG,lenght,arD1,arD2);
	*qrs_ms = ms_QRS(arECG,lenght,arD1,arD2);
	*qt_qtc_ms = ms_QT_QTc(arECG,lenght,arD1,arD2);

	if(select_segment==0)
		MoveArr(destMINMAX,arQRS,lenght);
	if(select_segment==1)
		MoveArr(destMINMAX,arPR,lenght);
	if(select_segment==2)
		MoveArr(destMINMAX,arQT,lenght);
	if(select_segment==3)
	{
		ZeroAll();
		MoveArr(destMINMAX,arD1,lenght);
	}
}

int DSP_ECG::QRS_COUNT(double* ECG,int lenght,double* destminmax)
{	
	//max lenght can be 5000 samples
	if(lenght>5000)
	{
		lenght=5000;
		MessageBox(NULL,"This ECG DSP program can't calculate lenght>5000smpl","Error",MB_OK);
	}
	
	int D;
	int Delta[5000];
	double AR_MINMAX[5000];
//	double AR_ECG[5000];
	int Deltamin[5000];
	for(int i=0;i<lenght;i++)
	{
		AR_MINMAX[i] = 0;
		Deltamin[i]= 0;
	}
	//Calculate the standard deviation of the D1 signal
	double dev = dsp_flf.StandardDeviation(arD1,lenght);
	dev = dev*1.2;
	for(i=0;i<lenght-1;i++)
	{
		D = arD1[i];
		Delta[i] = D;
		if(D>dev)
		*(AR_MINMAX+i) = 2;
		if(D<-dev)
		*(AR_MINMAX+i) = -2;
	}
	D = 0;
	int d_count;
	for(i=0;i<lenght-1;i++)
	{	
		d_count=0;
		if(*(AR_MINMAX+i)==2)
		{	//*(AR_REV+i-1)=400;//find the delta begining
			
			do{	i++;d_count++;}
			while(*(AR_MINMAX+i)==2);
		if(d_count>3)
		{
			int max=i;
			do{max++;}
			while(*(AR_MINMAX+max)==2);
			*(AR_MINMAX+max) = *(ECG+max);//fing max after delta+

			do{max++;}
			while(*(AR_MINMAX+max)<0);
			*(AR_MINMAX+max+1) = *(ECG+max);//find min after R max
			
			//Find end of S segment
			do
			{
				max++;
			}while(arD1[max]>0);
			arQRS[max] = -200;
	
			max = i;
			do{max--;}
			while(Delta[max]>0);
			*(AR_MINMAX+max+1) = *(ECG+max+1);//find min before P d+
			do
			{
				max--;
			}while(arD1[max]<0);
			arQRS[max] = 200;

			//*(AR_REV+i) = 400;//end of the delta+
		D++;	
		}
		}
	}
	for(i=0;i<lenght;i++)
	{
		if(arQRS[i]==200)
		{
			do
			{
				i++;
				arMark[i] = 200;
			}
			while(arQRS[i]>-200);
			arMark[i] = -200;
		}
	}
	//Move back the min max values
//	MoveArr(destminmax,arMark,lenght);
	//D contains QRS/10s to convert it in QRS/min
	//we must multiply it by 6, because 1 min contains 60 seconds
	return D*6;
}

int DSP_ECG::ms_QRS(double *ECG, int lenght, double *D1, double *D2)
{//return QRS complex deviation
	int ms;
	int mean = 0;
	int count =0;
	for(int i=0;i<lenght;i++)
	{
		if(arQRS[i]==200)
		{	
			do
			{
				arQRS[i]=200;
				count++;
				i++;
			}
			while((arQRS[i]>-200)&(i<lenght-1));	
			mean++;		
		}
	}	
	if(mean==0)mean=1;
	ms = count/mean;
	return ms*4;
}

int DSP_ECG::ms_PR(double *ECG, int lenght, double *D1, double *D2)
{
	//returns PR interval
	int ms;
	dsp_flf.RFilter_Low(arECG,lenght,250,0.36);
//	dsp_flf.Delta1(arD1,arECG,lenght);
	for(int i=0;i<lenght;i++)
	{
		int count=0;
		int q = 0;
		if(arQRS[i]==200)
		{
			do
			{
				q++;
			}
			while(arD1[i+q]<=0);
			arPR[i+q] = -100;
			do
			{
				q++;
			}
			while(arECG[i+q]<=0);
		//	arPR[i+q] = -100;
		
			do
			{
				count++;
			}
			while(arD1[i-count]<=0);
			arPR[i-count] = 100;
		/*	do
			{
				count++;
			}
			while(arECG[i-count]>=0);
			arPR[i-count] = 200;
	*/	}
	}
	int mean = 0;
	int count =0;
	for(i=0;i<lenght;i++)
	{
		if(arPR[i]==100)
		{	
			do
			{
				arPR[i]=100;
				count++;
				i++;
			}
			while((arPR[i]>-100)&(i<lenght-1));	
			mean++;
			
		}
	}
	
	if(mean==0)mean=1;
	ms = count/mean;
	
	return ms*4;
}

int DSP_ECG::ms_QT_QTc(double *ECG, int lenght, double *D1, double *D2)
{
//returns QT_QTc interval
	int ms = 0;
	int count = 0;
	for(int i=0;i<lenght;i++)
	{
		if(arQRS[i]==-200)
		{		
			arQT[i] = arECG[i];
			count=0;
			//Find the next QRS 
			//This count will be our interval to search T wave
			do
			{
				count++;
			}
			//very important check for i+count<lenght!!!!!!!!!!!!!!!!!
			while((arQRS[i+count]<200)&(i+count<lenght));
			//After finding the next Q fix it!
			arQT[i+count] = 100;
			
			int max = arECG[i];
			int max_index = 0;
			for(int j=i;j<i+count;j++)
			{
				//finds the maximum value
				//May be a T wave?
				if(arECG[j]>=max)
				{
					max = arECG[j];
					max_index = j;	
				}
							
			}
			//Finding the end of QT interval
			arQT[max_index] = arECG[max_index];
			do
			{
				max_index++;
				arQT[max_index] = 100;
			}
		//	while((arECG[max_index+10]>=0));
			//can use two while!
			while((arD1[max_index]<=0)&(arECG[max_index]>=0));
			arQT[max_index] = -100;
		}
	
	}
	//calculate QT interval meav
	int mean = 0;
	count =0;
	for(i=0;i<lenght;i++)
	{
		if(arQT[i]==100)
		{	
			do
			{
				arQT[i]=100;
				count++;
				i++;
			}
			while((arQT[i]>-100)&(i<lenght-1));	
			mean++;	
		}
	}	
	if(mean==0)mean=1;
	ms = count/mean;
//	dsp_flf.Message(ms*4);
	return ms*4;
}

void DSP_ECG::ZeroAll()
{
	//Zero all arrays
	for(int i=0;i<5000;i++)
	{
		arD1[i] = 0;
		arD2[i] = 0;
		arECG[i] = 0;
		arQRS[i] = 0;
		arQT[i] = 0;
		arMark[i] = 0;
		arPR[i] = 0;
	}
}

void DSP_ECG::MoveArr(double *dest, double *source, int lenght)
{
	//copy one array from source to dest array
	for(int i=0;i<lenght;i++)
	{
		*(dest+i) = *(source+i);
	}

}

⌨️ 快捷键说明

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