clear all
close all
clc

set(0,'defaultLineLineWidth', 2)
set(0,'DefaultAxesFontSize',14)
%% this scipt plots the second pressure calibration
load cal_P4.lvm

t=cal_P4(:,1);
P1=cal_P4(:,5);
P2=cal_P4(:,6);
P3=cal_P4(:,7);

V_P4=cal_P4(:,8);


%calc time the manometer measurements happend

tm_t=[100, 150,220,300,400,500,580,650];%seconds



for i=[P1, P2, P3, V_P4]
    figure
    plot(t,i)
    
    y=[0, max(i)];
    hold on
    for j=1:length(tm_t)
        plot([tm_t(j),tm_t(j)],y, '--r')
    end
    hold off
end
%%finner skjæring med tm_t
for k=1:length(tm_t)
    index(k)=find(t>tm_t(k),1,'first');
end

int_P1=P1(index);
int_P2=P2(index);
int_P3=P3(index);
int_V_P4=V_P4(index);

P_real= int_P1;

%plot avvik

figure
%plot(int_V_P4,int_P4,'-b', int_V_P4, P_real,'-Or')
plot(t,P1)
xlabel('Voltage[V]')
ylabel('Pressure[kPa]')
%axis([0.1 0.8 0 10])
legend('Measured pressure', 'Real pressure','Location','SouthEast')



%finne kurvetilpassing
dp3= polyfit(int_P3, P_real, 1);
p4=polyfit(int_V_P4, P_real, 1);

hold on 
plot(t, V_P4.*p4(1) + p4(2), '--r')
plot(t, P3.*dp3(1) + dp3(2), ':g')
%check
% 
% hold on
%plot([0,1],[p4(2), p4(2)+p4(1)],'-or')
% hold off
%endcheck
%endcheck= 12*60+19 - 739;