Appendix matlab, Study Guides, Projects, Research of Digital Signal Processing

command and functions used in signal processing in matlab

Typology: Study Guides, Projects, Research

2015/2016

Uploaded on 04/25/2016

dilip.ahirwar
dilip.ahirwar 🇬🇧

1 document

1 / 62

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ECE 2610 Signal and Systems A–1
MATLAB Basics
and More
Scalars, Vectors, and Matrices
MATLAB was originally developed to be a matrix laboratory
In its present form it has been greatly expanded to include
many other features, but still maintains inherent matrix and
vector based arithmetic at its core
The basic data structure of MATLAB is the matrix
A matrix with a single row is also known as a row vector
A matrix with a single column is also known as a column
vector
A matrix with just one row and one column (a single ele-
ment) is simply a scalar
A
a11 a12 a13
a21 a22 a23
a31 a32 a33
Bb11 b12
=,=
Cc11
c21
=Dd11
=,
Appendix
A
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e

Partial preview of the text

Download Appendix matlab and more Study Guides, Projects, Research Digital Signal Processing in PDF only on Docsity!

ECE 2610 Signal and Systems A–

M ATLAB Basics

and More

Scalars, Vectors, and Matrices

  • MATLAB was originally developed to be a matrix laboratory
  • In its present form it has been greatly expanded to include

many other features, but still maintains inherent matrix and

vector based arithmetic at its core

  • The basic data structure of MATLAB is the matrix
    • A matrix with a single row is also known as a row vector
    • A matrix with a single column is also known as a column

vector

  • A matrix with just one row and one column (a single ele-

ment) is simply a scalar

A

a 11 a 12 a 13

a 21 a 22 a 23

a 31 a 32 a 33

= , B = b 11 b 12

C

c 11

c 21

= , D = d 11

Appendix

A

Appendix A • MATLAB Basics and More

A–2 ECE 2610 Signals and Systems

Variable Initialization

  • Variable names in MATLAB
    • Must be begin with a letter
    • May contain digits and the underscore character
    • May be any length, but must be unique in the first 19 char-

acters

  • A common way to create a matrix instance is by simply

assigning a list of numbers to it, e.g.

A = [3.1415]; % A 1x1 matrix B = [23, 35.3]; % A 1x2 matrix C = [1, 2, 3; 4, 5, 6; 7, 8, 9]; % a 3x3 matrix

  • Note: a comma or a blank is used to separate elements in

the same row, while a semicolon is used to separate rows

  • The assignment statements listed above are terminated

with a semicolon to suppress MATLAB from echoing the

variables value

  • To continue one line into another we can break a line where a

comma occurs, and then follow the comma with an ellipsis

(three periods in a row), e.g.,

M = [.1, .2, .3, .4, .5, .6, .7, .8, .9, 1.0];

% or break the line M = [.1, .2, .3, .4, .5, ... .6, .7, .8, .9, 1.0];

  • One matrix may be used to define another matrix, e.g., A = [4, 5, 6]; B = [1, 2, 3, A]; % creates B = [1, 2, 3 ,4 ,5 ,6];

Appendix A • MATLAB Basics and More

A–4 ECE 2610 Signals and Systems

  • We can swap the rows and columns of a matrix using the

transpose operator , e.g.,

A = [1, 2, 3];

A_transpose = A’; % produces A_transpose = [1; 2; 3];

  • A simple formatting scheme to have MATLAB display the

values of several equal length vectors side-by-side is the fol-

lowing (actual MATLAB command line interaction)

>> A = [0, 1, 2, 3, 4];
>> B = [0, 1, 4, 9, 16];
>> C = [0, 1, 8, 27, 64];

