You are on page 1of 31

www.Vidyarthiplus.

in (VP)

www.vidyarthiplus.in

Thanks to:
Ganesh Murthy

Document name: DSP Lab Us


Using MATLAB

Powered by VidyarthiPlus.in

Copywriter Ganesh Murthy (VP Group)

www.Vidyarthiplus.in (VP)

DESIGN OF IIR FILTER BUTTERWORTH


clc
wp=input('enter the pass band edge freq.in r/sec=');
ap=input('enter the pass band riple in db=');
ws=input('enter the stop band edge freq.in r/sec=');
as=input('enter the stop band ripple in db=');
Fs=input('enter the sampling freq=');
[n,Wn]=buttord(wp,ws,ap,as,'s');
disp('the order of filter is n=');
disp(n);
[num,den]=butter(n,Wn,'s');
[b,a]=bilinear(num,den,Fs)
freqz(b,a,512,Fs);
grid on;
xlabel('freq in Hz');
ylabel('Gain in db');
title('Frequency response of the filter');

Powered by VidyarthiPlus.in

Copywriter Ganesh Murthy (VP Group)

www.Vidyarthiplus.in (VP)

OUTPUT FOR BUTTERWORTH:


enter the pass band edge freq.in r/sec=0.2*pi
enter the pass band riple in db=0.6*pi
enter the stop band edge freq.in r/sec=1.6
enter the stop band ripple in db=11.8
enter the sampling freq=1
the order of filter is n=
2

b=
0.0971

0.1941

0.0971

1.0000 -0.9463

0.3346

a=

Powered by VidyarthiPlus.in

Copywriter Ganesh Murthy (VP Group)

www.Vidyarthiplus.in (VP)

DESIGN OF IIR FILTER USING IMPULSE INVARIANCE


wp=input('enter the pass band frequency in rad');
ws=input('enter the stop band frequency in rad');
rp=input('enter the pass band frequency in ripple');
as=input('enter the stop band frequency in ripple');
T=input('enter the time period');
Fs=1/T;
omegap=(2/T)*tan(wp/2);
omegas=(2/T)*tan(ws/2);
[cs,ds]=afd_butt(omegap,omegas,rp,as)
[b,a]=bilinear(cs,ds,Fs);
[cs,ds]=afd_butt(omegap,omegas,rp,as)
func(b,a)=afd_butt(omegap,omegas,rp,as)
if omegas<=0
error('pass band edge must be larger then pass band edge');
end
if omegas<=omegap
error('stop band edge must be larger then pass band edge');
end
if(rp<=0)|(as<0)
error('pass band ripple and pass band attenuation must be larger then zero');
end
a=(10^(0.1*as)-1)/(10^(0.1*rp)-1);
b=sqart(a);
c=log10(b);
d=omegas/omegap;
e=log10(d);
N=ceil(c/e);
fprintf('\n butterworth filter order=%2f\n':N);
omegac=omegap/(((10^(0.1*rp))-1)^(1/(2*N)));
fprintf('\n omegac=%2f\n',omegac);
[b,a]=u_buttapl(N,omegac);
[z,P,k]=buttap(n);
P=P*omegac;
k=k*omagac^N;
b=real(poly(z));
b0=k;
b=k*b;
a=real(poly(p));

Powered by VidyarthiPlus.in

Copywriter Ganesh Murthy (VP Group)

www.Vidyarthiplus.in (VP)

OUT PUT FOR IMPULSE INVARIANCE


enter the pass band edge freq.in r/sec=0.2*pi
enter the pass band riple in db=0.6*pi
enter the stop band edge freq.in r/sec=1.6
enter the stop band ripple in db=11.8
enter the sampling freq=1
the order of filter is n=
2

b=
0.0971

0.1941

0.0971

1.0000 -0.9463

0.3346

a=

Powered by VidyarthiPlus.in

Copywriter Ganesh Murthy (VP Group)

www.Vidyarthiplus.in (VP)

STABILITY OF LTI SYSTEM:


clc
b=[1 2];
a=[3 4 5];
w=0:0.01:pi/2;
H=freqz(b,a,w);
subplot(3,1,1);
plot(w,H);
xlabel('w');
ylabel('Frequency');
tf(b,a)
A=abs(H);
subplot(3,1,2);
plot(w,A);
xlabel('W');
ylabel('magnitude');
B=angle(H);
subplot(3,1,3);
plot(w,B);
xlabel('w');
ylabel('phase');
% FREQUENCY RESPONSE
b=[1 2];
a=[3 4 5];
w=0:0.01:pi/2;
freqz(b,a,w);
tf(b,a)

