You are on page 1of 20

Lab 1

Sine Wave:
A=10;
O=pi/2;
W=(2*pi)/20;
T=0:0.5:100;
H=A*sin(W*T+O);
plot(T,H)
title('Sine Wave')
xlabel('Time in Sec')
ylabel('Amplitude')
grid
Exponential Wave:
a=.10;
T=0:0.5:10;
H=a*power(2.71,T);
plot(T,H)
title('Exponential Wave')
xlabel('Time in Sec')
ylabel('Amplitude')
grid

Decaying Exponential:
a=.5;
K=1.2;
t=0:0.1:10;
h=K*a.^t;
plot(t,h)
title('Decaying Exponential')
xlabel('Time in Sec')
ylabel('Amplitude')
grid

1
A Delayed sequence:
a=0:50;
z=zeros(1,5);
h=[z a];
subplot(2,1,1)
stem(a)
axis([0 50 0 50])
title('Sequence')
grid
subplot(2,1,2)
stem(h)
axis([0 55 0 55])
title('Delayed Sequence')
grid

Unit Impulse Function:


n=-10:10;
a=[zeros(1,10) 1 zeros(1,10)];
stem(n,a)
title('Unit Impulse')
grid

2
Lab 2
Convolving Two Signals:
1.
x=[1 3 5 7];
h=[9 5 2];
y=conv(x,h)
subplot(3,1,1)
stem(x)
title('Input')
grid
subplot(3,1,2)
stem(h)
title('Impulse Response')
grid
subplot(3,1,3)
stem(y)
title('Output')
grid
y=
9 32 62 94 45 14

Filtering an Input Signal:


2.
a=[1 -2];
b=[1 -6 -9];
input=[3 5 9 7];
y=filter(a,b,input)
subplot(2,1,1)
stem(input)
title('Input')
subplot(2,1,2)
stem(y)
title('Output')

3
3.
a=[0 6 -9];
b=[-5 9 -8 -7];
input=-pi:0.1:pi;
y=filter(a,b,input)
subplot(2,1,1)
stem(input)
subplot(2,1,2)
stem(y)

Lab 3
4
Channel Response
input=[3 -5 9 7]
chanl=[input*0.5]
subplot(2,1,1)
plot(input)
title('Input')
subplot(2,1,2)
plot(chanl)
title('Channel Response')
chanl =
1.5000 -2.5000 4.5000 3.5000

Sinc using linespace


x=linspace(-pi,pi);
y=sinc(x);
stem(x,y)
title('Sinc')
grid

Filtering:
a=[1];
b=[1 -0.7];
input=[1 0 0 0];
y=filter(a,b,input)
subplot(211)
stem(input)
title('Input')
subplot(212)
stem(y)
title('Filtered OutPut')

Filtering:

5
t=-10:10;
a=[1];
b=[1 -0.7];
input=[zeros(1,10) 1 zeros(1,10)];
y=filter(a,b,input)
subplot(211)
stem(t,input)
title('Input')
grid
subplot(212)
stem(t,y)
title('Output')

Freqz Command
t=-10:10;
a=[1];
b=[1 -0.7];
input=[zeros(1,10) 1 zeros(1,10)];
y=freqz(a,b,input)
subplot(211)
stem(t,input)
title('Input')
subplot(212)
stem(t,y)
title('Output')

ASSIGNMENT 1.

6
Write the code of discrete convolution.

Matlab code:
a=input('Enter 1st array: ');
b=input('Enter 2nd array: ');
bf=fliplr(b);
a1=length(a);
b1=length(b);
b2=b1-1;
j=1;
f=[zeros(1,length(a)+length(b)-1)];
for i=b2:-1:0
x1=[zeros(1,i) bf(1,j)*a zeros(1,b2-i)];
j=j+1;
f=f+x1;
end
f
subplot(3,1,1);
plot(a)
subplot(3,1,2);
plot(b)
subplot(3,1,3);
plot(f)

