%Ridwan A Kabir 
%#28083268
%EE416
%Project 3
%Due-11/13/2001

%**********************************************************************************************
%part 1:Impulse and Frequency response of a LPF plotted;
clear all;

wp=0.4*pi;      %This is the pass band cut off frequency which is given and denoted as wp;
n=0:30;         %We are using a matrix size of 30. The impulse response is plotted as h(n);
h=sin(wp*(n-15))./(pi*(n-15));
h(16)=wp/pi;

figure(1);
plot(n,h);
title('Impulse response,h(n)');
xlabel('n');
ylabel('h(n)');

[H,W]=freqz(h,1,256);   %Here we compute the frequency response of the signal. The magnitude
mag=20*log10(abs(H));   %of the frequency response is taken in dB.

figure(2);
plot(W/pi,mag);
title('Frequency response of h(n),H(expjw)');
xlabel('range of frequency');
ylabel('H(exp-jw)');

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

%Part 2:Design of an FIR filter defined by hf=h(n)w(n).The humming window defined as w(n);

length(h)
win=hamming(31);
length (win);
h1=h.*win';
figure(3);
plot(n,h1);
title('Impulse response of a FIR filter');
xlabel('n');
ylabel('h1(n)');


[H1,W1]=freqz(h1,1,256);
mag1=20*log10(abs(H1));
figure(4);
plot(W1/pi,mag1);
title('Frequency response of the FIR filter');
xlabel('range of frequency,0 to 1');
ylabel('H(exp-jw)');

h2=fir1(30,0.4);
[H2,W2]=freqz(h2,1,256);
mag2=20*log10(abs(H2));
figure(5);
plot(W2/pi,mag2);
title('');
xlabel('');
ylabel('');

phase=unwrap(angle(H1));
figure(6);
plot(phase);

phase2=unwrap(angle(H2));
figure(7);
plot(phase2);

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

%part 3:Different types of windows and applications in signal processing:

%Three types of windows are actually used in Signal processing;
%1.Kaiser-defined as W where W=kaiser(N,beta). This returns beta valued N-point kaiser window.

%2.Hamming-HAMMING(N) returns the N-point symmetric Hamming window in a column vector.
  
%    HAMMING(N,SFLAG) generates the N-point Hamming window using SFLAG window
%    sampling. SFLAG may be either 'symmetric' or 'periodic'. By default, a 
%    symmetric window is returned. 
 
%3.Gaussian window.- defined as GAUSSWIN(N),this command returns an N-point Gaussian window.
 
%    As of above example of hamming window,GAUSSWIN(N, ALPHA) returns the ALPHA-valued N-point Gaussian 
%    window.  As ALPHA increases, the width of the window will decrease.
%    If omitted, ALPHA is 2.5.
 
%    EXAMPLE:
%       N = 32; 
%       w = gausswin(N); 
%       plot(w); title('32-point Gaussian window');
%The simplest method to design an FIR (Finite Impulse Response) filter is the window 
%method. The window method begins with an ideal lowpass filter, whose impulse
%response is of infinite duration, and truncates it, producing a finite impulse 
%response. 

%An example of an FIR filter is shown below, where the length of the filter (L)is set 
%to 31 and the cutoff frequency is set to one-quarter of the sampling frequency. To
%obtain a causal FIR filter, the impulse response of the ideal lowpass filter is 
%shifted in time by 15 ((L-1)/2) samples and windowed.
%The increasing the filter length does not affect the ripple but does narrow 
%the transition width. The presence of the ripples whose amplitude does not change with
%the filter length is called Gibbs' phenomenom. One technique to reduce the ripples is to multiply the impulse response of the ideal filter by a tapered window. By
%changing the window type, one can see how the ripple in the frequency response. 

%The frequency response was plotted using an FFT algorithm to compute the 
%N coefficients where N is a power of 2. To see the effects of changing the number of
%coefficients, decrease the number of coefficients and notice that a low N results 
%in low resolution in the frequency response. A very large N results in too much
%computational time without significant improvement in the frequency resolution. 