Powered by VidyarthiPlus.in

Copywriter Ganesh Murthy (VP Group)

www.Vidyarthiplus.in (VP)

OUTPUT FOR STABILITY OF LTI SYSTEM:

Transfer function for time response:


s+2
--------------3 s^2 + 4 s + 5

GRAPH FOR TIME RESPONSE:

Transfer function for frequency response:


s+2
--------------3 s^2 + 4 s + 5

Powered by VidyarthiPlus.in

Copywriter Ganesh Murthy (VP Group)

www.Vidyarthiplus.in (VP)

GRAPH FOR FREQUENCY RESPONSE:

Powered by VidyarthiPlus.in

Copywriter Ganesh Murthy (VP Group)

www.Vidyarthiplus.in (VP)

REPRESENTATION OF BASIC SIGNALS:


% sine wave
t=0:0.01:1;
a=2;
b=a*sin(2*pi*2*t);
subplot(3,3,1);
stem(t,b);
xlabel('time');
ylabel('Amplitude');
title ('sinewave');
% Cosine wave
t=0:0.01:1;
a=2;
b=a*cos(2*pi*2*t);
subplot(3,3,2);
stem(t,b);
xlabel('time');
ylabel('Amplitude');
title ('Cos wave');
% Square wave
t=0:0.01:1;
a=2;
b=a*square(2*pi*2*t);
subplot(3,3,3);
stem(t,b);
xlabel('time');
ylabel('Amplitude');
title ('square wave');
% Exponential waveform
t=0:0.01:1;
a=2;
b=a*exp(2*pi*2*t);
subplot(3,3,4);
stem(t,b);
xlabel('time');
ylabel('Amplitude');
title ('exponential wave');
%sawtooth
t=0:0.01:1;
a=2;
b=a*sawtooth(2*pi*2*t);
Powered by VidyarthiPlus.in

Copywriter Ganesh Murthy (VP Group)

www.Vidyarthiplus.in (VP)

subplot(3,3,5);
stem(t,b);
xlabel('time');
ylabel('Amplitude');
title ('sawtooth wave');
% unit step signal
n=-5:5;
a = [zeros(1,5),ones(1,6)];
subplot(3,3,6);
stem(n,a);
Xlabel ('time');
Ylabel ('amplitude');
title('Unit step');
% unit impulse
n=-5:5;
a = [zeros(1,5),ones(1,1),zeros(1,5)];
subplot(3,3,7);
stem(n,a);
Xlabel ('time');
Ylabel ('amplitude');
title('Unit impulse');

Powered by VidyarthiPlus.in

Copywriter Ganesh Murthy (VP Group)

www.Vidyarthiplus.in (VP)

OUTPUT FOR REPRESENTATION OF BASIC SIGNALS:

Powered by VidyarthiPlus.in

Copywriter Ganesh Murthy (VP Group)

www.Vidyarthiplus.in (VP)

COMPUTATION OF CIRCULAR CONVOLUTION:


clc;
clear all;
close all;
x=input('enter the sequence');
h=input('enter the imp response');
% no of samples in x
n1=length(x);
n2=length(h);
n3=n1+n2-1;
n=max(n1,n2);
if(n3>=0)
h=[h,zeros(1,n3)];
else
x=[n,zeros(1,-n3)];
end;
for a =1:n
y(a)=0;
for i=1:n
j=a-i+1;
if(j<=0)
j=n+j;
end;
y(a)=y(a)+[x(i)*h(j)];
end;
end;
t=1:n
stem(t,y);
disp(y);
title('circular convolution');
xlabel('samples');
ylabel('amplitude');

Powered by VidyarthiPlus.in

Copywriter Ganesh Murthy (VP Group)

www.Vidyarthiplus.in (VP)

OUTPUT FOR CIRCULAR CONVOLUTION:


enter the sequence[1 2 2 1]
enter the imp response[1 1 1]
t=
1

GRAPH FOR CIRCULAR CONVOULTION:

Powered by VidyarthiPlus.in

Copywriter Ganesh Murthy (VP Group)

www.Vidyarthiplus.in (VP)

CALCULATION OF FFT:
function[Xk]=dft(x,N);
x=[1 1 1 1 4 5 6 1];
N=8;
n=[0:1:N-1];
k=[0:1:N-1];
a=(-i*2*pi/N);
WN=exp(a);
nk=n'*k;
WNnk=WN.^nk;
Xk=x*WNnk

