How to sample data at set intervals and then plot?

조회 수: 21 (최근 30일)
ImperialStar
ImperialStar 2020년 1월 5일
댓글: ImperialStar 2020년 1월 5일
I am currently trying to create code that samples ECG data at certain intervals and extracts the maximum value between each interval. I believe my code does this as the ouput A returns the peaks of the data. However when i try and then plot these peaks it instead plots it as the sample number. E.g. one of my peaks is 257 in amplitude but the graph plots it as the 257th sample instead of the sample corresponding to the max value. I have attched an example of what my code plots. Any help is greatly appreciated!
ECG = load('ECG.csv');
Fs = 350;
frame_duration = 0.5;
frame_len = frame_duration*Fs;
N = length(ECG);
num_frames = floor(N/frame_len);
Time = (0:length(ECG)-1)/Fs; %Number of samples divided by frquency
Amp = ECG(:,1); %ECG Amplitude
hold on
for k = 1:num_frames
frame = ECG((k-1)*frame_len + 1: frame_len*k);
max_val = max(frame);
if (max_val > 100)
A = max_val
figure
plot(Time,Amp,Time(A),Amp(A),'r*')
end
hold off
end

채택된 답변

Image Analyst
Image Analyst 2020년 1월 5일
You're giving the max value as the index when what you should be getting from max() is the index. And you don't need A at all.
Try
[max_val, indexAtMax] = max(frame);
if (max_val > 100)
hFig = figure
plot(Time,Amp, Time(indexAtMax),Amp(indexAtMax),'r*')
hFig.WindowState = 'maximized'; % Maximize the window, if desired.
  댓글 수: 3
Image Analyst
Image Analyst 2020년 1월 5일
Your index is wrong. Try this:
% Read data.
ECG = csvread('ECG.csv');
Fs = 350;
frame_duration = 0.5;
frame_len = frame_duration*Fs;
N = length(ECG);
num_frames = floor(N/frame_len);
Time = (0:length(ECG)-1)/Fs; %Number of samples divided by frquency
Amp = ECG(:,1); %ECG Amplitude
plot(Time, Amp, 'b-', 'LineWidth', 2);
xlabel('Time', 'FontSize', 20);
ylabel('Entire ECG', 'FontSize', 20);
hold on
threshold = 100;
line(xlim, [threshold, threshold], 'Color', 'r', 'LineWidth', 2);
grid on;
for k = 1:num_frames
index1 = (k-1)*frame_len + 1;
index2 = frame_len*k;
frame = Amp(index1 : index2);
fprintf('Extracted frame #%d, from index %d to %d.\n', ...
k, 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)
% Bring up a new figure.
figure
plot(Time,Amp, Time(originalIndex),Amp(originalIndex),'r*')
hold on
line(xlim, [threshold, threshold], 'Color', 'r', 'LineWidth', 2);
xlabel('Time', 'FontSize', 20);
ylabel('Entire ECG', 'FontSize', 20);
grid on;
hFig.WindowState = 'maximized'; % Maximize the window, if desired.
end
hold off
end
ImperialStar
ImperialStar 2020년 1월 5일
This is brilliant thank you. It has also helped me realise how to sort out my various other scripts so it was a big help. Much appreciated.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

David Hill
David Hill 2020년 1월 5일
ECG = load('ECG.csv');
Fs = 350;
frame_duration = 0.5;
frame_len = frame_duration*Fs;
N = length(ECG);
num_frames = floor(N/frame_len);
Amp = ECG(:,1)'; %ECG Amplitude
Amp = Amp(1:num_frames*frame_len);
Time = (0:length(Amp)-1)/Fs;
amp = reshape(Amp,frame_len,num_frames);
[mamp,idx] = max(amp);
Idx=(find(mamp>100)-1)*frame_len + idx;
plot(Time,Amp,Time(Idx),mamp(Idx),'r*');
  댓글 수: 2
ImperialStar
ImperialStar 2020년 1월 5일
Unfortunately this returns an error of "Matrix dimensions must agree".
David Hill
David Hill 2020년 1월 5일
ECG = load('ECG.csv');
Fs = 350;
frame_duration = 0.5;
frame_len = frame_duration*Fs;
N = length(ECG);
num_frames = floor(N/frame_len);
Amp = ECG(:,1)'; %ECG Amplitude
Amp = Amp(1:num_frames*frame_len);
Time = (0:length(Amp)-1)/Fs;
amp = reshape(Amp,frame_len,num_frames);
[mamp,idx] = max(amp);
Idx=(find(mamp>100)-1)*frame_len + idx;
plot(Time,Amp,Time(Idx),Amp(Idx),'r*');
This should work. Could not test without the ECG.csv file.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 AI for Signals에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by