Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

MATLAB PROGRAMS

GENERATION OF BASIC SIGNALS


subplot(3,4,1)
t=10:10
y=[zeros(1,10),ones(1,11)]
stem(t,y)
title('unitstepfunction')
xlabel('t(sec)')
ylabel('u(t)')

subplot(3,4,2)
t=10:10
y=[zeros(1,10),ones(1,1),zeros(1,10)]
stem(t,y)
title('unitimpulsefunction')
xlabel('t(sec)')
ylabel('i(t)')

subplot(3,4,3)
t=0:10
y=[t]
stem(t,y)
title('rampfunction')
xlabel('t(sec)')
ylabel('r(t)')

subplot(3,4,4)
t=linspace(10,10)
y=sin(t)
plot(t,y)
title('sinefunction')
xlabel('t(sec)')
ylabel('s(t)')

subplot(3,4,5)
t=linspace(10,10)
y=1+cos(t)
plot(t,y)
title('cosinefunction')
xlabel('t(sec)')
ylabel('c(t)')

subplot(3,4,6)
t=linspace(0,10)
y=exp(t)
plot(t,y)
title('fallingexponentialfunction')

xlabel('t(sec)')
ylabel('e(t)')

subplot(3,4,7)
t=linspace(10,10)
y=exp(0.1*t)
plot(t,y)
title('risingexponentialfunction')
xlabel('t(sec)')
ylabel('e(t)')

subplot(3,4,8)
t=10:10
y=[zeros(1,9),ones(1,3),zeros(1,9)]
stem(t,y)
title('rectagularfunction')
xlabel('t(sec)')
ylabel('r(t)')

subplot(3,4,9)
t=linspace(10,10)
y=sinc(2*3.14*0.1*t)
plot(t,y)
title('sincfunction')
xlabel('t(sec)')
ylabel('sinc(t)')

subplot(3,4,10)
t=10:10
output=((12*abs(t))/20)
plot(t,output)
title('triangularwave')
xlabel('t(sec)')
ylabel('x(t)')

subplot(3,4,11)
t=10:10
c=complex(3,2)
d=exp(c*t)
plot(t,abs(d))
disp(angle(d))
title('complexexponentialwave')
xlabel('t(sec)')
ylabel('x(t)')

GENERATIONOFTRIANGULARWAVE

n=input('No.ofcycles:')
p=input('Period:')
fori=0:n1
fort=0+(i*p):.01:p+(i*p)
ift<=p/2+(i*p)
y=(t(i*p))/(p/2)
elseift>=(p/2)+(i*p)
y=(p(t(i*p)))/(p/2)
end
stem(t10,y)
holdon
end
end

GENERATIONOFSQUAREWAVE(problemisthere)

n=input('No.ofcycles:')
p=input('Period:')
fori=0:n1
fort=0+(i*p):.01:p+(i*p)
ift<=p/2+(i*p)
y=(t(i*p))/(p/2)
elseift>=(p/2)+(i*p)
y=(p(t(i*p)))/(p/2)
end
stem(t10,y)
holdon
end
end


SAMPLINGWITHFOURCONDITIONS

N=input('No.ofSamples:')
fm=input('Enterfrequencyoffirstsignal:')
t=0:((1/fm)/1000):(1/fm)
y=sin(2*pi*fm*t)
subplot(2,2,1)
plot(t,y)
title('INPUUT')
fs=(fm)*N
n=0:N1
z=sin(2*pi*(fm)*n/fs)
subplot(2,2,2)
stem(n,z)
title('Fs>2Fm')
fs=(fm)*2
n=0:N1
z=sin(2*pi*(fm)*n/fs)
subplot(2,2,3)
stem(n,z)
title('Fs=2Fm')
fs=(fm)/N
n=0:N1
z=sin(2*pi*(fm)*n/fs)
subplot(2,2,4)
stem(n,z)
title('Fs<2Fm')

SAMPLING

