%  Name:  Kris Tuthill
%  Date: 11/13/01
%  Coures: EE416
%  
%  Project: 4
%
%  Kaiser Window  
%
%
%


% 1. 

% Explain the different types of FIR filters clearly citing the differences between each.
  
% There are four type of linear phase FIR filters;  

% Type 1: symmetric impulse response with odd length, this type has either an even number or
% no zeros at z=1 and z=-1.

% Type 2: symmetric impulse response with even length, this type has either an even number or
% no zeros at z=1, and an odd number of zeros at z=-1.

% Type 3: antisymmetric impulse response with odd length, this type has an odd number of zeros
% at z=1 and z=-1.

% Type 4: antisymmetric impulse response with even length, this type has an odd number of zeros
% at z=1, and either an even number or no zeros at z=-1.

% The presence of zeros at z=1,-1 leads to the following limitations on the use of these linear
% phase transfer functions for designing frequency-selective filters:

% Type 1 has no restrictions and can be used to design almost any type of filter.  
% Type 2 cannot be used to design a highpass filter since it always has a zero at z=-1.
% Type 3 cannot be used to design either a highpass or lowpass or bandstop filter.
% Type 4 cannot be used to design a lowpass filter due to presence of a zero at z=1.



% 2.

w1p= 0.3*pi;								% 1st passband cutoff frequency	
w1s= 0.5*pi;								% 1st stopband cutoff frequency
Rp= 1;	
w2s= 0.6*pi;								% 2nd stopband cutoff frequency
w2p= 0.8*pi;								% 2nd passband cutoff frequency
As= 80;

width1= w1s-w1p;						% width of 1st passband
width2= w2p-w2s;						% width of 2nd passband

w1c= (w1p+w1s)/2;						% 1st cutoff frequency
w2c= (w2p+w2s)/2;						% 2nd cutoff frequency

delta_f= min(width1/(2*pi),width2/(2*pi));	
M= [(As-7.95)/(14.36*delta_f)+1];
Mc= ceil(M);								% filter order

B= 0.1102*(As-8.7);						% sidelobe height

K= (Mc-1)/2;
N= [0:1:(Mc-1)];
M1= N-K + 0.0000000001;

h1= sin(w1c*M1)./(w1c*M1);			% ideal lowpass filter
h2= sin(w2c*M1)./(w2c*M1);			% ideal lowpass filter
h3= sin(pi*M1)./(pi*M1);		     	% ideal lowpass filter


hd= h1-h2+h3;							% bandpass filter impulse response
kw= kaiser(Mc,B);						% kaiser window
h= hd.*kw';								% multiply kaiser window with hd

h= fir1(Mc,0.4);						% lowpass FIR filter of length Mc, h
[H,W]= freqz(h,1,128);					% z-transform of h
mag= 20*log10(abs(H));					% magnitude of H in dB
phase= unwrap(angle(H));				% phase of H

figure(1);
plot(h);									% plot of h					
title('FIR filter, h(n)');

figure(2);
subplot(211);
plot(W/pi,mag);							% plot of magnitude of H vs. normalized frequency
title('H(e^jw) magnitude');
xlabel('frequency');
ylabel('magnitude');
subplot(212);
plot(phase);								% plot of phase of H vs. normalized frequency
title('H(e^jw) phase');
xlabel('frequency');
ylabel('phase');


% 3.

n= 0:449;									% length of n =450

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);  % function x(n)
[X,W1]= freqz(x,1,128);				% z-transform of x(n)
mag1= abs(X);							% magnitude of X

y= conv(x,h);							% convolution of x and h	
[Y,W2]= freqz(y,1,128);				% z-transform of convolved signal
mag2= abs(Y);							% magnitude of Y

figure(3);
subplot(211);
plot(W1/pi,mag1);						% plot of X magnitude vs. normalized frequency
title('X(e^jw) magnitude');
xlabel('frequency');
ylabel('magnitude');
subplot(212);
plot(W2/pi,mag2);						% plot of Y magnitude vs. normalized frequency
title('Y(e^jw) magnitude');
xlabel('frequency');
ylabel('magnitude');


% 4. 

% The Gibbs Phenomenon:  The Gibbs Phenomenon shows that a Fourier series does not converge uniformly
% to a discontinous function in an arbitrary small interval of the dicontinuity point.  Theorem, let
% f be a piecewise smooth function 2pi-periodic.  At each discontinuity point xo, its Fourier series 
% either overshoots or undershoots f(xo) by about 9% of the magnitude of the discontinuity jump.  The 
% overshoot factor depends only on the type of discontinuity and not the values of the function.

% The Gibbs Phenomenon does not vanish as the filter length increases, but a non-rectangular window
% reduces its length.






