% Travis Mattingly  12/7/98
% EE 262 Homework 29
%
%  FIR lowpass filter design
%
%
%
% Samples in fft.  FFT alternate way to get frequency response, not as good, though
n=8;
%
% Samples of FIR filter
N0=9;
%
% Sample frequency, LP cutoff frequency in Hz
Fs=2000; Fc=100;
%
%
% Figure other parameters
T=1/Fs; T0=n*T; F0=1/T0;
index=1:n;
%
%
k=-(N0-1)/2:(N0-1)/2;
% prevent divide by zero
k(((N0-1)/2)+1)=1;
hk=2*T*Fc*sin(2*pi*Fc*T*k)./(2*pi*Fc*T*k);
hk(((N0-1)/2)+1)=2*T*Fc;
disp('rectangular window');
hk
%
% plot impulse response
subplot(111);
plot(hk)
title('Original impulse response');
figure;
%
% Get frequency response and plot it
z=-pi:pi/400:pi;
w=z/(2*pi*T); % convert to Hz
den1=[1 zeros(1,N0-1)];
H=freqz(hk, den1, z);
Fm=20*log10(abs(H));
Fp=180/pi*unwrap(angle(H));
%Fr=fft(hk, n); %Using FFT
%[Fp, Fm] = cart2pol( real(Fr), imag(Fr));
%Fp=unwrap(Fp)*180/pi;
%Fm=20*log10(Fm);
%w=F0*index;
subplot(211);
plot(w, Fm)
title('Spectra of filter with rect window')
subplot(212);
plot(w, Fp)
figure;
%
%
% Now window with Bartlett (triangular) window
wind=triang(N0);
wind=wind'; % make a row vector
h1=wind.*hk;
subplot(111);
plot(h1)
title('Bartlett window applied');
figure;
disp('Bartlett window')
h1
%
% Get frequency response and plot it
H=freqz(h1, den1, z);
Fm=20*log10(abs(H));
Fp=180/pi*unwrap(angle(H));
%Fr=fft(h1, n);
%[Fp, Fm] = cart2pol( real(Fr), imag(Fr));
%Fp=unwrap(Fp)*180/pi;
%Fm=20*log10(Fm);
%w=F0*index;
subplot(211);
plot(w, Fm)
title('Spectra of filter with Bartlett window')
subplot(212);
plot(w, Fp)
figure;
%
%
% Now window with Hanning window
wind=hanning(N0);
wind=wind'; % make a row vector
h2=wind.*hk;
subplot(111);
plot(h2)
title('Hanning window applied');
figure;
disp('Hanning window');
h2
%
% Get frequency response and plot it
H=freqz(h2, den1, z);
Fm=20*log10(abs(H));
Fp=180/pi*unwrap(angle(H));
%Fr=fft(h2, n);
%[Fp, Fm] = cart2pol( real(Fr), imag(Fr));
%Fp=unwrap(Fp)*180/pi;
%Fm=20*log10(Fm);
%w=F0*index;
subplot(211);
plot(w, Fm)
title('Spectra of filter with Hanning window')
subplot(212);
plot(w, Fp)
figure;
%
%
% Now window with Hamming window
wind=hamming(N0);
wind=wind'; % make a row vector
h3=wind.*hk;
subplot(111);
plot(h3)
title('Hamming window applied');
figure;
disp('Hamming window');
h3
%
% Get frequency response and plot it
H=freqz(h3, den1, z);
Fm=20*log10(abs(H));
Fp=180/pi*unwrap(angle(H));
%Fr=fft(h3, n);
%[Fp, Fm] = cart2pol( real(Fr), imag(Fr));
%Fp=unwrap(Fp)*180/pi;
%Fm=20*log10(Fm);
%w=F0*index;
subplot(211);
plot(w, Fm)
title('Spectra of filter with Hamming window')
subplot(212);
plot(w, Fp)
