%lab2
%#2

clear;
b=[2, 1, 0];
a=[1, -1.8*cos(pi/16), .81];
n=[0:60]';
x=ones(size(n));
y=filter(b,a,x);
yss=y(end)
ytr=y-yss;

energy=0;
for i=1:length(n)
   energy=energy+(ytr(i))^2;
end
energy

for g=1:length(n)
   if (abs(ytr(g))>.01*abs(yss))
       N=g
   end
end

Nss=N+1

yimp1=diff(y);
length_yimp1=length(yimp1)
length_y=length(y)
n1=n(2:61);
yimp2=impz(b,a,n);
yimp2=yimp2(2:end);

subplot(3,2,1)
plot(n,x);
xlabel('n');
ylabel('Amplitude');
title('Lab 2, #2, x(n)=u(n)')
grid on;

subplot(3,2,2)
plot(n,y);
xlabel('n');
ylabel('Amplitude');
title('Lab 2, #2: Output signal, y')
grid on;

subplot(3,2,3)
plot(n,yss,'x');
xlabel('n');
ylabel('Amplitude');
title('Lab 2, #2: Steady state response, yss')
grid on;

subplot(3,2,4)
plot(n,ytr);
xlabel('n');
ylabel('Amplitude');
title('Lab 2, #2: Transient response, ytr')
grid on;

subplot(3,2,5)
plot(n1,yimp1);
xlabel('n1');
ylabel('Amplitude');
title('Lab 2, #2: Impulse Resp. using diff, yimp1')
grid on;

subplot(3,2,6)
plot(n1,yimp2);
xlabel('n1');
ylabel('Amplitude');
title('Lab 2, #2: Impulse Resp. using impz, yimp2')
grid on;