clc
clear all


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

n = length(z);
z_f = zeros(1,n);
alpha = 0.25; % alpha=1 means no filtering

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',1.5);
hold on
grid on

plot(t,p_buff,'k','Linewidth',1.5);
legend ('Setpoint','Measurement',2);
xlim ([0 1317]);
ylabel ('Inlet Pressure[Kpa]');
title ('IMC based PI Controller');



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


