Digital Signal Processing
(EC-601)
Submitted By:
(Name of Student)
(Roll No.)
(Class/Year/Sem)
[LAB ASSIGNMENT Digital Signal Processing,EC-601]
INDEX
S. No Name of Experiment Date of Date of Remarks
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.
Some very important functions performed by MATLAB are given as follows:
• Matrix computations
• Vector Analysis
• Differential Equations computations
• Integration is possible
• Computer language programming
• Simulation
• Graph Plotation
• 2-D & 3-D Plotting
Benefits: Some Benefits of MATLAB are given as follows:
• Simple to use
• Fast computations are possible
• Wide working range
• Solution of matrix of any order
• Desired operations are performed in matrices
• Different Programming languages can be used
• Simulation is possible
Basic Commands:
Some basic MATLAB commands are given as follows:
Addition:
A+B
Subtraction:
A-B
Multiplication:
A*B
Division:
A/B
Power:
A^B
Power Of each Element individually:
A.^B
Range Specification:
A:B
Square-Root:
A=sqrt(B)
Where A & B are any arbitrary integers
Basic Matrix Operations:
This is a demonstration of some aspects of the MATLAB language.
Creating a Vector:
Let’s create a simple vector with 9 elements called a.
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.
Notice how MATLAB requires no special handling of vector or matrix math.
Adding an element to a Vector:
b=a+2
b=
3 4 5 6 8 6 5 6 7
Plots and Graphs:
Creating graphs in MATLAB
is as easy as one command.
Let's plot the result of our
vector addition with grid lines.
Plot (b)
grid on
MATLAB can make other
graph types as well, with axis
labels.
bar(b)
xlabel('Sample #')
ylabel('Pounds')
MATLAB can use symbols in
plots as well. Here is an
example using stars to mark the
points. MATLAB offers a
variety of other symbols and
line types.
plot(b,'*')
axis([0 10 0 10])
One area in which MATLAB
excels is matrix computation.
Creating a matrix:
Creating a matrix is as easy as making a vector, using semicolons (;) to separate
the rows of a matrix.
A = [1 2 0; 2 5 -1; 4 10 -1]
A=
1 2 0
2 5 -1
4 10 -1
Adding a new Row:
B(4,:)=[7 8 9]
ans=
1 2 0
2 5 -1
4 10 -1
7 8 9
Adding a new Column:
C(:,4)=[7 8 9]
ans=
1 2 0 7
2 5 -1 8
4 10 -1 9
Transpose:
We can easily find the transpose of the matrix A. B
= A'
B=
1 2 4
2 5 10
0 -1 -1
Matrix Multiplication:
Now let's multiply these two matrices together.
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
Matrix Multiplication by corresponding elements:
Instead of doing a matrix multiply, we can multiply the corresponding elements
of two matrices or vectors using the’.* ‘operator.
C = A .* B C
=
1 4 0
4 25 -10
0 -10 1
Inverse:
Let's find the inverse of a matrix ...
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] = {…., x[-1], x[0] ,x[1] ,
…..}
Where the up-arrow indicates the sample at n=0.
In MATLAB, we can represent a finite-duration sequence like above by a row of
vector of appropriate values. However such a vector does not have any information
about sample position n. therefore a correct representation of x[n] would require
two vectors, one each for x and n. for example a signal
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];
• An arbitrary infinite duration signal cannot be represented by MATLAB
due to finite memory limitations.
Basic Signals:
Unit Sample Sequence
>>function[x, n] =impseq (n0, n1, n2)
% Generates x[n] =delta (n-n0) ; n1<=n<=n2
% n1 is lower limit of required sequence;n2 is upper limit of required 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)
>> title('Delayed Impulse Sequence')
>> xlabel('n')
>> ylabel('x[n]')
PLOT:
Unit Step Sequence
>>function[x, n] =stepseq (n0, n1, n2)
% Generates x[n] =u (n-n0) ; n1<=n<=n2
% n1 is lower limit of required sequence;n2 is upper limit of required sequence
>>n=n1:n2;x=(n-n0)>=0;
>>stem(n,x);
>>title(‘Delayed Step Sequence’);
>>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)
>> title('Delayed Step Sequence')
>> xlabel('n')
>> ylabel('x[n]')
PLOT:
Real-valued Exponential Sequence
X[n] = an
In MATLAB, we can write
>>n=0:10; x=(0.9).^n
MATLAB RESULT OF ABOVE COMMAND:
x=
Columns 1 through 7
1.0000 0.9000 0.8100 0.7290 0.6561 0.5905 0.5314
Columns 8 through 11
0.4783 0.4305 0.3874 0.3487
Complex-valued Exponential Sequence
X[n] = e(a+jw0)n , for all n
MATLAB function exp is used to generate exponential sequences. For example to
generate
X[n]=exp[(2+3j)n],0<=n<=10, we can write:
>>n=0:10;x=exp((2+3j)*n)
MATLAB RESULT OF ABOVE COMMAND:
x=
1.0e+008 *
Columns 1 through 4
0.00 -0.00+0.00i 0.00-0.00i -0.00+0.00i
Columns 5 through 8
0.00-0.00i -0.0002+0.0001i 0.0011-0.0012i -0.0066+0.0101i
Columns 9 through 11
0.0377-0.0805i -0.1918+0.6280i 0.7484-4.7936i
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
2.000 1.050+0.606i 0.367+0.637i 0.000+0.446i
Columns 5 through 8
-0.135+0.234i -0.142+0.082i -0.099+0.000i -0.052-0.030i
Columns 9 through 11
-0.018-0.032i -0.000-0.022i 0.007-0.012i
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:
Many practical sequences cannot be described by mathematical expressions like
those above. These signals are called random or stochastic signals and are
characterized by parameters of associated probability density functions or their
statistical moments. In MATLAB rand (1, N) generates a length N random
sequence whose elements are uniformly distributed between [0, 1].randn (1, N)
generates a length N Guassian random sequence with mean 0 and variance 1.
Above mentioned functions can be transformed to generate other random
sequences.
Experiment no: 03
Aim: To compute different operations on sequences using MATLAB.
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]
%y=sum sequence over n which includes n1 and n2
%x1=first sequence over n1
%x2=second sequence over n2(n2 can be different from n1)
n=min(min(n1),min(n2)):max(max(n1),max(n2));
y1=zeroes(1,length(n));y2=y1;
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1;
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2;
y=y1+y2;
Practice:
x1[n]=[3,11,7,0,-1,4,2] and x2[n]=[2,3,0,-5,2,11]
Use above function to add two signals. MATLAB
CODE:
x1=[3 11 7 0 -1 4 2];
x2=[2 3 0 -5 2 11];
n1=4;%initial element number of x1
n2=3;%initial element number of x2 if
n2<n1
x2=[zeros(1,length(n1-n2)) x2];
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:
In this operation each sample of x[n] is shifted by an amount k to obtain a
shifted sequence y[n].
y[n]=x[n-k]
This operation has no effect on vector x but vector n is changed by adding k to
each element.
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]
In MATLAB, this function is implemented by flipr(x) function for sample
values and by –flipr(n) function for sample positions.
Function[y,n]=sigfold(x,n)
%implements y[n]=x[-n]
Y=flipr(x);
n=-flipr(n)
Sample Summation and Sample Product:
It adds all sample values of x[n] between n1 and n2. It is implemented by
sum(x(n1:n2)).
Sample multiplication multiplies all sample values of x[n] between n1 and n2. It is
implemented by prod(x(n1:n2)).
MATLAB CODE FOR SUMMATION:
x=[1 2 3 4 5 6];
n1=x(1,3);
n2=x(1,5);
sum(x(n1:n2))
RESULT:
ans =12
MATLAB CODE FOR PRODUCT:
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
Aim: To study and generation of sinusoidal signals in MATLAB.
Theory: In this lab, discrete-time sinusoidal signal has been introduced. A discrete
sinusoidal signal can be either sine or cosine time discrete signal and it can be
expressed by:
X(n)=Asin(wn+ф), -∞<n<∞
Where w=2*pi*f
Therefore
x(n)=Asin(2*pi*fn+ ф), -∞<n<∞
Similarly, discrete-time sinusoidal signal for a cosine function can be expressed as
X(n)=Acos(2*pi*fn+ ф), -∞<n<∞
Where
n=integer variable called the sampler no.
A=Amplitude of the sinusoid.
w=frequency in radian per sample
ф =phase in radians
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:
• To learn the generation of discrete time sinusoidal signals
• To perform the elementary operations of shifting(advanced and delayed)
• To learn the techniques of doing scaling(time and amplitude)
• To learn the application of some basic commands of MATLAB for simple
digital signal processing problems
MATLAB CODE:
n=0:40;
f=0.1;
phase=0;
A=1.5;
arg=2*pi*f*n-phase;
x=A*cos(arg);
clf; %clear and graph
stem(n,x);
axis([0 40 -2 2]);
grid;
title('Sinusoidal Sequence');
xlabel('Time index n');
ylabel('Amplitude');
axis;
RESULT:
ANSWERS TO THE QUESTIONS:
(1)What is the period of the Sequence?
ANS: n=10
(2)What command line controls the FREQUENCY in the program?
ANS: f=0.1
(3)What parameter controls the PHASE of this sequence?
ANS: phase
(4)What parameter controls the AMPLITUDE of the sequence?
ANS: A
(5)What is the FREQUENCY of this Sequence?
ANS: f=0.1 Hz
Experiment no: 05
Aim: To study and generation of complex exponential signals in MATLAB
Theory: In this lab, discrete-time complex exponential signal has been introduced. By
using complex numbers the exponential and sinusoidal signals can be written as special
cases of a more general signal, the complex exponential.
Consider the case:
X(n)=Aean
If ‘a’ is real, it is a simple discrete-time exponential signal. Taking, a=jw the
signal becomes:
X(n)=Aejwn
X(n)=Acos(wn)+jsin(wn)
Hence, the discrete-time exponential signal can be generalized to represent a
sinusoidal of arbitrary phase by writing:
Ѳ+ф) Ѳ
X(n)=Aej(n =Aejn *ejф
OBJECTIVES:
• To learn the generation of discrete time complex exponential signals
• To learn the effect of changing different parameters of the signal
• To learn the application of some basic commands of MATLAB for simple
digital signal processing problems
MATLAB CODE:
%Generation of a complex EXPONENTIAL SEQUENCE
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:
%Illustration of sampling process
%in the time domain
clf;
t=0:0.0005:1;
f=13;
xa=cos(2*pi*f*);
subplot(2,1,1)
plot(t,xa);
grid;
xlabel('Time,msec');
ylabel('Amplitude');
title('Continuous time signalx_{a}(t)');
axis([0 1 -1.2 1.2])
subplot(2,1,2);
T=0.1; n=0:T:1;
xs=cos(2*pi*f*n);
k=0:length(n)-1;
stem(k,xs);
grid;
xlabel('Time index n');
ylabel('Amplitude');
title('Discrete time signalx[n]');
axis([0 (length(n)-1) -1.2 1.2
MATLAB RESULT:
Experiment no: 07
Aim: To Compute of Linear and Circular convolution using MATLAB.
Theory:
The convolution of the equation
Y[n]=h[n]*x[n]
is implemented in MATLAB by the command copy, provided the two sequences
to be convolved are of finite length. For example, the output sequence of an FIR
system can be computed by convolving its impulse response with a given finite-
length input sequence.
The following MATLAB program illustrates this approach;
MATLAB CODE:
>> clf;
>> h=[3 2 1 -2 1 0 -4 0 3];
>> x=[1 -2 3 -4 3 2 0 0 1];
>> y=conv(h,x);
>> n=0:4;
>> plot(1,1);
>> stem(n:y);
>> xlabel('Time Index');
>> ylabel('Amplitude');
>> title('Output Obtained by Convolution');
>> grid;
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.
Z-Transform is an important tool in DSP that gives the solution of difference
equation in one go.
The Z-Transform of a discrete time system x (n) is defined as power series;
X(z)=∑∞
And the inverse Z-Transform is denoted by;
X(n)=Z-1[X(n)]
Since, Z-Transform is the infinite power series; it exists only for the region for
which this series converges (region of convergence). Inverse Z-Transform is the
method for inverting the Z-Transform of a signal so as to obtain the time domain
representation of signal.
The features of Z-Transform which are explained are as fellows;
Z-Transform of a Discrete time function
MATLAB CODE:
>> b=[1 -1.6 180 1];
>> a=[1 -1.5 161 0.878];
>> 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:
Z-TRANSFORM OF A DISCRETE TIME FUNCTION:
Z-Transform is defined as,
X(z)=
Or
X(z)=Z[x(n)]
Let the difference equation be,
MATLAB CODE:
>> 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
POLE ZERO DIAGRAM FOR A FUNCTION IN Z-DOMAIN
‘zplane’ command computes and displays the pole-zero diagram of z-function.
The command is
>>zplane(b,a)
Experiment no: 09
Aim: Study of Time Shifting Property of Discrete-Time Fourier Transform
in MATLAB
Theory:
Most of the properties of DTFT can be verified using MATLAB. In the
following we shall verify the different properties of DTFT using MATLAB.
Since all data in MATLAB have infinite length vectors, the sequences being
used to verify the properties are of finite length.
TIME-SHIFTING:
%Time-shifting Properties of DTFT
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
title('Magnitude Spectrum of orig Seq')
subplot(2,2,2)
plot(w/pi,abs(h2));
grid;
title('Magnitude Spectrum of Time-Shifted Seq')
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:
%Frequency-Shifting Properties of DTFT
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
title('Magnitude Spectrum of orig Seq')
subplot(2,2,2)
plot(w/pi,abs(h2));
grid;
title('Magnitude Spectrum of Freq-Shifted Seq')
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:
%Convolution Property of DTFT
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
title('Product of Magnatude Spectra')
subplot(2,2,2)
plot(w/pi,abs(h3));
grid
title('Magnatude Spectra of Convolved Sequence')
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
title('Phase Spectra of Convolved Sequence')
MATLAB RESULTS:
Experiment no: 12
Aim: Study of Modulation Shifting Property of Discrete-Time
Fourier Transform in MATLAB
MODULATION PROPERTY:
%Modulation Property of DTFT
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
title('Magnatude Spectrum of first Seq')
subplot(3,1,2)
plot(w/pi,abs(h2));
grid
title('Magnatude Spectrum of Second Seq')
subplot(3,1,3)
plot(w/pi,angle(h3));
grid
title('Magnitude Spectrum of Product Sequence')
MATLAB RESULTS: