You are on page 1of 21

FACULTY OF ENGINEERING

UNIVERSITY OF LUCKNOW

SIGNALS AND SYSTEM

(LAB)

SHAMBO BASU
ELECTRONICS AND COMMUNICATION
46
INDEX
Sr No. Program Signature

1. INTRODUCTION TO MATLAB
The MATLAB is a high-level language and interactive environment
for numerical computation, visualisation and programming. The
MATLAB platform is optimized for solving engineering and scientific
problems. The matrix-based MATLAB language is the world’s most
natural way to express computational mathematics. Built-in graphics
make it easy to visualize and gain insights from data. A vast library of
pre-built toolboxes lets you get started right away with algorithms
essential to your domain. The desktop environment invites
experimentation, exploration, and discovery. These MATLAB tools
and capabilities are all rigorously tested and designed to work together.
MATLAB works beyond the desktop and can run analyses on larger
data sets, and scale up to clusters and clouds. MATLAB code can be
integrated with other languages, enabling algorithms and applications
within web, enterprise, and production systems.

Key Features
• High-level language for scientific and engineering computing.
• Desktop environment tuned for iterative exploration, design, and
problem-solving.
• Graphics for visualizing data and tools for creating custom plots
• Apps for curve fitting, data classification, signal analysis, control
system tuning, and many other tasks.
• Add-on toolboxes for a wide range of engineering and scientific
applications.
• Tools for building applications with custom user interfaces
• Interfaces to C/C++, Java®, .NET, Python, SQL, Hadoop, and
Microsoft® Excel®.
• Royalty-free deployment options for sharing MATLAB programs
with end users.

The MATLAB screen looks like:


Command Window: Enter commands at the command line, indicated by the
prompt (>>).

Editor: Used to write scripts and save m files.

Current folder: Access your files.

Workspace: Explore data that you create or import from files.

Command History: View or rerun commands that you entered at the command
line.

(a) To define and use variables and functions in MATLAB


To create a new variable, enter the variable name in the Command
Window, followed by an equal sign (=) and the value you want to assign
to the variable. For example, if you run these statements, MATLAB adds
the three variables x, A, to the workspace
x = 5.71;
A = [1 2 3; 4 5 6; 7 8 9];

You can create symbolic numbers by using sym. Symbolic numbers are
exact representations, unlike floating-point numbers. To declare multiple
symbolic variables it can be done by
syms t s w;
(b) To define and use Vectors and Matrices in MATLAB
A matrix, vector can be defined in MATLAB as

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


B=[1;2;3]; //B is a column vector

(c) To study various MATLAB arithmetic operators and mathematical


functions

Basic mathematical functions work as intended like ‘+’ , ‘-‘ , ’/’ and ‘*’.

Other mathematical functions are

Factorial(3) //This returns 3!

(d) To create and use m file


Editor can be used to write scripts and saved as m files. These m files
can then be run anytime to get the desired results

2. Write a MATLAB program to plot the following continuous


