Signals & Systems Lab Manual
Signals & Systems Lab Manual
JUNE 2013-14
Basic of MATLAB:
Whenever you are unsure of the syntax of a command, or want to find how to do
some-thing you can rely on a few helpful commands that can quickly provided
with the needed information. The most basic command we will use is help. Typing:
HELP, by itself, lists all primary help topics. Each primary topic corresponds to a
directory name on the MATLABPATH.
"HELP TOPIC" gives help on the specified topic. The topic can be a command
name, a directory name, or a MATLABPATH relative partial pathname (see HELP
PARTIALPATH). If it is a command name, HELP displays information on that
command. If it is a directory name, HELP displays the Table-Of-Contents for the
specified directory. For example, "help general" and "help matlab/general" both list
the Table-Of-Contents for the directory toolbox/matlab/general.
LOOKFOR XYZ looks for the string XYZ in the first comment line of the HELP
text in all M-files found on the MATLABPATH. For all files in which a match
occurs, LOOKFOR displays the matching lines.
MORE ON causes HELP to pause between screenfuls if the help text runs to
several screens.
In the online help, keywords are capitalized to make them stand out. Always type
commands in lowercase since all command and function names are actually in
lowercase. For tips on creating help for your m-files 'type help.m'.
For example, to learn more about the function sum you should type help sum. This
is the general style of help. It not only gives a detailed description of the topic
often including examples, but refers to related topics as well. For example, when
seeking help about the function sum you also learn that there is a function prod that
is somewhat related.
This style of help is displayed on the prompt. As you probably noticed the screen is
cluttered with text and we need to scroll up in order to read the full article. An
arguably better way to get help is to use the HTML version. The command doc
works the same way as help with the difference that it launches a window
displaying the HTML version of the help file. This way is preferable as links to
related commands as well as graphics are included. Typing:
Variables:
Defining variables
As we have seen in previous sections we can type commands and execute them in
the command prompt >>. Now if we type:
We define a variable x and assign the value 5. Notice that we don't need to define
the type (such as string or number) of the variable. MATLAB is able to infer the
type (within reason).
Clearing variables:
The command:
clears the variable from the workspace. This is useful for example when we want
to reclaim system memory. We can also clear variables when we want to make sure
that any old values are not going to be used by accident.
Built-in variables:
MATLAB includes a number of special variables that are built in the system. For
example, entering pi gives us the value of pi
Other special variables include i and j , that, as you guessed, correspond to the
imaginary unit. It is possible to overwrite special variables. For example, typing pi
= 5 assigns a new value, thus loosing its previous, built-in value. The original
values of the built-in variables can be returned by using the clear statement as
illustrated below:
Scalars, Vectors and Matrices:
MATLAB's simplest mathematical objects are scalars, vectors and matrices. These
are the quantities you are going to manipulate more often, as vectors and matrices
can be used to represent signals, images, among other things. The command
interpreter is highly optimized to perform operations with vectorial quantities. This
is in fact what the name MATrix to LABratory denotes. In this lab session you will
get a first contact with these objects, and will learn more in subsequent lab
sessions.
There are two types of vectors. Row vectors and column vectors. By convention
one typically uses column vectors to represent signals. For displaying purposes we
will use row vectors since they occupy less lines when printed. The simplest way
to create a column vector is by the bracket concatenation operator. We separate the
various values by using a semi-colon.
Typing:
If we want to create a row vector instead we can use spaces or commas as
separators instead:
Operators:
One can see MATLAB as a glorified calculator. In this sense operators are the
special characters that provide MATLAB the calculator functionality. The most
basic operators
are:
The use of most operators is self explanatory but if you need more help in any of
them you can use the help system, e.g. help m times gives:
Elementary functions:
The elementary functions can be found by typing help elfun. The most basic
functions follow.
sin - Sine.
cos - Cosine.
tan - Tangent.
sec - Secant.
csc - Cosecant.
cot - Cotangent.
Exponential.
exp - Exponential.
Complex.
Remember to use the help command to get more information for any of those
functions.
Flow control:
The for statement:
Repeating statements for an arbitrary number of times is one of the most important
features
of most programming languages. It used to be the case that MATLAB was not able
to repeat statements efficiently but this is not true anymore. Starting with version
6.5 MATLAB includes a JIT-compiler which is responsible for optimizing loops.
The for loop repeats a group of statements for a fixed, predetermined number of
times. A matching end denotes the end of the loop. For example:
The semicolon terminating the inner statement suppresses repeated printing, and
the s after the loop displays the final result. Nested loops are good to be indented
for readability. The MATLAB editor smart-indents nested loops and other
constructs as we type. For example indentation makes the following loop more
readable.
The if statement:
The if statement executes a group of commands depending on the value of a
logical expression. The else and elseif constructs are optional and provide
additional branching. For example
EXPERIMENT NO.-2
PART 1
1. Introduction :
MATLAB is a high-performance language for technical computing. It
integrates computation, visualization, and programming in an easy-to-use
environment where problems and solutions are expressed in familiar
mathematical notation.
that does not require dimensioning. This allows you to solve many technical
computing problems, especially those with matrix and vector formulations, in a
fraction of the time it would take to write a program in a scalar non interactive
language such as C or Fortran. The name MATLAB stands for matrix laboratory.
Starting MATLAB on Windows platforms, start MATLAB by double-clicking the
MATLAB shortcut icon on your Windows desktop.
Quitting MATLAB: To end your MATLAB session, select File > Exit MATLAB in
the desktop, or type quit in the Command Window. You can run a script file
named finish.m each time MATLAB quits that, for example, executes functions
to save the workspace.
2. MATLAB desktop:
Command Window : Use the Command Window to enter variables and to run
functions and M-file scripts. Press the up arrow key to recall a statement
previously typed. Edit the statement as needed and then press Enter to run it.
(ii) Current Directory Browser and Search Path : MATLAB file operations use the
current directory and the search path as reference points. Any file required to
run must either be in the current directory or on the search path.
(iii) Workspace Browser and Array Editor :The MATLAB workspace consists of
the set of variables (named arrays) built up during a MATLAB session and
stored in memory. The variables can be added to the workspace by using
functions, running M-files, and loading saved workspaces. To delete variables
from the workspace, select the variables and select Edit > Delete.
Array Editor : Double-click a variable in the Workspace browser, or use open
var variable name, to see it in the Array Editor. Use the Array Editor to view and
edit a visual representation of variables in the workspace.
user interface for text editing, as well as for M-file debugging. To create or edit
an M-file use File > New or File > Open, or use the edit function.
1. clc (Remove items from workspace, freeing up system memory) clears all
input and output from the Command Window display, giving "clean
screen." After using clc, the scroll bar cannot be used to see the history of
functions, but still the up arrow can be used to recall statements from the
command history.
2. close (Remove specified figure): close deletes the current figure or the
specified figure(s). It optionally returns the status of the close operation.
3. xlabel, ylabel, zlabel (Label x-, y-, and z-axis) : Each axes graphics object
can have one label for the x-, y-, and z-axis. The label appears beneath its
respective axis in a two-dimensional plot and to the side or beneath the axis
in a three-dimensional plot.
xlabel('string') labels the x-axis of the current axes.
ylabel(...) and zlabel(...) label the y-axis and z-axis, respectively, of the
current axes.
4. title( Add title to current axes) : Each axes graphics object can have one
title. The title is located at the top and in the center of the axes.
title('string') outputs the string at the top and in the center of the current
axes.
5. figure (create figure graphics object) : figure creates figure graphics objects.
Figure objects are the individual windows on the screen in which MATLAB
displays graphical output.
6. subplot (Create axes in tiled positions): subplot divides the current figure
into rectangular panes that are numbered row wise. Each pane contains an
axes object. Subsequent plots are output to the current pane.
subplot(2,1,1), plot(income)
subplot(2,1,2), plot(outgo) plots income on the top half of the window and
outgo on the bottom half.
8. bar(Plot bar graph (vertical and horizontal)) : A bar graph displays the
values in a vector or matrix as horizontal or vertical bars.
bar(Y) draws one bar for each element in Y. If Y is a matrix, bar groups the
bars produced by the elements in each row. The x-axis scale ranges from 1
up to length(Y) when Y is a vector, and 1 to size(Y,1), which is the number of
rows, when Y is a matrix.
barh(...) and h = barh(...) create horizontal bars. Y determines the bar
length. The vector x is a vector defining the y-axis intervals for horizontal
bars.
10.input (Request user input) : The response to the input prompt can be any
MATLAB expression, which is evaluated using the variables in the current
workspace.
user_entry = input('prompt') Displays prompt as a prompt on the
screen, waits for input from the keyboard, and returns the value entered in
user_entry. user_entry = input('prompt', 's') returns the entered string as a
text variable rather than as a variable name or numerical value.
r = roots(p) which returns a column vector whose elements are the roots
of the polynomial specified by the coefficients row vector p. For vectors,
roots and poly are inverse functions of each other, up to ordering, scaling,
and round off error.
B = sqrt(X) returns the square root of each element of the array X. For the
elements of X that are negative or complex, sqrt(X) produces complex
results.
B = fliplr(A) returns A with columns flipped in the left-right direction, that is,
about a vertical axis.If A is a row vector, then fliplr(A) returns a vector of the
same length with the order of its elements reversed. If A is a column vector,
then fliplr(A) simply returns A.
ind = find(X) locates all nonzero elements of array X, and returns the linear
indices of those elements in vector ind. If X is a row vector, then ind is a row
vector; otherwise, ind is a column vector. If X contains no nonzero elements
or is an empty array, then ind is an empty array.
[r,p,k] = residuez(b,a) finds the residues, poles, and direct terms of a partial
fraction expansion of the ratio of two polynomials, b(z) and a(z). Vectors b
and a specify the coefficients of the polynomials of the discrete-time system
b(z)/a(z) in descending powers of z.
PART 2
Basic Plotting of Signals
Using MATLAB, make plots of the signals below. Put your code in a Matlab script
file so you can rerun it from the Matlab command after you make revisions to
your life.
Use the subplot command to put several plots on the same page. Print out the plots
and turn them in with your code. Use help subplot to find out how to use the
command.
Use the plot command to plot continuous-time signals. Use the stem command to
plot discrete-time signals. Use help plot and help stem to find out how to
use these commands. Our convention is that f (t) denotes a continuous-time
signal, and that f (n) denotes a discrete-time signal.
Output:-
Unit Impulse :-
% program for the generation of the unit impulse signal
clc;
clear all;
close all;
t=-2:1:2;
y=[zeros(1,2),ones(1,1),zeros(1,2)];
subplot(2,2,1);
stem(t,y);
ylabel('Amplitude.........>');
xlabel('n..........>');
Output:-
(Fig. :- Unit Impulse signal)
Ramp Sequence:-
%Program for the generation of Ramp sequence
clc;
close all;
clear all;
n1 = input('Enter the length of Ramp sequence');
t = 0:n1;
subplot(2,2,3);
stem(t,n1);
ylabel('Amplitude--------->');
xlabel('n------------->');
Output:-
( Fig: Ramp Sequence)
Exponential Sequence:-
Sine Waveform:-
%Program for the generation of Sine waveform
clc;
close all;
clear all;
t = 0:0.01:pi;
y=sin(2*pi*t);
figure(2);
subplot(2,2,1);
plot(t,y);
ylabel('Amplitude........>');
xlabel('n............>');
OUTPUT:-
(Fig: Sine waveform)
Cosine waveform:-
% Program for the generation of cosine sequence
clc;
close all;
clear all;
t=0:0.01:pi;
y=cos(2*pi*t);
figure(2);
subplot(2,1,1);
plot(t,y);
ylabel('Amplitude.........>');
xlabel('n........>');
OUTPUT:-
(Fig: Cosine waveform)
CONCLUSION:
AIM:
APPARATUS REQUIRED:
PROGRAM:
clear all
close all
%Shifting
t=-2:1:2;
x=[1 2 3 4 5];
subplot(2,2,1);
stem(t,x);
title('input sgnal');
ylabel('ampli--->');
xlabel('n---->');
n=input('enter n value');
t1=t+n;
subplot(2,2,2);
stem(t1,x);
title('shifting');
ylabel('ampli---->');
xlabel('n----->');
%time scaling
t=-2:1:2;
x=[2 3 4 5 6 ];
subplot(2,2,3);
stem(t,x);
title('input signal');
ylabel('ampli---->');
xlabel('n---->');
n=input('enter the value of n');
t1=t.*n;
subplot(2,2,4);
stem(t1,x);
title('time scaling signal');
ylabel('amp---->');
xlabel('n---->');
4 4
ampli---->
ampli--->
2 2
0 0
-2 -1 0 1 2 4 5 6 7 8
n----> n----->
input signal time scaling signal
6 6
4 4
ampli---->
amp---->
2 2
0 0
-2 -1 0 1 2 -10 -5 0 5 10
n----> n---->
CONCLUSION:
EXPERIMENT NO.-4
AIM:
1. Write a MATLAB program to obtain linear convolution of the given CT and
DT sequence using the function conv.
2. Compare and verify the result obtained with the expected theoretical results.
APPARATUS REQUIRED:
PROGRAM:
clc;
clear all;
close all;
x=input('enter the 1st sequence');
h=input('enter the 2nd sequence');
y=conv(x,h);
figure;
subplot(3,1,1);
stem(x);
ylabel('Amplitude-->');
xlabel('(a)n-->');
subplot(3,1,2);
stem(h);
ylabel('Amplitude-->');
xlabel('(b)n-->');
subplot(3,1,3);
stem(fliplr(y));
ylabel('Amplitude-->');
xlabel('(c)n-->');
disp('The resultant signal is');
OUTPUT:-
CONCLUSION:
From this experiment we have concluded that a program for discrete convolution
can be developed using MATLAB successfully.
EXPERIMENT NO.-5
AIM:
1. Write a MATLAB program to compute autocorrelation of a sequence x(n)
and verify the property.
2. Write a MATLAB program to compute cross-correlation of a sequence x(n)
and y(n) and verify the property.
APPARATUS REQUIRED:
PROGRAM: [CROSS-CORRELATION]
%Program for computing cross correlation of the
%sequences x=[1,2,3,4] and h=[4,3,2,1]
clc;
clear all;
close all;
x=input('enter the 1st sequence');
h=input('enter the 2nd sequence');
y=xcorr(x,h);
figure;
subplot(3,1,1);
stem(x);
ylabel('Amplitude-->');
xlabel('(a)n-->');
subplot(3,1,2);
stem(h);
ylabel('Amplitude-->');
xlabel('(b)n-->');
subplot(3,1,3);
stem(fliplr(y));
ylabel('Amplitude');
xlabel('(c)n-->');
disp('The resultant signal is');
fliplr(y)
OUTPUT:-
CONCLUSION:-
PROGRAM: [AUTOCORRELATION]
clear all
close all
% Define range of t in the interval [-1;1] with steps of 0.01
t = [-1:.01:1];
% Compute the function with input vector t (with absolute values)
R = 10 * exp(-2 * abs(t));
plot(R);
title('auto-correlation');
xlabel('time index');
ylabel('amplitude');
R
UOTPUT:
auto-correlation
10
7
amplitude
1
0 50 100 150 200 250
time index
SECOND PROGRAM:
clear all;
x=input('enter the sequence x=');
N=length(x);
y=x;
z=x;
for i=1:N-1
for i=N-1:-1:1
x(i+1)=x(i);
end
x(1)=0;
z=[z; x];
end;
m=[z]*[y'];
m=m/N
stem(m);
title('auto-correlation');
xlabel('time index');
ylabel('amplitude');
OUTPUT:
enter the sequence x=[1 3 5]
m=
11.6667
6.0000
1.6667
auto-correlation
12
10
8
amplitude
0
1 1.2 1.4 1.6 1.8 2 2.2 2.4 2.6 2.8 3
time index
EXPERIMENT NO.-6
AIM:
1. To calculate Fourier series coefficients associated with Square Wave and
Triangular wave.
2. To sum the first 10 terms and plot the Fourier series as a function of time.
3. To sum the first 50 terms and plot the Fourier Series as a function of time .
APPARATUS REQUIRED:
OUTPUT:
Triangular wave
3
2.5
1.5
x(t)
0.5
-0.5
-1
-4 -3 -2 -1 0 1 2 3 4
t (seconds)
The DC value:
a0 =
An =
(4*sin(pi*k)^2 - 8*sin((pi*k)/2)^2)/(pi^2*k^2)
Bn =
x(t)=
1 - (cos(pi*k*t)*(2*cos(2*pi*k) - 4*cos(pi*k) + 2))/(pi^2*k^2)
Xn =
(2*(-1)^k - 2)/(pi^2*k^2)
Pavg =
SQUARE WAVE:
CONCLUSION:
EXPERIMENT NO.-7
AIM:
1. Calculate Fourier Transform of a given signal and plot the magnitude and
the phase spectrum.
2. Calculate the Z-transform of a given signal and plot the poles and zeros of
the Z-transform
APPARATUS REQUIRED:
OUTPUT:
Magnitude part real part
2 2
Magnitude
1.5 1.5
real
1 1
0.5 0.5
0 0.5 1 0 0.5 1
frequency in pi units frequency in pi units
Angle part Imaginary part
0 0
-0.2 -0.2
Imaginary
Radians
-0.4 -0.4
-0.6 -0.6
-0.8 -0.8
0 0.5 1 0 0.5 1
frequency in pi units frequency in pi units
0.8
0.6
0.4
Imaginary Part
0.2
-0.2
-0.4
-0.6
-0.8
-1
-1 -0.5 0 0.5 1
Real Part
CONCLUSION:
EXPERIMENT NO.-8
AIM:
1. Write a MATLAB program to find the impulse response and step response of
a system from its difference equation.[ y(n) = x(n) + x(n-1) – 0.7y(n-1) at
-20 ≤ n ≤ 100.]
2. Compute and plot the sequence of a given system to a given input.
APPARATUS REQUIRED:
PROGRAM:
OUTPUT:
Impulse Response
1
0.5
Amplitude
-0.5
-20 -15 -10 -5 0 5 10
Samples
Step Response
1.5
1
Amplitude
0.5
0
-20 -15 -10 -5 0 5 10
Samples
CONCLUSION:
EXPERIMENT NO.-9
AIM:
1. For a given system function write a MATLAB program to plot the poles,
zero configuration and Bode plot.
2. Analyze the stability of the given system from the Bode plot
APPARATUS REQUIRED:
OUTPUT:
1
Imaginary Part
-1
-2
-3
-6 -5 -4 -3 -2 -1 0 1
Real Part
BODE DIAGRAM:
FOR
PROGRAM:
OUTPUT:
Bode Diagram
0
-10
Magnitude (dB)
-20
-30
-40
0
Phase (deg)
-45
-90
3 4 5 6
10 10 10 10
Frequency (rad/sec)
STABILITY TEST:
PROGRAM:
%Program for stability test
clc;
clear all;
close all;
b=input('enter the denominator cofficients of the
filter');
k=poly(b);
knew=fliplr(k);
s=all(abs(knew));
if(s==1)
disp('stable system');
else
disp('Non-stable System');
end;
OUTPUT:
stable system
CONCLUSION:-
BODE PLOT:
PROGRAM:
%MATLAB Code FOR STABILITY FROM BODE PLOT
clear all;
close all;
%BODE_MARGINS_MAT
fig_size = [232 84 774 624];
olz = [-1;-2]; % Transfer function zeros
olp = [0;-.3;-.7+j*2;-.7-j*2;-15]; % Transfer function poles
num = real(poly(olz)); % Numerator polynomial
den = real(poly(olp)); % Denominator polynomial
w = logspace(-3,3,601); % frequency vector
[mag1,ph1] = bode(num,den,w); % open-loop frequency response for K=1
K1 = 2; % 1st Transfer function gain
OUTPUT:
Bode Magnitude and Phase Plots, K = 2
50
0
M agnitude (db) & P hase (deg)
-50
-100
-150
-200
-250
-300
-3 -2 -1 0 1 2 3
10 10 10 10 10 10 10
Frequency (r/s)
0
M agnitude (db) & P hase (deg)
-50
-100
-150
-200
-250
-300
-3 -2 -1 0 1 2 3
10 10 10 10 10 10 10
Frequency (r/s)
Gain and Phase Margins, K = 25
100
50
M agnitude (db) & P has e (deg)
-50
-100
-150
-200
-250
-300
-3 -2 -1 0 1 2 3
10 10 10 10 10 10 10
Frequency (r/s)
50
0
M agnitude (db) & P hase (deg)
-50
-100
-150
-200
-250
-300
-3 -2 -1 0 1 2 3
10 10 10 10 10 10 10
Frequency (r/s)
EXPERIMENT NO.-10
AIM:
1. Write a MATLAB program to plot the frequency response of the first order
system.
2. Also write MATLAB program to calculate the rise time and bandwidth of
same system.
APPARATUS REQUIRED:
Note that they have to be the same length and start with the coefficient of the
highest order term. Then define w as the vector of frequencies over which you
want to plot the magnitude and phase responses.
PROGRAM:
OUTPUT:
-0.87
10
Magnitude
-0.96
10
-1 0
10 10
Frequency (rad/s)
200
Phase (degrees)
100
-100
-1 0
10 10
Frequency (rad/s)
clear all;
close all;
sys = tf([1 5],[1 2 5 7 2]);
S = stepinfo(sys,'RiseTimeLimits',[0.05,0.95]);
S
fb = bandwidth(sys);% bandwidth in radians
fb
OUTPUT:
S=
RiseTime: 7.4454
SettlingTime: 13.9378
SettlingMin: 2.3737
SettlingMax: 2.5201
Overshoot: 0.8032
Undershoot: 0
Peak: 2.5201
PeakTime: 15.1869
fb =
0.3724
CONCLUSION: