Sie sind auf Seite 1von 7

# Matlab Code For FIR Digital Filter Design

(Rectangular,Hanning,Hamming Window)

By
Vishal Mishra
111ec0179

## N=input('Enter Length of Window: ');

T=(N-1)/2;
disp('1 for high pass');
disp('2 for low pass');
disp('3 for band pass');
disp('4 for band reject');
disp('Enter the filtertype :');
filtertype=input('filtertype ');
switch(filtertype)
case 1
%Rectangular Window High-Pass
wc=input('Enter Cut-off Frequency(wc): ');
s=0;
w=0:0.001:pi;
for n=1:N
if ((n-1)==T)
hd(n)=1-(wc/pi);
else
hd(n)=((1/(pi*((n-1)-T)))*(-sin(((n-1)-T)*wc)));
end
end
for n=1:((N-1)/2)
s=s+hd((((N+1)/2)-n))*cos(n*w);
end
hr=hd((N+1)/2)+2*s;
figure;
plot(w,hr,'b');
%Hamming Window High-Pass
s=0;
w=0:0.001:pi;
for n=1:N
if ((n-1)==T)

hd(n)=1-(wc/pi);
else
hd(n)=((1/(pi*((n-1)-T)))*(-sin(((n-1)-T)*wc)));
end
end
for k=1:N
ham(k)=0.54-(0.46*(cos((2*pi*(k-1))/(N-1))));
end
for k=1:N
h(k)=hd(k)*ham(k);
end
for n=1:((N-1)/2)
s=s+h((((N+1)/2)-n))*cos(n*w);
end
hr=h((N+1)/2)+2*s;
hold on;
plot(w,hr,'r');
%Hanning Window High-Pass
s=0;
w=0:0.001:pi;
for n=1:N
if ((n-1)==T)
hd(n)=1-(wc/pi);
else
hd(n)=((1/(pi*((n-1)-T)))*(-sin(((n-1)-T)*wc)));
end
end
for k=1:N
han(k)=0.5*(1-(cos((2*pi*(k-1))/(N-1))));
end
for k=1:N
h(k)=hd(k)*han(k);
end
for n=1:((N-1)/2)
s=s+h((((N+1)/2)-n))*cos(n*w);
end
hr=h((N+1)/2)+2*s;
hold on;
plot(w,hr,'g');
legend('Rectangular','Hamming','Hanning');
title('High Pass Filter Design');
ylabel('Magnitude');
xlabel('Normalized Fequency');
case 2
%Rectangular Window Low-Pass

## wc=input('Enter Cut-off Frequency(wc): ');

s=0;
for n=1:N
if ((n-1)==T)
hd(n)=wc/pi;
else
hd(n)=((1/(pi*((n-1)-T)))*(sin(((n-1)-T)*wc)));
end
end
for n=1:((N-1)/2)
s=s+(hd((((N+1)/2)-n))*cos(n*w));
end
hr=hd((N+1)/2)+2*s;
figure;
plot(w,hr);
%Hamming Window Low-Pass
s=0;
for n=1:N
if ((n-1)==T)
hd(n)=wc/pi;
else
hd(n)=((1/(pi*((n-1)-T)))*(sin(((n-1)-T)*wc)));
end
end
for k=1:N
ham(k)=0.54-(0.46*(cos((2*pi*(k-1))/(N-1))));
end
for k=1:N
h(k)=hd(k)*ham(k);
end
for n=1:((N-1)/2)
s=s+h((((N+1)/2)-n))*cos(n*w);
end
hr=h((N+1)/2)+2*s;
hold on;
plot(w,hr,'r');
%Hanning Window Low-Pass
s=0;
for n=1:N
if ((n-1)==T)
hd(n)=wc/pi;
else
hd(n)=((1/(pi*((n-1)-T)))*(sin(((n-1)-T)*wc)));
end
end

