Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Tagged as

Go to top

ECG recording, storing, filtering and recognition

, 14 Apr 2004
Full open code project for making driver and application software for ECG medical measurements.
ecg_dsp_src.zip
drvECG_1
drvECG_1.clw
drvECG_1.def
drvECG_1.dsp
drvECG_1.dsw
drvECG_1.exp
drvECG_1.ilk
drvECG_1.lib
res
Ecg_1
ECG_1.clw
ECG_1.dsp
ECG_1.dsw
ECG_DRAW.obj
ECG_Statistic_View.lib
ECG_VIEW.exp
ECG_VIEW.ilk
ECG_VIEW.lib
TestOCX.ocx
drvECG_1.exp
drvECG_1.ilk
drvECG_1.lib
resource.hm
res
ECG_1.ico
bitmap1.bmp
bmp00001.bmp
setup.bmp
ECG_Statistic_View
ECG_Statistic_View.clw
ECG_Statistic_View.def
ECG_Statistic_View.dsp
ECG_Statistic_View.dsw
res
ECG_VIEW
ECG_DRAW.obj
ECG_VIEW.APS
ECG_VIEW.clw
ECG_VIEW.def
ECG_VIEW.dll
ECG_VIEW.dsp
ECG_VIEW.dsw
ECG_VIEW.exp
ECG_VIEW.lib
res
testrecord2.zip
georgi.ecg
test_file1.zip
test_file1.ecg
// 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);
	}

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Georgi Petrov
Instructor / Trainer
Bulgaria Bulgaria
PhD, Cum Laude in digital automation systems
M.S. in Telemommunication management
B.S. in Telecommunication systems engineering
Programming: CUDA, C/C++, VHDL
Software and Hardware development and consulting:
data acquisition, image processing, medical instrumentation

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 15 Apr 2004
Article Copyright 2003 by Georgi Petrov
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid