0% found this document useful (0 votes)
80 views15 pages

Design of HPF FIR Filter Using Hanning Window

The document discusses sampling rate and the sampling theorem through MATLAB simulations. It demonstrates oversampling, Nyquist rate sampling, and undersampling of a test signal consisting of three frequencies. For each case, it shows the original, sampled, and reconstructed signals, and calculates the maximum error between the original and reconstructed signals. Oversampling has the lowest error, while undersampling has the highest error due to aliasing from violating the Nyquist rate.

Uploaded by

rjsmith123
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views15 pages

Design of HPF FIR Filter Using Hanning Window

The document discusses sampling rate and the sampling theorem through MATLAB simulations. It demonstrates oversampling, Nyquist rate sampling, and undersampling of a test signal consisting of three frequencies. For each case, it shows the original, sampled, and reconstructed signals, and calculates the maximum error between the original and reconstructed signals. Oversampling has the lowest error, while undersampling has the highest error due to aliasing from violating the Nyquist rate.

Uploaded by

rjsmith123
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 15

Design of HPF FIR Filter Using Hanning Window

clc;
wc=0.5*pi;
w=0:pi/4:pi;
N=9;
n=-(N-1)/2:1:(N-1)/2;
hd=[sin(pi*(n+eps))-sin((pi*(n+eps))/2)]./(pi*(n+eps));
disp('hd=');
disp(hd);
wh=hann(N);
disp('wh');
disp(wh);
hn=hd.*wh';
disp('hn');
disp(hn);
H=freqz(hn,1,w);
subplot(2,2,3);
plot(w,H);
xlabel('w');
ylabel('mag');
mag=0.5+.0308*cos(3*w)-0.5424*cos(w);
disp('magnitude');
disp(mag);
subplot(2,2,1);
plot(w,mag);
title('magnitude response');
xlabel('w');
ylabel('mag');
ang=-4*w;
disp('phase');
disp(ang);
subplot(2,2,2);
plot(w,ang);
title('phase response');
xlabel('w');
ylabel('phase');
Output
hd=
Columns 1 through 8
-0.0000 0.1061 -0.0000 -0.3183 0.5000 -0.3183 -0.0000 0.1061
Column 9
-0.0000
wh
0 0.1464 0.5000 0.8536 1.0000 0.8536 0.5000 0.1464 0
hn
Columns 1 through 8
0 0.0155 -0.0000 -0.2717 0.5000 -0.2717 -0.0000 0.0155
Column 9
0
magnitude
-0.0116 0.0947 0.5000 0.9053 1.0116
phase
0 -3.1416 -6.2832 -9.4248 -12.5664
Design of HPF FIR Filter Using Blackman Window

clc;
wc=0.5*pi;
w=0:pi/4:pi;
N=9;
n=-(N-1)/2:1:(N-1)/2;
hd=[sin(pi*(n+eps))-sin((pi*(n+eps))/2)]./(pi*(n+eps));
disp('hd=');
disp(hd);
wh=blackman(N);
disp('wh');
disp(wh);
hn=hd.*wh';
disp('hn');
disp(hn);
H=freqz(hn,1,w);
subplot(2,2,3);
plot(w,H);
xlabel('w');
ylabel('mag');
mag=0.5+0.0128*cos(3*w)-0.4919*cos(w);
disp('magnitude');
disp(mag);
subplot(2,2,1);
plot(w,mag);
title('magnitude response');
xlabel('w');
ylabel('mag');
ang=-4*w;
disp('phase');
disp(ang);
subplot(2,2,2);
plot(w,ang);
title('phase response');
xlabel('w');
ylabel('phase');
Output
hd=
Columns 1 through 8
-0.0000 0.1061 -0.0000 -0.3183 0.5000 -0.3183 -0.0000 0.1061
Column 9
-0.0000
wh
-0.0000 0.0664 0.3400 0.7736 1.0000 0.7736 0.3400 0.0664 -0.0000
hn
Columns 1 through 8
0.0000 0.0071 -0.0000 -0.2462 0.5000 -0.2462 -0.0000 0.0071
Column 9
0.0000
magnitude
0.0212 0.1433 0.5000 0.8567 0.9788
phase
0 -3.1416 -6.2832 -9.4248 -12.5664
Design of HPF FIR Filter Using Hamming Window

