0
$\begingroup$

This is a code I made for cleaning and visualizing raw ECG data from physioNet. I want to make some enhancements to the output and can't figure out how so I am asking if someone could help or give me a hint as to where to search. In the output, we have 2 figures each with 4 subplots.

enter image description here

I want to make the output appear similar to this:

enter image description here

The things I want to make:

  1. I want to enable the grid but with specifications similar to an ECG paper: Each large square consists of 5 small squares, Each small square is 1 mm in length and represents 0.04 seconds. Each larger square is 5 mm in length and represents 0.2 seconds.

  2. I want each signal to appear in a row without intersecting and without having to disable the grid. And to stay in one subplot

  3. and finally, I want to disable the labels of the drawing scale of the horizontal and vertical axes.

an example of the data can be found here instead of downloading the whole data

%import data
data=("D:\Matlab\a-large-scale-12-lead-electrocardiogram-database-for-arrhythmia-study-1.0.0\a-large-scale-12-lead-electrocardiogram-database-for-arrhythmia-study-1.0.0\WFDBRecords\01\010\JS00001.mat");
x=load('JS00012.mat');
y = x.val/2000;
y = y';
fs=500;
t = (0:length(y)-1)/fs; % Time vector
% Assuming you have 12 signals stored in the variable 'y'

% Create separate variables for each signal
signal1 = y(:, 1);
signal2 = y(:, 2);
signal3 = y(:, 3);
signal4 = y(:, 4);
signal5 = y(:, 5);
signal6 = y(:, 6);
signal7 = y(:, 7);
signal8 = y(:, 8);
signal9 = y(:, 9);
signal10 = y(:, 10);
signal11 = y(:, 11);
signal12 = y(:, 12);

% Combine the signals
combined_signal1 = signal1 + signal4 + signal7 + signal10;
combined_signal2 = signal2 + signal5 + signal8 + signal11;
combined_signal3 = signal3 + signal6 + signal9 + signal12;

% Plot the combined signals
figure;
subplot(411)
plot(t(1:5*fs), combined_signal1(1:5*fs));
subplot(412)
plot(t(1:5*fs), combined_signal2(1:5*fs));
subplot(413);
plot(t(1:5*fs), combined_signal3(1:5*fs));
subplot(414);
plot(t(1:5*fs), y(1:5*fs,2));

%Filter the signal data
fcutlow = 0.5; % Lower cutoff frequency in Hz
fcuthigh = 40; % Upper cutoff frequency in Hz
[b,a] = butter(4, [fcutlow/(fs/2) fcuthigh/(fs/2)], 'bandpass');
filtered_signal = zeros(size(y));
for i = 1:size(y, 2)
    filtered_signal(:,i) = filtfilt(b, a, y(:,i));
end

% Amplify the filtered signal data
amplified_signal = 2 * filtered_signal;

% Apply baseline correction to each lead of the filtered signal data
baseline_corrected_signal = zeros(size(amplified_signal));
for i = 1:size(amplified_signal, 2)
    baseline_corrected_signal(:,i) = amplified_signal(:,i) - medfilt1(amplified_signal(:,i), fs/4);
end

% Apply wavelet denoising to each lead of the amplified signal data
denoised_signal = zeros(size(baseline_corrected_signal));
for i = 1:size(baseline_corrected_signal, 2)
    denoised_signal(:,i) = wden(baseline_corrected_signal(:,i),'minimaxi','s','mln',8,'sym4');
end

% Create separate variables for each signal
signal_clean1 = denoised_signal(:, 1);
signal_clean2 = denoised_signal(:, 2);
signal_clean3 = denoised_signal(:, 3);
signal_clean4 = denoised_signal(:, 4);
signal_clean5 = denoised_signal(:, 5);
signal_clean6 = denoised_signal(:, 6);
signal_clean7 = denoised_signal(:, 7);
signal_clean8 = denoised_signal(:, 8);
signal_clean9 = denoised_signal(:, 9);
signal_clean10 = denoised_signal(:, 10);
signal_clean11 = denoised_signal(:, 11);
signal_clean12 = denoised_signal(:, 12);

