% Changing the path from main_folder to a particular chapter
main_path=fileparts(which('Main_Content.mlx'));
%addpath(append(main_path,'/Chapter2'))
cd (append(main_path,'Chapter5/Blood_pressure_signals'))
addpath(append(main_path,'/Service'))
SAVE_FLAG=0; % saving the figures in a file
load('BP_data_250Hz.mat')
[m_sbp,i_sbp]=findpeaks(data(1:L,2),'MinPeakProminence',6,'MinPeakDistance',fs/2 );
[m_dbp_dic,i_dbp_dic]=findpeaks(-data(1:L,2),'MinPeakProminence',0.1,'MinPeakDistance',fs/5 );
hold on, area(t(i_dbp_dic(1):i_dbp_dic(2)),data(i_dbp_dic(1):i_dbp_dic(2),2),'FaceColor','y', "EdgeColor","none")
area(t(i_dbp_dic(3):i_dbp_dic(4)),data(i_dbp_dic(3):i_dbp_dic(4),2),'FaceColor','y',"EdgeColor","none")
area(t(i_dbp_dic(5):i_dbp_dic(6)),data(i_dbp_dic(5):i_dbp_dic(6),2),'FaceColor','y',"EdgeColor","none")
area(t(i_dbp_dic(2):i_dbp_dic(3)),data(i_dbp_dic(2):i_dbp_dic(3),2),'FaceColor','g',"EdgeColor","none")
area(t(i_dbp_dic(4):i_dbp_dic(5)),data(i_dbp_dic(4):i_dbp_dic(5),2),'FaceColor','g',"EdgeColor","none")
area(t(i_dbp_dic(6):L),data(i_dbp_dic(6):L,2),'FaceColor','g',"EdgeColor","none")
%plot(t,70*ones(1,length(t)),'k')
ylabel('Arterial blood pressure (mmHg)')
t=1/fs:1/fs:length(data)/fs;
[m,i]=findpeaks(data(:,2),'MinPeakProminence',6,'MinPeakDistance',fs/2 );
hold on, plot(t(i),m,'o')
[m,i]=findpeaks(-data(:,2),'MinPeakProminence',6,'MinPeakDistance',fs/2 );
hold on, plot(t(i),-m,'*')
ylabel('Arterial blood pressure (mmHg)')
title('Arterial blood pressure with labeled SBP and DBP')
annonation_save('',"Fig5.3.jpg", SAVE_FLAG);
time_int=[10 16; 32 38; 42 48]; % intervals for systolic, MAP and diastolic BP
t=1/fs:1/fs:length(data)/fs;
plot(t(1:60*fs),data(1:60*fs,3))
plot(t(time_int(1,1)*fs:time_int(1,2)*fs),136*ones(1,6*fs+1),'r')
plot(t(time_int(2,1)*fs:time_int(2,2)*fs),110*ones(1,6*fs+1),'k')
plot(t(time_int(3,1)*fs:time_int(3,2)*fs),90*ones(1,6*fs+1),'m')
legend('Cuff pressure', 'Systolic region', 'MAP region','Diastolic region')
ylabel('Pressure (mmHg)')
plot(t(time_int(1,1)*fs:time_int(1,2)*fs),data(time_int(1,1)*fs:time_int(1,2)*fs,3),'r')
ylabel('Pressure (mmHg)')
plot(t(time_int(2,1)*fs:time_int(2,2)*fs),data(time_int(2,1)*fs:time_int(2,2)*fs,3),'k')
ylabel('Pressure (mmHg)')
plot(t(time_int(3,1)*fs:time_int(3,2)*fs),data(time_int(3,1)*fs:time_int(3,2)*fs,3),'m')
ylabel('Pressure (mmHg)')
title('Diastolic region')
exportgraphics(gcf,"Fig5.4.jpg", 'Resolution',600)
% %plot(t,P) % and another way is manual integration
% xlabel("Time (s)"), ylabel("Pressure (mmHg)"), title('Arterial pressure')
% xlabel("Time (s)"), ylabel("Pressure (mmHg)")
% title('Total pressure')
% %plot(t(peak_ind), omwe)
% xlabel("Time (s)"), ylabel("Pressure (mmHg)")
% title('Oscillometric waveform')
% exportgraphics(gcf,"Fig5.17.jpg", 'Resolution',600)