N=input('No.ofsamples:')
t=0:(0.001/150):(5/150)
y=sin(2*pi*100*t)+sin(2*pi*50*t)+sin(2*pi*150*t)
subplot(2,2,1)
plot(t,y)
title('INPUT')
fs=(150)*N
n=0:250
z=sin(2*pi*100*n/fs)+sin(2*pi*50*n/fs)+sin(2*pi*150*
n/fs)
subplot(2,2,2)

stem(n,z)

DFT AND IDFT OF THE GIVEN SEQUENCE

x=input('enterx[n]:')
N=input('entervalueofN:')
a=length(x)
x=[xzeros(1,Na)]
fork=0:N1
sum(k+1)=0
forn=0:N1
sum(k+1)=sum(k+1)+(x(n+1)*exp((i*2*pi*(k)*(n)/N)))
end
subplot(2,2,1)
stem(real(sum))
title('DFT')
xlabel('n')
ylabel('Real(x)')
subplot(2,2,2)
stem(imag(sum))
title('DFT')
xlabel('n')
ylabel('imag(x)')
holdon
end
disp(sum)
forn=0:N1
sum2(n+1)=0
fork=0:N1
sum2(n+1)=sum2(n+1)+(sum(k+1)*exp(i*2*pi*(n)*(k)/N))
end
subplot(2,2,3)
stem(real(sum2/N))
title('IDFT')
ylabel('real(x1)')
xlabel('n')
subplot(2,2,4)
stem(imag(sum2/N))
title('IDFT')
ylabel('imag(x1)')
xlabel('n')
end
disp(sum2/N)

LINEARCONVOLUTION

x=input('enterx[n]')
h=input('Enterh[n]')
a=length(x)
b=length(h)
q=input('zeropositionofx[n]')
w=input('zeropositionofh[n]')
forn=1:a+b1
s=0
fork=1:a
if(n+1)>k&&((n+1)k)<=b
s=s+(x(k)*h((n+1k)))
end
end
y(n)=s
t=qw
stem(n+t+1,y(n))
holdon
end
disp(y)

CIRCULARCONVOLUTIONINTIMEDOMAIN

x1=input('Entersequencex[n]:')
l1=length(x1)
x2=input('Entersequenceh[n]:')
l2=length(x2)
M=max(l1,l2)
x1=[x1zeros(1,Ml1)]
x2=[x2zeros(1,Ml2)]
form=0:M1
x3(1,m+1)=0
forn=0:M1
x3(1,m+1)=x3(1,m+1)+(x1(1,n+1)*x2(mod(mn,M)+1))
end
end
disp(x3)
m=0:M1

stem(m,x3)
title('CircularConvolutionTimedomain')
xlabel('n')
ylabel('y[n]')

CIRCULARCONVOLUTININFREQUENCYDOMAIN

x1=input('enterthesequencex[n]:')
L=length(x1)
h1=input('enterthesequenceh[n]:')
M=length(h1)
N=max(L,M)
x=[x1zeros(1,NL)]
h=[h1zeros(1,NM)]
fork=1:1:N
X(k)=0
H(k)=0
form=1:1:N
X(k)=X(k)+(x(m)*exp(j*2*pi*(m1)*(k1)/N))
H(k)=H(k)+(h(m)*exp(j*2*pi*(m1)*(k1)/N))
end
Y(k)=H(k)*X(k)
end
fork=1:1:N
y(k)=0
form=1:1:N
y(k)=y(k)+(Y(m)*exp(j*2*pi*(m1)*(k1)/N))
end
y(k)=y(k)/N
end

%n=1:N
%display(x)
%n=1:N
%display(h)

n=0:N1
display(y(n+1))
n=0:N1
stem(n,y(n+1))

FFTDIF

