clc
clear all

load Data_gs
t = Data_gs(:,1);
r = Data_gs(:,2);
p_buff = Data_gs(:,3);
z = Data_gs(:,4);
%%
%Low pass filter

n = length(z);
z_f = zeros(1,n);
alpha = 0.25; 

for k=1:n
    if (k==1)
        z_f(k) = z(k);
    elseif (k==2)
        z_f(:,k) = alpha*z(k) + (1-alpha)*z_f(:,k-1);
    else
        z_f(:,k) = alpha^2*z(k) + 2*(1-alpha)*z_f(:,k-1) - (1-alpha)^2*z_f(:,k-2);
    end
    z_rev(1,n-k+1) = z_f(:,k);
end


for k=1:n
    if (k==1)
        zf(k) = z_rev(k);
    elseif (k==2)
        zf(:,k) = alpha*z_rev(k) + (1-alpha)*zf(:,k-1);
    else
        zf(:,k) = alpha^2*z_rev(k) + 2*(1-alpha)*zf(:,k-1) - (1-alpha)^2*zf(:,k-2);
    end
    zff(1,n-k+1) = zf(:,k);
end
%end of low pass filter
%%

figure(1)
clf

subplot(2,1,1);
plot (t,r,'r','Linewidth',3);
hold on
grid on

plot(t,p_buff,'k','Linewidth',2);
legend ('Setpoint','Measurement',2);
xlim ([0 1800]);
ylabel ('Inlet Pressure[Kpa]');
title ('Multiple controllers with gain scheduling');



subplot(2,1,2);
plot(t,zff,'k', 'Linewidth',2);
xlim ([0 1800]);
xlabel ('time[sec]')
grid on
ylabel ('Z [%]');


