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

%Hok-Lim Li (29045906)
%EE 416 Project 4
%11-13-2001

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

%(1) Explain the different types of FIR filters clearly citing the differences between
% each.

% There are 4 different types of linear FIR Filters we learned in class: 

% (i) Odd symmetry, odd length  :  
%     ~ Frequency response: H(ejw) = e(-jw)*2j*sinw                           %                                          
%     ~ Magnitude: |H| = 2sinw
%     ~ Phase: Theta(w) = -w + pi/2
%     ~ Time delay: tau = 1

% (ii)Odd symmetry, even length : This type has  whose general
%     ~ Frequency response: H(ejw) = e(-jw/2)*2j*sin(w/2)                      
%     ~ Magnitude: |H| = 2sin(w/2)                                   
%     ~ Phase: Theta(w) = -w/2 + pi/2                   
%     ~ Time delay: tau = 0.5                                  
                                          
% (iii)Even symmetry, odd length : This type has  whose general
%		~ Frequency response: H(ejw) = e(-jw)*(1+2cosw)								 
%		~ magnitude: |H| = 1 + 2cosw											 
%		~ Phase: Theta(w) = -w					 
% 		~ Time delay: tau = 1										  

% (vi)Even symmetry, even length : This type has  whose general
%		~ Frequency response: H(ejw) = e(-jw/2)*2cos(w/2)								  
%		~ Magnitude: |H| = 2cos(w/2)											 
%		~ Phase correspoond:	Theta(w) = -w/2						  
%		~ Time delay: tau = .5									  
									 

%(2) given that for a Type 1 FIR filter denoted by h(n) . Implement the filter h(n) using
%Kaiser window and verify the result.

w1p=0.3*pi;														% Set the 1st passband frequency
w1s=0.5*pi;														% Set the 1st stopband frequency
w2p=0.8*pi;														% Set the 2nd passband frequency
w2s=0.6*pi;														% Set the 2nd stoppass frequency

width1=w1s-w1p;												% Set the Width1
width2=w2p-w2s;												% Set Width2
w1c=(w1p+w1s)/2;												% Define 1st Cutoff
w2c=(w2p+w2s)/2;												% Define 2nd cutoff

As=80;															% Set the stopband amplitude

delta_f=min(width1/(2*pi),width2/(2*pi));				% Set Size

M=ceil((As-7.95)./(14.36*delta_f)+1);					% Set points for passband 
beta=0.1102*(As-8.7);										% Set beta values
hdd=ideal_lph(M,w1c)-ideal_lph(M,w2c)+ideal_lph(M,pi); % Define Impulse Response
w_kai=kaiser(M,beta);										% Define kaiser Window
h=hdd.*w_kai';													% set FIR type 1 filter

figure(1)														
stem(h);															% Plot filter function
title('Filter h(n) Using Kaiser Window ');													 
xlabel('Frequency ');													
ylabel('Amplitude');

[H,W]=freqz(h,1,500);										% Define Frequency Response
db=20*log10(abs(H));											% Set Magnitude Response

figure(2)														
subplot(2,1,1);
plot(W/pi,db);														% Plot Magnitude Response
title('Magnitude Response for Type 1 FIR Filter wiht Kaiser Window ');
xlabel(' Frequency');
ylabel('Amplitude ');

subplot(2,1,2);
phase=angle(H);												% define phase
plot(phase);
title('Phase Resposne for Type 1 FIR filter with Using Kaiser Window');
xlabel('Frequency');
ylabel('Angle');

%(3) Plot X(e^jw) and Y(e^jw). Explain results.

n=0:449;															% Set the sample range
x=cos(0.2*pi*n)+sin(0.54*pi*n)+sin(0.56*pi*n)+cos(0.58*pi*n)+cos(0.9*pi*n); % Input Signal


figure(3)														
subplot(2,1,1);
X=freqz(x,1,256);										% Take the Frequency Response
XX=abs(X)
plot(XX);											% Plot the magnitude of input filter
title('Magnitude X(e^jw)');	
xlabel('Frequency ');
ylabel('Amplitude ');

y=filter(h,1,x);												% Filter output

subplot(2,1,2);														
Y=freqz(y,1,256);										% Take the Frequency Response
plot(abs(Y));											% Plot the Magnitude of output Filter
title('Magnitude Y(e^jw) ');	
xlabel('Frequency ');
ylabel('amplitude ');

%(4) Explain Gibbs Phenomenon.

%Gibbs Phenomenon shows that the Fourier Series does not converge uniformly to 
%a discontinuous function in an arbitrary small interval of the discontinuity point.
%Near a point, where f has a jump discontinuity, the partial sums Sn of a Fourier 
%Series exhibit a substantial overshoot near these endpoints, and an increase in n 
%will not diminish the amplitude of the overshoot, although with increasing n the 
%overshoot occurs over smaller and smaller intervals.