X1=input('enteri/psequence')
r=input('Entertheradix')
a=length(X1)
foru=1:r
if((2^u)>=r)
u1=2^u
u2=u
break
end
end
X=[X1zeros(1,ra)]
N1=length(X)
N=u1
L=log2(N)
k1=Nk2=1k3=N/2
fori1=1:L
L1=1
fori2=1:k2
k=1
fori3=1:k3
i=i3+L11
j=i+k3
YY=2*pi*sqrt(1)*(k1)/N
W=exp(YY)
t1=X(i)X(j)t2=X(i)+X(j)
X(i)=t2
X(j)=t1*W
k=k+k2
end
L1=L1+k1
end
k1=k1/2
k2=k2*2
k3=k3/2
end
X2=bitrevorder(X)
k=0:N1
subplot(2,2,1)
stem(k,real(X2))

xlabel('K')
ylabel('Real(X(K)')
title('Real(X(K)')
subplot(2,2,2)
stem(k,imag(X2))
xlabel('K')
ylabel('imag(X(K))')
title('imag(X(k))')
disp(X2)

FFTDIT

data=input('enteri/psequence')
r=input('Entertheradix')
a=length(data)
foru=1:r
if((2^u)>=r)
t1=2^u
t2=u
break
end
end
X2=[datazeros(1,ra)]

X=bitrevorder(X2)

N=t1l=log2(N)
k1=2
k2=N/2
k3=1
fori1=1:l
l1=1
fori2=1:k2
k=1
fori3=1:k3
i=i3+l11
j=i+k3
Y=2*pi*sqrt(1)*(k1)/N
W=exp(Y)
T=X(j)*W
X(j)=X(i)T
X(i)=X(i)+T
k=k+k2
end
l1=l1+k1
end
k1=k1*2
k2=k2/2

k3=k3*2
end
disp(X)
k=0:N1
subplot(2,2,1)
stem(k,real(X))
xlabel('K')
ylabel('Real(X(K)')
title('Real(X(K)')
subplot(2,2,2)
stem(k,imag(X))
xlabel('K')
ylabel('imag(X(K))')
title('imag(X(k))')

SPECTRALESTIMATION

N=input('Enterno.ofsamples:')
fm=input('Enterfm:')
fs=input('Enterfs:')
a=input('Enteramplitude:')
t=0:0.001:(1/fm)
n=0:N1
z=a*sin(2*pi*fm*n/fs)
O=fs/N
p=fft(z,N)
q=rem(fm,O)
if(q==0)
disp('Nospectralleakage')
else
disp('Spectralleakage')
end
m=0:N1
subplot(1,2,1)
stem(m,abs(p))
title('Spectralestimation')
xlabel('k')
ylabel('abs(X(k))')
subplot(1,2,2)t=0:.01:(1/fm)
x=a*sin(2*pi*fm*t)
stem(t,x)
title('Fm')
xlabel('time')
ylabel('amp')

WINDOWS

m=input('Enterlength:')

forn=(m1)/2:(m1)/2
rect(1,n+((m+1)/2))=1
subplot(3,3,1)
stem(n,rect(1,n+((m+1)/2)))
title('RECTANGULAR')
xlabel('n')
ylabel('w(n)')
holdon
bar(1,n+((m+1)/2))=1(2*abs(n)/(m1))
subplot(3,3,5)
stem(n,bar(1,n+((m+1)/2)))
title('Barlet')
xlabel('n')
ylabel('w(n)')
holdon
han(1,n+((m+1)/2))=0.5+0.5*cos((2*pi*n)/(m1))
subplot(3,3,3)
stem(n,han(1,n+((m+1)/2)))
title('Hanning')
holdon
ham(1,n+((m+1)/2))=0.54+0.46*cos((2*pi*n)/(m1))
subplot(3,3,2)
stem(n,ham(1,n+((m+1)/2)))
xlabel('n')
ylabel('w(n)')
title('Hamming')
holdon
black(1,n+((m+1)/2))=0.42+0.5*cos((2*pi*n)/(m1))+.08*cos((4*pi*n)/(m1))
subplot(3,3,4)
stem(n,black(1,n+((m+1)/2)))
title('Blackman')
holdon
end

