DSP.docx
Transcript of DSP.docx
EXPERIMENT 1
GENERATION OF BASIC SIGNALS
a) UNIT IMPULSEb) UNIT STEPc) EXPONENTIALd) RAMPe) SINUSODIAL
%program for generating unit step sequence
clearallcloseallclcx=[-25:20]z=[zeros(1,25) ones(1,21)]stem(x,z)title('unit step sequence')xlabel('n')ylabel('f(n)')
-25 -20 -15 -10 -5 0 5 10 15 200
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1unit step sequence
n
f(n)
%program for generating discrete time unit impulse signal
clear all
clc
x=[-20:20];
z=[zeros(1,20) 1 zeros(1,20)];stem(x,z)title('unit impulse sequence')xlabel('n')ylabel('f(n)')
-20 -15 -10 -5 0 5 10 15 200
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1unit impulse sequence
n
f(n)
%program for generating unit ramp function
clear all close allclcx=[-18:25];z=((sign(x)+1)/2).*xstem(x,z)title('unit ramp signal')xlabel('n')ylabel('f(n)')
-20 -15 -10 -5 0 5 10 15 20 250
5
10
15
20
25unit ramp signal
n
f(n)
%program for generating signum
clear all close allclcx=[-20:20];z=sign(x)stem(x,z)title('signum')xlabel('n')ylabel('f(n)')
-20 -15 -10 -5 0 5 10 15 20-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1signum
n
f(n)
%program for generating sinc signal
clear all close allclcx=[-10:0.1:10];z=sinc(x)stem(x,z)title('sinc signal')xlabel('n')ylabel('f(n)')
-10 -8 -6 -4 -2 0 2 4 6 8 10-0.4
-0.2
0
0.2
0.4
0.6
0.8
1sinc signal
n
f(n)
%program for generating sinusoidal signal
clear all close allclck=input('enter constant')%constant=1x=[-8:0.3:10];z=sin(k.*x)stem(x,z)title('sinusoidal signal')xlabel('n')ylabel('f(n)')
-8 -6 -4 -2 0 2 4 6 8 10-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1sinusoidal signal
n
f(n)
%program for generating triangular signal
clcclear all close allA=1;w0=10*pi;w=0.5;t=-1:0.02:1;tri=A*sawtooth(w0*t,w);stem(t,tri)title('triangular signal')xlabel('n')ylabel('f(n)')
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1triangular signal
n
f(n)
%program for generating real exponential signal
clcclear all close allx=[-20:20];a=input('enter constant')%constant=0.75z=a.^x;stem(x,z)title('real exponential')xlabel('n')ylabel('f(n)')
-20 -15 -10 -5 0 5 10 15 200
50
100
150
200
250
300
350real exponential
n
f(n)
%program for generating complex exponential signal
clcclear all close allx=[-10:0.5:10];a=input('enter constant')%constant=1.5y=input('enter frequency')%frequency=60z=(a.^x).*(exp(j.*(y.*x)+5))m=real(z)n=imag(z)subplot(2,1,1)stem(x,m)subplot(2,1,2)stem(x,n)title('complex exponential')xlabel('n')ylabel('f(n)')
-10 -8 -6 -4 -2 0 2 4 6 8 10-1
-0.5
0
0.5
1x 10
4
-10 -8 -6 -4 -2 0 2 4 6 8 10-10000
-5000
0
5000complex exponential
n
f(n)
EXPERIMENT 2BASIC OPERATION ON SIGNALS
a) Shiftingb) Foldingc) Time scalingd) Signal additione) Signal multiplication
%program for signal addition
clcclear all close alln1=input('enter sample range for signal 1');%sample range[-10:10]x1=input('enter range of values for signal 1');%range of values[-10:10]n2=input('enter sample range for signal 2');%sample range[-5:5]x2=input('enter range of values for signal 2');%range 0f values[-5:5]subplot(2,2,1)stem(n1,x1)xlabel('n1')ylabel('x1')title('input signal 1')subplot(2,2,2)stem(n2,x2)xlabel('n2')ylabel('x2')title('input signal 2')n=min(min(n1),min(n2)):max(max(n1),max(n2));y1=zeros(1,length(n));y2=y1;y1(find((n>=min(n1))&(n<=max(n1))))=x1;y2(find((n>=min(n2))&(n<=max(n2))))=x2;y=y1+y2;subplot(2,1,2)stem(n,y);xlabel('n')ylabel('y')title('signal addition')
-10 -5 0 5 10-10
-5
0
5
10
n1
x1
input signal 1
-5 0 5-5
0
5
n2
x2
input signal 2
-10 -8 -6 -4 -2 0 2 4 6 8 10-10
-5
0
5
10
n
y
signal addition
%program for signal multiplication
clcclear all close alln1=input('enter sample range for signal 1');%sample range[-10:10]x1=input('enter range of values for signal 1');%range of values[-10:10]n2=input('enter sample range for signal 2');%sample range[-5:5]x2=input('enter range of values for signal 2');%range 0f values[-5:5]subplot(2,2,1)stem(n1,x1)xlabel('n1')ylabel('x1')title('input signal 1')subplot(2,2,2)stem(n2,x2)xlabel('n2')ylabel('x2')title('input signal 2')n=min(min(n1),min(n2)):max(max(n1),max(n2));y1=zeros(1,length(n));y2=y1;y1(find((n>=min(n1))&(n<=max(n1))))=x1;y2(find((n>=min(n2))&(n<=max(n2))))=x2;y=y1.*y2;
subplot(2,1,2)stem(n,y);xlabel('n')ylabel('y')title('signal multiplication')
-10 -5 0 5 10-10
-5
0
5
10
n1
x1
input signal 1
-5 0 5-5
0
5
n2
x2input signal 2
-10 -8 -6 -4 -2 0 2 4 6 8 100
5
10
15
20
25
n
y
signal multiplication
% program for shifting
clcclear allclose allx=[-5:20]z=[zeros(1,5) ones(1,21)]subplot(2,2,1)stem(x,z)
title('unit step')xlabel('n')ylabel('f(n)')x1=x+3subplot(2,2,2)stem(x1,z)title('shifted signal')xlabel('n')ylabel('f(n+3)')
%program for folding
clcclear allclose alln=input('starting point')%starting point=5m=input('length')%length=8p=n+mx=[n:p]y=sign(x).*xsubplot(2,2,3)stem(x,y)title('sample signal')x1=-xsubplot(2,2,4)stem(x1,y)title('folded signal')
%program for time scaling
clcclear allclose allm=input('scaling constant')%m=1x=input('input sequence')%x=0:10n=input('sample range')%n=0:5if m>=1 k=mod(n,m); j=1;for l=min(n)+1:max(n)+1if(k(l)==0)y(j)=x(l); j=j+1;endendelsefor k=min(n)+1:max(n)+1y((k/m)+(1/m)-(2/m)+1)=x(k);endendq=length(y);n=0:q-1;stem(n,y)xlabel('n')ylabel('x[n]')title('down sampling')axis([min(n)-2 max(n)+2 min(y)-2 max(y)+2])
-2 -1 0 1 2 3 4 5 6 7-2
-1
0
1
2
3
4
5
6
7
n
x[n
]
down sampling
EXPERIMENT 3
IMPLEMENTATION OF CONVOLUTION SUM
% program for convolution without using standard commands
clcclear allclose allx=[1 2 3 4 5]h=[40 41 42 43 44 45 46 47]m=length(x);n=length(h);if(m>=n) p=m;else p=n;endX=[x,zeros(1,n)];H=[h,zeros(1,m)];for i=1:n+m-1Y(i)=0;for j=1:pif(i-j+1>0)Y(i)=Y(i)+X(j)*H(i-j+1);elseendendendstem(Y);ylabel('Y[n]');xlabel('n');title('convolution of two signals without conv function');
0 2 4 6 8 10 120
100
200
300
400
500
600
700Y
[n]
n
convolution of two signals without conv function
% program for overlap add convolution
clcclearallcloseallx=[1 3 5 7 9 11 12 13 14 17]h=[10 5 2]cconv(x,h)p=length(x);q=length(h);if(p>=q) N=p;else N=q;endx1=zeros(1,10);Nx=length(x);M=length(h);L=ceil(N-M+1);h=[h zeros(1,N-M)];d=floor(Nx/L);for j=0:d-1 k=1:L;x1(k)=x(k+j*L);x1=[x, zeros(1,N-L)]; r=1:j*L+1;z(j+1,r)=0; y=cconv(x1,h,N);
t=1:N;z(j+1,t+j*L)=y(t);endfor j=1:length(z)b(j)=0;for k=1:db(j)=b(j)+z(k,j);endendstem(b);
1 2 3 4 5 6 7 8 9 100
50
100
150
200
250
300
350
400
450
EXPERIMENT 5
COMPUTATION OF DFT
% program for computation of DFTfor t=1:45h(t)=sin(2*pi*(11^11)*t)endN=length(h)n=1:Ny=0for k=1:N y=y+h(k)*exp(-j*2*pi*(k-1)*(n-1)/N)endsubplot(3,1,1)stem(n,real(y))title('spectrum of given signal:real part')xlabel('n')ylabel('real part:x(n)')subplot(3,1,2)stem (n,imag(y))title('spectrum of given signal:imag part')xlabel('n')ylabel('imaginary part of x(n)')subplot(3,1,3)stem(n,angle(y))xlabel('phase')ylabel('n')title('angle plot')
0 5 10 15 20 25 30 35 40 45-0.1
0
0.1spectrum of given signal:real part
n
real part
:x(n
)
0 5 10 15 20 25 30 35 40 45-0.05
0
0.05spectrum of given signal:imag part
nimagin
ary
part
of
x(n
)
0 5 10 15 20 25 30 35 40 45-5
0
5
phase
n
angle plot
EXPERIMENT 4
COMPUTATION OF IMPULSE RESPONSE OF THE LTI SYSTEM
% program for impulse response
clcclearallcloseallx=input('enter the input coefficients=');%x=20y=input('enter the output coefficients=');%y=20m=input('enter start of sequence with negative value');%m=-2n=input('enter length of sequence');%n=30p=m;q=m+n;z=p:q;w=-p;i=[zeros(1,w) 1 zeros(1,q)];X1=filter(x,y,i);figure;stem(z,X1);xlabel('sample range')ylabel('y(n)')title('impulse response')
-5 0 5 10 15 20 25 300
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
sample range
y(n
)
impulse response