Signal and Sytem Lab #1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 85

LAB SESSION#1

Lab Title:

“Getting started with MATLAB”

Aim of Experiment:

The Experiment aims to familiarize with the Matlab environment and running some

basic Matlab commands in Matlab.

Theory:

MATLAB is a software package for high-performance mathematical computation,

visualization, and programming environment. It provides an interactive environment with

hundreds of built-in functions for technical computing, graphics, and animations. Some

features of MATLAB include: computation of numeric data, creating graphics for

scientific use, modeling and simulating data, analyzing data. MATLAB is a modern

programming language environment, and it has refined data structures, includes built-in

editing and debugging tools, and supports object-oriented programming. MATLAB

is Multi-paradigm. So, it can work with multiple types of programming approaches,

such as Functional, Object-Oriented, and Visual.

Lab Work:

Generate a 6X6 Matrix:

Coding:

>>a= [2 2 2 2 2 2; 3 3 3 3 3 3; 4 4 4 4 4 4; 5 5 5 5 5 5; 6 6 6 6 6 6; 7 7 7 7 7]

Results:
Page | 1
Generate a 6X1 Matrix:

Coding:

>>b= [3; 4; 5; 6; 7; 8]

Results:

Generate a 3X4 Matrix:

Coding:

>>c= [3 4 5 6; 1 2 3 4; 6 7 8 9]

Results:

Answer the following questions for the matrix shown below?

Page | 2
| |
1.1 0.0 2.1 −3.5 6.0
0.0 1.1 −6.6 2.8 r . no
1.1 0.1 0.3 −0.4 1.3
r . no 5.1 0.0 1.1 r . no

Where r.no is roll number so my roll number is 1 after putting my roll number in the
matrix will be:

| |
1.1 0.0 2.1 −3.5 6.0
0.0 1.1 −6.6 2.8 1
1.1 0.1 0.3 −0.4 1.3
1 5.1 0.0 1.1 1

1. What is the size of the matrix?

The Size of matrix is 4×5.

Coding:

>> [m, n] = size (d)

Results:

Page | 3
2. What is the value of the matrix (4, 1)?

The value of the matrix (4, 1) is 1

Coding:

>> d (4, 1)

Results:

3. What is the size and value of matrix (:, 1: 2)?


Coding:
>> e= d (: 1: 2)
>> [m, n] =size (e)
Results:

Page | 4
4. What is the size and Value of the matrix ([1 3], end)?

Coding:
>> f=d ([1 3], end)
>> [m, n] =size (f)
Results:

Page | 5
Determine the size and contents of the following arrays. Note that the

later arrays may depend on the definition of arrays defined earlier in

this exercise?

(i). A=1:2:5

The size of the array is 1×3 and the contents are 1, 3 and 5

Coding:

>> A=1:2:5;

>> [m, n] =size (A)

>> disp (A)


Results:

(ii).B= [A’ A’ A’]

The size of the array is 3×3


Coding:

