
%  **************************************************************************

%  								MATLAB    PROJECT    1

%  		This file will analyze the following transfer function H(z)

%  **************************************************************************

%  Given the transfer function of a causal filter :

%   H(z) =     0.025 - 0.07z^(-1) + 0.05z^(-2) -0.02z(-3)
%		 	  -------------------------------------------------
%          1 + 1.5z(-1) + 1.75z^(-2) + 1.02z^(-3) + 0.4z^(-4)



%  ----------------------------------------------
%  ( 1 )  Find the poles and zeros of H(z)
%  ----------------------------------------------

B=[0.025 -0.07 0.05 -0.02];				% Set the numerator coefficient of H(z)
A=[1 1.5 1.75 1.02 0.4];					% Set the denominator coefficient of H(z)

Zeros = roots(B)								% Find the zeros of H(z)
Poles = roots(A)								% Find the poles of H(z)

disp('The number of zeros are :');		% Display the number of zeros 
length(Zeros)

disp('The number of poles are :');	 	% Display the number of poles
length(Poles)

%  ---------------------------------------------
%  ( 2 ) Determine the pole/zero patterns
%  ---------------------------------------------

figure(1);										% Set plot 1
zplane(B,A);									% Plot zeros and poles onto unit circle
title('Plot of zeros and poles onto unit circle');			% Label the title

%  This filter is stable because the poles lies within the unit circle,
%  denoted by symbol 'x', while zeros by symbol 'o'.


%  ---------------------------------------------
%  ( 3 ) Plot frequency response of filter
%  ---------------------------------------------

figure(2);										% Set plot 2
[H,W] = freqz(B,A,'whole');				% Define the frequency response
freqzplot(H,W);								% Plot the frequency response											

%  ---------------------------------------------
%  ( 4 ) Plot finite samples of h(n)
%  ---------------------------------------------

figure(3);										% Set plot 3
impulse=zeros(100,1); 						% Assume I set the input vector 
impulse(1)=1;
impresp = filter(B,A,impulse);			% Find the filtered data using the transfer function coefficients
plot(impresp);									% Plot h(n)
title('Plot of h(n)');						% Define title
xlabel('n');									% Label x-axis
ylabel('h(n)');								% Label y-axis							


%  ---------------------------------------------
%  ( 5 ) Comments on stability of filter
%  ---------------------------------------------

%  A system is stable in the bounded-input, bounded-output (BIBO) sense if and only if 
% every bounded input sequence produces a bounded sequence. The input 'impulse' above
% is bounded if there exists a fixed positive value Bx such that  :
%
%                                   |impulse| <= Bx < infinity
%  Thus, the 'impresp' is also      |impresp| <= By < infinity

%  Based on the plots above, it shows that the poles are within the unit circle, 
% although one of the zeros are outside the unit circle. Thus, this filter will converges
% to zero as samples approaches infinity. This is illustrated in the plot 'impresp'.



% ----------------------------------------------

% NAME    : FOONG CHAN TOONG

% UB NO.  : 2764 - 1508

% CLASS   : EE 416

% PROJECT : MATLAB PROJECT 1

% ----------------------------------------------