clc;
wc=0.5*pi;
w=0:pi/4:pi;
N=9;
n=-(N-1)/2:1:(N-1)/2;
hd=[sin(pi*(n+eps))-sin((pi*(n+eps))/2)]./(pi*(n+eps));
disp('hd=');
disp(hd);
wh=hamming(N);
disp('wh');
disp(wh);
hn=hd.*wh';
disp('hn');
disp(hn);
H=freqz(hn,1,w);
subplot(2,2,3);
plot(w,H);
xlabel('w');
ylabel('mag');
mag=0.5+0.04*cos(3*w)-0.54*cos(w);
disp('magnitude');
disp(mag);
subplot(2,2,1);
plot(w,mag);
title('magnitude response');
xlabel('w');
ylabel('mag');
ang=-4*w;
disp('phase');
disp(ang);
subplot(2,2,2);
plot(w,ang);
title('phase response');
xlabel('w');
ylabel('phase');
Output
hd=
-0.0000 0.1061 -0.0000 -0.3183 0.5000 -0.3183 -0.0000 0.1061 -0.0000
wh
0.0800 0.2147 0.5400 0.8653 1.0000 0.8653 0.5400 0.2147 0.0800
hn
-0.0000 0.0228 -0.0000 -0.2754 0.5000 -0.2754 -0.0000 0.0228 -0.0000
magnitude
0 0.0899 0.5000 0.9101 1.0000

phase
0 -3.1416 -6.2832 -9.4248 -12.5664
Design of HPF FIR Filter Using Rectangular Window

clc;
wc=0.5*pi;
w=0:pi/4:pi;
N=9;
n=-(N-1)/2:1:(N-1)/2;
hd=[sin(pi*(n+eps))-sin((pi*(n+eps))/2)]./(pi*(n+eps));
disp('hd=');
disp(hd);
wh=boxcar(N);
disp('wh');
disp(wh);
hn=hd.*wh';
disp('hn');
disp(hn);
H=freqz(hn,1,w);
subplot(2,2,3);
plot(w,H);
xlabel('w');
ylabel('mag');
mag=0.5+2*[0.106*cos(3*w)-0.318*cos(w)];
disp('magnitude');
disp(mag);
subplot(2,2,1);
plot(w,mag);
title('magnitude response');
xlabel('w');
ylabel('mag');
ang=-4*w;
disp('phase');
disp(ang);
subplot(2,2,2);
plot(w,ang);
title('phase response');
xlabel('w');
ylabel('phase');
OUTPUT:
hd=
Columns 1 through 8
-0.0000 0.1061 -0.0000 -0.3183 0.5000 -0.3183 -0.0000 0.1061
Column 9
-0.0000
wh 1 1 1 1 1 1 1 1 1
hn
Columns 1 through 8
-0.0000 0.1061 -0.0000 -0.3183 0.5000 -0.3183 -0.0000 0.1061
Column 9
-0.0000
magnitude
0.0760 -0.0996 0.5000 1.0996 0.9240
phase
0 -3.1416 -6.2832 -9.4248 -12.5664

FIGURE:
SAMPLING RATE

% program for down sampling by M

N=50;
M=2;
n=0:1:N-1;
fs=10;
m=0:1:N*M-1;
x=2*sin((2*pi*m)/fs);
k=1:M:length(x);
yd=x(k);
figure(1);
subplot(2,1,1);
stem(n,x(1:N));
subplot(2,1,2);
stem(n,yd);

%up sampling by L

L=2;
x=sin((2*pi*n)/fs);
ye=zeros(1,L*length(x));
ye([1:L:length(ye)])=x;
figure(2);
subplot(2,1,1);stem(n,x);
subplot(2,1,2);
stem(n,ye(1:length(x)));

%Spectrum of the decimator


F=[0 .25 .4 .5 .6 .75 1];
A=[0 1 0 0 0 1 0];N=101;
x=fir2(N,F,A);disp(x);
[Xz,w]=freqz(x,1,1001);
figure(3);subplot(2,1,1);plot(w/pi,abs(Xz));
title('spectrum of input');
xlabel('normalized frequency');ylabel('amplitude');
M=2;yd=x([1:M:length(x)]);
[Yz1,w]=freqz(yd,1,1024);
figure(3);subplot(2,1,2);plot(w/pi,abs(Yz1));xlabel('normalized frequency');ylabel('amplitude');
title('spectrum of decimated signal')

