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

% example_windowDesignRectangular.m
%   Demonstration of window design method using
%   varying length rectangular windows

close all
clear all

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

% specify different values of N to compare
%  script works for odd values of N
N_all = [5 11 31 51];

% loop through each choice of N and, for each,
%  calculate frequency response
for ii=1:length(N_all)
    N = N_all(ii);
    n = -(N-1)/2 : (N-1)/2;
    h = sin(Omega_c * n)./(pi*n);
    h((N+1)/2) = Omega_c/pi; % fix n = 0 term
    [H(ii,:),Omega] = freqz(h,1,1e6);
end

% plot magnitude and phase response
figure
subplot(2,1,1)
plot(Omega,abs(H),'LineWidth',2)
xlabel('\Omega');
ylabel('|H(e^{j\Omega})|')
grid on
legend(num2str(N_all'))
set(gca,'FontSize',20)
subplot(2,1,2)
plot(Omega,angle(H),'LineWidth',2)
xlabel('\Omega');
ylabel('\angle H(e^{j\Omega}) (rad)')
grid on
legend(num2str(N_all'))
set(gca,'FontSize',20)

% plot magnitude response on a dB scale
figure
plot(Omega,20*log10(abs(H)),'LineWidth',2)
xlabel('\Omega');
ylabel('|H(e^{j\Omega})| (dB)')
ylim([-45 5]);
grid on
legend(num2str(N_all'))
set(gca,'FontSize',20)
