close all;clear
N = 50;
n = [0:N-1].';

fc=0.10; % typical cutoff frequency for lowpass filter
hIdeal = fir1(N-1, 2*fc, rectwin(N)); % Matlab code to create a
% truncated & delayed, but otherwise raw, ideal low pass filter


w = hamming(N); % Strong stopband attenuation
figure
subplot(7,1,1);stem(n,hIdeal);axis tight;ylabel('h ideal')
subplot(7,1,2);stem(n,w);axis tight;ylabel('w')
[W,f]=freqz(w,1,256,1);
subplot(7,1,3);plot(f,abs(W));axis tight;ylabel('|W|')
subplot(7,1,4);plot(f,20*log10(abs(W)/max(abs(W))));axis([0 0.5 -60 0]);ylabel('|W| (dB)')
h=fir1(N-1,2*fc,w);%Matlab code to create a windowed low pass filter
subplot(7,1,5);stem(n,h);axis tight;ylabel('h')
[H,f]=freqz(h,1,256,1);
subplot(7,1,6);plot(f,abs(H));axis tight;ylabel('|H|')
hold on;plot(f,f<fc,'k-');hold off
subplot(7,1,7);plot(f,20*log10(abs(H)));axis([0 0.5 -60 0]);ylabel('|H| (dB)')


beta=5;
w = kaiser(N,beta); % Very similar to Hamming window!
figure
subplot(7,1,1);stem(n,hIdeal);axis tight;ylabel('h ideal')
subplot(7,1,2);stem(n,w);axis tight;ylabel('w')
[W,f]=freqz(w,1,256,1);
subplot(7,1,3);plot(f,abs(W));axis tight;ylabel('|W|')
subplot(7,1,4);plot(f,20*log10(abs(W)/max(abs(W))));axis([0 0.5 -60 0]);ylabel('|W| (dB)')
h=fir1(N-1,2*fc,w);%Matlab code to create a windowed low pass filter
subplot(7,1,5);stem(n,h);axis tight;ylabel('h')
[H,f]=freqz(h,1,256,1);
subplot(7,1,6);plot(f,abs(H));axis tight;ylabel('|H| (dB)')
hold on;plot(f,f<fc,'k-');hold off
subplot(7,1,7);plot(f,20*log10(abs(H)));axis([0 0.5 -60 0]);ylabel('|H| (dB)')

w = rectwin(N); %rectangular window (i.e. "no window"). But not ideal!
figure
subplot(7,1,1);stem(n,hIdeal);axis tight;ylabel('h ideal')
subplot(7,1,2);stem(n,w);axis tight;ylabel('w')
[W,f]=freqz(w,1,256,1);
subplot(7,1,3);plot(f,abs(W));axis tight;ylabel('|W|')
subplot(7,1,4);plot(f,20*log10(abs(W)/max(abs(W))));axis([0 0.5 -60 0]);ylabel('|W| (dB)')
h=fir1(N-1,2*fc,w);%Matlab code to create a windowed low pass filter
subplot(7,1,5);stem(n,h);axis tight;ylabel('h')
[H,f]=freqz(h,1,256,1);
subplot(7,1,6);plot(f,abs(H));axis tight;ylabel('|H|')
hold on;plot(f,f<fc,'k-');hold off
subplot(7,1,7);plot(f,20*log10(abs(H)));axis([0 0.5 -60 0]);ylabel('|H| (dB)')


% can repeat for e.g. fc=0.10; 
