# How can I plot each local maxima of my ECG data

6 views (last 30 days)
Anon on 10 Jan 2020
Commented: Meg Noah on 10 Jan 2020
So far I have this code, to detect the local maxima of my ECG signal by separting the ECG signal into sections of time and finding the location of each maximum. However when I run this script, I only get one maximum showing up. I would like to edit the code for it to look this, so that I get a local maxima on each line. f_s = 350;
frame_duration = 0.5;
frame_len = frame_duration*f_s;
N = length(ECG);
num_frames = floor(N/frame_len);
Time = (0:length(ECG)-1)/f_s; %Number of samples divided by frequency
Amp = ECG(:,1); %ECG Amplitude
plot(Time, Amp, 'b-', 'LineWidth', 2);
xlabel('Time', 'FontSize', 20);
ylabel('ECG', 'FontSize', 20);
hold on
threshold = 100;
line(xlim, [threshold, threshold], 'Color', 'r', 'LineWidth', 2);
grid on;
for m = 1:num_frames
index1 = (m-1)*frame_len + 1;
index2 = frame_len*m;
frame = Amp(index1 : index2);
fprintf('Extracted frame #%d, from index %d to %d.\n', ...
m, index1, index2);
[max_val, indexAtMax] = max(frame);
originalIndex = indexAtMax + index1 - 1;
fprintf(' Max of %f occurs at index %d of cropped signal, or %d of original signal.\n', ...
max_val, indexAtMax, originalIndex);
if (max_val > threshold)
plot(Time,Amp, Time(originalIndex),Amp(originalIndex),'r*')
hold on
line(xlim, [threshold, threshold], 'Color', 'r', 'LineWidth', 2);
xlabel('Time', 'FontSize', 20);
ylabel('ECG', 'FontSize', 20);
grid on;
hFig.WindowState = 'maximized'; % Maximize the window
hold on
end
hold off
end
Anon on 10 Jan 2020
I should add that I have removed figure, but the plot looks a bit weird now

Meg Noah on 10 Jan 2020
Remove the
hold off
in the outer loop and remove the replotting of the entire dataset in each sement processing step - change to:
plot(Time(originalIndex),Amp(originalIndex),'r*')
The code is now
f_s = 350;
frame_duration = 0.5;
frame_len = frame_duration*f_s;
N = length(ECG);
num_frames = floor(N/frame_len);
Time = (0:length(ECG)-1)/f_s; %Number of samples divided by frequency
Amp = ECG(:,1); %ECG Amplitude
plot(Time, Amp, 'b-', 'LineWidth', 2);
xlabel('Time', 'FontSize', 20);
ylabel('ECG', 'FontSize', 20);
hold on
threshold = 100;
line(xlim, [threshold, threshold], 'Color', 'r', 'LineWidth', 2);
grid on;
for m = 1:num_frames
index1 = (m-1)*frame_len + 1;
index2 = frame_len*m;
frame = Amp(index1 : index2);
fprintf('Extracted frame #%d, from index %d to %d.\n', ...
m, index1, index2);
[max_val, indexAtMax] = max(frame);
originalIndex = indexAtMax + index1 - 1;
fprintf(' Max of %f occurs at index %d of cropped signal, or %d of original signal.\n', ...
max_val, indexAtMax, originalIndex);
if (max_val > threshold)
plot(Time(originalIndex),Amp(originalIndex),'r*')
hold on
line(xlim, [threshold, threshold], 'Color', 'r', 'LineWidth', 2);
xlabel('Time', 'FontSize', 20);
ylabel('ECG', 'FontSize', 20);
grid on;
hFig.WindowState = 'maximized'; % Maximize the window
hold on
end
% hold off
end ##### 2 CommentsShowHide 1 older comment
Meg Noah on 10 Jan 2020
clc
close all
clear all
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;
subplot(3,1,2);
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;
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;
% 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');
for ipeak = 1:length(xpeak)
fprintf(1,'%4.4d %f\n',xpeak(ipeak),ECG(xpeak(ipeak)));
end