BANDPASSFILTER

wc1=input('enterthecutofffrequency')
wc2=input('enterthecutofffrequency')
%ws=input('entersamplingfrequency')
m=input('enterno.of.samples')
%wc1=2*pi*wc/ws
forn=pi:0.1:pi

if(((n<=wc1)&(n>=wc2))|((n>=wc1)&(n<=wc2)))
hd=1
elsehd=0
end
subplot(4,3,1)
stem(n,hd)
title('hd(w)')

holdon
end

forn=(m1):(m1)
ifn==0
hd(n+m)=(wc2wc1)/pi
else
hd(n+m)=(sin(n*wc2)sin(n*wc1))/(n*pi)
end

subplot(4,3,2)
stem(n,hd(n+m))
title('hd(n)')
holdon
end
forn=(m1)/2:(m1)/2

wr(n+(m+1)/2)=1
subplot(4,3,4)
stem(n,wr(n+(m+1)/2))
title('wr(n)')
holdon

h(n+(m+1)/2)=hd(n+m)*wr(n+(m+1)/2)
subplot(4,3,7)
stem(n,h(n+(m+1)/2))
title('h(n)=hd(n)*wr(n)')
holdon

whn(n+(m+1)/2)=0.5+0.5*cos((2*pi*n)/(m1))
subplot(4,3,5)
stem(n,whn(n+(m+1)/2))
title('whn(n)')
holdon
h1(n+(m+1)/2)=hd(n+m)*whn(n+(m+1)/2)
subplot(4,3,8)
stem(n,h1(n+(m+1)/2))
title('h1(n)=hd(n)*whn(n)')
holdon
whm(n+(m+1)/2)=0.54+0.46*cos((2*pi*n)/(m1))
subplot(4,3,6)
stem(n,whm(n+(m+1)/2))
title('whm(n)')
holdon
h2(n+(m+1)/2)=hd(n+m)*whm(n+(m+1)/2)
subplot(4,3,9)
stem(n,h1(n+(m+1)/2))
title('h2(n)=hd(n)*whm(n)')
holdon
end
i=0:0.01:pi
p=freqz(h,1,i)
q=abs(p)
subplot(4,3,10)

plot(i,q)
title('rectangular')
xlabel('n')
ylabel('h(w)')
p=freqz(h1,1,i)
q=abs(p)
subplot(4,3,11)
plot(i,q)
title('hanning')
xlabel('n')
ylabel('h(w)')
p=freqz(h2,1,i)
q=abs(p)
subplot(4,3,12)
plot(i,q)
title('hamming')
xlabel('n')
ylabel('h(w)')

BANDSTOPFILTER

wc1=input('enterthecutofffrequency')
wc2=input('enterthecutofffrequency')
%ws=input('entersamplingfrequency')
m=input('enterno.of.samples')
%wc1=2*pi*wc/ws
forn=pi:0.1:pi

if(((n<=wc1)&(n>=wc2))|((n>=wc1)&(n<=wc2)))
hd=0
elsehd=1
end
subplot(4,3,1)
stem(n,hd)
title('hd(w)')
holdon
end

forn=(m1):(m1)
ifn==0
hd(n+m)=1((wc2wc1)/pi)
else
hd(n+m)=(sin(n*wc1)sin(wc2*n))/(n*pi)
end

subplot(4,3,2)
stem(n,hd(n+m))
title('hd(n)')
holdon
end

forn=(m1)/2:(m1)/2

wr(n+(m+1)/2)=1
subplot(4,3,4)
stem(n,wr(n+(m+1)/2))
title('wr(n)')
holdon

h(n+(m+1)/2)=hd(n+m)*wr(n+(m+1)/2)
subplot(4,3,7)
stem(n,h(n+(m+1)/2))
title('h(n)=hd(n)*wr(n)')
holdon

