%lab2
%#2

clear;
b=[2, 1, 0];
a=[1, -1.8*cos(pi/16), .81];
n=[0:50]';		%' --> a column vector
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

Nss=0;
UpBoundary=1.01*yss
LowBoundary=0.99*yss

for g=1:length(n)
    count=0;
   ans=y(g);
	
	count= count+1
end
%samp=find(n==.01*yss);
%stdstate=y(indn)
yimp1=diff(y);
length_yimp1=length(yimp1)
length_y=length(y)
n1=n(2:51);
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;

Nss=0;		%code to find Nss
stop=0;
ytr
Upper= 0.01*yss
Lower=-0.01*yss
for i=1:length(n)
    if (ytr(i)<=Upper & ytr(i)>=Lower) & stop==0
        Nss=i;
        stop=1;
    elseif (ytr(i)>Upper & stop==1)|(ytr(i)<Lower & stop==1)
        Nss=999;
        stop=0;
    end
end
Nss
