%#1
%%%%%%%%%%%%%%%%%%%%%
%B) Impulse response
A=[1,0.9];
B=[1.2,2.4,1.2];
n=[0:50]';
x=zeros(size(n));
x(1)=1;
y=filter(B,A,x);
subplot(2,1,1),stem(n,x,'r')
xlabel('n'),ylabel('Amplitude')
title('input signal, x(t)')
subplot(2,1,2),stem(n,y,'b')
xlabel('n'),ylabel('Amplitude')
title('output signal, y(t)')

%%%%%%%%%%%%%%%%%%%%%
%C)Step response
x=ones(size(n));
y=filter(B,A,x);
subplot(2,1,1),stem(n,x,'r')
xlabel('n'),ylabel('Amplitude')
title('input signal, x(t)')
subplot(2,1,2),stem(n,y,'b')
xlabel('n'),ylabel('Amplitude')
title('output signal, y(t)')

%#2
%%%%%%%%%%%%%%%%%%%%%
%A) Step response
A=[1,-1.8*cos(pi/16), 0.81];
B=[2,1];
n=[0:100]';
x=ones(size(n));
y=filter(B,A,x);
subplot(2,1,1),stem(n,x,'r')
xlabel('n'),ylabel('Amplitude')
title('input signal, x(t)')
subplot(2,1,2),stem(n,y,'b')
xlabel('n'),ylabel('Amplitude')
title('output signal, y(t)')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
b=[2,1];
a=[1,-1.8*cos(pi/16),0.81];
n=[0:50]'
x=ones(size(n));
y=filter(b,a,x);
subplot(2,1,1), plot(n,x), grid on
xlabel('n'),ylabel('Amplitude')
title('Input signal, x')
subplot(2,1,2),plot(n,y), grid on
xlabel('n'),ylabel('Amplitude')
title('Output signal, y')

yss=y(end);
ytr=y-yss;
subplot(2,1,1), plot(n,yss), grid on
xlabel('n'),ylabel('Amplitude')
title('Steady State Response,yss')
subplot(2,1,2),plot(n,ytr), grid on
xlabel('n'),ylabel('Amplitude')
title('Transient Response,ytr')

yimp1-diff(y);
lenght(yimp1)
lenght(y)
n1=n(2:51);
yimp2=impz(b,a,n);
yimp2-yimp2(2:end);
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
%#3
%%%%%%%%%%%%%%%%%%%%%
%A) Step response
A=[1,-1.8*cos(pi/16), 0.81];
B=[2,1];
n=[0:50]';
u=ones(size(n));
x=(1.5*cos(0.125*pi.*n-pi/4)).*u;
y=filter(B,A,x);
subplot(2,1,1),stem(n,x,'r')
xlabel('n'),ylabel('Amplitude')
title('input signal, x(t)')
subplot(2,1,2),stem(n,y,'b')
xlabel('n'),ylabel('Amplitude')
title('output signal, y(t)')

%%%%%%%%%%%%%%%%%%%%%%
%B) Yss and Ytr response
%Yss=a|H(e^jw)|cos(wn + O +<H(e^jw));
w=pi/8;
wvec=[0:pi/1048:pi]';
H=freqz(B,A,wvec);
x=(1.5*cos(w.*n-pi/4)).*u;
mag=abs(H);
phase=angle(H);
subplot(2,1,1), plot(wvec/pi,mag')
ylabel('amplitude')
title('Magnitude Response')
subplot(2,1,2),plot(wvec/pi,phase)
xlabel('w/pi,norm. freq.'),ylabel('Angle in Rad.')
title('Phase Response')

ind=find(wvec==w)
mag_w=abs(mag(ind))
phase_w=phase(ind)
yss=mag_w*cos(w*n-pi/4+phase_w);
plot(n,yss)
xlabel('n'), ylabel('Amplitude')
title('Steady State Response');

y=filter(B,A,x);
plot(n,y)
xlabel('n'), ylabel('Amplitude')
title('Output Response');

ytr=y-yss;
plot(n,ytr);
xlabel('n'), ylabel('Amplitude')
title('Transient Response');