DSP Lab Manual - MATLAB
DSP Lab Manual - MATLAB
(EC-601)
Submitted By:
(Name of Student)
(Roll No.)
(Class/Year/Sem)
Experim Submission
ent
01
02
03
04
05
06
07
08
09
10
11
12
Experiment no: 01
Aim: Introduction to MATLAB and its basic commands
Theory: MATLAB is a very important tool used for making long complicated
calculations and plotting graphs of different functions depending upon our requirement.
Using MATLAB an m-file is created in which the basic operations are performed which
leads to simple short and simple computations of some very complicated problems in no
or very short time.
• Matrix computations
• Vector Analysis
• Differential Equations computations
• Integration is possible
• Computer language programming
• Simulation
• Graph Plotation
• 2-D & 3-D Plotting
Addition:
A+B
Subtraction:
A-B
Multiplication:
A*B
Division:
A/B
Power:
A^B
A.^B
Range Specification:
A:B
Square-Root:
A=sqrt(B)
Creating a Vector:
a = [1 2 3 4 6 4 3 4 5]
a=
1 2 3 4 6 4 3 4 5
Now let's add 2 to each element of our vector, a, and store the result in a new
vector.
Plot (b)
grid on
bar(b)
xlabel('Sample #')
ylabel('Pounds')
plot(b,'*')
axis([0 10 0 10])
Creating a matrix:
A = [1 2 0; 2 5 -1; 4 10 -1]
A=
1 2 0
2 5 -1
4 10 -1
= A'
B=
1 2 4
2 5 10
0 -1 -1
Matrix Multiplication:
Note again that MATLAB doesn't require you to deal with matrices as a
collection of numbers. MATLAB knows when you are dealing with matrices
and adjusts your calculations accordingly.
C = A *B C
=
5 12 24
12 30 59
24 59 117
X = inv(A)
X=
5 2 -2
-2 -1 1
0 -2 1
... and then illustrate the fact that a matrix times its inverse is the identity
matrix.
I = inv(A) * A
I=
1 0 0
0 1 0
0 0 1
MATLAB has functions for nearly every type of common matrix calculation.
Experiment no: 02
Aim: Representation of various signals like sine, cosine, square and ramp
using and their plotting using MATLAB.
Theory:
Discrete time signals are defined only at certain specific values of time. They
can be represented by x[n] where ‘n’ is integer valued and represents discrete
instances in time. i.e:
X[n] ={2,1,-1,0,1,4,3}
>>n=[-3,-2,-1,0,1,2,3];
>>x=[2,1,-1,0,1,4,3];
Basic Signals:
Unit Sample Sequence
>>n=n1:n2;x=(n-n0)==0;
>>stem(n,x);
>>title(‘Delayed Impulse’);
>>xlabel(‘n’);
>>ylabel(‘x[n]’);
Practice:
Use above function to plot unit sample sequence that has a value at n=-9 in a
range fro n=-14 to n=-2. Use zeros(1,N) command to generate above unit
sample sequence function…
MATLAB CODE:
>> n0=-9;
>> n1=-14;
>> n2=-2;
>> n=n1:n2;
>> x=(n-n0)==0;
>> stem(n,x)
>> xlabel('n')
>> ylabel('x[n]')
PLOT:
>>n=n1:n2;x=(n-n0)>=0;
>>stem(n,x);
>>xlabel(‘n’);
>>ylabel(‘x[n]’);
Practice:
Use above function to plot unit step sequence having range between -5 and 15,
shifted at n=-3. Use zeros(1,N) and ones(1,N) commands to generate above unit
step sequence fu ction…
MATLAB CODE:
>> n0=-3;
>> n1=-5;
>> n2=15;
>> n=n1:n2;
>>x=(n-n0)>=0;
>> stem(n,x)
>> xlabel('n')
>> ylabel('x[n]')
PLOT:
Real-valued Exponential Sequence
X[n] = an
>>n=0:10; x=(0.9).^n
x=
Columns 1 through 7
Columns 8 through 11
>>n=0:10;x=exp((2+3j)*n)
x=
1.0e+008 *
Columns 1 through 4
Columns 9 through 11
Practice:
Generate a complex exponential given below in MATLAB and also plot the
output…
X[n] = 2e(-0.5+(∏/6)j)n
MATLAB CODE:
>> n=0:10;
>> a=2;
>> b=exp((-0.5+(pi/6)*j)*n);
>> x=a*b
>> plot(n,x)
MATLAB RESULT:
x=
Columns 1 through 4
Columns 5 through 8
PLOT:
Sinusoidal Sequence:
X[n]=cos(w0n+Ѳ), for all n
MATLAB function sin or cos is used to generate sinusoidal sequences.
Practice:
Generate a sinusoidal signal given below in MATLAB and also plot the
output…
>>x[n]=3cos(0.1n∏+∏/3)+2sin(0.5n∏)
MATLAB CODE:
>> n=0:10;
>> a=3*cos((0.1*n*pi)+pi/3);
>> b=2*sin(0.5*n*pi);
>> x=a+b;
>> plot(n,x)
PLOT:
Random Signals:
Theory:
Signal addition:
In MATLAB, two sequences are added sample by sample using the arithmetic
operator ‘+’. However, if lengths of sequences are different or if sample positions
are different for equal-length sequences, then we can not directly use the ‘+’
operator. In this case, we have to augment the sequences so that they have the same
position vector ‘n’(and hence the same length). Following is the code for sequence
addition keeping in view above mentioned facts.
Function[y,n]=sigadd(x1,n1,x2,n2)
%implemnts y[n]=x1[n]+x2[n]
x2=[2 3 0 -5 2 11];
x1=x1;
if length(x1)>length(x2)
x2=[x2 zeros(1,length(x1)-length(x2))]
x1=x1
x=x1+x2
elseif length(x1)<length(x2)
x1=[x1 zeros(1,length(x2)-length(x1))]
x2=x2
x=x1+x2;
elseif length(x1)==length(x2)
x=x1+x2;
end
elseif n2>n1
x1=[zeros(1,length(n2-n1)) x1];
x2=x2;
if length(x1)>length(x2)
x2=[x2 zeros(1,length(x1)-length(x2))]
x1=x1
x=x1+x2
elseif length(x1)<length(x2)
x1=[x1 zeros(1,length(x2)-length(x1))]
x2=x2
x=x1+x2;
elseif length(x1)==length(x2)
x=x1+x2;
end
elseif n2==n1
if length(x1)>length(x2)
x2=[x2 zeros(1,length(x1)-length(x2))]
x1=x1
x=x1+x2
elseif length(x1)<length(x2)
x1=[x1 zeros(1,length(x2)-length(x1))]
x2=x2
x=x1+x2;
elseif length(x1)==length(x2)
x=x1+x2;
end
end
MATLAB RESULTS:
x2 = 2 3 0 -5 2 11 0 0
x1 = 0 3 11 7 0 -1 4 2
x= 2 6 11 2 2 10 4 2
Signal multiplication:
In MATLAB, two signals are multiplied sample by sample using array operator
‘*’. Similar instructions regarding position vector imply for multiplication as for
addition.
Practice:
Write a MATLAB function sigmult to multiply two signals x1[n] and x2[n],
where x1[n] and x2[n] may have different durations. Call this function to
multiply any two signals.
MATLAB RESULTS:
x2 = 2 3 0 -5 2 11 0 0
x1 = 0 3 11 7 0 -1 4 2
x= 0 9 0 -35 0 -11 0 0
Signal shifting:
y[n]=x[n-k]
Function[y,n]=sigshift(x,m,n0)
%implements y[n]=x[n-n0]
n=m+n0;
y=x;
Signal Folding:
In this operation each sample of x[n] is flipped around n=0 to obtaib a folded
sequence y[n].
y[n]=x[-n]
Function[y,n]=sigfold(x,n)
%implements y[n]=x[-n]
Y=flipr(x);
n=-flipr(n)
Sample multiplication multiplies all sample values of x[n] between n1 and n2. It is
implemented by prod(x(n1:n2)).
x=[1 2 3 4 5 6];
n1=x(1,3);
n2=x(1,5);
sum(x(n1:n2))
RESULT:
ans =12
x=[1 2 3 4 5 6];
n1=x(1,3);
n2=x(1,5);
prod(x(n1:n2))
RESULT:
ans =60
Experiment no: 04
Where w=2*pi*f
Therefore
Where
As the maximum value of the functions SINE & COSINE is unity, the A acts as a
scaling factor giving maximum and minimum values of ±A.
OBJECTIVES:
MATLAB CODE:
n=0:40;
f=0.1;
phase=0;
A=1.5;
arg=2*pi*f*n-phase;
x=A*cos(arg);
stem(n,x);
axis([0 40 -2 2]);
grid;
title('Sinusoidal Sequence');
xlabel('Time index n');
ylabel('Amplitude');
axis;
RESULT:
X(n)=Aean
X(n)=Aejwn
X(n)=Acos(wn)+jsin(wn)
OBJECTIVES:
clf;
c=-(1/12)+(pi/6)*i;
k=2; n=0:40;
x=k*exp(c*n);
subplot(2,1,1);
stem(n,real(x));
xlabel('Time index n');
ylabel('Amplitude');
title('Real part');
subplot(2,1,2);
stem(n,imag(x));
xlabel('Time index n');
ylabel('Amplitude');
title('imaginary part');
Result:
Experiment no: 06
Aim: To study and verify the Sampling of discrete time signals in MATLAB.
MATLAB CODE:
Y[n]=h[n]*x[n]
>> y=conv(h,x);
>> n=0:4;
>> plot(1,1);
>> stem(n:y);
>> ylabel('Amplitude');
MATLAB RESULT:
Experiment no: 08
Aim: To Compute of Z Transform using MATLAB.
Theory:
Z-Transform technique is an important tool in the analysis of characterization of
discrete time signals and LTI systems; Z-Transform gives the response of various
signals by its pole zero locations.
X(z)=∑∞
>> A=roots(a)
>> B=roots(b)
>> zplane(b,a)
RESULTS:
A=
0.7527 +12.6666i
0.7527 -12.6666i
-0.0055
B=
0.8028 +13.3927i
0.8028 -13.3927i
-0.0056
Pole=Zero Diagram:
>> syms z n
>>a=ztrans(1/4^n)
MATLAB
RESULT:
ans= 4*z/4*z-1
INVERSE Z-TRANSFORM:
The inverse Z-Transform is denoted by,
X(n)=Z-1[X(z)]
Let the Z-domain is:
MATLAB CODE:
>> syms z n
>>iztrans(2*z/(2*z-1))
MATLAB RESULT:
ans=
(1/2)^n
Theory:
TIME-SHIFTING:
clf;
w=-pi:2*pi/255:pi ;
w0=0.4*pi;
D=10;
num=[1 2 3 4 5 6 7 8 9];
h1=freqz(num,1,w);
h2=freqz([zeros(1,D) num],1,w);
subplot(2,2,1); plot(w/pi,abs(h1));
grid
subplot(2,2,3)
plot(w/pi,angle(h1));
grid;
title('Phase Spectrum of Orig Seq')
subplot(2,2,4)
plot(w/pi,angle(h2));
grid;
title('Phase Spectrum of Time-Shifted Seq')
MATLAB RESULTS:
Experiment no: 10
Aim: Study of Frequency Shifting Property of Discrete-Time
Fourier Transform in MATLAB
FRQUENCY-SHIFTING PROPERTY:
clf;
w=-pi : 2*pi/255 : pi ;
w0=0.4*pi;
num1=[1 3 5 7 9 11 13 15 17];
L=length(num1);;
h1=freqz(num1, 1,w);
n=0:L-1;
num2=exp(w0*i*n).*num1;
h2=freqz(num2, 1, w);
subplot(2,2,1);
plot(w/pi,abs(h1));
grid
subplot(2,2,2)
plot(w/pi,abs(h2));
grid;
subplot(2,2,3)
plot(w/pi,angle(h1));
grid;
title('Phase Spectrum of Orig Seq')
subplot(2,2,4)
plot(w/pi,angle(h2));
grid;
title('Phase Spectrum of Freq-Shifted Seq')
MATLAB RESULTS:
Experiment no: 11
Aim: Study of Convolution Shifting Property of Discrete-Time
Fourier Transform in MATLAB
CONVOLUTION PROPERTY:
clf;
w=-pi : 2*pi/255 : pi ;
x1=[1 3 5 7 9 11 13 15 17];
x2=[1 -2 3 -2 1];
y=conv(x1,x2);
h1=freqz(x1, 1, w);
h2=freqz(x2,1,w);
hp=h1.*h2;
h3=freqz(y,1,w);
subplot(2,2,1)
plot(w/pi,abs(hp));
grid
subplot(2,2,2)
plot(w/pi,abs(h3));
grid
subplot(2,2,3)
plot(w/pi,angle(hp));
grid
title('Sum of Phase Spectra')
subplot(2,2,4)
plot(w/pi,angle(h3));
grid
MODULATION PROPERTY:
clf;
w=-pi:2*pi/255:pi ;
x1=[1 3 5 7 9 11 13 15 17];
x2=[1 -1 1 -1 1 -1 1 -1 1];
y=x1.*x2;
h1=freqz(x1,1,w);
h2=freqz(x2,1,w);
h3=freqz(y,1,w);
subplot(3,1,1)
plot(w/pi,abs(h1));
grid
subplot(3,1,2)
plot(w/pi,abs(h2));
grid
subplot(3,1,3)
plot(w/pi,angle(h3));
grid
title('Magnitude Spectrum of Product Sequence')
MATLAB RESULTS: