%
% Window Method of FIR Filter Design
%

% Truncated Ideal Low-Pass Filter
fc = 0.2; % Normalized so that 1 is full band
wc = pi*fc;
M = 40;
n = -M:M;
h = zeros(size(n));
h(n~=0) = sin(wc*n(n~=0))/pi./n(n~=0);
h(n==0) = wc/pi;

% Windows
w1 = window(@hann,2*M+1)';
h1 = h .* w1;
w2 = window(@chebwin,2*M+1)';
h2 = h .* w2;

% Use fft to compute sampled DTFT
N = 1024;
H = abs(fft(h,N));
H1 = abs(fft(h1,N));
H2 = abs(fft(h2,N));
W = abs(fft(ones(1,2*M+1),N));
W1 = abs(fft(w1,N));
W2 = abs(fft(w2,N));

% Plot Window Response
index = 1:(N/2);
f = 2*(index-1)/N;
figure(1);
semilogy(f,W(index),f,W1(index),f,W2(index),'LineWidth',2);
set(gca,'FontSize',18);
set(gca,'YLim',[10^(-4) 100]);
title('Window Response');
xlabel('Normalized Frequency');
ylabel('Magnitude Response');
legend('Rectangular Window','Hann Window','Chebyshev Window')
legend('Rectangular Window','Hann Window','Chebyshev Window')
grid on

pause

% Plot Filter Response
figure(2);
semilogy(f,H(index),f,H1(index),f,H2(index),'LineWidth',2);
set(gca,'FontSize',18);
set(gca,'YLim',[10^(-7) 1]);
title('Filter Response');
xlabel('Normalized Frequency');
ylabel('Magnitude Response');
legend('Rectangular Window','Hann Window','Chebyshev Window')
grid on