Powered by VidyarthiPlus.in

Copywriter Ganesh Murthy (VP Group)

www.Vidyarthiplus.in (VP)

OUTPUT FOR CALCULATION OF FFT:


Xk =
Columns 1 through 4
20.0000

-5.8284 + 7.8284i -2.0000 - 4.0000i -0.1716 - 2.1716i

Columns 5 through 8
4.0000 + 0.0000i -0.1716 + 2.1716i -2.0000 + 4.0000i -5.8284 - 7.8284i

ans =
Columns 1 through 4
20.0000

-5.8284 + 7.8284i -2.0000 - 4.0000i -0.1716 - 2.1716i

Columns 5 through 8
4.0000 + 0.0000i -0.1716 + 2.1716i -2.0000 + 4.0000i -5.8284 - 7.8284i

Powered by VidyarthiPlus.in

Copywriter Ganesh Murthy (VP Group)

www.Vidyarthiplus.in (VP)

VERIFICATION OF SAMPLING THEOREM:


clear all
t=-100:01:100;
fm=0.02;
x=cos(2*pi*t*fm);
subplot(2,2,1);
plot(t,x);
xlabel('time in sec');
ylabel('x(t)');
title('continuous time signal');
fs1=0.02;
n=-2:2;
x1=cos(2*pi*fm*n/fs1);
subplot(2,2,2);
stem(n,x1);
hold on
subplot(2,2,2);
plot(n,x1,':');
title('discrete time signal x(n) with fs<2fm');
xlabel('n');
ylabel('x(n)');
fs2=0.04;
n1=-4:4;
x2=cos(2*pi*fm*n1/fs2);
subplot(2,2,3);
stem(n1,x2);
hold on
subplot(2,2,3);
plot(n1,x2,':');
title('discrete time signal x(n) with fs>2fm');
xlabel('n');
ylabel('x(n)');
n2=-50:50;
fs3=0.5;
x3=cos(2*pi*fm*n2/fs3);
subplot(2,2,4);
stem(n2,x3);
hold on
subplot(2,2,4);
plot(n2,x3,':');
xlabel('n');
ylabel('x(n)');
title('discrete time signal x(n) with fs=2fm');

Powered by VidyarthiPlus.in

Copywriter Ganesh Murthy (VP Group)

www.Vidyarthiplus.in (VP)

OUTPUT OF SAMPLING THEOREM:

Powered by VidyarthiPlus.in

Copywriter Ganesh Murthy (VP Group)

www.Vidyarthiplus.in (VP)

DESIGN OF FIR FILTER:


LOW PASS FILTER:
clc;
clear all;
wc=input('enter the value of cut off frequency');
N=input('enter the value of filter');
alpha=(N-1)/2;
eps=0.001;
%Rectangular Window
n=0:1:N-1;
hd=sin(wc*(n-alpha+eps))./(pi*(n-alpha+eps));
hn=hd
w=0:0.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h));
hold on
%Hamming Window
n=0:1:N-1;
wh=0.54-0.46*cos((2*pi*n)/(N-1));
hn=hd.*wh
w=0:0.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'ms');
hold off;
hold on
%Hanning Window
n=0:1:N-1;
wh=0.5-0.5*cos((2*pi*n)/(N-1));
hn=hd.*wh
w=0:0.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'blue');
hold off;
hold on
%Blackman Window
n=0:1:N-1;
wh=0.42-0.5*cos((2*pi*n)/(N-1))+0.08*cos((4*pi*n)/(N-1));
hn=hd.*wh
w=0:0.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'green');
hold off;

Powered by VidyarthiPlus.in

Copywriter Ganesh Murthy (VP Group)

www.Vidyarthiplus.in (VP)

OUTPUT FOR LOW PASS FILTER:


enter the value of cut off frequency1.6
enter the value of filter11
hn =
Columns 1 through 8
0.0630

0.0092 -0.1057 -0.0090

0.3185

0.5093

0.3178 -0.0095

0.2905

0.5093

0.2899 -0.0065

Columns 9 through 11
-0.1056

0.0094

0.0630

hn =
Columns 1 through 8
0.0050

0.0015 -0.0421 -0.0062

Columns 9 through 11
-0.0420

0.0016

0.0050

hn =
Columns 1 through 8
0

0.0009 -0.0365 -0.0059

0.2881

0.5093

0.2875 -0.0062

