% EE-3221 Digital Signal Processing
% 10 May 2020
% Dr. Prust <prust@msoe.edu>

% example_windowDesignCommonWindows.m
%   Demonstration of window design method using
%   different window functions

close all
clear all

% specify ideal lowpass filter cutoff
Omega_c = pi/3;

% specify filter length and calculate h_d[n]
N = 31;
n = -(N-1)/2 : (N-1)/2;
h_d = sin(Omega_c * n)./(pi*n);
h_d((N+1)/2) = Omega_c/pi; % fix n = 0 term

h_rect = h_d.*rectwin(N)';
h_bart = h_d.*bartlett(N)';
h_hamm = h_d.*hamming(N)';
h_blac = h_d.*blackman(N)';

% compare windows
figure
subplot(2,2,1)
stem(n, rectwin(N))
title('Rectangular')
xlabel('n')
set(gca,'FontSize',20)
subplot(2,2,2)
stem(n, bartlett(N))
title('Bartlett')
xlabel('n')
set(gca,'FontSize',20)
subplot(2,2,3)
stem(n, hamming(N))
title('Hamming')
xlabel('n')
set(gca,'FontSize',20)
subplot(2,2,4)
stem(n, blackman(N))
title('Blackman')
xlabel('n')
set(gca,'FontSize',20)

% compare filter responses
Omega = linspace(0,pi,1e4);
figure
hold on
grid on
plot(Omega,20*log10(abs(freqz(h_rect,1,Omega))),'LineWidth',2)
plot(Omega,20*log10(abs(freqz(h_bart,1,Omega))),'LineWidth',2)
plot(Omega,20*log10(abs(freqz(h_hamm,1,Omega))),'LineWidth',2)
plot(Omega,20*log10(abs(freqz(h_blac,1,Omega))),'LineWidth',2)
xlabel('\Omega')
ylabel('|H(e^{j\Omega})| (dB)')
ylim([-100 10])
legend('Rectangular', 'Bartlett', 'Hamming', 'Blackman')
set(gca,'FontSize',20)

