% Kevin Brenon
% 26785079
% EE 416
% 11/13/01

% MATLAB Project 3: Hamming Window


% 1. If h(n) =  hd(n-15) for 0 < n < 30 , plot h(n) and H(ejw)

wp = 0.4*pi;                    % wp equals the passband cutoff
                                % frequency.
% The following for loop sets up the impulse response h(n).
for n = 1:30                   
    h(n) = (sin(wp*(n-15)))/(pi*(n-15)); 
    for n = 15
        h(n) = wp/pi;
    end
end
figure(1);
subplot(2,1,1);
plot(h)                         % The impulse response is plotted.
title('1. The Plot of h(n)');
ylabel('h(n)');
xlabel('n');

N = 1:30;                       % This sets up and performs the 
[H,NH] = fouriert(h,N);         % fourier transform of h(n) to 
dBH = 20*log10(abs(H));         % obtain H(e^jw).
subplot(2,1,2);
plot(NH,dBH)                    % It is then plotted.
title('1. The Plot of H(e^jw)')
ylabel('H(e^jw)');
xlabel('Frequency');


% 2. Design an FIR filter defined by hf(n)=h(n)w(n) where w(n) 
%    denotes the Hamming Window.

w = hamming(30);                % Obtains the Hamming window of
hf = h.*w';                     % length 30, then creates teh FIR
figure(2);                      % filter and plots the result.
freqz(hf)
title('2. FIR filter hf(n) Using a Hamming Window');

f = fir1(30,0.4);               % This creates an FIR filter to 
figure(3);                      % verify the design of the one 
freqz(f)                        % made using the Hamming Window.
title('2. Verification of FIR filter hf(n) Designed Using a Hamming Window');
% By compairing the two plots of the FIR filter created with the 
% Hamming Window and the one using the fir1 function, it is clear
% that both filter designs are very similar.  Perhaps the one 
% obtained using the fir1 command is a bit more precise, but the 
% other is close enough.


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

% In both digital filter design and power spectrum estimation, 
% the choice of a windowing function can play an important role 
% in determining the quality of overall results. The main role of 
% the window is to damp out the effects of the Gibbs phenomenon 
% that results from truncation of an infinite series.
% The simplest window is the Rectangular Window, which is just a 
% vector of ones of the appropriate length.  The next window 
% would be the Bartlett or Triangular Window.  This is the
% convolution of two rectangular windows.  Then there are the 
% generalized cosine windows, which consist of the Hamming
% Window, the Hann Window, and the Blackman Window.  In MATLAB,
% they are all generated by:

% ind = (0:n-1)'*2*pi/(n-1);
% w = A - B*cos(ind) + C*cos(2*ind);

% The concept behind these windows is that by summing the 
% individual terms to form the window, the low frequency peaks 
% in the frequency domain combine in such a way as to decrease 
% sidelobe height. This has the side effect of increasing the 
% mainlobe width.  The Hamming Window has constants A = 0.54,
% B = 0.46, and C = 0.  the Hann Window has A = 0.5, B = 0.5,
% and C = 0.  The Blackman Window has A = 0.42, B = 0.5, and 
% C = 0.08.  There is also the Kaiser Window.  The Kaiser window 
% is an approximation to the prolate-spheroidal window, for 
% which the ratio of the mainlobe energy to the sidelobe energy 
% is maximized.  There are two design formulas that can help
% design FIR filters to meet a set of filter specifications 
% using a Kaiser window. To achieve a sidelobe height of -a dB, 
% the beta parameter is

%           0.1102(a-8.7),                    a > 50
% beta =    0.5842((a-21)^0.4)+0.07886(a-21), 50 >= a >= 21

%           0,                                a < 21

% For a transition width of delta omega rad/s, use the length

% n = ((a-8)/(2.285 delta omega))+1

% Last, the Chebyshev window minimizes the mainlobe width, 
% given a particular sidelobe height. It is characterized by 
% an equiripple behavior, that is, its sidelobes all have the 
% same height.

