clear all;clc
% PATH_ROOT = 'C:\Users\Jingyuan Hong\';
PATH_ROOT = 'C:\Users\jh22\';
load([PATH_ROOT,'\Downloads\pwdb_data_w_aorta_finger_path'])
%%
load([PATH_ROOT,'OneDrive - King''s College London\PhD Work\code\PW_studies\pwdb_1\PWs_mat\mat\PWs_Digital.mat'])

%% Aortic Backward wave amplitude and reflection magnitude
wave_a.Pf_A = zeros(length(data.haemods),1);
wave_a.Pb_A = zeros(length(data.haemods),1);
wave_a.RM = zeros(length(data.haemods),1);
for sim_no = 1
    
    wave_a.p = data.path_waves.aorta_finger(sim_no).P{1, 1};  % seg. 1-aorta
    wave_a.v = data.path_waves.aorta_finger(sim_no).U{1, 1}; % seg. 1-aorta
   
    wave_a.rho = data.config.constants.rho(sim_no);
    wave_a.c = sqrt(sum(diff(133.322*wave_a.p).^2)./sum(diff(wave_a.v).^2))/wave_a.rho;
    
    wave_a.dP_f = (diff(133.322*wave_a.p) + (wave_a.rho*wave_a.c*diff(wave_a.v)))/2;
    wave_a.dP_b = (diff(133.322*wave_a.p) - (wave_a.rho*wave_a.c*diff(wave_a.v)))/2;
    wave_a.P_f = cumsum(wave_a.dP_f)/133.322;%+0.5*wave_a.p(1);
    wave_a.P_b = cumsum(wave_a.dP_b)/133.322;%+0.5*wave_a.p(1);
    wave_a.Pf_A(sim_no) = max(wave_a.P_f);
    wave_a.Pb_A(sim_no) = max(wave_a.P_b);
    wave_a.RM(sim_no) = wave_a.Pb_A(sim_no)/wave_a.Pf_A(sim_no);
    
end

%%
PWs_indfinger = PWs;

for sim_no = 1%: length(PWs_indfinger.PPG)
    sig.v = PWs_indfinger.PPG{1, sim_no};
    sig.fs = 500;
    options.do_plot = 0; sig.ht = 1.75;
    [cv_inds, fid_pts, ~, ~] = PulseAnalyse11(sig, options);

end
%%
paper_size = [300, 400];
figure('Position', [20,20,paper_size])
fig_settings.lwidth = 1;
fig_settings.ftsize = 10;
% tiledlayout(3,2,'TileSpacing','Compact');
% subplot(1,2,1)
% nexttile(1,[3 1]);
plot([1:length(wave_a.p)]/500,wave_a.p,'Color',0*[1 1 1],'LineWidth', fig_settings.lwidth)
hold on
plot([1:length(wave_a.P_f)]/500,wave_a.P_f,'--','Color',0*[1 1 1],'LineWidth', fig_settings.lwidth+0.2)
hold on
plot([1:length(wave_a.P_b)]/500,wave_a.P_b,'-.','Color',0*[1 1 1],'LineWidth', fig_settings.lwidth+0.2)
hold on
plot((find(wave_a.p == struct2table(data.haemods).P1in_a(1)))/500,struct2table(data.haemods).P1in_a(1)*1.01,'rv','MarkerSize',3);%,'MarkerSize',8,'Linewidth',1)
hold on
plot((find(wave_a.p == struct2table(data.haemods).P2pk_a(1)))/500,struct2table(data.haemods).P2pk_a(1)*1.01,'rv','MarkerSize',3);%,'MarkerSize',8,'Linewidth',1)
hold on
plot((find(wave_a.P_b == wave_a.Pb_A(1)))/500,wave_a.Pb_A(1)*1.05,'rv','MarkerSize',3);%,'MarkerSize',8,'Linewidth',1)
hold on
plot((find(wave_a.P_f == wave_a.Pf_A(1)))/500,wave_a.Pf_A(1)*1.05,'rv','MarkerSize',3);%,'MarkerSize',8,'Linewidth',1)
l1 = legend(gca,{'Central','Forward','Backward'},'FontSize',fig_settings.ftsize-2);
l1.Position(2) = 0.72;
l1.Position(1) = 0.30;

box off
xlabel('Time [s]','FontSize', fig_settings.ftsize-1)
ylabel('Pressure [mmHg]','FontSize', fig_settings.ftsize-1)

xlim([0,length(wave_a.p)/500])
ylim([-5,110])
set(gca, 'FontSize', fig_settings.ftsize-1)
breakyaxis([25 70]);
box off
%%
paper_size = [300, 400];
figure('Position', [20,20,paper_size])
fig_settings.lwidth = 1;
fig_settings.ftsize = 10;
tiledlayout(3,1,'TileSpacing','Compact');

PPG = PWs_indfinger.PPG{1, 1};
dPPG = PPG(2:end)-PPG(1:end-1);
sdPPG = dPPG(2:end)-dPPG(1:end-1);
nexttile(1);
plot([1:length(PPG)]/500,PPG,'Color',0*[1 1 1],'LineWidth', fig_settings.lwidth)
hold on
plot(fid_pts.s/500,PPG(fid_pts.s),'ro','MarkerSize',3);
hold on
plot(fid_pts.dia/500,PPG(fid_pts.dia),'ro','MarkerSize',3);
hold on
plot(fid_pts.d/500,PPG(fid_pts.d),'ro','MarkerSize',3);
hold on
plot(fid_pts.b/500,PPG(fid_pts.b),'ro','MarkerSize',3);

xlim([0,length(sdPPG)/500])
ylim([0,1.08])
set(gca, 'FontSize', fig_settings.ftsize-1)
ylabel('PPG [au]','FontSize', fig_settings.ftsize-1)
xticklabels({})
% xlabel('Time [s]','FontSize', fig_settings.ftsize-1)
box off
nexttile(2);
plot([1:length(dPPG)]/500,dPPG,'Color',0*[1 1 1],'LineWidth', fig_settings.lwidth)
xlim([0,length(sdPPG)/500])
ylabel('1st devPPG [au]','FontSize', fig_settings.ftsize-1)
xticklabels({})
box off

nexttile(3);
plot([1:length(sdPPG)]/500,sdPPG,'Color',0*[1 1 1],'LineWidth', fig_settings.lwidth)
hold on
plot((fid_pts.a-1)/500,sdPPG(fid_pts.a-1),'ro','MarkerSize',3);
hold on
plot(fid_pts.b/500,sdPPG(fid_pts.b),'ro','MarkerSize',3);
hold on
plot((fid_pts.c-1)/500,sdPPG(fid_pts.c-1),'ro','MarkerSize',3);
hold on
plot(fid_pts.d/500,sdPPG(fid_pts.d),'ro','MarkerSize',3);
hold on
plot((fid_pts.e-1)/500,sdPPG(fid_pts.e-1),'ro','MarkerSize',3);
hold on
plot([1:length(sdPPG)]/500,zeros(length(sdPPG)),"--",'Color',0.6*[1 1 1],'LineWidth', fig_settings.lwidth);
xlim([0,length(sdPPG)/500])
ylim([min(sdPPG)*1.4,max(sdPPG)*1.2])
ylabel('2nd devPPG [au]','FontSize', fig_settings.ftsize-1)
xlabel('Time [s]','FontSize', fig_settings.ftsize-1)

box off
PATH_SAVE = [PATH_ROOT,'OneDrive - King''s College London\PhD Work\2022\Comprehensive Vascular Ageing Analysis\figure\fig 3\'];
% saveas(gcf, [PATH_SAVE 'indices_plot_new.png'])