0% found this document useful (0 votes)
67 views

Signals and Systems Practical File

Uploaded by

Tarun Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Signals and Systems Practical File

Uploaded by

Tarun Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Signals And Systems

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.

The desktop includes these panels:


 Current Folder — Access your files.
 Command Window — Enter commands at the command line, indicated by the
prompt (>>).
 Workspace — Explore data that you create or import from files.
As you work in MATLAB, you issue commands that create variables and call functions. For
example, create a variable named a by typing this statement at the command line:
a=1
MATLAB adds variable a to the workspace and displays the result in the Command Window.
a=

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 

Matrices and Arrays

Try This ExampleCopy Command  Copy Code


MATLAB is an abbreviation for "matrix laboratory." While other programming languages
mostly work with numbers one at a time, MATLAB® is designed to operate primarily on
whole matrices and arrays.
All MATLAB variables are multidimensional arrays, no matter what type of data. A matrix is
a two-dimensional array often used for linear algebra.
Array Creation
To create an array with four elements in a single row, separate the elements with either a
comma (,) or a space.
a = [1 2 3 4]
a = 1×4

1 2 3 4

This type of array is a row vector.


To create a matrix that has multiple rows, separate the rows with semicolons.
a = [1 3 5; 2 4 6; 7 8 10]
a = 3×3

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

0.8415 0.1411 -0.9589


0.9093 -0.7568 -0.2794
0.6570 0.9894 -0.5440

To transpose a matrix, use a single quote ('):


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

Reset the display to the shorter format using


format short
format affects only the display of numbers, not the way MATLAB computes or saves them.
To perform element-wise multiplication rather than matrix multiplication, use
the .* operator:
p = a.*a
p = 3×3

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

Concatenating arrays next to one another using commas is called horizontal concatenation.


Each array must have the same number of rows. Similarly, when the arrays have the same
number of columns, you can concatenate vertically using semicolons.

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

3.0000 + 4.0000i 4.0000 + 3.0000i


0.0000 - 1.0000i 0.0000 +10.0000i
Array Indexing

Try This ExampleCopy Command  Copy Code


Every variable in MATLAB® is an array that can hold many numbers. When you want to
access selected elements of an array, use indexing.
For example, consider the 4-by-4 matrix A:
A = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]
A = 4×4

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

A 4x4 128 double


B 3x5x2 240 double
The variables also appear in the Workspace pane on the desktop.

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

Text and Characters

Text in String Arrays


When you are working with text, enclose sequences of characters in double quotes. You can
assign text to a variable.
t = "Hello, world";
If the text includes double quotes, use two double quotes within the definition.
q = "Something ""quoted"" and something else."

9
q=

"Something "quoted" and something else."


t and q are arrays, like all MATLAB® variables. Their class or data type is string.
whos t
Name Size Bytes Class Attributes
t 1x1 174 string
Note

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

Try This ExampleCopy Command  Copy Code


MATLAB® provides a large number of functions that perform computational tasks. Functions
are equivalent to subroutines or methods in other programming languages.
To call a function, such as max, enclose its input arguments in parentheses:
A = [1 3 5];
max(A)
ans = 5
If there are multiple input arguments, separate them with commas:
B = [3 6 9];
union(A,B)
ans = 1×5

1 3 5 6 9

Return output from a function by assigning it to a variable:


maxA = max(A)
maxA = 5
When there are multiple output arguments, enclose them in square brackets:
[minA,maxA] = bounds(A)
minA = 1
maxA = 5
Enclose any text inputs in quotes:
disp("hello world")
hello world
To call a function that does not require any inputs and does not return any outputs, type
only the function name:
clc
The clc function clears the Command Window.

11
2-D and 3-D Plots

Try This ExampleCopy Command  Copy Code


Line Plots
To create two-dimensional line plots, use the plot function. For example, plot the sine
function over a linearly spaced vector of values from 0 to 2π:
x = linspace(0,2*pi);
y = sin(x);
plot(x,y)

You can label the axes and add a title.


xlabel("x")
ylabel("sin(x)")
title("Plot of the Sine Function")

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")

Programming and Scripts

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.

% Find the surface area and volume.


A = 4*pi*r^2;
V = (4/3)*pi*r^3;
Save the file in the current folder. To run the script, type its name at the command line:
mysphere
You can also run scripts from the Editor using the Run button,  .
Live Scripts
Instead of writing code and comments in plain text, you can use formatting options in live
scripts to enhance your code. Live scripts allow you to view and interact with both code and
output and can include formatted text, equations, and images.
For example, convert mysphere to a live script by selecting Save As and changing the file
type to a MATLAB live code file (*.mlx). Then, replace the code comments with formatted
text. For instance:
 Convert the comment lines to text. Select each line that begins with a percent
symbol, and then select Text,  . Remove the percent symbols.
 Rewrite the text to replace the comments at the end of code lines. To apply a
monospace font to function names in the text, select  . To add an equation,
select Equation on the Insert tab.

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

Aim : To demonstrate some signals like sine, cosine, tangent.


Tools used : MATLAB R2021a

Theory : Sinusoidal Signal


Sinusoidal signal is in the form of x(t) = A cos(w0±ϕw0±ϕ) or A sin(w0±ϕw0±ϕ)

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

You might also like