% Display in side-by-side columns: [A' B' C'] % commas may be included but not needed ans = 0 0 0 1 1 1 2 4 8 3 9 27 4 16 64

Special Values and Matrices

  • To make life easier, MATLAB comes with several predefined

values and matrix generators

  • pi represents in MATLAB floating point precision, e.g.,

    pi ans = 3.

A sub 5 6

Scalars, Vectors, and Matrices

ECE 2610 Signals and Systems A–

  • i, j represents the value
  • Inf is the MATLAB notation for infinity, i.e., 1/
  • Nan is the MATLAB representation for not-a-number ;

often a result of a 0/0 operation

  • clock displays the current time, e.g.,

    clock ans = 1.0e+003 *

1.9970 0.0080 0.0270 0.0230 0.0160 0.

  • date is the current date in a string format

date ans = 23-Aug-

  • eps is the smallest floating-point number by which two

numbers can differ, e.g.,

eps ans = 2.2204e-

  • A matrix of zeros can be generated with A_3x3 = zeros(3,3); % or A_3x3 = zeros(3); B_3x2 = ones(3,2); C_3x3 = ones(size(A_3x3)); - 1

Output Display/Print Formatting

ECE 2610 Signals and Systems A–

  • We create a simple script file (more on this in the next

chapter) user_prompt

  • To run the script we type the file name at the MATLAB

prompt (making sure the file is in the MATLAB path)

clear % clear all variables form the workspace user_prompt % run the script file user_prompt.m Please enter your height and weight: [100 200] x x = 100 200

Output Display/Print Formatting

  • Globally the display format for the command window can be

changed by:

  • Typing commands directly, e.g., format short % four decimal digits format long % 14 decimal digits format short e % short with scientific notation format long e % long with scientific notation % others available

% the *.m file user_prompt.m

x = input(‘Please enter your height and weight: ’);

Appendix A • MATLAB Basics and More

A–8 ECE 2610 Signals and Systems

  • Selecting displays formats from the command window pull

down menus, e.g.,

  • Formatted output from script files is also possible, that is the

format of variables printed to the screen can be made unique

for each variable you want the program to print, and custom

text be distributed around the numeric values

  • The command disp() is used to display text and print the

contents of a matrix

disp('A Text String to Display.') A Text String to Display. M = [1, 2; 3, 4]; disp(M) 1 2 3 4

Appendix A • MATLAB Basics and More

A–10 ECE 2610 Signals and Systems

Data File Commands

  • Data file commands are used to save and load files in

either standard ASCII text or the more compact MATLAB

binary format which uses the *.mat extension

  • The binary .mat format is useful for files which will be used

with MATLAB, while the ASCII format is useful when

working with other programs, or sharing data with others

who may not have MATLAB

Data File Commands

ECE 2610 Signals and Systems A–

x = 0:5; y = rand(size(x)); [x' y'] ans = 0 0. 1.0000 0. 2.0000 0. 3.0000 0. 4.0000 0. 5.0000 0. save mat_data x y; % creates the file mat_data.mat save mat_data.dat x y /ascii; % creates the text file % mat_data.dat

  • To verify that these files are valid we first clear the MATLAB

work space using the command clear

clear whos

  • Next we load the .mat file back into the work space

load mat_data whos Name Size Bytes Class x 1x6 48 double array y 1x6 48 double array Grand total is 12 elements using 96 bytes

  • To see how the ASCII file is loaded we again clear the work

space

load mat_data.dat whos Name Size Bytes Class mat_data 2x6 96 double array Grand total is 12 elements using 96 bytes

Scalar and Array Operations

ECE 2610 Signals and Systems A–

% Assign constants to a and b: a = 3.1; b = 5.234; c = a + b c = 8. c = a/b c = 0. c = a^b c = 373.

Array Operations

  • When working with vectors and matrices we have the choice

of performing operations element-by-element or according

to the rules of matrix algebra

  • In this section we are only interested in element-by-element

operations

  • Basically element-by-element operations on vectors and

matrices are the same as those of Table A.2, except ‘.’ must

be added before the operator

Table A.2: Vector and Matrix element-by-element operations

Operation

Algebraic

Form

MATLAB syntax

addition a + b

subtraction a - b

multiplication a.*b

division a./b

exponentiation a.^b

a + b

a – b

a × b

a ÷ b

a

b

Appendix A • MATLAB Basics and More

A–14 ECE 2610 Signals and Systems

  • Another related case is when a scalar operates on a vector or

matrix

  • In this case the scalar is applied to each vector or matrix ele-

ment in a like fashion

Examples:

>> A = [1 3 5 7]
A = 1 3 5 7

B = 2A % Scalar operating on a vector B = 2 6 10 14 B = 2.A B = 2 6 10 14 C = B./A % Vector-to-vector point wise C = 2 2 2 2 D = A.^ D = 1 27 125 343

Operator Precedence

  • The order in which expressions are evaluated in MATLAB is

fixed according to Table A.

  • As long as there are matching left and right parenthesis there

Table A.3: Operator precedence in MATLAB

Precedence Operation

1 parenthesis, innermost first

2 exponentiation, left to right

3 multiplication and division, left to right

4 addition and subtraction, left to right

Appendix A • MATLAB Basics and More

A–16 ECE 2610 Signals and Systems

from to

  • If overflow (number too large) occurs MATLAB indicates a

result of Inf, meaning infinity

  • If underflow (number too small) occurs MATLAB indicates a

result of 0

Example:

a = 5.00e200; % same as 510^ b = 3.00e150; % same as 310^ ab ans = Inf % should be 15.0e 1/a * 1/b ans = 0 % should be (1/5)(1/3)e-

  • Other special cases occur when the conditions of nearly

or occur

  • For the first case MATLAB gives an error message of divide

by zero and returns Inf

  • In the second case MATLAB gives an error message of

divide by zero and returns NaN

Example:

1e-30/1e-309 %according to MATLAB like somthing/ B = 2A % Scalar operating on a vector B = 2 6 10 14 >> B = 2.A B = 2 6 10 14 >> C = B./A % Vector-to-vector point wise C = 2 2 2 2 >> D = A.^ D = 1 27 125 343 ### Operator Precedence - The order in which expressions are evaluated in MATLAB is ### fixed according to Table A. - As long as there are matching left and right parenthesis there Table A.3: Operator precedence in MATLAB ### Precedence Operation ### 1 parenthesis, innermost first ### 2 exponentiation, left to right ### 3 multiplication and division, left to right ### 4 addition and subtraction, left to right Appendix A • MATLAB Basics and More A–16 ECE 2610 Signals and Systems ### from to - If overflow (number too large) occurs MATLAB indicates a ### result of Inf, meaning infinity - If underflow (number too small) occurs MATLAB indicates a ### result of 0 ### Example: >> a = 5.00e200; % same as 510^ >> b = 3.00e150; % same as 310^ >> ab ans = Inf % should be 15.0e >> 1/a * 1/b ans = 0 % should be (1/5)(1/3)e- - Other special cases occur when the conditions of nearly ### or occur - For the first case MATLAB gives an error message of divide ### by zero and returns Inf - In the second case MATLAB gives an error message of ### divide by zero and returns NaN ### Example: >> 1e-30/1e-309 %according to MATLAB like somthing/ Warning: Divide by zero. ans = Inf %IEEE notation for ‘infinity’ 1e-309/1e-309 %according to MATLAB like 0/ Warning: Divide by zero. D = A.^ D = 1 27 125 343 ### Operator Precedence - The order in which expressions are evaluated in MATLAB is ### fixed according to Table A. - As long as there are matching left and right parenthesis there Table A.3: Operator precedence in MATLAB ### Precedence Operation ### 1 parenthesis, innermost first ### 2 exponentiation, left to right ### 3 multiplication and division, left to right ### 4 addition and subtraction, left to right Appendix A • MATLAB Basics and More A–16 ECE 2610 Signals and Systems ### from to - If overflow (number too large) occurs MATLAB indicates a ### result of Inf, meaning infinity - If underflow (number too small) occurs MATLAB indicates a ### result of 0 ### Example: >> a = 5.00e200; % same as 510^ >> b = 3.00e150; % same as 310^ >> ab ans = Inf % should be 15.0e >> 1/a * 1/b ans = 0 % should be (1/5)(1/3)e- - Other special cases occur when the conditions of nearly ### or occur - For the first case MATLAB gives an error message of divide ### by zero and returns Inf - In the second case MATLAB gives an error message of ### divide by zero and returns NaN ### Example: >> 1e-30/1e-309 %according to MATLAB like somthing/ Warning: Divide by zero. ans = Inf %IEEE notation for ‘infinity’ >> 1e-309/1e-309 %according to MATLAB like 0/ Warning: Divide by zero. ans = NaN %IEEE notation for ‘not a number’

something ⁄ 0 → ∞ 0 ⁄ 0 →

Additional Plot Types and Plot Formatting

ECE 2610 Signals and Systems A–

Additional Plot Types and Plot Formatting

  • The xy plot is the most commonly used plot type in MAT-

LAB

  • Engineers frequently plot either a measured or calculated

dependent variable, say y , versus an independent variable, say

x

  • MATLAB has huge array of graphics functions and there are

many variations and options associated with plot()

  • To find out more type: >> help plot

Special Forms of Plot

  • Linear axes for both x and y are not always the most useful
  • Depending upon the functional form we are plotting we may

wish to use a logarithmic scale (base 10) for x or y , or both

axes

  • When a log axes is used the variable may range over many

orders of magnitude, yet still show detail at the smaller val-

ues

Table A.4: Common variations of the plot command

Command Explanation

plot(x,y) Linear plot of y versus x

semilogx(x,y) Linear y versus log of x

semilogy(x,y) Log of y versus linear of x

loglog(x,y) Log of y versus log of x

Multiple (Overlay) Plots

ECE 2610 Signals and Systems A–

Multiple (Overlay) Plots

  • Using the basic command plot() we can easily plot multiple

curves on the same graph

  • The most direct way is to simply supply plot() with addi-

tional arguments, e.g.,

plot(x1,y1,x2,y2,...)

  • Another way is to use the hold command plot(x1,y1) hold %turns plot holding on for the current %figure plot(x2,y2) hold %turns plot holding off for current figure

Example: Plotting a family of polynomials, say

for

x = -1:.01:1; plot(x,x),axis('square'),grid hold Current plot held plot(x,x.^2) plot(x,x.^3) plot(x,x.^4) title('Plots of y = x^n for n = 1, 2, 3, & 4',... 'fontsize',16) ylabel('y','fontsize',14), xlabel('x','fontsize',14) print -depsc -tiff powers.eps

y x

n

= ,– 1 ≤ x ≤ 1

n = 1 2 3 4, , ,

Appendix A • MATLAB Basics and More

A–20 ECE 2610 Signals and Systems

Line and Mark Styles

  • A wide variety of line and mark styles can be obtained by

including a character string argument to plot()

plot(x,y,s)

where s is a character string (‘a string’) composed of at

most one element from each column of Table A.

  • The basic variations are line color, line style, and mark

styles

−1 −1 −0.5 0 0.5 1

−0.

−0.

−0.

−0.

0

1

Plots of y = xn^ for n = 1, 2, 3, 4

y

x