>> B = [A' A' A'];

>> [m, n] =size (B)

Page | 6
>> disp (B)
Results:

(iii). C= B (1:2:3, 1:2:3)

The size of the array is 2×2.


Coding:
>> C= B (1:2:3, 1:2:3);
>> [m, n] =size(C)
>> disp (C)
Results:

(iv). D= A+B (2, : )

The size of the array is 1×3.


Coding:

>> D= A+B (2, : );

Page | 7
>> [m, n] =size (D)

>> disp (D)


Results:

(v). W= [zeros (1, 3) ones (3, 1)’ 3: 5’]

The size of the array is 1×9.

Coding:

>> W = [zeros (1, 3) ones (3, 1)' 3:5'];

>> [m, n] =size (W)

>> disp (W)

Results:

Page | 8
(vi).B ([1 3], 2) = B ([3 1], 2)

The size of the array is 3×3

Coding:

>> B ([1 3], 2) = B ([3 1], 2);

>> [m, n] =size (B)

>> disp (B)

Results:

Assume that value has been initialized to (r.no)*pi and determine what

is printed out by each of the following statements?

My roll number is 1

(i). disp ([‘value=’num2str (value)]);

Coding:

>> value =1*pi;

>> disp (['value= ' num2str (value)]);

Page | 9
Results:

(ii). disp ([‘value=’int2str (value)]);

Coding:

>> value =1*pi;

>> disp (['value= ' int2str (value)]);


Results:

(iii). fprintf (‘value=%e\n’, value);

Coding:

>> value =1*pi;


>> fprintf ('value=%e\n’, value);
Results:

(iv). fprintf (‘value=%f\n’, value);

Coding:

>> value =1*pi;

Page | 10
>> fprintf ('value=%f\n', value);
Results:

Evaluate the following expressions?

(a) r.no /5+6


My roll number is 1

Coding:

>> r.no=1;

>> a=r.no/5+6;

>> disp (a)

Results:

(b)(r.no/5)+6
My roll number is 1

Coding:

>> r.no=1;
>> b= (r.no/5) +6;
>> disp (b)

Page | 11
Results:

(c) r.no /(5+6)


My roll number is 1

Coding:

>> r.no=1;
>> c=r.no / (5+6);
>> disp (c)
Results:

(d). r.no ^2^3

Coding:

>> r.no=1;
>> d=r.no ^2^3;
>> disp (d)

Results:

Page | 12
(e). (r.no ^2) ^3

Coding:

>>r.no=1;
>>e= (r.no^2) ^3;
>>disp (e)

Results:

(f). r.no ^ (2^3)

Coding:

>> r.no=1;
>> f=r.no ^ (2^3);
>> disp (f)
Results:

(g). round (-11/5) +6

Page | 13
Coding:

>> r.no=1;
>> g=round (-11/5)+6;
>> disp (g)

Results:

(h). ceil (-11/5) +6

Coding:

>> r.no=1;
>> h=ceil (-11/5) +6;
>> disp (h)
Results:

(i). floor (-11/5) +6

Coding:

>> r.no=1;
>> i=floor (-11/5) +6;
>> disp (i)
Results:

Page | 14
Page | 15
LAB SESSION#2

Lab Title:

“Programming in MATLAB”

Aim of Experiment:

This experiment aims to introduce programming within Matlab i.e. how to

create matrix, different functions in Matlab, basics of plotting and formatting graphs in

Matlab using different commands.

Theory:

MATLAB is a software package for high-performance mathematical computation,

visualization, and programming environment. It provides an interactive environment with

hundreds of built-in functions for technical computing, graphics, and animations. Some

features of MATLAB include: computation of numeric data, creating graphics for

scientific use, modeling and simulating data, analyzing data. MATLAB is a modern

programming language environment, and it has refined data structures, includes built-in

editing and debugging tools, and supports object-oriented programming. MATLAB

is Multi-paradigm. So, it can work with multiple types of programming approaches,

such as Functional, Object-Oriented, and Visual.

Lab Work:

Suppose that u=r.no and v=r.no+5. Plot the following expressions using

Subplot on a single figure with proper labels and title

(a) 4u/3v
(b) 2v-2/(u+v)2

Page | 1
(c) v3/v3-u3
(d) 4/3 pi.v2

Coding:

>>clear all

>>close all

>>u =1;

>>v=u+5;

>>subplot (2, 2, 1)

>>plot (u, v, ‘r :*');

>>xlabel ('-----u-----')

>>ylabel ('-----v-----')

>>title ('plot # 1')

>>hold on

>>x=2*v^ (-2);

>>y= (u+v) ^2;

>>subplot (2, 2, 2)

>>plot (x, y,’g :*');

>>xlabel ('-----x=2*v^2-----')

>>ylabel ('-----y= (u+v) ^2-----')

>>title ('plot # 2')

>>x= v^3;

>>y= v^3 - u^3;

>>subplot (2, 2, 3)

Page | 2
>>plot (x, y,’g: o')
>>xlabel('-----u=v^3-----')
>>ylabel ('-----v=v^3 - u^3-----')
>>title ('plot # 3')
>>hold on
>>x=4;
>>y= 3* 3.14 * v^2;
>>subplot (2, 2, 4)
>>plot (x, y,’g :+')
>>xlabel('-----u=4-----')
>>ylabel ('-----v=3* 3.14 * v^2-----')
>>title ('plot # 4')
Graph:

 The position   as a function of time of a particle that moves along a


straight line is given by

x (t)=−0.1t 4 + 0.8t 3 +10 t−r . no

Page | 3
Coding:
>>clear all
>>close all
>>t=5:25;
>>x (t) = -0.1*t. ^4 + 0.8*t. ^3 + 10*t - 1
>>plot (x (t), y,’g--o')
>>xlabel('----x(t)----')
>>ylabel ('----t----')
>>title ('GRAPH')
Graph:

2. Use subplot command to make the three plots of position, velocity, and


Acceleration on the same page and also label the graph.
 Plot the function y=3 x 3−26 x +r . no , and its first derivative and second

derivatives,-2≤x≤4, all in the same plot. Use hold and legend commands and

draw in different colors

Coding
>>clear all
>>close all
>>x = -2:4;
>>y = 3*x. ^3-26.*x + 1;
>>subplot (2, 2, 1)

Page | 4
>>plot (x, y,’g :*')
>>xlabel('-----x-----')
>>ylabel ('-----y-----')
>>title ('function')
>>hold on
>>y2=9*x. ^2 -26;
>>subplot (2, 2, 2)
>>plot (x, y2,’b :*')
>>xlabel('-----x-----')
>>ylabel ('-----y2-----')
>>title ('first derivative')
>>hold on
>>y3 = 18*x;
>>subplot (2, 1, 2)
>>plot(x, y3,'r: o')
>>xlabel('-----x-----')
>>ylabel ('-----y3-----')
>>title ('second derivative')
Graph:

 plot of the function

Page | 5
Coding:
>>clear all
>>close all
>>x=0:2*pi;
>>r= 3*cos(x/2). ^2 + x;
>>plot(x, r)
>>xlabel ('-----x= theta-----')
>>ylabel ('-----r-----')
>>title ('function graph')
Graph:

Use polar command to graph it.

 Plot   versus   from 0 to   in steps of . The points


should be connected by a red line and each point should be marked
with a plus sign.

Coding:
>>clear all
>>close all
>>x=0: pi/10:2*pi;
>>a = sin(x);
>>y = cos (2*x);

Page | 6
>> Plot (a, y,’g :+')
>> xlabel ('-----sin(x)-----')
>> Ylabel ('-----cos (2x) -----')
>>title ('function graph')
Graph:

Page | 7
Page | 8
LAB SESSION#3

Lab Title:

“Basic signal generation in MATLAB”

Aim of Experiment:

This experiment aims to introduce techniques within Matlab for generating

basic signals i.e. Continuous and Discrete. The experiment also aims to study

characteristics of Even and Odd signals.

Theory:

Continuous-time signal is the “function of continuous-time variable that has

uncountable or infinite set of numbers in its sequence. The continuous-time signal can be

represented and defined at any instant of the time in its sequence. The continuous-time

signal is also termed as analog signal. It is a continuous function of time defined on the

real line (or axis) R. It has continuous amplitude and time. That is, the continuous-time

signals will have certain value at any instant of time. Discrete-time signal is the

“function of discrete-time variable that has countable or finite set of numbers in its

sequence”. The discrete-time signal can be represented and defined at certain instants of

time in its sequence. That is, the discrete-time signal is able to define only at the sampling

instants. Digital signal can be obtained from the discrete-time signal by quantizing and

encoding the sample values. The discrete-time signals are represented with binary bits

and stored on the digital medium. A signal which is symmetrical about the vertical axis

or time origin is known as even signal or even function. Therefore, the even signals are

also called the symmetrical signals. Cosine wave is an example of even signal. A signal

Page | 1
that is anti-symmetrical about the vertical axis is known as odd signal or odd function.

Therefore, the odd signals are also called the antisymmetric signals. Sine wave is an

example of odd signal.

Lab Work:
 Lab-3a:
        Book: Signal & Systems
        Author: Alan V. Oppenheim
        Edition: Second
        Page: 57
        Question: 1.3 a, c, d, f

        Plot these signals as both i.e. Continuous & Discrete


Question 1.3
a. x1 (t) =e-2t u (t)
>>close all
>>clear all
>>e=2.718;
>>t=0:0.05:2;
>>x1=e. ^ (-2*t);
>>subplot (2, 2, 1)
>>plot (t, x1);
>>xlabel('time')
>>ylabel ('amplitude')
>>title ('continuous signal')
>>hold on;
>>subplot (2, 2, 2)
>>stem (t, x1);
>>xlabel('time')
>>ylabel ('amplitude')
>>title ('discrete signal')
Graph:

Page | 2
c. x3(t)=cos(t)
>>close all
>>clear all
>>t=0:0.5:10;
>>x3=cos (t);
>>subplot (2, 2, 1)
>>plot (t, x3);
>>xlabel ('X')
>>ylabel ('y')
>>title ('continuous signal')
>>hold on;
>>subplot (2, 2, 2)
>>stem (t, x3)
>>xlabel ('x')
>>ylabel ('y')
>>title ('discrete')
Graph:

d. x1[n] =(1/2)n u[n]


Coding:

>>close all;

Page | 3
>>clear all;
>>n=0:0.5:5;
>>x= (1/2). ^n;
>>subplot (2, 2, 1)
>>plot (n, x);
>> xlabel ('x')
>>ylabel ('y')
>>title ('continuous signal')
>>subplot (2, 2, 4)
>>stem (n, x);
>>xlabel ('x')
>>ylabel ('y')
>>title ('discrete signal')
Graph:

f. x3[n]=cos(Π/4
Coding:
>>close all;
>>clear all;
>>n=1:0.1:2;
>>pi=3.14;
>>x=cos (pi/4*n);
>>subplot (2, 1, 1)
>>plot (n, x)
>>title ('continues signal')
>>xlabel ('n')
>>ylabel ('x')
>>subplot (2, 1, 2)
>>stem (n, x)
>>title ('discrete signal')
>>xlabel ('n')
>>ylabel ('x')
Graph:

Page | 4
Lab-3b: 
        Book: Signal & Systems
        Author: Alan V. Oppenheim
        Edition: Second
        Page: 57, 60, 61
        Question: 1.7 b, 1.23 a, 1.24 c, 

 Plot these signals with their even and odd part. Also determine that the given signal
is even or odd?
Question: 1.7 b:
x2(t) =sin (1/2*t)
Coding:
>>clear all;
>>close all;
>> t=0:0.001:4*pi;
>> %defining functions
>> x1=sin (t/2);
>> x2=sin(-t/2);
>> %checking even or odd
>> If(x1==x2)
>> disp ('Even')
>> else
>> If(x1==-x2)
>> disp (‘odd')
>> else
>> disp ('neither even nor odd')
>> end
>> end;
>> figure

Page | 5
Question 1.23 a
Determine and sketch the even and odd parts of the signals depicted in
Figure Pl.23.Label your sketches carefully.

>>clear all;
>>close all;
>>t=-2:1:2;
>>%defining function
>>x1=zeros (size (t));
>>x2=zeros (size (t));
>>x1(4) =1;
>>x2(2) =1;
>>%checking even or odd
>>if(x1==x2)
>>disp ('Even')
>>else
>>if(x1==-x2)
>>disp (‘odd')
>>else
>>disp ('neither even nor odd')
>>end
>>end;
>>figure
>>%plot original signal

Page | 6
>>subplot (2, 2, 1)
>>plot (t, x1)
>>title ('original signal')
>>xlabel('t')
>>ylabel (‘x (t)')
>>%plot flipped signal
>>subplot (2, 2, 2)
<<plot (t, x2)
>>title ('flipped signal')
>>xlabel ('t')
>>ylabel (‘x (-t)')
>>%plot even signal
>>e=1/2.*(x1+x2);
>>subplot (2, 2, 3)
>>plot (t, e)
>>title ('even signal')
>>xlabel('t')
>>ylabel ('1/2[x (t) +x (-t)]')
>>%plot odd signal
>>o=1/2.*(x1-x2);
<<subplot (2, 2, 4)
>>plot (t, o)
>>title ('odd signal')
>>xlabel('t')
>>ylabel ('1/2[x(t)-x(-t)]')

Question# 1.24 c

Page | 7
Determine and sketch the even and odd parts of the signals depicted in
Figure P 1.24. Label your sketches carefully

>>clear all;
>>close all;
>>t=-5:1:4;
>>x1(1) =0; x1(2) =-1; x1(3) =2; x1(4) =2; x1(5) =1; x1(6) =1; x1(7) =2; x1(8) =1; x1(9) =-1;
>>x1(10) =0;
>>x2(1) =0; x2(2) =0; x2(3) =-1; x2(4) =1; x2(5) =2; x2(6) =1; x2(7) =1; x2(8) =2;
>>x2(9) =2; <<x2(10) =-1;
>>%checking even or odd
>>if(x1==x2)
>>disp ('Even')
>>else
>>if(x1==-x2)
>>disp (‘odd')
>>else
>>disp ('neither even nor odd')
>>end
>>end;
>>figure
>>%plot original signal
>>subplot (2, 2, 1)
>>stem (t, x1)
>>title ('original signal')
>>xlabel('n')
>>ylabel ('x[n]')
>>%plot flipped signal
>>subplot (2, 2, 2)
>>stem (t, x2)
>>title ('flipped signal')
>>xlabel('n')
>>ylabel (‘x [-n]')
>>%plot even signal
>>e=1/2.*(x1+x2);
>>subplot (2, 2, 3)
>>stem (t, e)

Page | 8
>>title ('even signal')
>>xlabel('n')
>>ylabel ('1/2[x[n] +x [-n]]')
>>%plot odd signal
>>o=1/2.*(x1-x2);
>>subplot (2, 2,4)
>>stem (t, o)
>>title ('odd signal')
>>xlabel ('n')
>>ylabel ('1/2[x[n]-x [-n]]')

Page | 9
Page | 10
LAB SESSION#4

Lab Title:

“Signal Transformations using MATLAB”


Aim of Experiment:

This experiment aims to study the transformations of the Time Variable for

Continuous-Time Signals i.e., Time Shifting/Scaling/Folding/Addition.

Theory:

A signal x (t) may be shifted in time by replacing the independent variable t by

either t−t0 or t+t0 . Here t0 is called as the shifting factor. Shifting in time may results in

time delay or time advancement. A signal x (t) may be scaled in time by replacing the

independent variable t by at. Here ‘a’ is called as the scaling factor. Time scaling may

results in signal compression or signal expansion. If the independent variable t is replaced

by ‘−t’, this operation is known as time reversal of the signal about the y-axis or

amplitude axis. This can be achieved by taking mirror image of the signal x(t) about y-

axis or by rotating x(t) by 180° about y-axis. Hence, time reversal is known

as folding or reflection.

Lab Work:

 Book: Signal & Systems    


 Author: Alan V. Oppenheim      
 Edition: Second       
 Page: 59, 60     

1
1. Question: 1.23 (b, c), 1.22 (g) (shift and scale the signals by ) 
Roll no
2. Plot original, shifted, scaled and flipped version of signals.

Page | 1
Question 1.22(g)

1/2x[n] +1/2(-1) nx[n]

>> close all


>> clear all
%defining the x axis
>> n=-5:1:5;
%Plotting The Original Signal
>> Y= [0 -1 -0.5 0.5 1 1 1 1 0.5 0 0];
>> Subplot (3, 3, 1)
>> stem (n, Y,'m*')
>> xlabel ('n')
>> ylabel ('Y')
>> title ('x[n]')
%plotting The Required signal
>> Y1=Y/2+(Y/2).*(-1). ^n
>> Subplot (3, 3, 2)
>> stem (n, Y1,'g-')
>> xlabel ('n')
>> ylabel ('Y1')
>> Title ('y[n] =1/2x[n]+1/2((-1)^n).*x[n]')
%flipped version of the signal
>> Subplot (3, 3, 3)
>> stem (-n, Y1,'r')
>> xlabel ('n')
>> Ylabel ('Y2')
>> Title (‘flipped y[n]')
%plotting the shifted signal
>> Subplot (3, 3, 4)
>> stem (n+ (1/11), Y1)
>> xlabel('n')
>> ylabel ('Y3')
>> title ('Shifted signal')
%plotting the Scaled signal
>> Subplot (3, 3, 5)
>> stem (n.*(1), Y1,'y')
>> Xlabel ('n')
>> Ylabel ('Y4')

Page | 2
>> Title ('Scaled Signal by 1/1')

Question 1.23 (b+c)

Coding for part b:


>>t=-5:1:5;
%defining the function
>> X1=1;
>> X2=0;
>> X3=t;
>> X4=-t;
%Defining First signal
>> x=x1*(t>=1) + x2*(t<=-2) + x3.*(t>=0 & t<1) + x4.*(t<0 & t>=-1);
>> Subplot (2, 2, 1)
>> Plot (t,x)

Page | 3
>> title('x(t)')
%Defining and plotting The Flipped Version the Required Signal
>> subplot(2,2,2)
>> Plot (-t,x)
>> title ('x(-t)')
%defining and plotting The Shifted Version of the Required Signal
>> Subplot (2, 2, 3)
>> plot (t+1,x)
>> title('x(t+1)')
%defining and plotting The Scaled Version of the Required Signal
>> subplot(2,2,4)
>> plot(t.*(1),x)
>> title('x(1t)')

Coding for part C:


>> t=-2:1:2;
>> x1=t;
>> x2=(-1/2).*t;x=x1.*(t>=0)+x2.*(t<0);
>> subplot(2,2,1)
>> plot(t,x,'r')
>> title('x(t)')
>> subplot(2,2,2)
>> plot(-t,x,'m')

Page | 4
>> title ('x(-t)')
%plotting the shifted signal
>> plot (t+(1),x,’g')
>> title ('x[t-1]')
%plotting the scaled signal
>> subplot (2,2,4)
>> plot (t.*(1),x)
>> title ('x[t.*(1)]')

Page | 5
Page | 6
Page | 7
LAB SESSION#5

Lab Title:

“Signal Transformations using MATLAB”


Aim of Experiment:

This experiment aims to learn discrete time and continuous time convolution of signals.

Theory:
Convolution is a mathematical tool for combining two signals to produce a third signal.
In other words, the convolution can be defined as a mathematical operation that is used
to express the relation between input and output an LTI system. Consider two
signals x1(t) and x2(t). Then, the convolution of these two signals is defined as

Lab Work:
• Book: Signal & Systems
• Author: Alan V. Oppenheim
• Edition: Second
• Page: 138, 141, 144
• Question: 2.4, 2.21 c, 2.25
• Plot x[n], h[n] and y[n].

Code:
>> close all
>> clear all
%Question # 2.4
% Defining x-axis

Page | 1
>>n= [0:20]
>>x1=1
>>x2=0
%Defining First signal
>>x=x1*(n>=3 & n<=8) + x2*(n<=3 & n>=8)
>> Subplot (3, 1, 1)
>> stem (n,x,'blue')
>>title('x[n]')
>>h=x1*(n>=4 & n<=15) + x2*(n<=4 & n>=15)
>>subplot(3,1,2)
>>stem (n,h,'k')
>>title ('h[n]')
%Calculating Convolution and plotting the result
>>y1=conv(x,h)
>>L=length (y1)
>>n=[0:L-1]
>>subplot (3, 1, 3)
>>stem (n, y1,'red')
>>title ('x[n]*h[n]')

Code:
>>close all
>>clear all
%Question No#2.21c

Page | 2
>>n= [-5:20]
>>x1=1
>>x2=0
%defining first signal
>>u1 = x1*(n>=4) + x2*(n<4)
>>signal1= ((-1/2). ^n).*u1;
>>subplot (2, 2,1)
>>stem (n, signal1,'blue')
>>title ('x1[n]')
%defining Second signal
>>u2 = x1*(n>=2) + x2*(n<2)
>>signal2=(4.^n).*u2
>>subplot (2,2,2)
>>stem (n, signal2, ‘k’)
>>title ('h[n]')
%Calculating Convolution and plotting the result
>>y1=conv(signal1,signal2)
>>L=length(y1)
>>n=[0:L-1]
%plotting the Convolved signal
>>subplot (2, 2, 3)
>>stem (n, y1,'red')
>>title ('x[n] * h[n]')

Page | 3
Code:
>>close all
>>clear all
%===========
%Question No#2.25

>>n= [-10:10]
>>x1=1
>>x2=0
%defining first signal
>>u0 = x1*(n>=0) + x2*(n<0) %unit step Function
>>u1=x1*(n<=-1) + x2*(n>-1) %For u[-n-1]
>>signal1=((3.^n).*u1)+(((1/3).^n ).*u0 );
>>subplot(2,2,1)
>>stem(n,signal1,'blue')
>>title('x[n]')
%defining Second signal
>>u2 = x1*(n>=-3) + x2*(n<-3) %for u[n+3]
>>signal2=((1/4).^n).*u2
>>subplot(2,2,2)
>>stem(n,signal2,'k')
>>title('h[n]')
%Calculating Convolution and plotting the result
>>y1=conv(signal1,signal2)
>>L=length(y1)
>>n=[0:L-1]
%plotting the Convolved signal
>>subplot(2,2,3)
>>stem (n,y1,'red')
>>title ('x[n] * h[n]')

Page | 4
Page | 5
Page | 6
LAB SESSION#6

Lab Title:

“Study of LTI Systems using MATLAB”


Aim of Experiment:

This laboratory experiment aims to learn different properties of LTI systems i.e.
Linear/Non-Linear/Time Invariant/Time variant.

Theory:

Linear time-invariant systems (LTI systems) are a class of systems used in signals and
systems that are both linear and time-invariant. Linear systems are systems whose outputs
for a linear combination of inputs are the same as a linear combination of individual
responses to those inputs. Time-invariant systems are systems where the output does not
depend on when an input was applied.

Lab Work:
Book: Signal & System by Alan V. Oppenheim Second edition

• Page: 138 (Question: 2.3, 2.4) + consider x1= Q# 1.25 a, page# 61

>>close all;

>>n=-1:1:10;

>>a=input ('Enter the constant a');

>>b=input ('Enter the constant b');

>>x1=3.*cos ((4*n) + (pi/3));

>>subplot (2, 2, 1);

>>stem (n, x1);

Page | 1
>>xlabel ('n');

>>ylabel ('amp');

>>title ('x1[n]');

>>x2= (1/2). ^ (n-2).*heaviside (n-2);

>>h=n.*heaviside(n+2);

%for Linearity and Non-Linearity

>>y01= conv (a*x1, h);

>>y02= conv (b*x2, h);

>>y1=y01+y02;

>>x= (a.*x1) + (b.*x2);

>>y2= conv(x, h);

>>L=length(x) +length (h)-1;

>>n=0: L-1;

>>subplot (2, 2, 2);

>>stem (n, y1);

>>xlabel ('n');

>>ylabel ('amp');

>>title ('Sum of Individual Responses');

>>subplot (2, 2, 3:4);

>>stem (n, y2);

>>xlabel ('n');

>>ylabel ('amp');

>>title ('Total Responses');

Page | 2
>>if y1==y2

>>disp ('System is Linear');

>>else

>>disp ('System is Non-Linear');

>>end

For Time Invariance and Time Variance:

• Coding:

%for Time Invariance and Time Variance;

>>close all;

>>n=-1:1:10;

>>x= (1/2). ^ (n-2).*heaviside (n-2);

>>h=n.*heaviside (n+2);

Page | 3
>>d=input('Enter the Delay:');

>>x dn= [zeros (1, d), x];

>>y n=conv (xdn, h);

>>y=conv(x, h);

>>y dn = [zeros (1, d), y];

>>figure;

>>subplot (2, 1, 1);

>>stem (0: length(x) -1, x);

>>xlabel ('n');

>>ylabel ('amp');

>>title ('sequence of x[n]');

>>subplot (2, 1, 2);

>>stem (0: length (xdn) -1, xdn);

>>xlabel ('n');

>>ylabel ('amp');

>>title ('Delayed Sequence of x[n]');

>>figure;

>>subplot (2, 1, 1);

>>stem (0: length(y) -1, y);

>>xlabel ('n');

>>ylabel ('amp');

>>title ('sequence of y[n]');

>>subplot (2, 1, 2);

Page | 4
>>stem (0: length (ydn) -1,ydn);

>>xlabel ('n');

>>ylabel ('amp');

>>title ('Delayed Sequence of y[n]');

>>if yn==yndn

>> disp ('System is Time Invariant');

>>else

>> disp ('System is Time Variant');

>>end

Page | 5
Question 2.4:
• Coding:
>>close all;
>>n= [0:1:10];
>>a=input ('Enter the constant a');
>>b=input ('Enter the constant b');
>>n1 = 1;
>>n2 = 0;
%for First Signal;
>>x1=3.*cos ((4*n) + (pi/3));
>>subplot (2, 2, 1);
>>stem (n, x1);
>>xlabel ('n');
>>ylabel ('amp');
>>title ('x1[n]');
%for Second Signal;
>>x2=n1*(n>=3 & n<=8) + n2 (n<=3 & n>=8);
>>subplot (2, 2, 2);
>>stem (n, x2);
>>xlabel ('n');
>>ylabel ('amp');
>>title ('x2[n]');
%for Impulse Response Signal;
>>h=n1*(n>=4 & n<=15) + n2*(n<=4 & n>=15);
>>subplot (2, 2, 3:4);
>>stem (n, h)
>>xlabel ('n');
>>ylabel ('amp');
>>title ('h[n]');
>>figure;
%for Linearity and Non -Linearity
>>y01= conv (a*x1, h);
>>y02= conv (b*x2, h);
>>y1=y01+y02;
>>x= (a.*x1) + (b.*x2);
>>y2= conv(x, h);
>>L=length(x) +length (h) -1;
>>n=0: L -1;
>>subplot (2, 2, 1:2);
>>stem (n, y1);
>>xlabel ('n');
>>ylabel ('amp');
>>title ('Sum of Individual Responses');
>>subplot (2, 2, 3:4);

Page | 6
>>stem (n, y2);
>>xlabel ('n');
>>ylabel ('amp');
>>title ('Total Responses');
>>if y1==y2
>>disp ('System is Linear');
>>else
>> disp ('System is Non-Linear');
>>end
Result:

Graph:

Page | 7
Coding for Question 2.4:
• For Time Invariance and Time Variance:
%for Time Invariance and Time Variance;
>>close all;
>>n=-1:1:10;
>>n1 = 1;
>>n2 = 0;
>>x = n1*(n>=3 & n<=8) + n2*(n<=3 & n>=8);
>>h=n1*(n>=4 & n<=15) + n2*(n<=4 & n>=15);
>>d=input ('Enter the Delay :');
>>xdn= [zeros (1, d), x];
>>yn=conv (xdn, h);
>>y=conv(x, h);
>>ydn= [zeros (1, d), y];
>>figure;
>>subplot (2, 1, 1);
>>stem (0: length(x)-1, x);
>>xlabel ('n');
>>ylabel ('amp');
>>title ('sequence of x[n]');
>>subplot (2, 1, 2);
>>stem (0: length (xdn)-1, xdn);
>>xlabel ('n');
>>ylabel ('amp');
>>title ('Delayed Sequence of x[n]');
>>figure;
>>subplot (2, 1, 1);

Page | 8
>>stem (0: length(y)-1, y);
>>xlabel ('n');
>>ylabel ('amp');
>>title ('sequence of y[n]');
>>subplot (2, 1, 2);
>>stem (0: length (ydn)-1, ydn);
>>xlabel ('n');
>>ylabel ('amp');
>>title ('Delayed Sequence of y[n]');
>>if yn==yndn;
>>disp ('System is Time Invariant');
>>else
>>disp ('System is Time Variant');
>>end

Page | 9
Page | 10
LAB SESSION#7

Lab Title:

“Correlation between signals”


Aim of Experiment:

This laboratory experiment aims how to find the auto-correlation and cross-correlation
between signals.

Theory:

The correlation of two functions or signals or waveforms is defined as the measure of


similarity between those signals. There are two types of correlations (a).Cross-
correlation (b).Autocorrelation. Cross-correlation between two different signals is
defined as the measure of similarity or coherence between one signal and the time-
delayed version of another signal. The autocorrelation function is defined as the measure
of similarity or coherence between a signal and its time delayed version.

Lab Work:

Signal & Systems by Alan V. Oppenheim (2nd edition)

Page 57 (Question: 1.3 a, c, d, f) and Plot original and correlated signals

(a) X1(t) = e-2t u (t) (c) X3(t) = cos (t) (d) X1(n) = (½) n u (n) (f) X3(t) = cos (n)
Code:
%Question No (1.3 part (a and c)
>>close all
>>clear all
>>t=0:0.1:10
>>u=heaviside (t)
>>e=2.73
%signal in part (a)
>>x1=e. ^ (-2.*t).*u
>>subplot (2, 2, 1)
>>plot (t, x1)
>>title ('x1(t)')
>>xlabel ('t')
>>ylabel ('e^ (-2t)')
%second signal in part (c)
>>x3=cos (t)

Page | 1
>>subplot (2, 2, 2);
>>plot (t, x3)
>>title ('x3(t)');
>>xlabel ('t');
>>ylabel ('cos(t)')
%xcorrfuncton co-relate the two signals
>>y1=x corr(x1, x3)
>>subplot (2, 2, 3);
>>plot (y1);
>>title ('Cross Co-Related Signal');
>>xlabel ('t');
>>ylabel ('y1')
%auto co-relation
>>y2=x corr(x1)
>>subplot (2, 2, 4)
>>stem (y2)
>>title ('auto co-relation')
>>xlabel ('t')
>>ylabel ('y2')
%Part d and f
%co-relation of signals
>>figure
>>n=0:1:20
>>u=heaviside (n)
>>stem (u)
%signal part d
>>x1= (1/2). ^ (n).*u
>>subplot (2, 2, 1)
>>stem (n, x1)
%signal part f
>>x3=cos (45*n)
>>subplot (2, 2, 2)
>>stem(x3)
%crosses co-relation
>>y1=x corr(x1, x3)
>>subplot (2, 2, 3)
>>stem (y1)
>>xlabel ('n')
>>ylabel ('y1')
>>title ('cross co-relation')
%auto co-relation
>>y2=x corr(x1)
>>subplot (2, 2, 4)
>>stem (y2)

Page | 2
>>x label ('n')
>>y label ('y2')
>>title ('auto co-relation signal')

For part a and c

For part d and f

Page | 3
Page | 4
LAB SESSION#8

Lab Title:

“Impulse response and Step response of LTI Systems”


Aim of Experiment:

This laboratory experiment aims at how to find the Impulse response and step response of
LTI systems.

Theory:

A system for which the principle of superposition and the principle of homogeneity are
valid and the input/output characteristics do not change with time is called the linear
time-invariant (LTI) system. When the impulse signal is applied to a linear system, then
the response of the system is called the impulse response. The convolution integral can
be used to obtain the step response of a continuous-time LTI system.

Lab Work:
Signal & Systems by Alan V. Oppenheim (2nd edition)

Page # 721 Qs: 9.5 a, b, c and Plot impulse response and step response of these questions.

Coding:

>>close all

>>syms s complex

>>H= (1/(s+1)) + (1/(s+3));

>> disp ('Impulse response of system is')

Page | 1
>>h = ilaplace (H);

>>simplify (h)

>> disp (h)

>> Y = (1/s)*((1/(s+1))+(1/(s+3)));

>> disp ('Step response of system is')

>> y = ilaplace(Y);

>> simplify(y)

>> disp (y)

>> t = 0:0.1:20;

>> h1 = subs (h, t);

>> Subplot (2, 1, 1)

>> plot (t, h1)

>> Xlabel ('time')

>> Ylabel (‘h (t)')

>> title ('Impulse response of system by Faryal ')

>> y1 = subs(y, t);

>> Subplot (2, 1, 2)

>> plot (t, y1)

>> xlabel ('time')

>> ylabel (‘h (t)')

>> title ('Step response of system by Faryal ')

Page | 2
Coding (b) part

>>clear all

>> syms s complex

% symbolic variable created here it is complex, it can be real

>>H=(s/ (s. ^2-1)) + (1/ (s. ^2-1)) %system

>>h= ilaplace (H) %inverse Laplace

>>figure;

>>simplify (h);

>>disp (h)

>>t=.1:1:20;

>>h1=subs (h, t) %substitution

>>subplot (2, 1, 1)

>>plot (t, h1,'c--')

>>title ('impulse response')

Page | 3
>>u=1/s*H % step response of H

>>h2=subs (u, t)

>>subplot (2, 1, 2)

>>plot (h2,'g--')

>>title ('step response')

Coding (c) part:


>>clear all
>>syms s complex
% symbolic variable created here it is complex, it can be real
>>H= ((s. ^3)-1/ (s.^2+s+1))
%system
>>h= ilaplace (H)
%inverse laplace
>>figure;
>>simplify (h);
>>disp (h)
>>t=.1:1:20;
>>h1=subs (h, t)
%substitution
>>subplot (2, 1, 1)
>>plot (t, h1,'c--')
>>title ('impulse response')

Page | 4
>>u=1/s*H
% step response of H
>>h2=subs (u, t)
>>subplot (2, 1, 2)
>>plot (h2,'g--')
>>title ('step response')

Page | 5
Page | 6
LAB SESSION#9

Lab Title:

“Fourier Series”
Aim of Experiment:

The aim of this experiment is to find the Fourier and inverse Fourier transforms.

Theory:

Fourier series simply states that, periodic signals can be represented into sum of sines and
cosines when multiplied with a certain weight. The Fourier transform simply states that
that the non periodic signals whose area under the curve is finite can also be represented
into integrals of the sines and cosines after being multiplied by a certain weight.
The inverse Fourier transform is a mathematical formula that converts a signal in the
frequency domain ω to one in the time (or spatial) domain t.

Lab Work:
For Fourier Transforms
>>close all;
>>clear all;
>>syms t s;
>>syms w real;
>>syms A real;
>>syms b real;
>>syms f floor;
>>f=dirac (t);
>>F=fourier (f);
>>disp (‘the fourier transform of dirac (t) is');
>>disp (F);
>>f1=A*heaviside (t);
>>F1=fourier (f1);
>>disp (‘the fourier transform of A= ');
>>disp (F1);
>>f2=A*exp (-t)*heaviside (t);
>>F2=fourier (f2);
>>disp ('The fourier transform of exp(-t) is');
>>disp (F2);
>>f3=A*t*exp (-b*t)*heaviside (t);
>>F3=fourier (f3);
>>disp (‘the fourier a transform of A*t*exp (-b*t)*4(t) = ');

Page | 1
>>disp (F3);
>>f4=sin (0*t);
>>F4=fourier (f4);
>>disp ('The fourier transform of sin (0*t) is');
>>disp (F4);
>>figure
Result:

For Inverse Fourier Transform:

% for Inverse Fourier Transform


>>F1=A*pi*(dirac (w-0) + (w+0));
>>f1=ifourier (F1, t);
>>disp('The inverse fourier transform of A*pi**(dirac(w-0)+dirac(w+0)= ');
>>disp (f1);
>>F2=A*pi*(Dirac (w-0)-dirac (w+0))/i;
>>f2=ifourier (F2, t);
>>disp (‘the inverse fourier transform of A*pi*(dirac (w-0) +dirac (w+0)/i= ');
>>disp (f2);
>>F3=A/ (1+j*w);
>>f3=ifourier (F3,t);
>>disp (‘the inverse fourier transform of A/ (1+jw) =');
>>disp (f3);
>>F4= (3*i*w+14)/ ((i*w) ^2+7*i*w+12);
>>f4=ifourier (F4, t);
>>disp (‘the inverse fourier transform of (3*i*w+14)/ ((i*w) ^2+7*i*w+12) =');
>>disp (f4);

Result:

Page | 2
Page | 3
Page | 4
LAB SESSION#10

Lab Title:

“Magnitude and Phase Spectrum”


Aim of Experiment:

This lab aims to calculate the Magnitude and Phase Spectrum of Fourier Transforms.

Lab Work:

Coding:
>>clear all;
>>close all;
>>syms t s;
>>syms w float;
>>f=3*exp (-t)*heaviside (t);
%given function
>>F=fourier (f);
>>disp (‘the fourier transform of 3*exp (-t)*u (t) =');
>>disp (F);
>>w=-2*pi: pi/50:2*pi;
>>F1=subs (F, w);
%substitute w in F function
>>Fmag=abs (F1);
%to find magnitude
>>Fphas=angle (F1);
%to find phase
>>subplot (2, 1, 1);
>>plot (w, Fmag);
>>xlabel ('w----');
>>ylabel ('Magnitude…');
>>title ('Magnitude spectrum');
>>grid;
>>subplot (2, 1, 2);
>>plot (w, Fphas);
>>xlabel ('w…');
>>ylabel ('Phase in Radians..');
>>title ('Phase spectrum');
>>grid;

Page | 1
Result:

Page | 2
LAB SESSION#11

Lab Title:

“Laplace Transform”
Aim of Experiment:

This lab aims to find Laplace Transform and inverse Laplace transforms.

Theory:

Laplace transforms permits to go from time domain to frequency domain whereas inverse
Laplace transform allows to go from frequency domain to time domain.

Lab Work:

>>close all;

>>clear all;

>>syms s;

>>F= (1/(s^2))*(1-exp (-s)-(1/2)*exp (-3*5) + (1/2)*exp (5*s));

>>f=ilaplace (F);

>>pretty (simplify (f));

>>ezplot (f, [0, 5]);

>>grid;

Results:

Page | 1
Page | 2
LAB SESSION#12

Lab Title:

“Zeroes and Poles”


Aim of Experiment:

This lab aims to study Zeroes and poles in the S-plane, Zeroes and poles in Z-plane.

Theory:

The poles and zeros of a transfer function may be complex, and the system dynamics may
be represented graphically by plotting their locations on the complex s-plane, whose axes
represent the real and imaginary parts of the complex variable s. Such plots are known as
pole-zero plots. It is usual to mark a zero location by a circle (◦) and a pole location a
cross (×).

Lab work:

Zeroes and poles in the S-plane

>>clear all;

>>close all;

>>num=input ('Enter the numerator polynomial vector\n');

>>den=input ('Enter the denominator polynomial vector\n');

>>H=tf (num, den)

>> [p, z] =pzmap (H);

>>disp ('zeroes are at');

>>disp (z);

>>disp ('poles are at');

>>disp (p);

>> [p, z] =pzmap (H);

Page | 1
>>if max (real (p))>=0;

>>disp (‘all the poles do not lie in the left half of the S-plane');

>> disp ('The given LTI system is not a stable system');

>>else

>> disp ('All the poles lie in the left half of S-plane');

>>disp ('The given LTI system is a stable system');

>>end;

Result:

Zeroes and poles in the z-plane:

Coding

>>clear all;

>>close all;

Page | 2
>>num=input ('Enter the numerator polynomial vector\n');

>>den=input ('Enter the denominator polynomial vector\n');

>>H=filt (num,den)

>>Z=zero (H);

>>disp (‘the zeroes are at');

>>disp (Z);

>>[r p k] =residuez (num, den);

>>disp (‘the poles are at');

>>disp (p);

>>zplane (num, den);

>>title ('Pole-Zero map in the Z-plane');

>>if max (abs (p))>=1;

>>disp (‘all the poles do not lie within the unit circle');

>>disp ('The given LTI system is not a stable system');

>>else

>>disp (‘all the poles lie within the unit circle');

>>disp ('The given LTI system is a stable system');

>>end;

Result:

Page | 3
Page | 4
LAB SESSION#13

Lab Title:

“Sampling Theorem”
Aim of Experiment:

This lab aims to implement the sampling theorem using MATLAB.

Theory:

A continuous time signal can be represented in its samples and can be recovered back
when sampling frequency fs is greater than or equal to the twice the highest frequency
component of message signal. i. e.

f s ≥ 2fm

Lab work:

>> close all;


>> clear all;
>>f1=3;
>>f2=23;
>>t=-0.4:0.0001:0.4;
>>x= cos (2*pi*f1*t) +cos (2*pi*f2*t);
>>figure (1);
>>plot (t, x,'-r')
>>xlabel ('time…');
>>ylabel ('amp…');
>>title (‘the original signal');
%case 1 :( fs<2fm)
>>fs1=1.4*f2;
>>ts1=1/fs1;
>>n1=-0.4:ts1:0.4;
>>xs1=cos (2*pi*f1*n1) +cos (2*pi*f2*n1);
>>figure (2);
>>stem (n1, xs1);
>>holdon;
>>plot (t, x,'-r');
>>hold off;
>>legend ('fs<2fm');
>>xlabel ('time…');
>>ylabel ('amp…');
>>title ('Case 1');
Page | 1
%case 2 :( fs=2fm)
>>fs2=2*f2;
>>ts2=1/fs2;
>>n2=-0.4:ts2:0.4;
>>xs2=cos (2*pi*f1*n2) +cos (2*pi*f2*n2);
>>figure (3);
>>stem (n2, xs2);
>>holdon;
>>plot (t, x,'-r');
>>hold off;
>>legend ('fs=2fm');
>>xlabel ('time…');
>>ylabel ('amp…');
>>title ('Case 2');
%case 3 :( fs>2fm)
>>fs3=7*f2;
>>ts3=1/fs3;
>>n3=-0.4:ts3:0.4;
>>xs3=cos (2*pi*f1*n3) +cos (2*pi*f2*n3);
>>figure (4);
>>stem (n3, xs3);
>>holdon;
>>plot (t, x,'-r');
>>hold off;
>>legend ('fs>2fm');
>>xlabel ('time…');
>>ylabel ('amp…');
>>title ('Case 3');

Page | 2
Page | 3
Page | 4
LAB SESSION#14

Lab Title:

“Simulink”
Aim of Experiment:

Introduction to SIMULINK in MATLAB

Theory:
Simulink is a simulation and model-based design environment for dynamic and
embedded systems, integrated with MATLAB. Simulink, also developed by Math Works,
is a data flow graphical programming language tool for modeling, simulating and
analyzing multi-domain dynamic systems. It is basically a graphical block diagramming
tool with customizable set of block libraries. It allows you to incorporate MATLAB
algorithms into models as well as export the simulation results onto MATLAB for further
analysis. Simulink supports the following tasks:
1. System-level Design.
2. Simulation
3. Automatic Code Generation
4. Testing and Verification of Embedded Systems

Starting up SIMULINK
In order to use SIMULINK, you must first start MATLAB. With MATLAB running,

there are two ways to start Simulink:

 Click the SIMULINK icon on the MATLAB toolbar and Type ‘SIMULINK’ at

Page | 1
Basic Elements:
There are two major categories of elements in Simulink:

1. Blocks

2. Lines

Blocks are used to generate, modify, combine, output, and display signals. Lines, on the

other hand, are used to transfer signals from one block to another.

1. Blocks:
There are several general classes of blocks, some of which are:

 Sources: Used to generate various signals. Sources blocks have outputs but no inputs.

One may want to use a Constant input, a Sine Wave, a Step, a Ramp, a Pulse

Generator, or a Uniform Random number to simulate noise. The Clock may be used

to create a time index for plotting purposes.

 Sinks: Used to output or display signals. Sinks blocks have inputs but no outputs.

Examples are Scope, Display, To Workspace, Floating Scope, XY Graph, etc.

 Discrete: Discrete Filter, Discrete State-Space, Discrete Transfer Fcn, Discrete Zero-

Pole, Unit Delayed.

 Continuous: Integrator, State-Space, Transfer Fcn, Zero-Pole,etc.

 Signal routing: Mux, Demux, Switch,etc.

 Math Operations: Abs, Gain, Product, Slider Gain, Sign, Sum,etc.

2. Lines:
Lines transmit signal in the direction indicated by the arrow Lines must always transmit

signals from the output terminal of one block to the input terminal of another block .One

Page | 2
exception to this is that a line can tap off of another line. This sends the original signal to

two (or more) destination blocks. Lines can never inject a signal into another line; lines

must be combined through the use of block such as a summing junction. A signal can be

either a scalar signal or a vector signal.

Page | 3

You might also like