H = 0;
h = 0;
hd =0;

wp = 0.4*pi;
n = 0:30;
hd = sin(wp*(n-15))./(pi*(n-15));
hd(16) = wp/pi;
[Hd,Wd] = freqz(hd, 1);

win = hamming(length(n));
hdwin = hd.*win';
Hdwin = fft(hdwin);

h = fir1(32,0.4);
[H,W]=freqz(h,1);

m = 0:511;
sig = cos(0.6*pi*m) + sin(0.2*pi*m) + cos(0.45*pi*m);
[S,Ws]=freqz(sig,1);
%Sig = fft(sig);    %This gives same output as freqz
%SIG2 = Sig.*Hdwin;     %Trying to convolve in frequency domain
Sig = conv(hdwin, sig);
[SIG,Wsig] = freqz(Sig,1);

%<<<<<<<<<<< Plots >>>>>>>>>>>%
% Matlab 6.1 code
% s.xunits = 'rad/sample';
% s.yunits = 'squared';
% s.plot = 'mag';              % Plot magnitude only.
% figure(1);
% freqzplot(Hd,Wd,s)

%Filter Response Plots
figure(1);
subplot(1,2,1);
plot(Wd/pi, 20*log10(abs(Hd)), W/pi, 20*log10(abs(H)), 'r:');     %dB plot
legend('Filter using hamming window', 'Filter using fir1');
title('');
xlabel('Frequency - Normalized (x\pi rad/sample)');
ylabel('Magnitude (dB)');
grid;
subplot(1,2,2)
plot(Wd/pi, abs(Hd), W/pi, abs(H), 'r:');       %Absolute value plot
legend('Filter using hamming window', 'Filter using fir1');
title('');
xlabel('Frequency - Normalized (x\pi rad/sample)');
ylabel('Magnitude');
grid;

%Filtered Signal Plots
figure(2);
subplot(2,2,1);
plot(Ws/pi, 20*log10(abs(S)));
title('Test Signal');
xlabel('Frequency - Normalized (x\pi rad/sample)');
ylabel('Magnitude (dB)');
grid;
subplot(2,2,2);
plot(Wsig/pi, 20*log10(abs(SIG)));
title('Test signal after filtering');
xlabel('Frequency - Normalized (x\pi rad/sample)');
ylabel('Magnitude (dB)');
grid;
subplot(2,2,3);
plot(Ws/pi, abs(S));
title('');
xlabel('Frequency - Normalized (x\pi rad/sample)');
ylabel('Magnitude');
grid;
subplot(2,2,4);
plot(Wsig/pi, abs(SIG));
title('');
xlabel('Frequency - Normalized (x\pi rad/sample)');
ylabel('Magnitude');
grid;


% 3.	Explain the different types of Windows and the applications of Windows in signal 
% processing.  Emphasize on FIR filter design using Windows. 
% In digital filter design, there are a numbers of methods of achieving the desired filter.
% One of these methods applies a window to the transfer function h(n). When using windows to 
% implement a digital filter, there are many to choose from. They include Rectangular, Barlett,
% Hanning, and Hamming. These filters will give you different main lobe widths and different 
% sidelobe attenuation. For example, the rectangular window give you the smallest main lobe
% width but relatively little sidelobe attenuation where as the hamming give you a wide mainlobe
% with almost 40dB of attenuation. Windowing is usefull when taking the fft of a finite length 
% of a sample. It smooths the ripple that occurs in the output when the discontinuity of the
% ideal impulse responce is fft'ed.