ecg based report

28
MEHRAN UNIVERSITY OF ENGINEERING AND TECHNOLOGY JAMSHORO SUBJECT : NEUROSCIENCES AND NEURAL NETWORKS PROJECT TITLE : ECG Signal Processing in MATLAB - Detecting R-Peaks . SUBMITTED TO : DANIYAL MAHESHWARI

Upload: mehran-university-of-engineering-and-technology-jamshoro

Post on 20-Mar-2017

29 views

Category:

Education


5 download

TRANSCRIPT

Page 1: ECG BASED REPORT

MEHRAN UNIVERSITY OF ENGINEERING AND TECHNOLOGY JAMSHORO

SUBJECT : NEUROSCIENCES AND NEURAL NETWORKS

PROJECT TITLE : ECG Signal Processing in MATLAB - Detecting R-Peaks.

SUBMITTED TO : DANIYAL MAHESHWARI

SUBMITTED BY : SHANZA KAIMKHANI

INTRODUCTION

Page 2: ECG BASED REPORT

ELECTROCARDIOGRAM(ECG)

□ An electrocardiogram (ECG) is a test which measures the electrical activity of your heart to show whether or not it is working normally.

□ An ECG records the heart’s rhythm and activity on a moving strip of paper or a line on a screen. Your doctor can read and interpret the peaks and dips on paper or screen to see if there is any abnormal or unusual activity.

What can an ECG (electrocardiogram) show?□ An electrocardiogram can be a useful way to find out whether your high blood pressure has caused any damage to your heart or blood vessels. Because of this, you may be asked to have an ECG when you are first diagnosed with high blood pressure.

Some of the things an ECG reading can detect are:

□ cholesterol clogging up your heart’s blood supply

□ a heart attack in the past

□ enlargement of one side of the heart

□ abnormal heart rhythms

Page 3: ECG BASED REPORT

How is an ECG carried out?□ An ECG (electrocardiogram) is a safe and painless test which normally only takes a few minutes.

□ Leads from an electrocardiograph machine are attached to the skin on your arms, legs and chest using sticky patches. These leads read signals from your heart and send this information to the electrocardiograph. The machine then prints the reading on a paper strip or on a screen.

BASIC TASK

The basic task of electrocardiogram (ECG) processing is R-peaks detection. There are some difficulties one can encounter in processing ECG: irregular distance between peaks, irregular peak form, presence of low-frequency component in ECG due to patient breathing etc. To solve the task the processing pipeline should contain particular stages to reduce influence of those factors. Present sample demonstrates such a pipeline. The aim is to show results of processing in main pipeline stages.

□ An Ideal ECG Looks Like This And It Keeps Repeating Itself.

□ We Will Try To Detect The R-Peaks In This Project.

Page 4: ECG BASED REPORT

R-PEAKS DETECTION IN MATLAB

□ A General Noise Corrupted ECG signal Looks Like This□ The ECG Sinal We Are Going To Work With Looks Like This

□ A Closer Look At The Signal

STEPS FOR DETECTION

1 Remove Low Frequency Components

1 Change to frequency domain using fft

2 Remove low frequency components

3 Back to time domain using fft

Page 5: ECG BASED REPORT

2 find local maxima using widowed filter

3 Remove Small values,store significant ones

4 Adjust filter size and repeat 2,3

HOW IT WORKS ?

(Fig 1)

Let us have some digital ECG signal — that is our input data

(Fig 2)

As one can see the ECG is uneven. Thus our first step is to straighten it. To say that in mathematical language, we should remove low-frequency component. The idea is to apply direct fast Fourier transform — FFT, remove low frequencies and restore ECG with the help of inverse FFT. Here is the result of FFT processing

(Fig 3)

Page 6: ECG BASED REPORT

Our second step is to find local maxima. To do that we use windowed filter that “sees” only maximum in his window and ignores all other values. On this step we use window of default size. As result we get this

(fig 4)

Now we should remove small values and preserve significant ones — fig. 4. Here we are using a threshold filter.

(Fig 5)

In this case the result is good but in general case we cannot be sure we have all the peaks. So the next step is to adjust filter window size and repeat filtering — fig. 5. Compare the result with fig. 3 — now filtering quality is much better.

(Fig 6)

Now we are ready to get the final result and here it is

Page 7: ECG BASED REPORT

FINAL RESULT OF ALGORITHM

MATLAB DEMONSTRATIONThe demo package includes 5 files — two MatLab scripts, two data samples and description:

ecgdemo.m — main MatLab script, ecgdemowinmax.m — window peak filter script, ecgdemodata1.mat — first sample ECG data file, ecgdemodata2.mat — second sample ECG data file, readme.txt — description.

Some technical notes on demo:

To run the sample MatLab should be installed on your computer. Unpack the sample and copy .m and .mat files into MatLab work

directory. Start up MatLab and type in:

>>ecgdemo

Page 8: ECG BASED REPORT

□ After open MATLAB You Have To Go To The Directory Where You Have Downloaded The Code.□ Or Paste The Code In Script File.

% ECGDEMO ECG PROCESSING DEMONSTRATION - R-PEAKS DETECTION% % 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:% [email protected]%% 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 situationsfor 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);

Page 9: ECG BASED REPORT

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); if rem(WinSize,2)==0 WinSize = WinSize+1; end filtered1=ecgdemowinmax(corrected, WinSize); % Scale ecg peaks1=filtered1/(max(filtered1)/7); % 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); 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 % 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

Page 10: ECG BASED REPORT

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 offend

TO RUN THE CODE JUST TYPE <<ecgdemo IN COMMAND WINDOW

Page 11: ECG BASED REPORT

As you have already seen how the ALGORITHM works,Lets see its implementation in MATLAB.□ RUN M FILE

Page 12: ECG BASED REPORT

WE FIND OUTPUTSThis is your result For Sample 1

Page 13: ECG BASED REPORT
Page 14: ECG BASED REPORT

This is your Result For Sample 2

Page 15: ECG BASED REPORT
Page 16: ECG BASED REPORT

TRAIN NETWORKNow we have to train this code in NEURAL NETWORKTYPE nntool in command Window

Page 17: ECG BASED REPORT
Page 18: ECG BASED REPORT

Press new and select data and Network And Create The Data

Page 19: ECG BASED REPORT

Then,Select Target and create

Page 20: ECG BASED REPORT

Now create Network Select Input data And Target dataThen Click Create

Page 21: ECG BASED REPORT

Then CLICK View we find

These Input data,Target data And Network Will Be Added Into Neural Network/Data Manager(nntool)

Page 22: ECG BASED REPORT

Now CLICK ImportAnd Import The Values Which Are Provinding In MATLAB Code.

□ Select Input Data,Target Data,Initial Input States,Initial Layer States,Output Data And Error Data From The MATLAB Workspace In Order To TRAIN the NETWORK.□ These All Will Be Import In Neural Network/Data Manager(nntool)

Page 23: ECG BASED REPORT

Just CLICK Network

Now We Have To TRAIN This NetworkCLICK TRAINSelect Input And Target Data

Page 24: ECG BASED REPORT

Then,CLICK Train Network

Page 25: ECG BASED REPORT

CLICK performance

Page 26: ECG BASED REPORT

CLICK Training State

Page 27: ECG BASED REPORT

Now,CLICK Regression

<<THANKYOU