%spectrum of interpolator

F=[0 .25 .5 .75 1];N=101;


A=[0 0 1 0 0];
x=fir2(N,F,A);
[Xz,w]=freqz(x,1,101);
figure(4);subplot(2,1,1);plot(w/pi,abs(Xz));
%Up sampling
L=5;%fs=20; x=sin(2*pi*n/fs);
ye=zeros(1,L*length(x));
ye([1:L:length(ye)])=x;
%subplot(2,1,2);stem(n,ye(1:length(x)));
[Yz,w]=freqz(ye,1,1024);
figure(4);subplot(2,1,2);plot(w/pi,abs(Yz));

Output:
DOWN SAMPLING
Upsampling:
Sampling Theorem
%over sampling

t=0:0.1:1;xt=(2*sin(2*pi*t))+(4*cos(2*pi*3*t))+(sin(2*pi*4*t));
subplot(3,4,1);plot(t,xt);
title('actual signal');
xlabel('time');ylabel('amplitude');
fs=12;ts=1/fs;n=0:1:20;
xn=(2*sin(2*pi*n*ts))+(4*cos(2*pi*3*n*ts))+(sin(2*pi*4*n*ts));
subplot(3,4,2);stem(n,xn);
title('sampled signal');
xlabel('n');ylabel('amplitude');
xr=xn*(sinc(fs*((ones(length(n),1)*t)-((n*ts)'*(ones(1,length(t)))))));
subplot(3,4,3);plot(t,xr);
title('reconstructed signal');xlabel('time');
ylabel('amplitude');
xr1=spline((n*ts),xn,t);
subplot(3,4,4);plot(t,xr1);
title('spline function');
xlabel('time');ylabel('amplitude');
e=max(abs(xt-xr));
disp('over sampling');disp(e);

%Nyquist rate sampling

t=0:0.1:1;xt=(2*sin(2*pi*t))+(4*cos(2*pi*3*t))+(sin(2*pi*4*t));
subplot(3,4,5);plot(t,xt);
title('actual signal');
xlabel('time');ylabel('amplitude');
fs=8;ts=1/fs;n=0:1:20;
xn=(2*sin(2*pi*n*ts))+(4*cos(2*pi*3*n*ts))+(sin(2*pi*4*n*ts));
subplot(3,4,6);stem(n,xn);
title('sampled signal');
xlabel('n');ylabel('amplitude');
xr=xn*(sinc(fs*((ones(length(n),1)*t)-((n*ts)'*(ones(1,length(t)))))));
subplot(3,4,7);plot(t,xr);
title('reconstructed signal');xlabel('time');
ylabel('amplitude');
xr1=spline((n*ts),xn,t);
subplot(3,4,8);plot(t,xr1);
title('spline function');
xlabel('time');ylabel('amplitude');
e=max(abs(xt-xr));
disp('Nyquist rate');disp(e);
%Undersampling

t=0:0.1:1;xt=(2*sin(2*pi*t))+(4*cos(2*pi*3*t))+(sin(2*pi*4*t));
subplot(3,4,9);plot(t,xt);
title('actual signal');
xlabel('time');ylabel('amplitude');
fs=5;ts=1/fs;n=0:1:20;
xn=(2*sin(2*pi*n*ts))+(4*cos(2*pi*3*n*ts))+(sin(2*pi*4*n*ts));
subplot(3,4,10);stem(n,xn);
title('sampled signal');
xlabel('n');ylabel('amplitude');
xr=xn*(sinc(fs*((ones(length(n),1)*t)-((n*ts)'*(ones(1,length(t)))))));
subplot(3,4,11);plot(t,xr);
title('reconstructed signal');xlabel('time');
ylabel('amplitude');
xr1=spline((n*ts),xn,t);
subplot(3,4,12);plot(t,xr1);
title('spline function');
xlabel('time');ylabel('amplitude');
e=max(abs(xt-xr));
disp('Undersampling');disp(e);

Output
over sampling
0.0426
Nyquist rate
0.9654
Undersampling
8.1912
Figure:

You might also like