Columns 9 through 11
-0.0365

0.0009

hn =
Columns 1 through 8
-0.0000

0.0004 -0.0212 -0.0046

0.2705

0.5093

0.2699 -0.0049

Columns 9 through 11
-0.0212

0.0004 -0.0000

Powered by VidyarthiPlus.in

Copywriter Ganesh Murthy (VP Group)

www.Vidyarthiplus.in (VP)

GRAPH FOR LPF:

Powered by VidyarthiPlus.in

Copywriter Ganesh Murthy (VP Group)

www.Vidyarthiplus.in (VP)

DESIGN FOR HIGH PASS FILTER:


clc;
clear all;
wc=input('enter the value of cut off frequency');
N=input('enter the value of filter');
alpha=(N-1)/2;
eps=0.001;
%Rectangular Window
n=0:1:N-1;
hd=(sin(pi*(n-alpha+eps))-sin((n-alpha+eps)*wc))./(pi*(n-alpha+eps));
hn=hd
w=0:0.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h));
hold on
%Hamming Window
n=0:1:N-1;
wh=0.54-0.46*cos((2*pi*n)/(N-1));
hn=hd.*wh
w=0:0.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'ms');
hold off;
hold on
%Hanning Window
n=0:1:N-1;
wh=0.5-0.5*cos((2*pi*n)/(N-1));
hn=hd.*wh
w=0:0.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'blue');
hold off;
hold on
%Blackman Window
n=0:1:N-1;
wh=0.42-0.5*cos((2*pi*n)/(N-1))-0.08*cos((4*pi*n)/(N-1));
hn=hd.*wh
w=0:0.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'green');
hold off;

Powered by VidyarthiPlus.in

Copywriter Ganesh Murthy (VP Group)

www.Vidyarthiplus.in (VP)

OUTPUT FOR HIGH PASS FILTER:


enter the value of cut off frequency1.6
enter the value of filter11
hn =
Columns 1 through 8
-0.0628 -0.0094

0.1061

0.0085 -0.3175

0.4907 -0.3188

0.0100

0.0058 -0.2896

0.4907 -0.2908

0.0068

Columns 9 through 11
0.1053 -0.0091 -0.0632
hn =
Columns 1 through 8
-0.0050 -0.0016

0.0422

Columns 9 through 11
0.0419 -0.0015 -0.0051

hn =
Columns 1 through 8
0 -0.0009

0.0366

0.0056 -0.2872

0.4907 -0.2884

0.0066

Columns 9 through 11
0.0364 -0.0009

hn =
Columns 1 through 8
0.0100

0.0001

0.0350

0.0055 -0.2539

0.4122 -0.2550

0.0064

Columns 9 through 11
0.0348

0.0001

0.0101

Powered by VidyarthiPlus.in

Copywriter Ganesh Murthy (VP Group)

www.Vidyarthiplus.in (VP)

GRAPH FOR HPF:

Powered by VidyarthiPlus.in

Copywriter Ganesh Murthy (VP Group)

www.Vidyarthiplus.in (VP)

DESIGN FOR BAND PASS FILTER:

clc;
Wc1=input('enter the value of Wc1=');
Wc2=input('enter the value of Wc2=');
N=input('enter the value of N=');
alpha=(N-1)/2;
eps=0.001;
%Rectangular Window
n=0:1:N-1;
hd=(sin(Wc1*(n-alpha+eps))-sin(Wc2*(n-alpha+eps)*pi))./((n-alpha+eps)*pi);
hn=hd
W=0:0.01:pi;
h=freqz(hn,1,W);
plot(W/pi,abs(h));
hold on;
%Hamming Window
n=0:1:N-1;
Wn=0.54-0.46*cos((2*pi*n)/(N-1));
hn=hd.*Wn
W=0:0.01:pi;
h=freqz(hn,1,W);
plot(W/pi,abs(h),'green');
hold on;
%Hanning Window
n=0:1:N-1;
Wn=0.5-0.5*cos((2*pi*n)/(N-1));
hn=hd.*Wn
W=0:0.01:pi;
h=freqz(hn,1,W);
plot(W/pi,abs(h),'red');
hold off;
%Blackman Window
n=0:1:N-1;
wh=042-0.5*cos((2*pi*n)/(N-1))-0.08*cos((4*pi*n)/(N-1));
hn=hd.*wh
w=0:0.01:pi;
h=freqz(hn,1,w);
plot(W/pi,abs(h),'green');
hold off;

Powered by VidyarthiPlus.in

