%  Name:  Kris Tuthill
%  Date: 11/13/01
%  Coures: EE416
%  
%  Project: 3
%
%  Hamming Window  
%



%1.

wp= 0.4*pi;   							% passband cutoff frequency
n= 0:30;									% sample length
h= sin(wp*(n-15))./(pi*(n-15));    % impulse response of lowpass filter
h(16)= wp/pi;							% midpoint of impulse response




[H,W]= freqz(h,1,128);					% calculate the z-transform frequency response of h
mag= 20*log10(abs(H));					% magnitude of H in dB

figure(1);								 
subplot(2,1,1);
plot(n,h);								% plot of h vs. n	
title('h(n), magnitude');
ylabel('magnitude');
xlabel('input sample');
subplot(2,1,2);
plot(W/pi,mag);							% plot of H magnitude vs. normalized frequency					
title('H(e^jw), mabnitude');
xlabel('frequency');
ylabel('magnitude');

%2.

win= hamming(31);						% Hamming window of size 31
h1= h.*win';								% multiply impulse response with Hamming window

[H1,W1]= freqz(h1,1,128);				% z-transform of impulse response, h1		
mag1= 20*log10(abs(H1));				% magnitude of H1 in dB
phase1= unwrap(angle(H1));		   % phase of H1

figure(2);
subplot(211);
plot(W1/pi,mag1);						% plot of H1 magnitude vs. normalized frequency
title('Hamming Window, magnitude');
ylabel('magnitude');
xlabel('frequency');
subplot(212);
plot(W1/pi,phase1);					   % plot of H1 phase vs. normalized frequency
title('phase')
ylabel('angle');
xlabel('frequency');

h2= fir1(30,0.4);					   % lowpass FIR filter type 1 of length 30, h2
[H2,W2]= freqz(h2,1,128);				% z-transform of impulse response h2
mag2= 20*log10(abs(H2));				% magnitude of h2 in dB
phase2= unwrap(angle(H2));			% phase of h2

figure(3);
subplot(211);
plot(W2/pi,mag2);						% plot of H2 magnitude vs. normalized frequency			
title('FIR filter, magnitude');
ylabel('magnitude');
xlabel('frequency');
subplot(212);
plot(W2/pi,phase2);						% plot of H2 phase vs. normalized frequency
title('phase')
ylabel('angle');
xlabel('frequency');



%3.

% Explain the different types of Windows and the applications of Windows in signal processing.  
% Emphasize on FIR filter design using Windows.

% The simplest method of FIR filter design is called the window method.  The windowing method is 
% used to truncate an infinitely long, non-causal impulse response.  This is the most straightforward
% approach to obtaining a causal FIR approximation to such systems.  The approximation of an ideal
% filter by truncation of the ideal impulse response is identical to the issue of the convergence of 
% Fourier series.

% Hamming Window:  The Hamming window is a weighted moving average transformation used to smooth the 
% periodogram values.  In the Hamming window, for each frequency, the weights for the weighted 
% average of the periodogram values are computed as:  wj= 0.54+0.46*cos(pi*j/p) (for j=0 to p), where
% p= (M-1)/2.  This weght function will assign the greates weight to the observation being smoothed in 
% the center of the window, ad increasingly smaller weights to values that are further away from the center.

% Kaiser Window:  The Kaiser window has an adjustable parameter, a, which controls how quickly it approaches 
% zero at the edges.  It is defined by, Kaiser(x,t,a)= Io(a(1-(x/t)^2)^0.5)/Io(a), where Io is the zeroth 
% order Bessel function.  The higher a, the narrower gets the window and therefore, due to the not so
% severe truncation then, the less severe are the bumps above pi.

% Hann Window:  The Hann window and the Hamming window are quite similar, they only differ in the choice of 
% one parameter, a:  H(x,t,a)= a+(1-a)cos(pi*x/t).  With a= 0.5 being the Hann window and a= 0.54 the Hamming
% window.  The Hamming window also has the disadvantage of being discontinous at the edges (and has 
% therefore a, however not so severe, bump above pi), which leads to clearly visible artifacts in the images.

% Blackman Window:  The Blackman window is quite similar, too, to Hann, and Hamming, but it has one additonal
% cosine term to further reduce the ripple ratio.  Blackman(x,t)= 0.42+0.5cos(pi*x/t)+0.08cos(2pi*x/t).
% Although its frequency response is slightly worse below pi, it approaches zero more smoothly above and 
% turns out to produce quite satisfying results.