whn(n+(m+1)/2)=0.5+0.5*cos((2*pi*n)/(m1))
subplot(4,3,5)
stem(n,whn(n+(m+1)/2))
title('whn(n)')
holdon
h1(n+(m+1)/2)=hd(n+m)*whn(n+(m+1)/2)
subplot(4,3,8)
stem(n,h1(n+(m+1)/2))
title('h1(n)=hd(n)*whn(n)')
holdon
whm(n+(m+1)/2)=0.54+0.46*cos((2*pi*n)/(m1))
subplot(4,3,6)
stem(n,whm(n+(m+1)/2))
title('whm(n)')
holdon
h2(n+(m+1)/2)=hd(n+m)*whm(n+(m+1)/2)
subplot(4,3,9)
stem(n,h1(n+(m+1)/2))
title('h2(n)=hd(n)*whm(n)')
holdon
end
i=0:0.01:pi
p=freqz(h,1,i)
q=abs(p)
subplot(4,3,10)
plot(i,q)
title('rectangular')
xlabel('n')
ylabel('h(w)')
p=freqz(h1,1,i)
q=abs(p)
subplot(4,3,11)
plot(i,q)
title('hanning')
xlabel('n')
ylabel('h(w)')
p=freqz(h2,1,i)
q=abs(p)
subplot(4,3,12)
plot(i,q)

title('hamming')
xlabel('n')
ylabel('h(w)')

HIGHPASSFILTER

wc1=input('enterthecutofffrequency')
%ws=input('entersamplingfrequency')
m=input('enterno.of.samples')
%wc1=2*pi*wc/ws
forn=pi:0.1:pi

if((n<=wc1)|(n>=wc1))
hd=1
elsehd=0
end
subplot(4,3,1)
stem(n,hd)
title('hd(w)')
holdon
end

forn=(m1):(m1)
ifn==0
hd(n+m)=1(wc1/pi)
else
hd(n+m)=(sin(n*pi)sin(n*wc1))/(n*pi)
end

subplot(4,3,2)
stem(n,hd(n+m))
title('hd(n)')
holdon
end
forn=(m1)/2:(m1)/2

wr(n+(m+1)/2)=1
subplot(4,3,4)
stem(n,wr(n+(m+1)/2))
title('wr(n)')
holdon

h(n+(m+1)/2)=hd(n+m)*wr(n+(m+1)/2)
subplot(4,3,7)
stem(n,h(n+(m+1)/2))
title('h(n)=hd(n)*wr(n)')
holdon

whn(n+(m+1)/2)=0.5+0.5*cos((2*pi*n)/(m1))
subplot(4,3,5)
stem(n,whn(n+(m+1)/2))
title('whn(n)')

holdon
h1(n+(m+1)/2)=hd(n+m)*whn(n+(m+1)/2)
subplot(4,3,8)
stem(n,h1(n+(m+1)/2))
title('h1(n)=hd(n)*whn(n)')
holdon
whm(n+(m+1)/2)=0.54+0.46*cos((2*pi*n)/(m1))
subplot(4,3,6)
stem(n,whm(n+(m+1)/2))
title('whm(n)')
holdon
h2(n+(m+1)/2)=hd(n+m)*whm(n+(m+1)/2)
subplot(4,3,9)
stem(n,h1(n+(m+1)/2))
title('h2(n)=hd(n)*whm(n)')
holdon
end
i=0:0.01:pi
p=freqz(h,1,i)
q=abs(p)
subplot(4,3,10)
plot(i,q)
title('rectangular')
xlabel('n')
ylabel('h(w)')
p=freqz(h1,1,i)
q=abs(p)
subplot(4,3,11)
plot(i,q)
title('hanning')
xlabel('n')
ylabel('h(w)')
p=freqz(h2,1,i)
q=abs(p)
subplot(4,3,12)
plot(i,q)
title('hamming')
xlabel('n')
ylabel('h(w)')

LOWPASSFILTER

wc1=input('enterthecutofffrequency')
%ws=input('entersamplingfrequency')
m=input('enterno.of.samples')
%wc1=2*pi*wc/ws
forn=wc1:0.1:wc1

