%ECE 480 Lab #2

%Problem #1 b.
a = [1, 0, .9];
b = [1.2, 2.4, 1.2];
n = [0:60]';
x = zeros(size(n)); %array of zeros
x(1) = 1; %creating an impulse at n = 0
y = filter (b,a,x);
subplot(2,1,1)
stem(n,x) ,xlabel('Time in Seconds'), ylabel('Amplitude'), title('Input');
subplot(2,1,2)
stem(n,y) ,xlabel('Time in Seconds'), ylabel('Amplitude'), title('Output');
figure;
subplot(2,1,1)
plot(n,x) ,xlabel('Time in Seconds'), ylabel('Amplitude'), title('Input');
subplot(2,1,2)
plot(n,y) ,xlabel('Time in Seconds'), ylabel('Amplitude'), title('Output');
figure;

%Problem #1 c.
x1 = ones(size(n)); %unit step
y1 = filter (b,a,x1);
subplot(2,1,1)
stem(n,x1) ,xlabel('Time in Seconds'), ylabel('Amplitude'), title('Input');
subplot(2,1,2)
stem(n,y1) ,xlabel('Time in Seconds'), ylabel('Amplitude'), title('Output');
figure;

%Problem #2
a2 = [1, -1.8*cos(pi/16), .81];
b2 = [2, 1, 0];
n2 = [0:200]';
x2 = ones(size(n2)); %unit step
y2 = filter(b2, a2, x2);
subplot(2,1,1);
stem(n2,x2) ,xlabel('Time in Seconds'), ylabel('Amplitude'), title('Input');
subplot(2,1,2);
stem(n2,y2) ,xlabel('Time in Seconds'), ylabel('Amplitude'), title('Output');
figure;

%Problem #2 a.
yss = y2(201);
ytr = y2 - yss;
yss1 = zeros(size(n2));
ind1 = find(n2 >= 0);
yss1(ind1) = yss;
subplot(2,1,1);
v=[0 60 -1 1];
axis (v);
stem(n2,yss1) ,xlabel('Time in Seconds'), ylabel('Amplitude'), title('Steady State Response');
subplot(2,1,2);
w=[0 60 -1 2];
axis (w);
stem(n2,ytr);,xlabel('Time in Seconds'), ylabel('Amplitude'), title('Transient Response');
figure;
ytr2 = (ytr.^2);
sum(ytr2)

%Problem #2 d.
yimp = diff(y2);
length(yimp);
length(y2);
n3 = n2(2:200);
yimp2 = impz(b2, a2, n2);
yimp3 = yimp2(2:200);
stem(n3,yimp3) ,xlabel('Time in Seconds'), ylabel('Amplitude'), title('Impulse Response');
figure;

%Problem #3
a4 = [1, -1.8*cos(pi/16), .81];
b4 = [2, 1, 0];
n4 = [0:200]';
x4 = zeros(size(n4));
ind4 = find(n4 >= 0);
x4(ind4) = 1.5*cos(.125*pi*n4 - pi/4);
y4 = filter(b4, a4, x4);
subplot(2,1,1);
stem(n4,x4) ,xlabel('Time in Seconds'), ylabel('Amplitude'), title('Input');
subplot(2,1,2);
stem(n4,y4) ,xlabel('Time in Seconds'), ylabel('Amplitude'), title('Output');
figure;

yss4 = y4(201);
ytr4 = y4 - yss4;
yss5 = zeros(size(n2));
ind5 = find(n4 >= 0);
yss5(ind5) = yss4;
subplot(2,1,1);
stem(n4,yss5) ,xlabel('Time in Seconds'), ylabel('Amplitude'), title('Steady State Response');
subplot(2,1,2);
stem(n4,ytr4);,xlabel('Time in Seconds'), ylabel('Amplitude'), title('Transient Response');