Copywriter Ganesh Murthy (VP Group)

www.Vidyarthiplus.in (VP)

OUTPUT FOR BANG PASS FILTER:

enter the value of Wc1=0.95


enter the value of Wc2=1.4
enter the value of N=11

hn =

Columns 1 through 8
-0.0639

0.0272 -0.0314

0.0566

0.5616 -1.0976

0.5617

0.0575

0.0386

0.5123 -1.0976

0.5123

0.0393

Columns 9 through 11

-0.0323

0.0268 -0.0633

hn =

Columns 1 through 8

-0.0051

0.0046 -0.0125

Columns 9 through 11

-0.0129

0.0045 -0.0051

Powered by VidyarthiPlus.in

Copywriter Ganesh Murthy (VP Group)

www.Vidyarthiplus.in (VP)

hn =

Columns 1 through 8

0.0026 -0.0108

0.0370

0.5080 -1.0976

0.5081

0.0377

Columns 9 through 11

-0.0112

0.0026

hn =

Columns 1 through 8

-2.6472

1.1293 -1.3160

2.3884 23.8004 -46.5602 23.8044

2.4297

Columns 9 through 11

-1.3548

1.1148 -2.6228

Powered by VidyarthiPlus.in

Copywriter Ganesh Murthy (VP Group)

www.Vidyarthiplus.in (VP)

GRAPH FOR BAND PASS FILTER:

Powered by VidyarthiPlus.in

Copywriter Ganesh Murthy (VP Group)

www.Vidyarthiplus.in (VP)

DESIGN FOR BAND STOP FILTER:


clc;
Wc1=input('enter the value of Wc1=');
Wc2=input('enter the value of Wc2=');
N=input('enter the value of N=');
alpha=(N-1)/2;
eps=0.001;
%Rectangular Window
n=0:1:N-1;
hd=(sin(Wc1*(n-alpha+eps))-sin(Wc2*(n-alpha+eps)*pi))./((n-alpha+eps)*pi);
hn=hd
W=0:0.01:pi;
h=freqz(hn,1,W);
plot(W/pi,abs(h));
hold on;
%Hamming Window
n=0:1:N-1;
Wn=0.54-0.46*cos((2*pi*n)/(N-1));
hn=hd.*Wn
W=0:0.01:pi;
h=freqz(hn,1,W);
plot(W/pi,abs(h),'green');
hold on;
%Hanning Window
n=0:1:N-1;
Wn=0.5-0.5*cos((2*pi*n)/(N-1));
hn=hd.*Wn
W=0:0.01:pi;
h=freqz(hn,1,W);
plot(W/pi,abs(h),'red');
hold off;
%Blackman Window
n=0:1:N-1;
wh=0.42-0.5*cos((2*pi*n)/(N-1))-0.08*cos((4*pi*n)/(N-1));
hn=hd.*wh
w=0:0.01:pi;
h=freqz(hn,1,w);
plot(W/pi,abs(h),'green');
hold off;

Powered by VidyarthiPlus.in

Copywriter Ganesh Murthy (VP Group)

www.Vidyarthiplus.in (VP)

OUTPUT FOR BSF:


enter the value of Wc1=1.4
enter the value of Wc2=1.5
enter the value of N=11

hn =

Columns 1 through 8

0.1054 -0.0500 -0.1986

0.0528

0.6325 -1.0544

0.6314

0.0538

0.0360

0.5770 -1.0544

0.5760

0.0367

Columns 9 through 11

-0.1986 -0.0505

0.1055

hn =

Columns 1 through 8

0.0084 -0.0084 -0.0790

Columns 9 through 11

-0.0790 -0.0085

0.0084

Powered by VidyarthiPlus.in

Copywriter Ganesh Murthy (VP Group)

www.Vidyarthiplus.in (VP)

hn =

Columns 1 through 8

0 -0.0048 -0.0686

0.0346

0.5721 -1.0544

0.5711

0.0352

Columns 9 through 11

-0.0686 -0.0048

hn =

Columns 1 through 8

-0.0169

0.0005 -0.0656

0.0338

0.5059 -0.8857

0.5050

0.0344

Columns 9 through 11

-0.0656

0.0005 -0.0169

Powered by VidyarthiPlus.in

Copywriter Ganesh Murthy (VP Group)

www.Vidyarthiplus.in (VP)

GRAPH FOR BSF:

Powered by VidyarthiPlus.in

Copywriter Ganesh Murthy (VP Group)

You might also like