%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%                                                                                                            %%%
%%%  Description: Script to plot 12 lead ECG from the simulated data obtained at our torso model surface       %%%
%%%                                                                                                            %%% 
%%%  Author: Cristóbal Rubén Ruiz Gálvez                                                         Version: 1.0  %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear all
%clc
close all

%% Input data

path = pwd;
pathdata=[path,'\data\'];
file_name = 'SimECGdata1_healthy';     % Specify the file name containing the simulated data
input_data = load([pathdata,file_name]); 

V = input_data.V;
time = input_data.time;


%% Leads computation

[x,y] = size(V);

LA = V(:,y-2);
RA = V(:,y-1);
LF = V(:,y);

I = LA-RA;
II = LF-RA;
III = LF-LA;

aVR = RA-(LA+LF)/2;
aVL = LA-(RA+LF)/2;
aVF = LF-(RA+LA)/2;
                                        
Wct = (LA+RA+LF)/3;  % Wilson Central Terminal computation
                                             
V1_6=zeros(x,6);
for i=1:6 
    V1_6(:,i) = V(:,i)-Wct;
end

V1_12 = [I, II, III, aVR, aVL, aVF, V1_6];

% Save leads
save(strcat(pathdata,'V1_12.mat'),'V1_12','time');        % save all leads


%% Plot 12 leads

signal = V1_12';
max_t=250;      % Specify time to plot the leads (ms)
case_name = "healthy";

insig = NaN*ones(12,1000);
insig(1:12,1:max_t)=signal(1:12, 1:max_t);
                     
figure;
set(gcf, 'Position',  [400,200,1340,1010])

axes('XLim', [-400 4400], 'YLim', [-1 10], 'XTickMode', 'manual', ...
    'YTickMode', 'manual', 'DataAspectRatio', [1000/25 1/10 1], ...
    'XColor', 'w', 'YColor', 'w')
axis tight

hold on

% Display a grid

YL = min(floor((min(insig(9, :))+1.5)/0.5)*0.5,0);          %lower limit

% Minor grid
for i = linspace(0, 4000, 20*5+1), line([i, i], [YL 9], 'Color', [1 0.7 0.7], 'LineWidth', 0.25); end
for i = linspace(YL, 9, (9-YL)*2*5+1), line([0 4000], [i, i], 'Color', [1 0.7 0.7], 'LineWidth', 0.25); end
   
% Major grid
for i = linspace(0, 4000, 20+1), line([i, i], [YL 9], 'Color', [1 0.5 0.5], 'LineWidth', 0.8); end
for i = linspace(YL, 9, (9-YL)*2+1), line([0 4000], [i, i], 'Color', [1 0.5 0.5], 'LineWidth', 0.8); end

% Plot the different leads

for i = 1:3, plot(linspace(1, 1000, 1000), insig(i, :) + 1.5 + (3-i) * 3, 'r','LineWidth', 1); end
for i = 1:3, plot(linspace(1001, 2000, 1000), insig(i+3, :) + 1.5 + (3-i) * 3, 'r','LineWidth', 1); end
for i = 1:3, plot(linspace(2001, 3000, 1000), insig(i+6, :) + 1.5 + (3-i) * 3, 'r','LineWidth', 1); end
for i = 1:3, plot(linspace(3001, 4000, 1000), insig(i+9, :) + 1.5 + (3-i) * 3, 'r','LineWidth', 1); end

% Add lead delimiters
for i = 1:3, line([0 0], 3*(i-1) + [0.5 2.5], 'Color', 'k', 'LineWidth', 1.5); end
for i = 1:3, line([1000 1000], 3*(i-1) + [0.5 2.5], 'Color', 'k', 'LineWidth', 1.5); end
for i = 1:3, line([2000 2000], 3*(i-1) + [0.5 2.5], 'Color', 'k', 'LineWidth', 1.5); end
for i = 1:3, line([3000 3000], 3*(i-1) + [0.5 2.5], 'Color', 'k', 'LineWidth', 1.5); end

%for i = 1:3, line([0 0], 3*(i-1) + [1.0 1.0], 'Color', 'k', 'LineWidth', 1); end
for i = 1:3, line([950 1000], 3*(i-1) + [1.5 1.5], 'Color', 'k', 'LineWidth', 1.5); end
for i = 1:3, line([1950 2000], 3*(i-1) + [1.5 1.5], 'Color', 'k', 'LineWidth', 1.5); end
for i = 1:3, line([2950 3000], 3*(i-1) + [1.5 1.5], 'Color', 'k', 'LineWidth', 1.5); end

% Add lead labels
text(200, 8, 'I','Fontsize',15);   text(1200, 8, 'aVR','Fontsize',15); text(2200, 8, 'V1','Fontsize',15); text(3200, 8, 'V4','Fontsize',15);    
text(200, 5, 'II','Fontsize',15);  text(1200, 5, 'aVL','Fontsize',15); text(2200, 5, 'V2','Fontsize',15); text(3200, 5, 'V5','Fontsize',15);    
text(200, 2, 'III','Fontsize',15); text(1200, 2, 'aVF','Fontsize',15); text(2200, 2, 'V3','Fontsize',15); text(3200, 2, 'V6','Fontsize',15);    

% Add specifications
text(200, -0.65, '25mm/s - 10mm/mV', 'FontSize', 12)
line([3600 3800], [-0.5 -0.5], 'Color', 'k', 'LineWidth', 1.5);
line([3600 3600], [0 -0.5], 'Color', 'k', 'LineWidth', 1.5);
text(3350, -0.3, '0.5mV', 'FontSize', 12)
text(3610, -0.65, '200ms', 'FontSize', 12)
       
hold off

% Save figure
saveas(gcf,'12leads', 'fig')
saveas(gcf,'12leads', 'png')
