%Demo of window FIR filtering using windows .....
% This version compares frequency response as a function of shape

a = 1;
wc = .4*pi;
N = 51;
n = -(N-1)/2:(N-1)/2;

% First, freq response, one at a time:


% Rectangular window

window = boxcar(N);
[H,W] = freqz(window,1 );
H = H/max(abs(H));
plot(W/pi,20*log10(abs(H)))
title('DTFT of rectangular window, N = 51','fontsize',14)
xlabel('\omega/\pi','fontsize',14)
ylabel('|H(\omega)|, dB','fontsize',14)
axis([0 1 -100 10]);
pause


% Bartlett window

window = bartlett(N);
[H,W] = freqz(window,1 );
H = H/max(abs(H));
plot(W/pi,20*log10(abs(H)))
title('DTFT of Bartlett window, N = 51','fontsize',14)
xlabel('\omega/\pi','fontsize',14)
ylabel('|H(\omega)|, dB','fontsize',14)
axis([0 1 -100 10]);
pause


% Hanning window

window = hanning(N);
[H,W] = freqz(window,1 );
H = H/max(abs(H));
plot(W/pi,20*log10(abs(H+eps)))
title('DTFT of Hanning window, N = 51','fontsize',14)
xlabel('\omega/\pi','fontsize',14)
ylabel('|H(\omega)|, dB','fontsize',14)
axis([0 1 -100 10]);
pause


% Hamming window

window = hamming(N);
[H,W] = freqz(window,1 );
H = H/max(abs(H));
plot(W/pi,20*log10(abs(H)))
title('DTFT of Hamming window, N = 51','fontsize',14)
xlabel('\omega/\pi','fontsize',14)
ylabel('|H(\omega)|, dB','fontsize',14)
axis([0 1 -100 10]);
pause


% Blackman window

window = blackman(N);
[H,W] = freqz(window,1 );
H = H/max(abs(H));
plot(W/pi,20*log10(abs(H)))
title('DTFT of Blackman window, N = 51','fontsize',14)
xlabel('\omega/\pi','fontsize',14)
ylabel('|H(\omega)|, dB','fontsize',14)
axis([0 1 -100 10]);
pause


% Repeat, but superimposed:


% Rectangular window

window = boxcar(N);
[H,W] = freqz(window,1 );
H = H/max(abs(H));
plot(W/pi,20*log10(abs(H)))
title('DTFT of rectangular window, N = 51','fontsize',14)
xlabel('\omega/\pi','fontsize',14)
ylabel('|H(\omega)|, dB','fontsize',14)
hold on
axis([0 1 -100 10]);
pause


% Bartlett window

window = bartlett(N);
[H,W] = freqz(window,1 );
H = H/max(abs(H));
plot(W/pi,20*log10(abs(H)),'g')
title('DTFT of Bartlett window, N = 51','fontsize',14)
xlabel('\omega/\pi','fontsize',14)
ylabel('|H(\omega)|, dB','fontsize',14)
axis([0 1 -100 10]);
pause


% Hanning window

window = hanning(N);
[H,W] = freqz(window,1 );
H = H/max(abs(H));
plot(W/pi,20*log10(abs(H+eps)),'c')
title('DTFT of Hanning window, N = 51','fontsize',14)
xlabel('\omega/\pi','fontsize',14)
ylabel('|H(\omega)|, dB','fontsize',14)
axis([0 1 -100 10]);
pause


% Hamming window

window = hamming(N);
[H,W] = freqz(window,1 );
H = H/max(abs(H));
plot(W/pi,20*log10(abs(H)),'r')
title('DTFT of Hamming window, N = 51','fontsize',14)
xlabel('\omega/\pi','fontsize',14)
ylabel('|H(\omega)|, dB','fontsize',14)
axis([0 1 -100 10]);
pause


% Blackman window

window = blackman(N);
[H,W] = freqz(window,1 );
H = H/max(abs(H));
plot(W/pi,20*log10(abs(H)),'m')
title('DTFT of Blackman window, N = 51','fontsize',14)
xlabel('\omega/\pi','fontsize',14)
ylabel('|H(\omega)|, dB','fontsize',14)
axis([0 1 -100 10]);
pause
hold off


%Re-initializing to avoid memory overflows:

clear
a = 1;
wc = .4*pi;
N = 51;
n = -(N-1)/2:(N-1)/2;

