% file e310dem0.m
% This program implements the filters of Assignment 1

% =======================================
% MATLAB HELP
% to learn more about Matlab, type 'intro'
% (without quotes) at the command prompt
% also type 'demo' for more complete intro
% for help on any function, type 'help functionname'
% for help on syntax:
% type 'help punct' 'help ops'  'help arith'
% for web-based help, type 'helpdesk'
% keyword search, type 'lookfor keyword'

% use round brackets for subscripts b(1)
% vector index must be 1 or greater
% use square brackets for vectors [b(1) b(2) b(3)]

% to view the contents of a variable, type its name without ;
% to see a list of all variables, type 'whos'

% =======================================
% set universal parameters
% =======================================
fs=1; % sampling rate, sample times are k*Ts=k/fs
% since the sampling rate is not specified in the
% assignment 1, we assume fs=1

% consider system with input x(k), filter h(k), output y(k)

% ============
% question 1
% ============
% define input vector x
x=[1 1 0 0 0 0]; % x(1)=1, x(2)=1, could add more zeros
% cannot use index 0 in matlab, so shift index k up by 1

% define filter coefficient vector h
h=[0.5 1 0.5];
b=h;
% find output vector y using
% y(n) = b(0)*x(n) + b(1)*x(n-1) + b(2)*x(n-2)
% but index k for the vector b must be shifted up by 1, so write
% y(n) = b(1)*x(n) + b(2)*x(n-1) + b(3)*x(n-2)
y=filter(b,1,x);
% type 'help filter' to see the
% equations for the filter function
% impulse response h is same as b in this case
%z=conv(h,x);     % type 'help conv', same as filter in this case
% print out values using variable names without ; at end of line
x
y

% ============
% question 2
% ============
% define filter coefficient vector b
h2=[1 -1];
b2=h2;
% find output vector y using
% y(k) = b(1)*x(k) + b(2)*x(k-1)
y2=filter(b2,1,x);
% impulse response h2 is same as b2 in this case 
z2=conv(h2,x);
x
y2
z2

% ===========
% question 3
% ===========
% define filter coefficient vectors a and b
a=[1 -0.5]; % revised sign
b3=[0 1];
% find output vector y using
% y(k)=0.5*y(k-1) + x(k-1) 
y3=filter(b3,a,x); % type 'help filter' to see equations
x
y3
% to obtain the impulse response, change 
% the input x to [1 0 0 0 0 0]
% then the output y3 will be the impulse response h3
% here the impulse response h3 is not equal to b3 (due to feedback)
