0% found this document useful (0 votes)
4 views39 pages

MATLAB Basics for Signal Design Lab

Signal

Uploaded by

umarbek777001
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views39 pages

MATLAB Basics for Signal Design Lab

Signal

Uploaded by

umarbek777001
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Signal and System Design

(IGS2137)

Week 2 Lab
ISE Department
Prof. Mehdi Pirahandeh

School of Global Convergence Studies


Content

• Introduction to MATLAB
• Creating Variables
• Some Useful MATLAB Functions
• Data Types
• Q &A
• Survey

School of Global Convergence Studies 2


What is MATLAB®?

MATLAB® /Simulink® is a powerful software tool for:


 Performing mathematical computations and signal processing
 Analyzing and visualizing data (excellent graphics tools)
 Modeling physical systems and phenomena
 Testing engineering designs

School of Global Convergence Studies


Industry Applications
 Aircraft/Defense: control and guidance system design and si
mulation, communications
 Robotics: design and control
 Automotive: cruise control, stability enhancement, fuel inject
ion systems, hybrid power-train, sound suppression …
 Communications: voice over internet, cell-phone, satellite, a
ntenna design, wireless, error coding …
 Biotech, Pharmaceutical, Medical: drug discovery and devel
opment, imaging procedures, cancer diagnosis …

School of Global Convergence Studies


Industry Applications (con’t)
 Electronics: chip design, acoustics, voice processing and reco
gnition
 Industrial Automation and Machinery: sensor design, mach
inery design and control
 Utilities and Energy: power conversion and control
 Computers: security systems, printer design
 Financial: portfolio management and risk, commodity trading
, currency markets

School of Global Convergence Studies


MATLAB® DESKTOP

School of Global Convergence Studies


MATLAB Desktop
 The Command window is where you type MATLAB
commands following the prompt: EDU>>
 The Workspace window shows all the variables you
have defined in your current session. Variables can a
ctually be manipulated within the workspace window.
 The Command History window displays all the MA
TLAB commands you have used recently – even incl
udes some past sessions.
 The Current Folder window displays all the files in
whatever folder you select to be current.

School of Global Convergence Studies


MATLAB Desktop

• You can select what is on your desktop by Clicking on Layout.


• Go down to Command History and select docked.

School of Global Convergence Studies


USING MATLAB® AS A
CALCULATOR

School of Global Convergence Studies


Arithmetic Operators and Order
of Operations
 Addition (+), Subtraction (-), Multiplication (*), Division (/), P
ower (^)
 Order of Operations (same rules you should already know fro
m math class and using a calculator)
1. Complete all calculations inside parenthesis or
brackets using the precedent rules below
2. Powers (left to right)
3. Multiplication and Division (left to right)
4. Addition and Subtraction (left to right)

School of Global Convergence Studies


Arithmetic Operators and Order
of Operations
Some Examples:

EDU>> 10/5*2

EDU>> 5*2^3+4(2)

EDU>> -1^4

EDU>> 8^1/3

School of Global Convergence Studies


Exercise 1
Calculate the area and circumference of a circle with a
radius of 4 cm.

Area = 50.27 cm2


Circumference = 25.13 cm

School of Global Convergence Studies


CREATING AND USING VARIA
BLES

School of Global Convergence Studies


Variables
 In the previous exercise, you probably typed in a
value for pi.

 MATLAB has a built-in variable for pi. Try this:

EDU>> pi

School of Global Convergence Studies


Variables
You can even create your own variables. Try this:
EDU>> radius = 4

What do you see in your workspace window?

Now try this:


EDU>> area = pi*radius^2

What do you see in your workspace window now?

School of Global Convergence Studies


Naming Rules for Variables
1. Variable names must begin with a letter
2. Names can include any combinations of letters, numbers, and
underscores
3. Maximum length for a variable name is 63 characters
4. MATLAB® is case sensitive. The variable name A is differe
nt than the variable name a.
5. Avoid the following names: i, j, pi, and all built-in MATLAB
® function names such as length, char, size, plot, break, cos, l
og, …
6. It is good programming practice to name your variables to ref
lect their function in a program rather than using generic x, y,
z variables.

School of Global Convergence Studies


Creating Variables & Assigning Values
At the MATLAB command prompt (EDU>>) type:
x = 10.57;
What happens? (Hint: look at workspace window)

Several things happen with this simple MATLAB command:

 A variable, x, of type double is created


 A memory location for the variable x is assigned
 The value 10.57 is stored in that memory location called x.

School of Global Convergence Studies


Creating Variables & Assigning Values
At the MATLAB command prompt (EDU>>) type:

x = 73.65

What happens?

 The old value for x (10.57) is replaced by the new value (73.65)
 Also, since the semicolon was left off the end, we see the result in
the command window (as well as in the workspace window)

School of Global Convergence Studies


Exercise 2
1. In MATLAB create two variables: a = 4 and b = 17.2
2. Now use MATLAB to perform the following set of calculatio
ns:

5a

School of Global Convergence Studies


Creating Strings (Text Variables)
Variable do not have to be numbers. At the MATLAB command
prompt type:
month = ‘August’
What happens?

Several things happen with this simple MATLAB command:

 A variable, month, of type string (character array) is created


 A memory location for the variable month is assigned
 The string August is stored in that memory location called month.
 Notice that to enter a string, we must put single quotes around it

School of Global Convergence Studies


Displaying Variables
We can display a variable (i.e., show its value) by simply
typing the name of the variable at the command prompt
(leaving off the semicolon). Try this!

We can also use a function called disp to display variables.


Type the following commands at the command prompt:

>> disp(‘The value of x is:’); disp(x)