% Combine the cleaned signals
combined_signal_clean1 = signal_clean1 + signal_clean4 + signal_clean7 + signal_clean10;
combined_signal_clean2 = signal_clean2 + signal_clean5 + signal_clean8 + signal_clean11;
combined_signal_clean3 = signal_clean3 + signal_clean6 + signal_clean9 + signal_clean12;


%Plot the cleaning signals
figure;
subplot(411)
plot(t(1:5*fs), combined_signal_clean1(1:5*fs));
title('Denoised ECG signal');
subplot(412)
plot(t(1:5*fs), combined_signal_clean2(1:5*fs));
subplot(413)
plot(t(1:5*fs), combined_signal_clean3(1:5*fs));
subplot(414)
plot(t(1:5*fs), denoised_signal(1:5*fs,2));
$\endgroup$
4
  • $\begingroup$ Could you upload the specific .mat file to some filesharing service and share it here. I don't want to download the entire 5 GB dataset $\endgroup$ Commented Jul 16, 2023 at 8:15
  • 1
    $\begingroup$ To my opinion, this is a programming question. It has nothing to do with processing the data, but it's about plotting them. I believe this question is best fitted for the StackOverflow site (stackoverflow.com). $\endgroup$
    – ZaellixA
    Commented Jul 16, 2023 at 10:05
  • $\begingroup$ @AhsanYousaf I am sorry for that, I added a link in the post for an example of the data (.mat file and header file). Thank you $\endgroup$
    – Kafka
    Commented Jul 16, 2023 at 10:57
  • 1
    $\begingroup$ @ZaellixA Sorry for that. I think you are right. I will ask there. Thank you $\endgroup$
    – Kafka
    Commented Jul 16, 2023 at 10:58

1 Answer 1

2
$\begingroup$

First of all, Matlab does not support a grid with different square sizes. Secondly, (afaik) you cannot overflow a grid from one subplot to another. And third, if you turn the axis off you cannot use "grid on" since there are no values to make the grid.

A workaround can be to plot all 4 signals on one plot and then place them on top of each other by adding vertical shifts. Then you can make your custom grid as shown below:

ECG

However, the grid specifications do not exactly match the specifications given but you can change them as you wish using this code:

lh.Color=[0.5 0.5 0.5];

% Plot the combined signals
figure;
set(gcf, 'color', 'w')

% Add vertical grid lines for each small square (0.04 seconds)
for i = 1:200
    xline(i * 0.04, '-', 'LineWidth', 0.2, 'Color', [0.5 0.5 0.5]);
end

% Add horizontal grid lines for each small square
for i = -20:150
    yline(i * 0.2, '-', 'LineWidth', 0.2, 'Color', [0.5 0.5 0.5]);
end

% Add horizontal grid lines for each large square
for i = -2:2:20
    yline(i * 1, '-', 'LineWidth', 2, 'Color', lh.Color);
end

for i = -1:2:19
    yline(i * 1, '-', 'LineWidth', 0.8, 'Color', [0.5 0.5 0.5]);
end


% Add vertical grid lines for each large square (0.2 seconds)
for i = 1:2:25
    xline(i * 0.2, '-', 'LineWidth', 2, 'Color', lh.Color);
end

% Add vertical grid lines for each large square (0.2 seconds)
for i = 2:2:23
    xline(i * 0.2, '-', 'LineWidth', 0.8, 'Color', [0.5 0.5 0.5]);
end

hold on;
plot(t(1:5*fs), combined_signal1(1:5*fs), 'k','LineWidth', 1.5);

plot(t(1:5*fs), combined_signal2(1:5*fs)+ 4, 'k','LineWidth', 1.5);

plot(t(1:5*fs), combined_signal3(1:5*fs) + 8, 'k','LineWidth', 1.5);

plot(t(1:5*fs), y(1:5*fs,2)+ 12,'k','LineWidth', 1.5);

axis off;
ylim ([-2 ,14]) ;
xlim ([0 ,5]) ;
plot_xlim = xlim;
plot_ylim = ylim;
rectangle('Position', [plot_xlim(1), plot_ylim(1), plot_xlim(2)-plot_xlim(1), plot_ylim(2)-plot_ylim(1)], 'EdgeColor', 'r', 'LineWidth', 2.5);

hold off;
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.