%Time domain response
hd = (wc/pi)*sinc(n*wc/pi);
stem(n,hd)
title('h[n] for LPF with rectangular window, N = 51','fontsize',14)
xlabel('n','fontsize',14)
ylabel('h[n]','fontsize',14)
pause


%Frequency domain responses

% Rectangular window
w = boxcar(N);
[H,W] = freqz(w'.*hd,1 );
plot(W/pi,20*log10(abs(H+eps)))
title('DTFT of LPF with rectangular window, N = 51','fontsize',14)
xlabel('\omega/\pi','fontsize',14)
ylabel('|H(\omega)|, dB','fontsize',14)
axis([0 1 -100 10]);
pause

% Bartlett window
w = bartlett(N);
[H,W] = freqz(w'.*hd,1 );
plot(W/pi,20*log10(abs(H+eps)))
title('DTFT of LPF with Bartlett window, N = 51','fontsize',14)
xlabel('\omega/\pi','fontsize',14)
ylabel('|H(\omega)|, dB','fontsize',14)
axis([0 1 -100 10]);
pause

% Hanning window
w = hanning(N);
[H,W] = freqz(w'.*hd,1 );
plot(W/pi,20*log10(abs(H+eps)))
title('DTFT of LPF with Hanning window, N = 51','fontsize',14)
xlabel('\omega/\pi','fontsize',14)
ylabel('|H(\omega)|, dB','fontsize',14)
axis([0 1 -100 10]);
pause

% Hamming window
w = hamming(N);
[H,W] = freqz(w'.*hd,1 );
plot(W/pi,20*log10(abs(H+eps)))
title('DTFT of LPF with Hamming window, N = 51','fontsize',14)
xlabel('\omega/\pi','fontsize',14)
ylabel('|H(\omega)|, dB','fontsize',14)
axis([0 1 -100 10]);
pause


% Blackman window
w = blackman(N);
[H,W] = freqz(w'.*hd,1 );
plot(W/pi,20*log10(abs(H+eps)))
title('DTFT of LPF with Blackman window, N = 51','fontsize',14)
xlabel('\omega/\pi','fontsize',14)
ylabel('|H(\omega)|, dB','fontsize',14)
axis([0 1 -100 10]);
pause



% and holding .....



% Rectangular window
w = boxcar(N);
[H,W] = freqz(w'.*hd,1 );
plot(W/pi,20*log10(abs(H+eps)))
title('DTFT of LPF with rectangular window, N = 51','fontsize',14)
xlabel('\omega/\pi','fontsize',14)
ylabel('|H(\omega)|, dB','fontsize',14)
axis([0 1 -100 10]);
hold on
pause

% Bartlett window
w = bartlett(N);
[H,W] = freqz(w'.*hd,1 );
plot(W/pi,20*log10(abs(H+eps)),'g')
title('DTFT of LPF with Bartlett window, N = 51','fontsize',14)
xlabel('\omega/\pi','fontsize',14)
ylabel('|H(\omega)|, dB','fontsize',14)
axis([0 1 -100 10]);
hold on
pause

% Hanning window
w = hanning(N);
[H,W] = freqz(w'.*hd,1 );
plot(W/pi,20*log10(abs(H+eps)),'c')
title('DTFT of LPF with Hanning window, N = 51','fontsize',14)
xlabel('\omega/\pi','fontsize',14)
ylabel('|H(\omega)|, dB','fontsize',14)
axis([0 1 -100 10]);
hold on
pause

% Hamming window
w = hamming(N);
[H,W] = freqz(w'.*hd,1 );
plot(W/pi,20*log10(abs(H+eps)),'r')
title('DTFT of LPF with Hamming window, N = 51','fontsize',14)
xlabel('\omega/\pi','fontsize',14)
ylabel('|H(\omega)|, dB','fontsize',14)
axis([0 1 -100 10]);
hold on
pause


% Blackman window
w = blackman(N);
[H,W] = freqz(w'.*hd,1 );
plot(W/pi,20*log10(abs(H+eps)),'m')
title('DTFT of LPF with Blackman window, N = 51','fontsize',14)
xlabel('\omega/\pi','fontsize',14)
ylabel('|H(\omega)|, dB','fontsize',14)
axis([0 1 -100 10]);
hold on
pause