Figure:

7
Lab # 4
Drawing Poles and Zeros:
b=[7 -6 5]
a=[6 2]
[r,p,k]=residuez(b,a)
p = -0.3333
k = -10.5000 2.5000
Z Plane
a=[.5+.6i .2-.7i .9i]
b=[2+i .3 .3-.2i]
zplane(b,a)
Finding Polynomial from roots:
a=[-12 6 -19 22];
p=roots(a)
b=poly(p)
a=-12*b
b = 1.0000 -0.5000 1.5833 -1.8333
a =-12.0000 6.0000 -19.0000 22.0000
Finding a Transfer Function:
b=[.5+.7i .9-.4i 2+3i]
a=[2 1+.5i 3+.9i .5-.3i]
d=poly(b)
c=poly(a)
h=tf(c,d)
transfer function is given by
s^4 - (6.5+1.1i) s^3 + (13.97+4.1i) s^2 - (11.94+4.235i) s + (3.99+0.87i)
___________________________________________________________
s^3 - (3.4+3.3i) s^2 + (2.63+5.23i) s - (0.17+3.05i)

Finding Transfer Function From Z Plane:


b=[.5+.7i; .9-.4i; 2+3i] Transfer function:
a=[2; 1+.5i; 3+.9i; .5-.3i] s^4 - 6.5 s^3 + 13.97 s^2 - 11.94 s +
3.99
[c,d]=zp2tf(a,b,1)
_______________________________
h=tf(c,d) s^3 - 3.4 s^2 + 2.63 s - 0.17

8
Lab 5
Filtering:
num=[1 -.4 .75];
den=[2.2403 2.4908 2.2403];
n=0:40;
c=2;
d=-4;
ic=[0 0];
x1=cos(2*pi*.1*n);
x2=cos(2*pi*.4*n);
x=c*x1+d*x2;
y1=filter(den,num,x1,ic);
y2=filter(den,num,x2,ic);
y=filter(den,num,x,ic);
yt=c*y1+d*y2;
dd=y-yt;
subplot(311)
stem(n,y)
title('Y')
subplot(312)
stem(n,yt)
title('Yt')
subplot(313)
stem(n,dd)
title('DD')

9
Filtering:
m=input('M=')
n=0:100;
s1=cos(2*pi*.05*n);
s2=cos(2*pi*.47*n);
x=s1+s2;
den=ones(1,m);
y=filter(den,1,x)/m;
subplot(221)
plot(n,s1)
title('S1')
grid
subplot(222)
plot(n,s2)
title('S2')
subplot(223)
plot(n,x)
title('X')
subplot(224)
plot(n,y)
title('Y')

10
Lab # 6

t=0:.01:1;
ip=sin(2*pi*2*t);
deci=2;
op=decimate(ip,deci);
ipp=interp(op,2)
subplot(311)
stem(ip)
title('INPUT SIGNAL')
subplot(312)
stem(op)
title('DECIMATED SIGNAL')
subplot(313)
stem(ipp)
title('INTERPOLATED SIGNAL')

11
Lab # 7

Signal Decimation 1
in=0:0.01:1;
x=sin(2*pi*60*in);
deci=2;
y=decimate(x,deci);
stem(x(1:100))
subplot(2,1,1);
stem(x);
title('Original Signal')
stem(y(1:50))
subplot(2,1,2)
stem(out)
title('output signal');

Signal decimation 2
t = 0:.00025:1;
x = sin(2*pi*60*t);
y = decimate(x,4);
subplot(2,1,1);
stem(x(1:120)), axis([0 120 -2 2])
title('Original Signal')
subplot(2,1,2);
stem(y(1:30))
title('Decimated Signal')

12
ASSIGNMENT 2
Problem:
Take a sound wave and increase it with 3.5 and also decrease it 3.5 :Answer:

