%########################################################################################

%EE 416 MATLAB PROJECT 1
%Hok-Lim Li ( 29045906)
%10/18/2001

%########################################################################################


%Transfer function of a Causal Filter is given by:

%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)
      
      
%***************************************************************************************      


%Poles and Zeros of H(z) 
%----------------------------------------------------------------------------------------

B=[0.025 -0.07 0.05 -0.02];						 %define the polynomial expression
A=[1 1.5 1.75 1.02 0.4];							 %define the polynomial expression

z=roots(B)   										    %Obtain zeros from roots 
p=roots(A)		      								 %Obtain poles from roots	

display('The number of zeros are: ');         %Display the sentence with the number of 
length(z)                                     %zeros

display('The number of poles are: ');         %Display the sentence with the number of 
length(p)                                     %poles



%Pole/Zero Patterns of the Filter
%----------------------------------------------------------------------------------------

figure(1)												
zplane(B,A);											%Plot the Zeros and Poles of H(z)



%Plot the Frequency Response of the filter
%----------------------------------------------------------------------------------------

figure(2)
[H,W]=freqz(B,A,'whole') 							%define the frequency response
freqzplot(H,W); 										%plot graph
title('Frequency response of the Filter');

%********Poles are all within the unit circle;but not all zeros**************************


%Plot of the Finite Samples of h(n)
%----------------------------------------------------------------------------------------

figure(3)													
w=1:100													%Definite the amount of samples
[h,t]=impz(B,A,w)										%Find the Frequency Response of h(n)
plot(t,h); 												%Plot graph for h(n)
xlabel('t');											%labeling x-axis
ylabel('h')	;									 		%labeling y-axis
title('Plot of Finite Samples of h(n)');     %name the title





%Comment on the Stability of the filter based on Pole-Zero analysis

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

%		In figure one, we can see the ROC does contain inside the unit circle. 
%Any poles are within the ROC and inside the unit circle would be considered to be stable (The 
%Fourier transform of x[n] converges absolutely if and only if the ROC of the z-transform of x[n]
%includes the unit circle -- Property 2).So thehre is a ROC that wuld imply that the system is
%both stable and causal.


%		In figure 3, as well as the signal of h(n) is approaching to zero, that means the filter 
% is stable. Otherwise, you would see the signal is bouncing up and down vigorously.