MATLAB: How to plot each local maxima of the ECG data

combineecgfigurefigureshold onlocal maximalocationmamaxmaximumpeaksplotplotssignalsubplotstime

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.
% Read data.
ECG = csvread('ECG.csv');
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

Best Answer

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
% Read data.
ECG = csvread('ECG.csv');
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
HeartBeaTFigure.png