If you have asked a mathematician how to find peaks or either bottoms of a region, he/she will simply say that it is the highest/lowest point on the region.
But how computer learns about this? This is what I am going to explain in this file to get the peaks and bottoms of the ECG signal.
Electrocardiography (ECG or EKG) is the process of recording the electrical activity of the heart over a period of time using electrodes placed on the skin. These electrodes detect the tiny electrical changes on the skin that arise from the heart muscle's electrophysiologic pattern of depolarizing and repolarizing during each heartbeat. It is a very commonly performed cardiology test.
Source:
https://en.wikipedia.org/wiki/Electrocardiography
Find me on:
AFCIT
http://www.afcit.xyz
YouTube
https://www.youtube.com/channel/UCuewOYbBXH5gwhfOrQOZOdw
Google Plus
https://plus.google.com/u/0/+AhmedGadIT
SlideShare
https://www.slideshare.net/AhmedGadFCIT
LinkedIn
https://www.linkedin.com/in/ahmedfgad/
ResearchGate
https://www.researchgate.net/profile/Ahmed_Gad13
Academia
https://www.academia.edu/
Google Scholar
https://scholar.google.com.eg/citations?user=r07tjocAAAAJ&hl=en
Mendelay
https://www.mendeley.com/profiles/ahmed-gad12/
ORCID
https://orcid.org/0000-0003-1978-8574
StackOverFlow
http://stackoverflow.com/users/5426539/ahmed-gad
Twitter
https://twitter.com/ahmedfgad
Facebook
https://www.facebook.com/ahmed.f.gadd
Pinterest
https://www.pinterest.com/ahmedfgad/
MATLAB Code + Description : ECG Signal Peaks/Bottoms Finder Using Baseline Approach
1. ECG Signal Peaks/Bottoms Finder Using Baseline
Approach
2013
By
Ahmed Fawzy Gad
Faculty of Computers and Information (FCI)
Menoufia University
Egypt
ahmed.fawzy@ci.menofia.edu.eg
MENOUFIA UNIVERSITY
FACULTY OF COMPUTERS AND
INFORMATION
INFORMATION TECHNOLOGY
DEPARTMENT
DIGITAL SIGNAL PROCESSING
المنوفية جامعة
والمعلومات الحاسبات كلية
قسمالمعلومات تكنولوجيا
الرقمية اإلشارات معالجة
المنوفية جامعة
2. Introdution
If you have asked a mathematician how to find peaks or either bottoms of a re-
gion, he/she will simply say that it is the highest/lowest point on the region.
But how computer learns about this? This is what I am going to explain in this
file.
IDEA
As stated in the earlier approach, I can find the peaks or bottoms by finding
means of the columns, and depending on the result of the mean of each column
I have determined whether this is a separation point between parts of the signal
or not and so on.
This approach has not aided so much in extraction of all needed peak and bot-
tom points.
So, as we have agreed, I have tried to use the baseline to find such points and it
have given a better results.
Steps used are as follows:
1. Find baseline.
2. Find peaks.
3. Find bottoms.
Find Baseline
So first step is to find the baseline of the signal.
Baseline is defined to be the row that has the largest number of pixels that are
defined to be signal pixels.
It is also defend to be the row that has the lowest mean in the signal regarding
that signal pixels are defined in black and non-signal are white.
After checking all rows, I have successfully found baseline using the two meth-
ods and both gives the same result.
This is what shown in the following figure:
3. Find Peaks
I have followed a new simple approach to find peaks of the signal rather than
the one we have discussed in Monday 16/6/2014.
It can be described simply using an example:
If you are asked to find the peak of the following figure, how can you reach it?
You will simply say the peak is the highest pixel that is zero. WHY?
This is because no higher pixel is above the choose pixel.
So to say that a pixel is a peak, you will simply check if there is no pixels that
are neighbors to and higher than the current pixel and have a value of zero.
Find Bottoms
This is similar to finding peaks.
To say that a pixel is a bottom, you will simply check if there is no pixels that are
neighbors to and lower than the current pixel and have a value of zero.
This is shown in the next figure:
4. Applying the Approach
After determining the baseline, all peaks, and all bottoms, the result is as fol-
lows:
As you can note, there are a heavy number of peaks and bottoms that will not
aid in examining the signal.
To eliminate noise from the signal, I`ll not scan 7 lines above and below the
baseline and this is the only adjustment that can be done for the code.
Result is as follows:
5. MATLAB Code
%% Function to find base line of the signal.
function baseLineRow = baseLineFinder2(signal)
sig = imread(signal);
bSig = im2bw(sig);
imwrite(bSig,'npSignal.png');
bSig = imread('npSignal.png');
info = imfinfo('npSignal.png');
resultArray = zeros(1,info.Height);
for hit=1:info.Height
rowUnderProcessing = bSig(hit,:);
resultArray(hit)=sum(~rowUnderProcessing);
end
baseLineRowCounter =max(resultArray);
baseLineRowCounter = find(baseLineRowCounter==resultArray);
for hit=1:info.Height
rowUnderProcessing = bSig(hit,:);
resultArray(hit)=mean(rowUnderProcessing);
end
baseLineRowMean = min(resultArray);
baseLineRowMean = find(baseLineRowMean==resultArray);
%[0, info.Width],[baseLineRowMean, baseLineRowMean]
figure('Name','Detected Base Line');
imshow(bSig);
hold on;
plot([0, info.Width],[baseLineRowMean, baseLineRowMean],'Color','b','LineWidth',2);
%plot([0, info.Width],[baseLineRowCounter, baseLineRowCounter],'Color','r','LineWidth',2);
% disp(['Base line detected using counter is in row : ',num2str(baseLineRowCounter)]);
% disp(['Base line detected using mean is in row : ',num2str(baseLbaseLineRowMean)]);
baseLineRow = baseLineRowMean;
bSig(baseLineRow-7:baseLineRow+7,:)=1;
BoundingBox=uint16([1,baseLineRow-7,info.Width,14]);
rectangle('Position',BoundingBox,'LineWidth',2,'LineStyle','-','EdgeColor','c');
%Now I`ll delete the pre-written images.
delete('npSignal.png');
peakFi(signal ,baseLineRow);
bottomFi(signal ,baseLineRow);
%% Function to find the peaks of the signal.
function peakFi(signal,baseLine)
6. peakY=0;
peakX=0;
detectedPeaks=[];%Creating an array that holds the vertices of the peaks detected.
portionBeingProcessed=imread(signal);
portionBeingProcessed=im2bw(portionBeingProcessed);
%Special case for first column.
for m=1:baseLine-7
if portionBeingProcessed(m,1)==0
if portionBeingProcessed(m-1,2)~=0
%pointFound=1;
peakY=m;
%Now, drawing a rectangle over the peak to highlight it.
BoundingBox=uint16([1,peakY-1,2,2]);
rectangle('Position',BoundingBox,'LineWidth',2,'LineStyle','-','EdgeColor','r');
else
break;
end
end
end
k=2;
kk=2;
while k<=size(portionBeingProcessed,2)-1
while kk<=baseLine-7
if portionBeingProcessed(kk,k)==0
if portionBeingProcessed(kk-1,k+1)~=0 & portionBeingProcessed(kk-1,k-1)~=0
%pointFound=1;
peakY=kk;
peakX=k;
%Now, drawing a rectangle over the peak to highlight it.
BoundingBox=uint16([peakX-1,peakY-1,2,2]);
rectangle('Position',BoundingBox,'LineWidth',2,'LineStyle','-','EdgeColor','r');
k=k+1;
kk=1;
else
k=k+1;
kk=1;
if k>=size(portionBeingProcessed,2)
break;
end
end
else
kk=kk+1;
if(kk==baseLine-7)
k=k+1;
if k>=size(portionBeingProcessed,2)
7. break;
end
kk=1;
end
end
end
end
end%End of peakFi function.
%% Function to find the bottoms of the signal.
function bottomFi(signal,baseLine)
peakY=0;
peakX=0;
detectedBottoms=[];%Creating an array that holds the vertices of the bottoms detected.
portionBeingProcessed=imread(signal);
portionBeingProcessed=im2bw(portionBeingProcessed);
%Special case for last column.
for m=baseLine-7:-1:1
if portionBeingProcessed(m,info.Width)==0
if portionBeingProcessed(m-1,info.Width-1)~=0
%pointFound=1;
peakY=m;
%Now, drawing a rectangle over the peak to highlight it.
BoundingBox=uint16([info.Width-1,peakY-1,1,1]);
rectangle('Position',BoundingBox,'LineWidth',2,'LineStyle','-','EdgeColor','g');
else
break;
end
end
end
k=2;
kk=size(portionBeingProcessed,1)-1;
while k<=size(portionBeingProcessed,2)-1
while kk>=baseLine+7
if portionBeingProcessed(kk,k)==0
if portionBeingProcessed(kk+1,k-1)~=0 & portionBeingProcessed(kk+1,k+1)~=0
%pointFound=1;
peakY=kk;
peakX=k;
%Now, drawing a rectangle over the peak to highlight it.
BoundingBox=uint16([peakX-1,peakY-1,2,2]);
rectangle('Position',BoundingBox,'LineWidth',2,'LineStyle','-','EdgeColor','g');
k=k+1;
kk=size(portionBeingProcessed,1)-1;