School of Global Convergence Studies


SOME USEFUL MATLAB
FUNCTIONS

School of Global Convergence Studies


Some MATLAB® Math Functions
Function MATLAB® Function MATLAB®
cosine cos or cosd square root sqrt
sine sin or sind exponential exp
tangent tan or tand logarithm (base 10) log10
cotangent cot or cotd natural log (base e) log
arc cosine acos or acosd round to nearest integer round
arc sine asin or asind round down to integer floor
arc tangent atan or atand round up to integer ceil
arc cotangent acot or acotd

Note: cos(α) assumes α in radians; whereas, cosd(α) assumes α in degrees.


acos(x) returns the angle in radians; whereas, acosd(x) returns the
angle in degrees.
π radians = 180 degrees

School of Global Convergence Studies


Other Useful Stuff
 clear clears all variables in the MATLAB® workspace
 clear a, b just clears variables a and b
 clc clears the command window
 i and j are defined in MATLAB to be −𝟏𝟏.
 If you define these variables to be something else, you lose the abilit
y to work with complex numbers. So, avoid using i and j as variable
s.
 pi is defined in MATLAB as 3.14159….

School of Global Convergence Studies


Help!
 The help command provides information about a function. Type hel
p cos at the command prompt. This only works if you know the nam
e of the function you want help with.

 You can also click on Help in the MATLAB toolbar and search for in
formation by keyword(s).

School of Global Convergence Studies


HOW COMPUTERS STORE
VARIABLES

School of Global Convergence Studies


How Computers Store Variables

Suppose we type the following commands in MATLAB:

EDU>> y = 42;
EDU>> FavoriteDay = ‘Friday’

We know MATLAB stores the values associated with the


variables, y and FavoriteDay, in memory.

How are these values stored?

School of Global Convergence Studies


How Computers Store Variables

Computers store all data (numbers, letters, instructions, …) as strings


of 1s and 0s (bits).

A bit is short for binary digit. It has only two possible


values: On (1) or Off (0).

School of Global Convergence Studies


Terminology
A bit is short for binary digit. It has only two possible
values: On (1) or Off (0).

A byte is simply a string of 8 bits.


A kilobyte (kB) is 1000 bytes
A megabyte (MB) is 1,000,000 bytes
A gigabyte (GB) is 1,000,000,000 bytes

For a sense of size, click on link below:


[Link]
-[Link]

School of Global Convergence Studies


Numeric Data Types
MATLAB has several different options for storing numbers as bits.
Unless you specify otherwise, all numbers in MATLAB are stored as
doubles.

Name Description Range


-1.79769313486232E308 to -4.94065645841247E-324
double 64 bit floating point 4.94065645841247E-324 to 1.79769313486232E308
-3.402823E38 to -1.401298E-45
single 32 bit floating point
1.401298E-45 to 3.402823E38
uint8 8 bit unsigned integer Integers from 0 to 255
int8 8 bit signed integer Integers from -128 to 127
uint16 16 bit unsigned integer Integers from 0 to 65535
int16 16 bit signed integer Integers from -32768 to 32767
uint32 32 bit unsigned integer Integers from 0 to 4294967295
int32 32 bit signed integer Integers from -2147483648 to 2147483647
School of Global Convergence Studies
Data Types:
int8, uint8, int16, uint16, int32, uint32

 These data types work for integers as long as the integers don’t
exceed the range for the data type chosen.
 They take up less memory space than doubles.
 They don’t work for non-integers. If you create a variable that
is an int8 and try to assign it a value of 14.8, that variable will
be assigned a value of 15 instead (closest integer within the ran
ge).
 One common application for integer data types is image data (j
peg, png, …)

School of Global Convergence Studies


Data Types:
double and single
 A double uses 64 bits to store a number.
 A single uses 32 bits to store a number.
 Doubles and singles can be used to represent both integers and
non-integers.

School of Global Convergence Studies


Why should I care how data is stored in
a computer?

Example: Perform each of the following calculations in your head.

a = 4/3
b=a–1
c = 3*b
e=1–c

What does MATLAB get?

School of Global Convergence Studies


Why should I care how data is stored in a com
puter?
What does MATLAB get?
a = 4/3 = 1.3333
b = a – 1 = 0.3333
c = 3*b = 1.0000
e = 1 – c = 2.2204e-016

It is not possible to perfectly represent all real numbers us


ing a finite string of 1s and 0s.

School of Global Convergence Studies


Comments
 Not all numbers can be represented exactly even using 64 bit dou
bles. If we do many, many calculations with numbers which are ju
st a tiny bit off, that error can grow very, very large depending on
the type of computations being performed.
 64 bit doubles have a huge, but still limited range. What happens
if we exceed it? Try the following:

EDU>> a = 373^1500
EDU>> b = factorial(172)

School of Global Convergence Studies


ASCII Code

When you press a key on your computer keyboard, the key that y
ou press is translated to a binary code.

A = 1000001 (Decimal = 65)


a = 1100001 (Decimal = 97)
0 = 0110000 (Decimal = 48)

School of Global Convergence Studies


ASCII Code

School of Global Convergence Studies


Strings in MATLAB
 MATLAB stores strings as an array of characters using the ASCII co
de.
 Each letter in a string takes up two bytes (16 bits) and the two bytes
are the binary representation of the decimal number listed in the AS
CII table.

Try the following in MATLAB:

EDU>> month = ‘August’


EDU>> double(month)

School of Global Convergence Studies


Brief break (if on schedule)

Q&A?
Prof. Mehdi Pirahandeh
E-mail : mehdi@[Link]

School of Global Convergence Studies

You might also like