apa6903 november 4, 2014 presented by: catriona czyrnyj content by: giulia mantovani and catriona...

Post on 25-Dec-2015

217 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

APA6903November 4, 2014

Presented By: Catriona CzyrnyjContent By: Giulia Mantovani and Catriona Czyrnyj

Introduction to MATLAB and Matrix Review

Overview• Why MATLAB?

• Review of Lecture Prep– MATLAB user interface– MATLAB help tools– Working with MATLAB

• Using MATLAB– Naming variables– Working with matrices– plotting– Saving your work

Overview• Writing in MATLAB

– FOR loop statements– IF and WHILE conditional statements– Using functions

• Exercise

• Important Functions

• Biomechanics Applications

• Useful Links

Why MATLAB?• MATrix LABoratory

• Created in the 1970s

• Originally created for linear algebra so that students wouldn’t have to use Fortran

• Today: interactive system and high level programming language for general scientific and technical computation

Why MATLAB?

Common Uses for MATLAB

• Data Acquisition

• Analysis Tool

• Statistics

• Graphing

• Modelling

Click View then Header and Footer to change this footer

Why MATLAB?• Lots of other tools exist

– Maple, Mathematica, Excel

• Basic unit of MATLAB is a matrix (ie. Integer is a 1x1 matrix)

• What does our data look like?

Review – MATLAB UI

You can change the UI to meet your needs

Review – Help and Online Tools

MATLAB has one of the largest networks of online resources and help forums

Review – Working with MATLAB

We learned to…– Create variables in the command window

– Specify the current folder

– Save and load variables

– Create and use combinations of variables

– Use functions (cos, mean, linspace)

Review – Working with MATLAB

We learned to…– Plot commands

– Semi-colon to avoid echo in the command window

– Work with matrices (create, index, operate)

http://www.mathworks.com/academia/student_center/tutorials/mltutorial_launchpad.html#

Using MATLAB – Naming Variables

Warning:

Variable names are case sensitive

Be careful not to overwrite variables by accident

Using MATLAB – Working with Matrices• Spaces, commas, semicolons separate matrix elements

• Spaces or commas separate columns[1 2 3 4] or [1,2,3,4]

• Semicolons separate rowsA = [1 2 3 ;4 5 6; 7 8 9] = 1 2 3

4 5 67 8 9

• Colon operator identifies range of valuesB=[1:3;4:5;6:8] = A

Using MATLAB – Working with Matrices• Colon operator identifies range of values

C = [a:b : c]C = [1:2 : 9]

givesC = [1 3 5 7 9]

• It works in reverse tooD = [3:-1:0]

givesD = [3 2 1 0]

start incr. end

Using MATLAB – Indexing Matrices• m x n matrix has m rows and n columns

• Element of a matrix identified by A(i,j)ie. the element ith row and the jth column

Example:

A = 1 2 4 56 3 8 2

A(2,1) = ?

Using MATLAB – Indexing Matrices• We can always overwrite elements of a matrix

Example:

A = 1 2 4 56 3 8 2

A(2,1) = 9

this A = 1 2 4 5gives 9 3 8 2

Using MATLAB –Matrix Shortcuts• The ones and zeros functions can always be used to

create m x n matrices composed entirely of ones or zeros

Example:

A = zeros(2,3) this A = 0 0 0gives 0 0 0

A = ones(3,2) this A = 1 1gives 1 1

1 1

**if you don’t understand go to the online help!

Using MATLAB – Saving your Work• MATLAB data is saved to *.mat files

• This would appear in the command window as:

>> save filename

• OR your can select “save” in the file menu

• This command stores ALL variables in the workspace under the filename

What is it? *.mat is the extension of data files in Matlab. They contain any type of variable that can be created in Matlab and that show up in the ‘current workspace’ window

Using MATLAB – Loading your Work• To load a *.mat file, this would appear in the command window

as:>> load filename

• OR your can select “open” in the file menu

• If you don’t know how to use a function type:>> doc namefunction

Using MATLAB – PlottingBasic Plotting Commands

• figure creates new figure graphics object

• hold on retains the current figure for operations

• hold off does not retain the current figure

• axis tight controls axis scaling and appearance

• plot(a) plots the vector a

Using MATLAB – M-file• 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 the variables I created during my work session?

Using MATLAB – M-file• Write the list of commands, functions and variables in a

“New Script” (in the file menu) and save is as a *.m file

• To run access your work simply open your *.m file to continue working

Writing in MATLAB – FOR loop • for executes identical commands a specified number of times

examplefor i = 1:10

a(i) = i*2;end

• this implies at the end of each loop, increment i by +1• Result:

a = [2 4 6 8 10 12 14 16 18 20]

i = a(i) =1 22 43 64 85 106 127 148 169 18

10 20

Writing in MATLAB – FOR loop • in a for loop we call i our counter variable because it

counts through a prescribed set of values

• we can also count by different increments:

j = 1for i = 10:-1:1

a(j) = i*2;j = j + 1;

end

• Result:a = [20 18 16 14 12 10 8 6 4 2]

i j a(j)10 1 29 2 48 3 67 4 86 5 105 6 124 7 143 8 162 9 181 10 20

Writing in MATLAB – FOR loop • we can also count up by different increments:

b = [1 5 2 -2 3 10 -4 ]j = 1for i = b

