ECG analysis by using Matlab


Introduction

Electrocardiogram (ECG or EKG) is a diagnostic tool that measures and records the electrical activity of the heart in exquisite detail.


various conditions can be measured over a period of time by using electrodes placed on skin.

These electrodes detects small electrical charges on our skin that originate due to the process of depolarization and polarization after every heartbeat



ECG tracing of a normal heartbeat consists of a P-wave, a QRS -complex and a T-wave.

The electrical signal begins in the sinoatrial node which is the P-wave.

After the signal leaves the AV node it travels along a pathway called the bundle of His where this signals is known as the QRS-wave.

The ventricles then recover to their normal electrical state which is known as the T-wave.

METHODOLOGY

1 R-peaks Detection

For R-peaks Detection we need to follow the given procedure:


Step 1: Remove low frequency components

Change to fourier domain using the fft command (fast fourier transform)

Remove the low frequencies found

Back to time-domain using fft command

Step 2: Find local maxima using filter

Step 3: Remove small values, and store significant ones.

Step 4: Repeat the 2 and 3 steps.

2 Calculating Heart Beat


Large peaks in the ECG signal represents QRS complex which is obtained when the heart beats.

Therefore, the number of QRS complex gives us the number of times heart beats.

Heart Beat rate in (beats/second) can be calculated by the formula.


Rate=60*sampling rate / R-R interval

Ex:- 60(1min)*4(peaks)/2.5 seconds = 90


(where R-R interval = distance between first and last peak / length between two peaks)

ABNORMALITIES AND CONDITIONS OF HEART

qNormal heart beat range: 60-100 bpm


qHeart beat< 60-100bpm Bradycardia


qHeart beat > 60-100 bpm Tachycardia


qIf cycles not evenly placed Arrhythmia


If P-R interval > 0.2 secondsBlockage of AV node

Matlab CODE

% ECGDEMO ECG PROCESSING DEMONSTRATION - R-PEAKS DETECTION

%

% NOTE: Surya modified the code by adding Heart Rate calculation and

% some additional comments. Contact: p.surya1994@gmail.com

%

%

% This file is a part of a package that contains 5 files:

%

% 1. ecgdemo.m - (this file) main script file;

% 2. ecgdemowinmax.m - window filter script file;

% 3. ecgdemodata1.mat - first ecg data sample;

% 4. ecgdemodata2.mat - second ecg data sample;

% 5. readme.txt - description.

%

% The package downloaded from http://www.librow.com

% To contact the author of the sample write to Sergey Chernenko:

% S.Chernenko@librow.com

%

% To run the demo put

%

% ecgdemo.m;

% ecgdemowinmax.m;

% ecgdemodata1.mat;

% ecgdemodata2.mat

%

% in MatLab's "work" directory, run MatLab and type in

%

% >> ecgdemo

%

% The code is property of LIBROW

% You can use it on your own

% When utilizing credit LIBROW site


% We are processing two data samples to demonstrate two different situations

for demo = 1:2:3

% Clear our variables

clear ecg samplingrate corrected filtered1 peaks1 filtered2 peaks2 fresult

% Load data sample

switch(demo)

case 1,

plotname = 'Sample 1';

load ecgdemodata1;

case 3,

plotname = 'Sample 2';

load ecgdemodata2;

end

% Remove lower frequencies

fresult=fft(ecg);

fresult(1 : round(length(fresult)*5/samplingrate))=0;

fresult(end - round(length(fresult)*5/samplingrate) : end)=0;

corrected=real(ifft(fresult));

% Filter - first pass

WinSize = floor(samplingrate * 571 / 1000);%window size is 571

if rem(WinSize,2)==0

WinSize = WinSize+1;

end

filtered1=ecgdemowinmax(corrected, WinSize);

% Scale ecg

peaks1=filtered1/(max(filtered1)/8);

% Filter by threshold filter

for data = 1:1:length(peaks1)

if peaks1(data) < 4

peaks1(data) = 0;

else

peaks1(data)=1;

end

end

positions=find(peaks1);

distance=positions(2)-positions(1);

% Returns minimum distance between two peaks

for data=1:1:length(positions)-1

if positions(data+1)-positions(data)<distance

distance=positions(data+1)-positions(data);

end

end

% Optimize filter window size

QRdistance=floor(0.04*samplingrate);

if rem(QRdistance,2)==0

QRdistance=QRdistance+1;

end

WinSize=2*distance-QRdistance;

% Filter - second pass

filtered2=ecgdemowinmax(corrected, WinSize);

peaks2=filtered2;

for data=1:1:length(peaks2)

if peaks2(data)<4

peaks2(data)=0;

else

peaks2(data)=1;

end

end

%% This part of the code between the double comments is added by Surya Penmetsa

positions2=find(peaks2);

distanceBetweenFirstAndLastPeaks = positions2(length(positions2))-positions2(1);

averageDistanceBetweenPeaks = distanceBetweenFirstAndLastPeaks/length(positions2);

averageHeartRate = 60 * samplingrate/averageDistanceBetweenPeaks;

disp('Average Heart Rate = ');

disp(averageHeartRate);

% The code written by Surya Penmetsa Ends here.

%%

% Create figure - stages of processing

figure(demo); set(demo, 'Name', strcat(plotname, ' - Processing Stages'));

% Original input ECG data

subplot(3, 2, 1); plot((ecg-min(ecg))/(max(ecg)-min(ecg)));

title('\bf1. Original ECG'); ylim([-0.2 1.2]);

% ECG with removed low-frequency component

subplot(3, 2, 2); plot((corrected-min(corrected))/(max(corrected)-min(corrected)));

title('\bf2. FFT Filtered ECG'); ylim([-0.2 1.2]);

% Filtered ECG (1-st pass) - filter has default window size

subplot(3, 2, 3); stem((filtered1-min(filtered1))/(max(filtered1)-min(filtered1)));

title('\bf3. Filtered ECG - 1^{st} Pass'); ylim([0 1.4]);

% Detected peaks in filtered ECG

subplot(3, 2, 4); stem(peaks1);

title('\bf4. Detected Peaks'); ylim([0 1.4]);

% Filtered ECG (2-d pass) - now filter has optimized window size

subplot(3, 2, 5); stem((filtered2-min(filtered2))/(max(filtered2)-min(filtered2)));

title('\bf5. Filtered ECG - 2^d Pass'); ylim([0 1.4]);

% Detected peaks - final result

subplot(3, 2, 6); stem(peaks2);

title('\bf6. Detected Peaks - Finally'); ylim([0 1.4]);

% Create figure - result

figure(demo+1); set(demo+1, 'Name', strcat(plotname, ' - Result'));

% Plotting ECG in green

plot((ecg-min(ecg))/(max(ecg)-min(ecg)), '-g'); title('\bf Comparative ECG R-Peak Detection Plot');

% Show peaks in the same picture

hold on

% Stemming peaks in dashed black

stem(peaks2'.*((ecg-min(ecg))/(max(ecg)-min(ecg)))', ':k');

% Hold off the figure

hold off

end


391 views0 comments

Recent Posts

See All