How to sample data at set intervals and then plot?

22 次查看(过去 30 天)
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