SNR vs Amplitude plot for ECG Lead
Show older comments
Hello,
How to plot the figure SNR vs Amplitude for each ECG Lead separately:
%----available amplitudes
amplitudes = {'0.05', '0.10', '0.15', '0.20', '0.25', '0.30', '0.35',...
'0.40', '0.45', '0.50', '1.00', '1.50', '2.00', '2.50', '3.00',...
'3.50', '4.00', '4.50', '5.00', '5.50' }
%heartRates = {'30bpm', '40bpm', '45bpm', '60bpm', '80bpm', '90bpm', '100bpm'}
%heartRates = {'120bpm', '140bpm', '160bpm', '180bpm', '200bpm', '220bpm',...
% '240bpm', '260bpm', '280bpm', '300bpm'}
Fs = 1000;
s = struct;
% Build table
T = table();
SNR = {}
Leads = ["I" "II" "V_"+(1:6)];
ECGLeads = {ecgData.leadI, ecgData.leadII,...
ecgData.V1, ecgData.V2, ecgData.V3,...
ecgData.V4, ecgData.V5, ecgData.V6};
%----get amplitude/heart rate parameter from text file
file = fullFileName
field=textread(file,'%s',7,'delimiter','\n')
%-------for each amplitude parameters
for kleads = 1:length(ECGLeads)
for iamp = 1:length(amplitudes)
if (find(~cellfun(@isempty,strfind(field,amplitudes{iamp}))==true))
% Define parameters
total_samples = size(rawData,1);
sampling_frequency = 1000; % in Hz
duration = 3; % in seconds
% Generate a time vector for the entire signal
t = (0:total_samples-1) / sampling_frequency;
% Generate a e sigsamplnal (for example, a sine wave)
signal = ECGLeads{kleads}; %
% Extract sample of signal
sample_duration = duration; % in seconds
sample_samples = sample_duration * sampling_frequency;
sample_signal = signal(1:sample_samples);
sample_time = t(1:sample_samples);
sample_ecg{kleads} = sample_signal;
%s.(amplitudes{iamp}).(sample_ecg{kleads}) = sample_signal;
%calculate snr
SNR{iamp} = calc_snr(sample_ecg{kleads}, Fs)
end%iamp
end%if
tiledlayout(2, 4, TileIndexing='columnmajor')
for iamp = 1:length(amplitudes)
% snr_Min = min(cell2mat(SNR{:}));
% snr_Max= min(cell2mat(SNR));
% sample_snr;
sample_amplitude = vertcat(amplitudes{:});
nexttile
plot(amplitudes, SNR{iamp});
xlabel('Amplitude (mV)');
ylabel('SNR (dB)');
title(Leads{kleads}, [amplitudes, SNR ]);
% xlim([0 3])
% %ylim([-4.5e-03 50e-04])
% ylim([snr_Min snr_Max])
output_fig_snr = [path_data_fig, Leads{kleads},'.fig']
output_fig_snr_jpg = [path_data_fig, Leads{kleads},'.jpg']
saveas(gcf, output_fig_snr, 'fig')
saveas(gcf, output_fig_snr_jpg, 'jpg')
end %iamp
end %if amp
end %kleads
Answers (3)
Raghava S N
on 20 Nov 2024
To plot the SNR vs Amplitude graph for each lead separately, you may follow the below steps.
- Move this command -
tiledlayout(2, 4, 'TileIndexing', 'columnmajor');
before the for loop -
for kleads = 1:length(ECGLeads)
The “tiledlayout” function only needs to be called once to declare a tiled figure. For more information about the “tiledlayout” function, refer to this link - https://www.mathworks.com/help/matlab/ref/tiledlayout.html#:~:text=polarscatter(theta%2Crho)-,Reconfigure%20Content%20in%20Previous%20Tile,-Open%20in%20MATLAB.
- A for loop is not required to call the "plot" function, as the “plot” function is vectorized and can take vectors as inputs to plot. For more information about “plot”, refer to this link - https://www.mathworks.com/help/matlab/ref/plot.html#mw_6748a9ec-33eb-483b-b8f1-919954dfae40:~:text=x%2Dcoordinates-,scalar%20%7C%20vector%20%7C%20matrix,-Y%20%E2%80%94%20y. For vectorization in MATLAB, refer to this link - https://www.mathworks.com/help/matlab/matlab_prog/vectorization.html.
- Convert the amplitude vector to double using the “str2double” function, and the SNR cell array to a vector using the “cell2mat” function. The documentation for “str2double” can be found in this link - https://www.mathworks.com/help/matlab/ref/str2double.html. Refer to this link for more information about the “cell2mat” function - https://www.mathworks.com/help/matlab/ref/cell2mat.html. This is because, the “plot” function takes scalar, vector or matrices as the data to be plotted. Use a modified plot command instead in the for loop -
for kleads = 1:length(ECGLeads)
after SNR is calculated for all amplitudes-
plot(cellfun(@str2double, amplitudes), cell2mat(SNR))
Hope this helps!
Peter Perkins
on 21 Nov 2024
Edited: Peter Perkins
on 21 Nov 2024
0 votes
Elzbieta, I have not read your code closely but it seems very likely that stackedplot on a table or timetable will do what you want.
William Rose
on 25 Nov 2024
0 votes
You have 21 amplitudes, 17 heart rates, and 8 leads (I, II, and V1 thorugh V6). That is a lot of combinations.
I can't run your code since I do;t have the data files, and I dont have some of the functions called, such as calc_snr().
Can you describe, or better yet, attach a photo of a hand-drawn image, or a copy of someone else's image, that shows the kind of figure you want to create?
Categories
Find more on Measurements and Feature Extraction in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!