How can I plot each local maxima of my ECG data
조회 수: 1 (최근 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에서 Single-Rate Filters에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!