%% Matlab demo - filter
%
% In class Matlab demo
% EE 3054 September 22, 2014


%%

help filter

%% Difference equation
% y(n) = x(n) - (1/2) y(n-1) - (1/4) y(n-2)

b = 1;
a = [1 1/2 1/4]
roots(a)

% We found the poles were 0.5 * exp(+/- 2*pi/3 * i)

0.5 * exp(2*pi/3 * i)   % agrees with 'roots'

%% Pole/zero diagram

zplane(b, a)

%% Run difference equation

% Make input signal
x = zeros(1, 21);
x(1:5) = [5 2 3 1 9];

n = 0:20;

% plot input signal x
subplot(2, 1, 1)
stem(n, x)
title('INPUT SIGNAL')

% compute output signal y
y = filter(b, a, x);

% plot output signal y
subplot(2, 1, 2)
stem(n, y)
title('OUTPUT SIGNAL')

%% Find impulse response

imp = [1 zeros(1, 20)];
h = filter(b, a, imp);

figure(2)
subplot(2, 1, 1)
stem(n, imp)
title('IMPULSE')
subplot(2, 1, 2)
stem(n, h)
title('IMPULSE RESPONSE')

% adjust plot axes
for k = 1:2
    subplot(2, 1, k)
    xlim([-2 20])
    ylim([-1 2])
end

%% Verify formula

% Our formula for the impulse response:
h2 = 2/sqrt(3) * (1/2).^n .* cos(2*pi/3 * n + pi/6);

[h' h2' (h-h2)']
% impulse response formula is same as impulse response numerically computed using 'filter' function








