Diapositiva 1 - University of Ottawa

Download Report

Transcript Diapositiva 1 - University of Ottawa

12-Oct-2010 Giulia Mantovani

• • • •

Matrix Laboratory

Created in late 1970’s Intended for used in courses in matrix theory, linear algebra and numerical analysis Currently has grown into an interactive system and high level programming language for general scientific and technical computation

• • • • • •

Common Uses for Matlab in Research

Data Acquisition Multi-platform, Multi Format data importing Analysis Tools (Existing,Custom) Statistics Graphing Modeling

Data Acquisition

» A framework for bringing live, measured data into MATLAB using PC-compatible, plug-in data acquisition hardware

Multi-platform, Multi Format data importing

» Data can be loaded into Matlab from almost any format and platform » » » Binary data files (eg. REX, PLEXON etc.) Ascii Text (eg. Eyelink I, II) Analog/Digital Data files PC UNIX 100101010 Subject 1 143 Subject 2 982 Subject 3 87 …

Analysis Tools

» A Considerable library of analysis tools exist for data analysis » Provides a framework for the design, creation, and implementation of any custom analysis tool imaginable

Statistical Analysis

» A considerable variety of statistical tests available including: ˃ ˃ ˃ ˃ ˃ ˃ TTEST Mann-Whitney Test Rank Sum Test ANOVAs Linear Regressions Curve Fitting

Graphing

» A Comprehensive array of plotting options available from 2 to 4 dimensions » Full control of formatting, axes, and other visual representational elements

Modeling

» Models of complex dynamic system interactions can be designed to test experimental data

You can change the desktop arrangement to meet your needs, including resizing, moving, and closing tools.

1 2

» Ordered set of numbers: (1,2,3,4) » Example: (x,y,z) coordinates of pt in space.

v

 (

x

1 ,

x

2 ,  ,

x n

)

v

 If

v i n

  1

x i

2  1 ,

v

is a unit vecto r

Vector Addition

v

w

 (

x

1 ,

y

1 )  (

x

2 ,

y

2 )  (

x

1 

x

2 ,

y

1 

y

2 ) v V+w w

Scalar Product

a

v

a

(

x

1 ,

x

2 )  (

ax

1 ,

ax

2 ) av v

» » » sum max, min, mean, sort, … Pointwise: .^

Inner (dot) Product

v  w

v

.

w

 (

x

1 ,

y

1 ).(

x

2 ,

y

2 ) 

x

1

x

2 

y

1 .

y

2 The inner product is a SCALAR!

v

.

w

 (

x

1 ,

y

1 ).(

x

2 ,

y

2 )  ||

v

||  ||

w

|| cos 

v

.

w

 0 

v

w

Matrices

A n

m

   

a

11

a

21    

a

31

a

n

1

a

12

a

22

a

32 

a n

2     

a a

2

m a a

 1

m

3

m nm

      

B n

m

   

b

11

b

21    

b

31

b

n

1

b

12

b

22

b

32 

b n

2     

b b

1

m b

2

m b

3

m

nm

       Sum:

C n

m

A n

m

B n

m c ij

a ij

b ij

A and B must have the same dimensions

C n

m

        

c c c c

 11 21 31

n

1

c

12

c

22

c

32 

c n

2     

c c c

1 

m

2

m c

3

m nm

                

a a a a

11 21 31

n

1 

b

11 

b

21 

b

31  

b n

1

a

12

a

22

a

32 

b

12 

b

22 

b

32

a n

2  

b n

2     

a

1

m a

2

m a

3

m a nm

    

b b

2

m b b

1

m

3

m nm

       

Matrices

Product:

C n

p c ij

 

k m

  1

A n

m a ik b kj B m

p

A and B must have compatible dimensions

A n

n B n

n

B n

n A n

n

Identity Matrix: 1

I

   0   0 0 1  0     0 0    1

IA

AI

A

Transpose:

C m

n c ij

A T n

m

a ji

Matrices

(

A

B

)

T

A T

B T

(

AB

)

T

B T A T

If

A T

A

A is symmetric

Matrices

Determinant: A must be square ( n = m )

