How can I plot each local maxima of my ECG data
2 次查看(过去 30 天)
显示 更早的评论
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. ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/261124/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/261124/image.png)
% 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
采纳的回答
Meg Noah
2020-1-10
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](https://www.mathworks.com/matlabcentral/answers/uploaded_files/261170/HeartBeaTFigure.png)
2 个评论
Meg Noah
2020-1-10
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');
for ipeak = 1:length(xpeak)
fprintf(1,'%4.4d %f\n',xpeak(ipeak),ECG(xpeak(ipeak)));
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 ECG / EKG 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!