a(j) = i*2;j = j + 1;

end

• Result:a = [2 10 4 -4 6 20 -8]

i j a(j)1 1 25 2 102 3 4-2 4 -43 5 610 6 20-4 7 -8

Writing in MATLAB – IF condition • if executes specific commands if a certain condition is

met

examplea = 0;j = 1;if j == 1

a = -j;end

• Result:a = -1

ConditionalOperators

Meaning

== Equal to

~ Not

> greater than

< Less than

&& And

|| Or

Writing in MATLAB – IF condition • if executes specific commands if a certain condition is

met

examplea = 0;j = 1;if j ~= 1

a = -j;end

• Result:a = 0

ConditionalOperators

Meaning

== Equal to

~ Not

> greater than

< Less than

&& And

|| Or

Writing in MATLAB – WHILE condition • while repeats specific commands while a certain

condition is met (like an if plus for)

examplea = 0;j = 1;while j < 5

a(j) = -j;j = j + 1;

end

• Result:a = [-1 -2 -3 -4 -5]

j a(j)1 -12 -23 -34 -45 -5

Writing in MATLAB – WHILE condition BE WARNED!

THIS LOOP WILL NEVER END:

a = 0;j = 1;while j < 5

a(j) = -j; end

(HIT CTRL-C to cancel an ongoing process)

Writing in MATLAB – Functions• function is a set of commands that is either pre-written

into MATLAB (plot, mean, etc.) or added/written in by you (timenormalize, openc3d, readc3d)

• MATLAB has a wide variety of toolboxes that contain functions for specific topics

• MATLAB also has lots of open source exchanges for sharing functions people have written

• For a list of basic mathematical functions type:>> help elfun

ExcerciseCome up with the theoretical steps to complete the assignment described below. Help each other and as me, if needed. Then suggest possible functions to use to complete each task.

Available Data• 2 EMG signals RF BF @ 1000Hz• MVC signals RF BF @ 1000Hz• Knee Angles ext/flex @200Hz• Start/end events

TO DO: verify relation between EMG and knee angles. What muscles are active at maximum knee extension?

Theoretical Steps?MATLAB Steps?

Writing in MATLAB – Important Functions

• mean gives the average value of an array

• takes the form M = mean(A,dim)

• M = mean(A) implies dim = 1 and returns mean along the columns

• If dim = 2 returns mean along the rows

A = [1 1;1 2]M = mean(A) gives M = 1 1.5M = mean(A,2) gives M = 1

1.5

Writing in MATLAB – Important Functions

• linspace generates linearly spaced vectors

• takes the form y = linspace(a,b,n)

• y = linspace(a,b) implies n = 100 and returns vector of 100 equally spaced points from a to b

• If n < 2, linspace returns b

y = linspace(1,10,4) gives y = 1 4 7 10

Writing in MATLAB – Important Functions

• spline gives a cubic spline interpolation of the data

• takes the form yy = spline(X,Y,xx)

example:x = 0:10; y = sin(x); xx = 0:.25:10; yy = spline(x,y,xx); plot(x,y,'o',xx,yy)

Writing in MATLAB – Important Functions

• Plot X vs Y>> plot(x,y)

Writing in MATLAB – Important Functions

• spline gives the average value of an array

• Plot X vs Y as o’s and xx vs yy as line>> plot(x,y,'o',xx,yy)

Writing in MATLAB – Important Functions

• butter allows you to design coefficients for a Butterworth filter

• [b,a] = butter(n,Wn)

• Designs a n order lowpass digital Butterworth filter with normalized cutoff frequency Wn

• Returns coefficients in length n+1 row vectors b and a with descending powers of z

Writing in MATLAB – Important Functions

• Normalized Cutoff Frequency (Wn)

Fc = cutoff frequency (10Hz)Fs = sampling frequency (1000Hz)

Wn = Fc _ = ___10___(Fs/2) (1000/2)

Wn = 0.02

Writing in MATLAB – Important Functions

• filter allows you to filter your data

• Y = filter(b,a,X)

• Filters the data in vector X with the filter described by numerator coefficient vector b and denominator coefficient vector a

• filtfilt gives zero-phase (aka. Dual pass) digital filtering by processing the input data in both the forward and reverse directions

• Y = filtfilt(b,a,X)

What are the two important ways that we

normalize data?

Can anyone suggest functions we would use

to time normalize data?

Biomechanics Applications

Normalizing (interpolating) Data

• Express gait signal in percentage of gait

Cut the signal from FS1 to

FS2

Interpolate the signal in

101 points to express it

in percentage

of gait cycle

Work flow

Biomechanics Applications

Normalizing (interpolating) Data

• Express gait signal in percentage of gait

Cut the signal from FS1 to

FS2

Interpolate the signal in

101 points to express it

in percentage

of gait cycle

Work flowy_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)

Matlab functions

*Look at mygait.m in the folder

Biomechanics Applications

Smoothing Data using Low Pass Filters• EMG signals

Remove the bias

Rectify the signal (absolute value)

Low pass filter

Work flow

Biomechanics Applications

Smoothing Data using Low Pass Filters• EMG signals

*Look at myemg.m in the folder

Remove the bias

Rectify the signal (absolute value)

Low pass filter

Work flowEMG_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);

Matlab functions

top related