n = m = 2 n = m = 3

det   

a b c d

   

a b c d

 det r1 r2 r3     

a a a

11 21 31

a a a

12 22 32

a a a

13 23 33      

ad

cb

a

11

a

22

a

32

a

23

a

33 

a

12

a

21

a

31

a

23

a

33 

a

13

a

21

a

31

a

22

a

32 The area of the parallelogram is the absolute value of the determinant of the matrix formed by the vectors representing the parallelogram's sides.

The volume of this Parallelepiped is the absolute value of the determinant of the matrix formed by the rows r1, r2, and r3.

Matrices

Inverse: A must be square

A n

n

A n

  1

n

A n

  1

n

A n

n

I

  

a a

11 21

a

12

a

22     1 

a

11

a

22 1 

a

21

a

12    

a a

22 21 

a a

12 11   

P

2D Translation

P’ t

ty y

2D Translation Equation

x P’ P t

P

 (

x

,

y

)

t

 (

t x

,

t y

) tx

P

'  (

x

t x

,

y

t y

) 

P

t

ty y

2D Translation using Matrices

x P t P’

P

 (

x

,

y

)

t

 (

t x

,

t y

) tx

P

'    

x y

 

t t y x

       1 0 0 1 t

t t x y

      

x y

1     P

P P’

Scaling

s.y

y x P

Scaling Equation

P’ s.x

P

 (

x

,

y

)

P

'  (

sx

,

sy

)

P

' 

s

P P

'    

sx sy

      

s

0

P

' 

S S

P

0

s

     

x y

  

Sy.y

y Sx.x

P’

Stretching Equation

P x

P

 (

x

,

y

)

P

'  (

s x x

,

s y y

)

P

'    

s s x y x y

      

s

0

x

0

s y

      

x y

 

S P

' 

S

P

P’ P

Rotation

y’ y

Rotation Equations

Counter-clockwise rotation by an angle  P’  

x

'

y

'      cos sin    sin  cos     

x y

   x’ P x

R P'

R

P

Properties of rotation matrixes

 

x

'

y

'      cos sin    sin  cos     

x y

 

R

R is a rotation matrix and it must satisfy the following constraints:

R

R T

R T

det(

R

)  1 

R

I

y P x’ y’ x

P

'     cos sin    sin  cos 

t t y x

      

x y

1    

Navigating the Matlab Desktop Commonly Used Toolboxes

Solving equations using variables

Expression language

Expressions typed by the user are interpreted and evaluated by the

Matlab system

Variables are names used to store values Variable names allow stored values to be retrieved for calculations or

permanently saved

• When MATLAB encounters a new variable name, it automatically creates the variable and allocates the appropriate amount of storage.

Variable = Expression **Variable Names are Case Sensitive!

Ex. «a» and «A» are two different variables

Solving equations using variables

Variable = Expression

>> x = 6 x = 6 >> y = 2 y = 2 Reserved variables

!

They are special names used to indicate either a specific number (e.g. pi, inf, eps, …), a keyword (e.g. char, double, …), or the result of an expression (ans).

By default they have a specific meaning or value, if you assign a new expression to these names, you cannot use the default reserved variable anymore during the current session of Matlab.

• -

Basic Calculation Operators: + Addition >> x + y ans = 8 Subtraction >> x - y Multiplication ans = 4 >> x * y / Division ans = 12 >> x / y ans = 3 ^ Exponentiation >> x ^ y ans = 36

Working with Matrices

Matlab works with rectangular numerical matrixes

A matrix is a collection of numerical values that are organized into a specific configuration of rows and columns.

The number of rows and columns can be any number

Example 3 rows and 4 columns define a 3 x 4 matrix having 12 elements

Working with Matrices

Matlab works with rectangular numerical matrixes

A matrix is a collection of numerical values that are organized into a specific configuration of rows and columns.

The number of rows and columns can be any number

Example 3 rows and 4 columns define a 3 x 4 matrix having 12 elements

A

scalar is a single number and is represented by a 1 x 1 matrix in matlab.

A

vector is a one dimensional array of numbers and is represented by an n x 1 column vector or a 1 x n row vector of n elements

