MATLAB: How to separate ECG signal into time sections

ecgfrequencyheartbeatsindiceslocal maximapeakssamplesampling frequencysectionsectionssignalwidth

Hi,
I want to write a function that allows me to detect local maxima in a ECG signal. I want to separate the ECG signal into different time sections and then find the local maximum within each of those sections.
I am trying to do this but I am confused, how to approach this. I am new to MATLAB and not sure how to approach the coding

Best Answer

Wow - that is not the algorithm I would chose for this data at all! But here is a solution for implementing that algorithm
clc
close all
clear all
ECG = dlmread('ECG.csv');
nx = numel(ECG);
x = 1:nx;
figure('color','white','position',[70 100 600 900]);
subplot(3,1,1);
plot(x,ECG)
title({'Heartbeat data'})
xlabel('x')
ylabel('ECG(x)')
% visual inspection shows that a good width is 400 points
% need to zero pad this data to do the algorithm as directed
W = 400;
L = W/2;
xZeroPadded = 1:nx+W;
ECGZeroPadded = zeros(1,nx+W);
ECGZeroPadded(L+1:nx+L) = ECG;
subplot(3,1,2);
plot(xZeroPadded,ECGZeroPadded,'b')
title({'Zeropadded Heartbeat data'})
xlabel('x')
ylabel('ECG(x)');
% now look for the peaks in the data
nW = ceil((nx+W)./L) -2; % number of segments to search
xpeak = zeros(1,nW); % locations of the peaks
for isegment = 1:nW
xSegment0 = 1 + (isegment-1)*L;
xSegment1 = xSegment0 + W - 1;
ECGSegment = ECGZeroPadded(xSegment0:xSegment1);
xpeak(isegment) = xSegment0 + find(ECGSegment == max(ECGSegment),1,'first') - 1;
% fprintf(1,'%d %4.4d %4.4d %4.4d\n',isegment,xSegment0,xSegment1, ...
% xpeak(isegment));
end
% remove redundant data
xpeak = unique(xpeak);
hold on;
plot(xpeak,ECGZeroPadded(xpeak),'r+');
% subtract the zero-padding from the xpeak values
xpeak = xpeak - L;
subplot(3,1,3);
plot(x,ECG)
title({'Heartbeat data'})
xlabel('x')
ylabel('ECG(x)')
hold on;
plot(xpeak,ECG(xpeak),'r.','markersize',12);
fprintf(1,'index ECGValue\n');
Heartbeat2.png
for ipeak = 1:length(xpeak)
fprintf(1,'%4.4d %f\n',xpeak(ipeak),ECG(xpeak(ipeak)));
end
index ECGValue
0078 233.000000
0371 253.000000
0664 257.000000
0948 237.000000
1232 229.000000
1516 242.000000
1810 254.000000
2046 240.000000
2404 242.000000
2707 243.000000
2999 250.000000