METLAB Code:
x=wavread('Windows XP Startup.wav');
subplot(311);
plot(x);
title('Signal');
y=decimate(x,40);
subplot(312);
plot(y);
title('decimated signal');
z=interp(y,100);
subplot(313);
plot(z);
title('interpolated signal');

Figure:

13
Lab 8

Filtering By using legend command:


f=100;
t=0:1/f:1;
x=sin(10*pi*t)+.25*sin(150*pi*t);
b=ones(1,10)/10;
y=filtfilt(b,1,x);
yy=filter(b,1,x);
plot(t,x,t,y,'-',t,yy,':');
legend('Original Signal','Non-Causal
filtering','Normal Filtering')

Filtering By using legend command 2 :

f=100;
t=0:1/f:10;
x=exp(.1*pi*t)+.25*exp(.2*pi*t);
b=ones(1,10)/10;
y=filtfilt(b,1,x);
yy=filter(b,1,x);

plot(t,x,t,y,'--',t,yy,':');
legend('Original-Signal','Non-Causal
filtering','Normal Filtering')
grid
m=[0 1 1 1 0 1 1 1 0 0 0];
f=0:0.1:1;

14
[b a]=yulewalk(10,f,m);
[h w]=freqz(b,a,128);
plot(f,m,'g',w/pi,abs(h),'r');
legend('Desired Frequency Response','Actual
response')
grid

m=[1 1 1 0 1 1 1 0 1 1 1];

f=0:0.1:1;

[b a]=yulewalk(10,f,m);

[h w]=freqz(b,a,128);

plot(f,m,'g',w/pi,abs(h),'r');

legend('Desired Frequency Response','Actual


response')

Lab # 9
15
Group Delay And Phase Response:
b=[1 3 9 8 5];
a=[1 4 8 6 5 3];
gd=grpdelay(b,a,512);
gd(1)=[];
[h,w]=freqz(b,a,512);
h(1)=[];
w(1)=[];
pd=(angle(h))./w;
plot(w,gd,w,pd,'-');
xlabel('frequency in Rad/sec');
legend('Group Delay','Phase Delay');
grid on

Butter Worth Filter:


b=[1 3 9 8 5];
a=[1 4 8 6 5 3];
[b,a]=butter(5,0.4);
gd=grpdelay(b,a,512);
gd(1)=[];
[h,w]=freqz(b,a,512);
h(1)=[];
w(1)=[];
pd=(angle(h))./w;
plot(w,gd,w,pd,'-');
xlabel('frequency in Rad/sec');
legend('Group Delay','Phase Delay');
grid on

Magnitude Response:

16
t=(0:1/99:1);
x=sin(2*pi*t*70)+sin(2*pi*45*t);
y=fft(x);
m=abs(y);
z=unwrap(angle(y));
f=(0:length(y)-1)*99/length(y);
subplot(211);
plot(f,m);
set(gca,'Xtick',[28 45 55 70]);
title('Magnitude Response');
subplot(212);
plot(f,z*180/pi);
set(gca,'Xtick',[28 45 55 70]);
title('Phase Response');

Lab 10
17
1.
fp=0.2;
fs=0.4;
ap=3;
as=60;
[n,wn]=buttord(fp,fs,ap,as);
[b,a]=butter(n,wn);
[h,w]=freqz(b,a,512);
plot(w/pi,abs(h));

2.
fp=0.2;
fs=0.4;
ap=3;
as=60;
d=fdesign.lowpass(fp,fs,ap,as);
hd=butter(d);
fvtool(hd)

18
3.
fp=0.2;
fs=0.4;
ap=3;
as=60;
d=cheb2ord(fp,fs,ap,as);
[b,a]=cheby2(n,ap,wn);
[h,w]=freqz(b,a,512);
plot(w/pi,abs(h));

4.

19
fp=0.2;
fs=0.4;
ap=3;
as=60;
d=fdesign.lowpass(fp,fs,ap,as);
hd=ellip(d);
fvtool(hd)

20

You might also like