Working with Matrices c = 5.66 or c = [5.66] c is a scalar or a 1 x 1 matrix

Working with Matrices c = 5.66 or c = [5.66] c is a scalar or a 1 x 1 matrix x = [ 3.5, 33.22, 24.5 ] x is a row vector or a 1 x 3 matrix

Working with Matrices c = 5.66 or c = [5.66] c is a scalar or a 1 x 1 matrix x = [ 3.5, 33.22, 24.5 ] x is a row vector or a 1 x 3 matrix x1 = [ 2 5 3 -1] x1 is column vector or a 4 x 1 matrix

Working with Matrices c = 5.66 or c = [5.66] c is a scalar or a 1 x 1 matrix x = [ 3.5, 33.22, 24.5 ] x is a row vector or a 1 x 3 matrix x1 = [ 2 5 3 -1] x1 is column vector or a 4 x 1 matrix A = [ 1 2 4 2 -2 2 0 3 5 5 4 9 ] A is a 4 x 3 matrix

Working with Matrices

Spaces, commas, and semicolons are used to separate elements of a matrix

Working with Matrices

Spaces, commas, and semicolons are used to separate elements of a matrixSpaces or commas separate elements of a row

[1 2 3 4] or [1,2,3,4]

Working with Matrices

Spaces, commas, and semicolons are used to separate elements of a matrixSpaces or commas separate elements of a row

[1 2 3 4] or [1,2,3,4]

• • Semicolons separate columns

A = [1,2,3,4;5,6,7,8;9,8,7,6] = [1 2 3 4 5 6 7 8 9 8 7 6] Colon operator identifies a range of values: B = [1:4 ; 5:8 ; 9,8,7,6 ] = A

Working with Matrices

Colon operator identifies a range of values: [ 1 : 3 ] = [ 1 , 2 , 3] [ 1 : 2: 9 ] = [ 1 , 3 , 5, 7, 9] [ 5 : -1: 1 ] = [ 5 , 3 , 1]

start jump end

B = [1:4 ; 5:8 ; 9,8,7,6 ] = A A = [1,2,3,4;5,6,7,8;9,8,7,6] = [1 2 3 4 5 6 7 8 9 8 7 6]

Indexing Matrices

A

m x n matrix is defined by the number of m rows and number of n columns

• An individual element of a matrix can be specified with the notation A(4,1)=5

Indexing Matrices

A

m x n matrix is defined by the number of m rows and number of n columns

• An individual element of a matrix can be specified with the notation A(4,1)=5

Example: >> A = [1 2 4 5;6 3 8 2] A is a 4 x 2 matrix >> A(1,2) ans = 6

The colon operator can be used to index a range of elements

>> A(1:3,2) ans = 1 2 4

Indexing Matrices

Specific elements of any matrix can be overwritten using the matrix index

Example: A = [1 2 4 5 6 3 8 2] >> A(1,2) = 9 Ans A = [1 2 4 5 9 3 8 2]

Matrix Shortcuts

The ones and zeros functions can be used to create any m x n matrices

composed entirely of ones or zeros Example a = ones(2,3) a = [1 1 1 1 1 1] b = zeros(1,5) b = [0 0 0 0 0] (!) if you need to understand more about how to use these functions, go to the HELP!

Data Types and Formats

The semicolon operator determines whether the result of an expression is

displayed

• who lists all of the variables in your matlab workspace • whos

list the variables and describes their matrix size

Saving your Work To save data to a *.mat file:

Typing ‘ save filename ’ at the >> prompt and the file ‘ filename.mat

’ will be saved to the working directory

Select Save from the file pull down menu Automatically, all the variables in the workspace will be stored in the same mat-file named filename.mat.

To reload a *.mat file

• •

Type ‘

load filename

’ at the >> prompt to load ‘ filename.mat

