Presentation is loading. Please wait.

Presentation is loading. Please wait.

APA6903 November 4, 2014 Presented By: Catriona Czyrnyj Content By: Giulia Mantovani and Catriona Czyrnyj Introduction to MATLAB and Matrix Review.

Similar presentations


Presentation on theme: "APA6903 November 4, 2014 Presented By: Catriona Czyrnyj Content By: Giulia Mantovani and Catriona Czyrnyj Introduction to MATLAB and Matrix Review."— Presentation transcript:

1 APA6903 November 4, 2014 Presented By: Catriona Czyrnyj Content By: Giulia Mantovani and Catriona Czyrnyj Introduction to MATLAB and Matrix Review

2 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

3 Overview Writing in MATLAB –FOR loop statements –IF and WHILE conditional statements –Using functions Exercise Important Functions Biomechanics Applications Useful Links

4 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

5 Why MATLAB? Common Uses for MATLAB Data Acquisition Analysis Tool Statistics Graphing Modelling Click View then Header and Footer to change this footer

6 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?

7 Review – MATLAB UI You can change the UI to meet your needs

8 Review – Help and Online Tools MATLAB has one of the largest networks of online resources and help forums

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

10 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_cent er/tutorials/mltutorial_launchpad.html#

11 Using MATLAB – Naming Variables Warning: Variable names are case sensitive Be careful not to overwrite variables by accident

12 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 rows A = [1 2 3 ;4 5 6; 7 8 9] = 1 2 3 4 5 6 7 8 9 Colon operator identifies range of values B=[1:3;4:5;6:8] = A

13 Using MATLAB – Working with Matrices Colon operator identifies range of values C = [a:b : c] C = [1:2 : 9] gives C = [1 3 5 7 9] It works in reverse too D = [3:-1:0] gives D = [3 2 1 0] start incr. end

14 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 5 6 3 8 2 A(2,1) = ?

15 Using MATLAB – Indexing Matrices We can always overwrite elements of a matrix Example: A = 1 2 4 5 6 3 8 2 A(2,1) = 9 this A = 1 2 4 5 gives9 3 8 2

16 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 0 gives0 0 0 A = ones(3,2)this A = 1 1 gives1 1 1 **if you don’t understand go to the online help!

17 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

18 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

19 Using MATLAB – Plotting Basic 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

20 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?

21 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

22 Writing in MATLAB – FOR loop for executes identical commands a specified number of times example for 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) = 12 24 36 48 510 612 714 816 918 1020

23 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 = 1 for i = 10:-1:1 a(j) = i*2; j = j + 1; end Result: a = [20 18 16 14 12 10 8 6 4 2] ija(j) 1012 924 836 748 65 5612 4714 3816 2918 11020

24 Writing in MATLAB – FOR loop we can also count up by different increments: b = [1 5 2 -2 3 10 -4 ] j = 1 for i = b a(j) = i*2; j = j + 1; end Result: a = [2 10 4 -4 6 20 -8] ija(j) 112 5210 234 -24-4 356 10620 -47-8

25 Writing in MATLAB – IF condition if executes specific commands if a certain condition is met example a = 0; j = 1; if j == 1 a = -j; end Result: a = -1 Conditional Operators Meaning ==Equal to ~Not > greater than <Less than &&And ||Or

26 Writing in MATLAB – IF condition if executes specific commands if a certain condition is met example a = 0; j = 1; if j ~= 1 a = -j; end Result: a = 0 Conditional Operators Meaning ==Equal to ~Not > greater than <Less than &&And ||Or

27 Writing in MATLAB – WHILE condition while repeats specific commands while a certain condition is met (like an if plus for) example a = 0; j = 1; while j < 5 a(j) = -j; j = j + 1; end Result: a = [-1 -2 -3 -4 -5] ja(j) 1 2-2 3-3 4-4 5-5

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

29 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

30 Excercise Come 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?

31 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) givesM = 1 1.5 M = mean(A,2)givesM = 1 1.5

32 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)givesy = 1 4 7 10

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

34 Writing in MATLAB – Important Functions Plot X vs Y >> plot(x,y)

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

36 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

37 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

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

39 What are the two important ways that we normalize data?

40 Can anyone suggest functions we would use to time normalize data?

41 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

42 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 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) Matlab functions *Look at mygait.m in the folder

43 Biomechanics Applications Smoothing Data using Low Pass Filters EMG signals Remove the bias Rectify the signal (absolute value) Low pass filter Work flow

44 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 flow 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); Matlab functions

45 Useful Links http://ocw.mit.edu/courses/electrical-engineering-and- computer-science/6-094-introduction-to-matlab-january- iap-2010/lecture-notes/ Only the first 2! http://www.mathworks.com/help/pdf_doc/matlab/getstart.pdf Very good introduction. Covers everything necessary to learn the basics http://eecourses.technion.ac.il/matlab/Matlab%20PDFs/usi ng_ml.pdf More advanced


Download ppt "APA6903 November 4, 2014 Presented By: Catriona Czyrnyj Content By: Giulia Mantovani and Catriona Czyrnyj Introduction to MATLAB and Matrix Review."

Similar presentations


Ads by Google