time and discrete time signals
(a) Step function
n=0:1:20; y=ones(1,21);
subplot(2,1,1); plot(n,y); grid on
ylabel('Amplitude'); xlabel('Time');
title('Continuous Unit step signal');
subplot(2,1,2); stem(n,y); grid on
ylabel('Amplitude'); xlabel('Number
of sample'); title('Discrete Unit
step signal');
Impulse Function t=-1:0.01:1;
im = t==0; plot(t,im); grid on
title('Impulse Function');
xlabel('Time/ No. of Sample');
ylabel('Amplitude')

Exponential Function x=0:0.01:5;


y=exp(x); subplot(2,1,1); plot(x,y);
grid on ylabel('Amplitude');
xlabel('Time'); title('Continuous
exponential signal'); n=0:1:5;
y=exp(n); subplot(2,1,2); stem(n,y);
grid on ylabel('Amplitude');
xlabel('Number of sample');
title('Discrete expoential signal');
Ramp Function
n=0:1:20; y=n; subplot(2,1,1);
plot(n,y); grid on
ylabel('Amplitude'); xlabel('Time');
title('Continuous Unit Ramp
signal'); subplot(2,1,2); stem(n,y);
grid on ylabel('Amplitude');
xlabel('Number of sample');
title('Discrete Unit Ramp signal');
Sine Function
x=0:0.01:20; y=sin(x);
subplot(2,1,1); plot(x,y); grid
on ylabel('Amplitude');
xlabel('Time');
title('Continuous Sine
signal'); n=0:1:20; y=sin(n);
subplot(2,1,2); stem(n,y); grid
on ylabel('Amplitude');
xlabel('Number of sample')
4.Write a MATLAB program to obtain linear
convolution of two sequences shifting
%Linear Convolution
x=[1 4 3 2 5];
h=[1 3 6 2];
Nx=length(x);
Nh=length(h);
Ny=Nx+Nh-1;
fnx=[x zeros(1,Ny-Nx)];
fnh=[h zeros(1,Ny-Nh)];
for n=0:Ny-1
sum=0;
for k=0:n
sum=sum+fnx(k+1)*fnh(n-k+1);
end
y(n+1)=sum;
end
nx=0:1:Nx-1;
subplot(3,1,1);stem(nx,x);title('X(n)');
axis([min(nx)-0.5 max(nx)+0.5 0 max(x)+1]);
nh=0:1:Nh-1;
subplot(3,1,2);stem(nh,h);title('H(n)');
axis([min(nh)-0.5 max(nh)+0.5 0 max(h)+1]);
ny=min(nh)+min(nx):max(nh)+max(nx);
subplot(3,1,3);stem(ny,y);
title('Y(n)=X(n)*H(n)');
axis([min(ny)-0.5 max(ny)+0.5 0 max(y)+1]);
5.Auto-correlation and Cross-correlation
(a)Write a MATLAB program to compute auto correlation of a sequence x(n)

%auto-correlation
n=-0:1:3;
xn=[1 2 5 3];
yn=xcorr(xn);
subplot(2,1,1); stem(n,xn);
xlabel('n');ylabel('x(n)');
subplot(2,1,2);stem(yn);
xlabel('n');ylabel('y(n)');
title('Autocorrelation of x(n)=[1 2 5 3]');
(b)Write a MATLAB program to compute cross correlation of a sequences
x(n) and y(n)
%cross-correlation
nx=0:1:3;
xn=[1 2 5 3];
ny=-2:1:2;
yn=[3 -2 4 1 0];
yy=fliplr(yn);
lx=length(xn);
ly=length(yn);
lxy=lx+ly-1;
fnx=[xn zeros(1,lxy-lx)];
fny=[yy zeros(1,lxy-ly)];
for k=0:lxy-1
sum=0;
for n=0:k
sum=sum+fnx(n+1)*fny(k-n+1);
end
rxy(k+1)=sum;
end
subplot(3,1,1); stem(nx,xn);title('x(n)');
xlabel('n');ylabel('x(n)');axis([-0.5 3.5 0 5.5]);
subplot(3,1,2);stem(ny,yn);title('y(n)');
xlabel('n');ylabel('y(n)');axis([-2.5 2.5 -2.5 4.5]);
nxy=-2:1:5;
subplot(3,1,3);stem(nxy,rxy);
xlabel('n');ylabel('amplitude');axis([-2.5 4.5 min(rxy)-0.5
max(rxy)+0.5]);
title("Cross-Correaltion of x(n) and y(n)");
6.Laplace Transform
(a)Write a MATLAB program to calculate Laplace transform of signals
%laplace transform
syms t;
x=exp(-t);
y=x*cos(10*t);
subplot(4,2,1);ezplot(x,[0,5]);
axis([0 5 0 1.1]);title('x(t)');
subplot(4,2,2);ezplot(y,[-1,5]);
axis([0 5 -1.1 1.1]);title('y(t)');
disp('laplace transform of x(t) is')
X=laplace(x)
disp('laplace transform of y(t) is')
Y=laplace(y)
Xm=abs(X);
Ym=abs(Y);
subplot(4,2,3);ezplot(Xm);
title('Magnitue response of X(s)');
subplot(4,2,4);ezplot(Ym);
title('Magnitude response of Y(s)');

OUTPUT
laplace transform of x(t) is

X =1/(s + 1)

laplace transform of y(t) is

Y =(s + 1)/((s + 1)^2 + 100)


(b)Write a MATLAB program to generate partial fraction expansion in
Laplace Transform
num=input('Enter the numerator coefficients: ');
den=input('Enter the denominator coefficients: ');
[r,p,k]=residue(num,den);
disp('Residues in the partial fraction are')
disp(r);
disp('Poles of the system are')
disp(p);

OUTPUT
Enter the numerator coefficients:
[1 1]
Enter the denominator coefficients:
[2 2 1]
Residues in the partial fraction are
0.2500 - 0.2500i
0.2500 + 0.2500i

Poles of the system are


-0.5000 + 0.5000i
-0.5000 - 0.5000i
7.Write a MATLAB program to plot Fourier
transform and Z-transform of a given
signal
(a) Fourier Transform
syms t w;
x=2*(heaviside(t+2)-heaviside(t-2));
subplot(2,1,1);ezplot(x,[-2 2]);
axis([-2.5 2.5 0 2.5]);
X=int(x*exp(-1i*w*t),t,-5,5);
X=simplify(X);
disp('Fourier transform of x(t) is');
disp(X);
subplot(2,1,2);ezplot(X);

OUTPUT
Fourier transform of x(t) is
(4*sin(2*w))/w
(b) Z Transform
syms n w;
x1=n+1;
disp('The input signal is');
disp(x1);
X1=ztrans(x1);
disp('The Z-Transform is');
disp(X1);
X11=iztrans(X1);
disp('The inverse Z-Transform is');
disp(X11);
x2=sin(n*w);
disp('The input signal is');
disp(x2);
X2=ztrans(x2);
disp('The Z-Transform is');
disp(X2);
X22=iztrans(X2);
disp('The inverse Z-Transform is');
disp(X22);

OUTPUT
The input signal 1 is
n + 1

The Z-Transform is
z/(z - 1) + z/(z - 1)^2

The inverse Z-Transform is


n + 1

The input signal 2 is


sin(n*w)

The Z-Transform is
(z*sin(w))/(z^2 - 2*cos(w)*z + 1)

The inverse Z-Transform is


sin(n*w)
8.Pole Zero Diagram
(a) Write a MATLAB program to find the poles and the residues and
pole-zero plot of a given system function
disp('The input signal is');
disp('X(z)=z^3/((z-0.5)(z-0.75)(z-1))');
syms z;
d=(z-0.5)*(z-0.75)*(z-1);
a1=collect(d);
den=sym2poly(a1);
n=z^3;
a2=collect(n);
num=sym2poly(a2);
[r p k]=residue(num,den);
fprintf('r1=%4.2f\t',num(1));
fprintf('p1=%4.2f\n',den(1));
fprintf('r2=%4.2f\t',num(2));
fprintf('p2=%4.2f\n',den(2));
fprintf('r3=%4.2f\t',num(3));
fprintf('p3=%4.2f\n',den(3));
t=0.2;
H=tf('z');
H=tf(num,den,t);

OUTPUT
The input signal is
X(z)=z^3/((z-0.5)(z-0.75)(z-1))
r1=1.00 p1=1.00
r2=0.00 p2=-2.25
r3=0.00 p3=1.63
9.Discrete time Fourier Transform
(a) Write a MATLAB program to calculate the DTFT of a sequence
n=-4:1:4;
x=[zeros(1,2) ones(1,5) zeros(1,2)];
k=-4:4;
w=(pi)*k;
X=x*(exp(-1j*pi/100)).^(n'*k);
subplot(3,1,1);stem(n,x);
title('Discrete time n');ylabel('amplitude');
Xm=abs(x);
subplot(3,1,2);
plot(w/pi,Xm);
title('Magnitude of DTFT of rectangular signal');
xlabel('Frequency in pi units');ylabel('Phase');
Xp=phase(X);
subplot(3,1,3);plot(w/pi,Xp);
title('Phase of DTFT of rectangular signal');
xlabel('Frequency in pi units');ylabel('Magnitude');
10.Write a MATLAB program to plot
magnitude and phase response of a given
system
%fourier transform
syms t w;
x=exp(-2*t).*heaviside(t);
subplot(3,1,1);ezplot(x);
title('x(t)=e^-2t');
disp('The fourier transform of x(t) is')
X=fourier(x)
X=simplify(X);
subplot(3,1,2);ezplot(abs(X));
title('Magnitude response of x(t)');
subplot(3,1,3);ezplot(atan(imag(X)/real(X)));
title('Phase Response of x(t)');

You might also like