’ the filename is located in the current working directory) (ensure Select Open from the file pull down menu and manually find the data file (!) Again…to see all the options that are offered for a function, type doc namefunction

Using functions Matlab has a wide variety of toolboxes, whithin each toolbox you can find several type of functions specific for that topic. Some functions are generic and you can find them in the MATLAB TOOLBOX. RESULT: more or less, you can find any type of function you need.

For a list of the elementary mathematical functions, type

help elfun

For a list of more advanced mathematical and matrix functions, type

help specfun help elmat

You can just create an elaboration by using the available functions, or you can create your own function (see after).

Basic plotting commands Function

plot plot3

Description

Graph 2-D data Graph 3-D data Help – Search – type plot and obtain the following explanation!

plot(Y) plots the columns of Y versus the index of each value when Y is a real number. plot(X1,Y1,...,Xn,Yn) plots each vector Yn versus vector Xn on the same axes. If one of Yn or Xn is a matrix and the other is a vector, plots the vector versus the matrix row or column with a matching dimension to the vector. More advanced!

For example, plot a sinusoidal curve: plot3(t,t,y); grid on » t = 0:1/100:1; xlabel x » y = sin(2*pi*t); ylabel y » plot(t,y); zlabel z Frequency of the sinusoidal curve B=repmat(y,length(t),1); mesh(t,t,B);

Enter the following Matrices in matlab using spaces, commas, and semicolons to separate rows and columns: A =     1 5 31 21 17 2 6 9 7     C =       4 22 16 160       B =  1 64 122 78 38 D =       8 55 0 25 41 28 0 65 166 16 1 24 42 2 12 19       55  D = E = a 5 x 9 matrix of 1 ’s

Use the who and whos functions directly from the Command Window to confirm all of the variables and matrices in the work space are present and correct A =     1 5 31 21 17 2 6 9 7     C =       4 22 16 160       B =  1 64 122 78 38 55  D =       8 55 0 25 41 28 0 65 166 16 1 24 42 2 12 19       D = E = a 5 x 9 matrix of 1 ’s

Change the following elements in each matrix: A =     1 5 31 17 6 9 7     B =  1 122 38 55  C =       4 22

76

160       D =       8 0 25 41 28 0 65 166 1 24 42 2 12       Now save all the variables in the workspace in a mat-file named myvar.

• • • Create a variable named S, which contains two cycles of a sinusoidal curve of frequency 2Hz and resolution pi/10.

Plot S in a two dimensional graph.

Try to use the documentation to change from the solid blue line, to a dashed red line • To modify these parameters you need to add a third option to the plot command. Prompt plot(t,y,

‘ options ’

). Instead of using the word options, use the symbols that indicate the type and the color of the line • click on the LineSpec link on the plot page documentation to see which are the symbols for the dashed line and for the red.

» What happens if I have to close Matlab because it’s time to go home???

˃ I lose all the work I did in the Command Window!

˃ I lose all the variables I created during the current session of work.

» » I can write the list of command, functions and variables in a new Script and then save it as a file.m When I need to run again that list of commands I reopen the stored file.m and I can continue my work!

» » Smoothing data using low pass filters Normalizing (interpolating) data

» »

Smoothing data using low pass filters Ex EMG signals Work flow

Normalized for the average Rectify the signal (absolute value) Low pass filter

Matlab functions

EMG_m = mean( EMG ); EMG_n = EMG – EMG_m; EMG_r = abs( EMG_n ); [B,A] = butter(4,10/500); Z = filter( B, A, EMG_r); (!) Look at myemg.m in the folder

» »

Normalizing (interpolating) data Ex. Express the gait signal in percentange of gait Work flow

Cut the signal from FS1 to FS2 Interpolate the signal in 101 points to express it in percentage of gait cycle

Matlab functions

y_cut = Hip_FEangle(FS1:FS2) x_base_norm = linspace(0,length(y_cut),n_norm) y_norm = spline(x_base_current, y_cut, x_base_norm) (!) Look at mygait.m in the folder

Normalize from FS1 to FS2 in gait percentage the two signals relative to the Flexion/Extension angle at the hip joint. The variables stored in the mat-file hip_gait_data are relative to a patient A, and from hip_gait_data2 we have data from patient B.

Once that both signals are normalized, plot them together in the same figure and with two different colors.

Compute the Range of Motion for both signals (after normalization). Use the max and min functions to compute ROM.