hd=1
subplot(4,3,1)
stem(n,hd)
title('hd(w)')

holdon
end

forn=(m1):(m1)
ifn==0
hd(n+m)=wc1/pi
else
hd(n+m)=sin(n*wc1)/(n*pi)
end

subplot(4,3,2)
stem(n,hd(n+m))
title('hd(n)')
holdon
end
forn=(m1)/2:(m1)/2

wr(n+(m+1)/2)=1
subplot(4,3,4)
stem(n,wr(n+(m+1)/2))
title('wr(n)')
holdon

h(n+(m+1)/2)=hd(n+m)*wr(n+(m+1)/2)
subplot(4,3,7)
stem(n,h(n+(m+1)/2))
title('h(n)=hd(n)*wr(n)')
holdon

whn(n+(m+1)/2)=0.5+0.5*cos((2*pi*n)/(m1))
subplot(4,3,5)
stem(n,whn(n+(m+1)/2))
title('whn(n)')
holdon
h1(n+(m+1)/2)=hd(n+m)*whn(n+(m+1)/2)
subplot(4,3,8)
stem(n,h1(n+(m+1)/2))
title('h1(n)=hd(n)*whn(n)')
holdon
whm(n+(m+1)/2)=0.54+0.46*cos((2*pi*n)/(m1))
subplot(4,3,6)
stem(n,whm(n+(m+1)/2))
title('whm(n)')
holdon
h2(n+(m+1)/2)=hd(n+m)*whm(n+(m+1)/2)
subplot(4,3,9)
stem(n,h1(n+(m+1)/2))
title('h2(n)=hd(n)*whm(n)')
holdon
end
i=0:0.01:pi
p=freqz(h,1,i)
q=abs(p)
subplot(4,3,10)

plot(i,q)
title('rectangular')
xlabel('n')
ylabel('h(w)')
p=freqz(h1,1,i)
q=abs(p)
subplot(4,3,11)
plot(i,q)
title('hanning')
xlabel('n')
ylabel('h(w)')
p=freqz(h2,1,i)
q=abs(p)
subplot(4,3,12)
plot(i,q)
title('hamming')
xlabel('n')
ylabel('h(w)')



IIRFILTERUSINGIMPULSEINVARIANT

rs=input('enterstopbandripple:')
rp=input('enterpassbandripple:')
fp=input('enterpassbandfrequency:')
fs=input('enterstopbandfrequency:')
f=input('entersamplingfrequency:')

ws=2*pi*(fs/f)
wp=2*pi*(fp/f)
[N,wn]=buttord(wp,ws,rp,rs,'s')
display(N)
[B,A]=butter(N,wn,'s')
[Bz,Az]=impinvar(B,A,f)
freqz(Bz,Az)

IIRUSINGBILINEARTRANSFORMATION

rs=input('enterstopbandripple:')
rp=input('enterpassbandripple:')

fp=input('enterpassbandfrequency:')
fs=input('enterstopbandfrequency:')
f=input('entersamplingfrequency:')

ws=2*pi*(fs/f)
wp=2*pi*(fp/f)
[N,wn]=buttord(wp,ws,rp,rs,'s')
display(N)
[B,A]=butter(N,wn,'s')
[Bz,Az]=bilinear(B,A,f)
freqz(Bz,Az)

GENERATIONOFTHEGIVENSIGNAL

forx=0:0.001:4
ifx<1
y=2*x
end
ifx==1
y=0
end
if(x>1&&x<=2)
y=0
end
if(x>2&&x<=3)
y=2
end
ifx>3
y=6+(4(2*x))
end
plot(x,y)
holdon
end

forb=0:0.001:2
plot(2,b)
holdon
end
forb=0:0.001:2
plot(1,b)

holdon
end
forb=2:0.001:4
plot(3,b)
holdon
end
forb=0:0.001:2
plot(4,b)
holdon
end

You might also like