for k=1:N
han(k)=0.5*(1-(cos((2*pi*(k-1))/(N-1))));
end
for k=1:N
h(k)=hd(k)*han(k);
end
for n=1:((N-1)/2)
s=s+h((((N+1)/2)-n))*cos(n*w);
end
hr=h((N+1)/2)+2*s;
hold on;
plot(w,hr,'g');
legend('Rectangular','Hamming','Hanning');
title('Low Pass Filter Design');
ylabel('Magnitude');
xlabel('Normalized Fequency');
case 3
wc1=input('Enter Lower Cut-off Frequency(wc1): ');
wc2=input('Enter Higher Cut-off Frequency(wc2): ');
%Rectangular Window Band-Pass
s=0;
for n=1:N
if ((n-1)==T)
hd(n)=(wc2-wc1)/pi;
else
hd(n)=((1/(pi*((n-1)-T)))*((sin(((n-1)-T)*wc2))(sin(((n-1)-T)*wc1))));
end
end
for n=1:((N-1)/2)
s=s+(hd((((N+1)/2)-n))*cos(n*w));
end
hr=hd((N+1)/2)+2*s;
figure;
plot(w,hr);
%Hamming window Band-Pass
s=0;
for n=1:N
if ((n-1)==T)
hd(n)=(wc2-wc1)/pi;
else
hd(n)=((1/(pi*((n-1)-T)))*((sin(((n-1)-T)*wc2))(sin(((n-1)-T)*wc1))));
end
end

for k=1:N
ham(k)=0.54-(0.46*(cos((2*pi*(k-1))/(N-1))));
end
for k=1:N
h(k)=hd(k)*ham(k);
end
for n=1:((N-1)/2)
s=s+h((((N+1)/2)-n))*cos(n*w);
end
hr=h((N+1)/2)+2*s;
hold on;
plot(w,hr,'r');
%Hanning window Band-Pass
s=0;
for n=1:N
if ((n-1)==T)
hd(n)=(wc2-wc1)/pi;
else
hd(n)=((1/(pi*((n-1)-T)))*((sin(((n-1)-T)*wc2))(sin(((n-1)-T)*wc1))));
end
end
for k=1:N
han(k)=0.5*(1-(cos((2*pi*(k-1))/(N-1))));
end
for k=1:N
h(k)=hd(k)*han(k);
end
for n=1:((N-1)/2)
s=s+h((((N+1)/2)-n))*cos(n*w);
end
hr=h((N+1)/2)+2*s;
hold on;
plot(w,hr,'g');
legend('Rectangular','Hamming','Hanning');
title('Band Pass Filter Design');
ylabel('Magnitude');
xlabel('Normalized Fequency');
case 4
%Rectangular Window Band-Reject
wc1=input('Enter Lower Cut-off Frequency(wc1): ');
wc2=input('Enter Higher Cut-off Frequency(wc2): ');
s=0;
for n=1:N
if ((n-1)==T)

hd(n)=(pi-wc2+wc1)/pi;
else
hd(n)=((1/(pi*((n-1)-T)))*((sin(((n-1)-T)*wc1))(sin(((n-1)-T)*wc2))));
end
end
for n=1:((N-1)/2)
s=s+(hd((((N+1)/2)-n))*cos(n*w));
end
hr=hd((N+1)/2)+2*s;
figure;
plot(w,hr);
%Hamming Window Band-Reject
s=0;
for n=1:N
if ((n-1)==T)
hd(n)=(pi-wc2+wc1)/pi;
else
hd(n)=((1/(pi*((n-1)-T)))*((sin(((n-1)-T)*wc1))(sin(((n-1)-T)*wc2))));
end
end
for k=1:N
ham(k)=0.54-(0.46*(cos((2*pi*(k-1))/(N-1))));
end
for k=1:N
h(k)=hd(k)*ham(k);
end
for n=1:((N-1)/2)
s=s+h((((N+1)/2)-n))*cos(n*w);
end
hr=h((N+1)/2)+2*s;
hold on;
plot(w,hr,'r');
%Hanning Window Band-Reject
s=0;
for n=1:N
if ((n-1)==T)
hd(n)=(pi-wc2+wc1)/pi;
else
hd(n)=((1/(pi*((n-1)-T)))*((sin(((n-1)-T)*wc1))(sin(((n-1)-T)*wc2))));
end
end
for k=1:N

ham(k)=0.5*(1-(cos((2*pi*(k-1))/(N-1))));
end
for k=1:N
h(k)=hd(k)*ham(k);
end
for n=1:((N-1)/2)
s=s+h((((N+1)/2)-n))*cos(n*w);
end
hr=h((N+1)/2)+2*s;
hold on;
plot(w,hr,'g');
legend('Rectangular','Hamming','Hanning');
title('Band Reject Filter Design');
ylabel('Magnitude');
xlabel('Normalized Fequency');
otherwise
disp('Error');
end