Signals and Systems Practical File
Signals and Systems Practical File
Practical File
Submitted By:
Tarun Sharma
Roll no. : 252001003
Electronics and Communications Engineering
U.I.E.T. (KUK)
Submitted To:
Miss. Ekta Ma’am
Assistant Professor
Department of Electronics And
Communications Engineering
U.I.E.T. (KUK)
1
Experiment 1
AIM: To get introduced about MATLAB software, views, widgets, etc.
Tools used : MATLAB R2021a
Theory :
Desktop Basics
When you start MATLAB®, the desktop appears in its default layout.
2
Create a few more variables.
b=2
b=
2
c=a+b
c=
3
d = cos(a)
d=
0.5403
When you do not specify an output variable, MATLAB uses the variable ans, short
for answer, to store the results of your calculation.
sin(a)
ans =
0.8415
If you end a statement with a semicolon, MATLAB performs the computation, but
suppresses the display of output in the Command Window.
e = a*b;
You can recall previous commands by pressing the up- and down-arrow keys, ↑ and ↓.
Press the arrow keys either at an empty command line or after you type the first few
characters of a command. For example, to recall the command b = 2, type b, and then press
the up-arrow key.
Get Started with MATLAB
3
NEXT
1 2 3 4
1 3 5
2 4 6
7 8 10
Another way to create a matrix is to use a function, such as ones, zeros, or rand. For
example, create a 5-by-1 column vector of zeros.
z = zeros(5,1)
z = 5×1
0
0
0
0
0
4
Matrix and Array Operations
MATLAB allows you to process all of the values in a matrix using a single arithmetic operator
or function.
a + 10
ans = 3×3
11 13 15
12 14 16
17 18 20
sin(a)
ans = 3×3
1 2 7
3 4 8
5 6 10
You can perform standard matrix multiplication, which computes the inner products
between rows and columns, using the * operator. For example, confirm that a matrix times
its inverse returns the identity matrix:
p = a*inv(a)
p = 3×3
1.0000 0 0
0 1.0000 0
0 -0.0000 1.0000
Notice that p is not a matrix of integer values. MATLAB stores numbers as floating-point
values, and arithmetic operations are sensitive to small differences between the actual
value and its floating-point representation. You can display more decimal digits using
the format command:
format long
p = a*inv(a)
p = 3×3
5
1.0000 0 0
0 1.0000 0
0 -0.0000 1.0000
1 9 25
4 16 36
49 64 100
The matrix operators for multiplication, division, and power each have a corresponding
array operator that operates element-wise. For example, raise each element of a to the
third power:
a.^3
ans = 3×3
1 27 125
8 64 216
343 512 1000
Concatenation
Concatenation is the process of joining arrays to make larger ones. In fact, you made your
first array by concatenating its individual elements. The pair of square brackets [] is the
concatenation operator.
A = [a,a]
A = 3×6
1 3 5 1 3 5
2 4 6 2 4 6
7 8 10 7 8 10
6
A = [a; a]
A = 6×3
1 3 5
2 4 6
7 8 10
1 3 5
2 4 6
7 8 10
Complex Numbers
Complex numbers have both real and imaginary parts, where the imaginary unit is the
square root of -1.
sqrt(-1)
ans = 0.0000 + 1.0000i
To represent the imaginary part of complex numbers, use either i or j.
c = [3+4i, 4+3j; -i, 10j]
c = 2×2 complex
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
There are two ways to refer to a particular element in an array. The most common way is to
specify row and column subscripts, such as
A(4,2)
ans = 14
Less common, but sometimes useful, is to use a single subscript that traverses down each
column in order:
7
A(8)
ans = 14
Using a single subscript to refer to a particular element in an array is called linear indexing.
If you try to refer to elements outside an array on the right side of an assignment statement,
MATLAB throws an error.
test = A(4,5)
Index in position 2 exceeds array bounds (must not exceed 4).
However, on the left side of an assignment statement, you can specify elements outside the
current dimensions. The size of the array increases to accommodate the newcomers.
A(4,5) = 17
A = 4×5
1 2 3 4 0
5 6 7 8 0
9 10 11 12 0
13 14 15 16 17
To refer to multiple elements of an array, use the colon operator, which allows you to
specify a range of the form start:end. For example, list the elements in the first three rows
and the second column of A:
A(1:3,2)
ans = 3×1
2
6
10
The colon alone, without start or end values, specifies all of the elements in that dimension.
For example, select all the columns in the third row of A:
A(3,:)
ans = 1×5
9 10 11 12 0
The colon operator also allows you to create an equally spaced vector of values using the
more general form start:step:end.
B = 0:10:100
B = 1×11
0 10 20 30 40 50 60 70 80 90 100
8
If you omit the middle step, as in start:end, MATLAB uses the default step value of 1.
Workspace Variables
The workspace contains variables that you create within or import into MATLAB ® from data
files or other programs. For example, these statements create variables A and B in the
workspace.
A = magic(4);
B = rand(3,5,2);
You can view the contents of the workspace using whos.
whos
Name Size Bytes Class Attributes
Workspace variables do not persist after you exit MATLAB. Save your data for later use with
the save command,
save myfile.mat
Saving preserves the workspace in your current working folder in a compressed file with
a .mat extension, called a MAT-file.
To clear all the variables from the workspace, use the clear command.
Restore data from a MAT-file into the workspace using load.
load myfile.mat
9
q=
Creating string arrays with double quotes was introduced in R2017a. If you are using an
earlier release, create character arrays. For details, see Data in Character Arrays.
To add text to the end of a string, use the plus operator, +.
f = 71;
c = (f-32)/1.8;
tempText = "Temperature is " + c + "C"
tempText =
"Temperature is 21.6667C"
Similar to numeric arrays, string arrays can have multiple elements. Use
the strlength function to find the length of each string within an array.
A = ["a","bb","ccc"; "dddd","eeeeee","fffffff"]
A=
2×3 string array
"a" "bb" "ccc"
"dddd" "eeeeee" "fffffff"
strlength(A)
ans =
1 2 3
4 6 7
Data in Character Arrays
Sometimes characters represent data that does not correspond to text, such as a DNA
sequence. You can store this type of data in a character array, which has data type char.
Character arrays use single quotes.
seq = 'GCTAGAATCC';
whos seq
Name Size Bytes Class Attributes
seq 1x10 20 char
Each element of the array contains a single character.
seq(4)
ans =
'A'
Concatenate character arrays with square brackets, just as you concatenate numeric arrays.
10
seq2 = [seq 'ATTAGAAACC']
seq2 =
'GCTAGAATCCATTAGAAACC'
Character arrays are common in programs that were written before the introduction of
string arrays. All MATLAB functions that accept string data also accept char data, and vice
versa.
Calling Functions
1 3 5 6 9
11
2-D and 3-D Plots
12
By adding a third input argument to the plot function, you can plot the same variables using
a red dashed line.
plot(x,y,"r--")
13
"r--" is a line specification. Each specification can include characters for the line color, style,
and marker. A marker is a symbol that appears at each plotted data point, such as a +, o,
or *. For example, "g:*" requests a dotted green line with * markers.
Notice that the titles and labels that you defined for the first plot are no longer in the
current figure window. By default, MATLAB® clears the figure each time you call a plotting
function, resetting the axes and other elements to prepare the new plot.
To add plots to an existing figure, use hold on. Until you use hold off or close the window, all
plots appear in the current figure window.
x = linspace(0,2*pi);
y = sin(x);
plot(x,y)
hold on
y2 = cos(x);
plot(x,y2,":")
legend("sin","cos")
hold off
14
3-D Plots
Three-dimensional plots typically display a surface defined by a function in two
variables, z=f(x,y). For instance, calculate z=xe−x2−y2 given row and column
vectors x and y with 20 points each in the range [-2,2].
x = linspace(-2,2,20);
y = x';
z = x .* exp(-x.^2 - y.^2);
Then, create a surface plot.
surf(x,y,z)
15
Both the surf function and its companion mesh display surfaces in three
dimensions. surf displays both the connecting lines and the faces of the surface in
color. mesh produces wireframe surfaces that color only the connecting lines.
Multiple Plots
You can display multiple plots in different parts of the same window using
either tiledlayout or subplot.
The tiledlayout function was introduced in R2019b and provides more control over labels
and spacing than subplot. For example, create a 2-by-2 layout within a figure window. Then,
call nexttile each time you want a plot to appear in the next region.
t = tiledlayout(2,2);
title(t,"Trigonometric Functions")
x = linspace(0,30);
nexttile
plot(x,sin(x))
title("Sine")
nexttile
plot(x,cos(x))
title("Cosine")
16
nexttile
plot(x,tan(x))
title("Tangent")
nexttile
plot(x,sec(x))
title("Secant")
The simplest type of MATLAB® program is called a script. A script is a file that contains
multiple sequential lines of MATLAB commands and function calls. You can run a script by
typing its name at the command line.
Scripts
To create a script, use the edit command,
edit mysphere
This command opens a blank file named mysphere.m. Enter some code that creates a unit
sphere, doubles the radius, and plots the results:
[x,y,z] = sphere;
r = 2;
17
surf(x*r,y*r,z*r)
axis equal
Next, add code that calculates the surface area and volume of a sphere:
A = 4*pi*r^2;
V = (4/3)*pi*r^3;
Whenever you write code, it is a good practice to add comments that describe the code.
Comments enable others to understand your code and can refresh your memory when you
return to it later. Add comments using the percent (%) symbol.
% Create and plot a sphere with radius r.
[x,y,z] = sphere; % Create a unit sphere.
r = 2;
surf(x*r,y*r,z*r) % Adjust each dimension and plot.
axis equal % Use the same scale for each axis.
18
To create a new live script using the edit command, include the .mlx extension with the file
name:
edit newfile.mlx
Loops and Conditional Statements
Within any script, you can define sections of code that either repeat in a loop or
conditionally execute. Loops use a for or while keyword, and conditional statements
use if or switch.
Loops are useful for creating sequences. For example, create a script named fibseq that uses
a for loop to calculate the first 100 numbers of the Fibonacci sequence. In this sequence, the
first two numbers are 1, and each subsequent number is the sum of the previous
two, Fn = Fn-1 + Fn-2.
N = 100;
f(1) = 1;
f(2) = 1;
for n = 3:N
f(n) = f(n-1) + f(n-2);
end
f(1:10)
19
When you run the script, the for statement defines a counter named n that starts at 3. Then,
the loop repeatedly assigns to f(n), incrementing n on each execution until it reaches 100.
The last command in the script, f(1:10), displays the first 10 elements of f.
ans =
1 1 2 3 5 8 13 21 34 55
Conditional statements execute only when given expressions are true. For example, assign a
value to a variable depending on the size of a random number: 'low', 'medium', or 'high'. In
this case, the random number is an integer between 1 and 100.
num = randi(100)
if num < 34
sz = 'low'
elseif num < 67
sz = 'medium'
else
sz = 'high'
end
The statement sz = 'high' only executes when num is greater than or equal to 67.
Script Locations
MATLAB looks for scripts and other files in certain places. To run a script, the file must be in
the current folder or in a folder on the search path.
By default, the MATLAB folder that the MATLAB Installer creates is on the search path. If
you want to store and run programs in another folder, add it to the search path. Select the
folder in the Current Folder browser, right-click, and then select Add to Path.
20
EXPERIMENT 2
Where T0 = 2πw0
Tangent signal
Since, tan(x)=sin(x)cos(x)tan(x)=sin(x)cos(x) the tangent function is undefined
when cos(x)=0cos(x)=0 . Therefore, the tangent function has a vertical
asymptote whenever cos(x)=0cos(x)=0 .
Similarly, the tangent and sine functions each have zeros at integer multiples
of ππ because tan(x)=0tan(x)=0 when sin(x)=0sin(x)=0 .
The graph of a tangent function y=tan(x)y=tan(x) is looks like this:
21
Program :
FIGURE :
22