introduction to matlab. outline 2 introduction matlab environment getting help vectors,...

36
Introduction to MATLAB

Upload: victoria-bridges

Post on 30-Dec-2015

247 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

Introduction to MATLAB

Page 2: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

Outline

2

Introduction MATLAB Environment Getting Help Vectors, Matrices, and Linear Algebra Plotting User Defined Functions Image Manipulation and Processing Audio Processing Filters and Operations

Multimedia Systems ([email protected])

Page 3: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

Introduction MATLAB Stands for MATrix LABoratory. The MATLAB environment allows the user to:

manage variables import and export data perform calculations generate plots develop and manage files for use with MATLAB.

A script can be made with a list of MATLAB commands like other programming language.

Multimedia Systems ([email protected])3

Page 4: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

Environment

Multimedia Systems ([email protected])4

Page 5: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

Getting Help Type one of following commands in the command

window: help – lists all the help topic help topic – provides help for the specified topic help command – provides help for the specified command help help – provides information on use of the help

command helpwin – opens a separate help window for navigation lookfor keyword – Search all M-files for keyword

Or simply press ‘F1’ and use the graphical help window.

Multimedia Systems ([email protected])5

Page 6: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

Variables Variable names:

Must start with a letter May contain only letters, digits, and the underscore “_” Matlab is case sensitive, i.e. one & OnE are different variables. Matlab only recognizes the first 31 characters in a variable

name. when a semi-colon

>> tutorial = 1234; >> tutorial = 1234

”;” is placed at the end of each command, the result is not displayed.

Multimedia Systems ([email protected])6

Page 7: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

Variables (Cont’d) Special variables:

ans : default variable name for the result pi: π = 3.1415926………… eps: = 2.2204e-016, smallest amount by which 2 numbers can differ.∈ Inf or inf : ∞, infinity NaN or nan: not-a-number

Commands involving variables: who: lists the names of defined variables whos: lists the names and sizes of defined variables clear: clears all variables, reset the default values of special variables. clear name: clears the variable name clc: clears the command window clf: clears the current figure and the graph window.

Multimedia Systems ([email protected])7

Page 8: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

Vectors >> x = [ 0 0.25*pi 0.5*pi 0.75*pi pi ]

x =

0 0.7854 1.5708 2.3562 3.1416 >> y = [ 0; 0 25*pi; 0 5*pi; 0 75*pi; ]

y =

0

0.7854

1.5708 y

2.3562

3.1416

Multimedia Systems ([email protected])8

Page 9: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

Vectors (Cont’d) Vector Addressing – A vector element is addressed in

MATLAB with an integer index enclosed in parentheses. >> x(3) The colon notation may be used to address a block of

elements (start : increment : end) start is the starting index, increment is the amount to add to

each successive index, and end is the ending index. A shortened format (start : end) may be used if increment is 1.

>> x(1:3)ans =

0 0.7854 1.5708 NOTE: MATLAB index starts at 1.

Multimedia Systems ([email protected])9

Page 10: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

Vectors (Cont’d)

Multimedia Systems ([email protected])10

Page 11: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

Array Operations Scalar-Array Mathematics:

Element-by-Element Array-ArrayMathematics:

Multimedia Systems ([email protected])11

Page 12: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

Matrices A Matrix array is two-dimensional,

having both multiple rows and multiple columns, similar to vector arrays: It begins with [, and end with ] spaces or commas are used to separate

elements in a row semicolon or enter is used to separate rows.

Matrix Addressing: matrixname(row, column) colon may be used in place of a row or

column reference to select the entire row or column.

Multimedia Systems ([email protected])12

Page 13: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

Matrices (Cont’d)

Multimedia Systems ([email protected])13

Page 14: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

Matrices (Cont’d)

Multimedia Systems ([email protected])14

Page 15: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

Example, Linear Equations

Multimedia Systems ([email protected])15

Page 16: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

Plotting Plotting Curves:

plot (x,y) – generates a linear plot of the values of x (horizontal axis) and y (vertical axis). semilogx (x,y) – generate a plot of the values of x and y using a logarithmic scale for x and a

linear scale for y semilogy (x,y) – generate a plot of the values of x and y using a linear scale for x and a

logarithmic scale for y. loglog(x,y) – generate a plot of the values of x and y using logarithmic scales for both x and y

Multiple Curves: plot (x, y, w, z) – multiple curves can be plotted on the same graph by using multiple arguments in

a plot command. The variables x, y, w, and z are vectors. Two curves will be plotted: y vs. x, and z vs. w.

legend (‘string1’, ‘string2’,…) – used to distinguish between plots on the same graph Multiple Figures:

figure (n) – used in creation of multiple plot windows. place this command before the plot() command, and the corresponding figure will be labeled as “Figure n”

close – closes the figure n window. close all – closes all the figure windows.

Subplots: subplot (m, n, p) – m by n grid of windows, with p specifying the current plot as the pth window

Multimedia Systems ([email protected])16

Page 17: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

Plotting (Cont’d)

Multimedia Systems ([email protected])17

Page 18: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

Flow Control

Multimedia Systems ([email protected])18

Page 19: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

Loops

break – is used to terminate the execution of the loop.

Multimedia Systems ([email protected])19

Page 20: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

M-Files A M-file is a group of MATLAB commands. MATLAB can open and execute the commands exactly as

if they were entered at the MATLAB command window. To run the M-files, just type the file name in the command

window. (make sure the current working directory is set correctly)

Multimedia Systems ([email protected])20

Page 21: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

User Defined Functions Add the following command in the beginning of your m-

file: function [output variables] = function_name (input

variables); Note that the file name should be the same as the function

name.

Multimedia Systems ([email protected])21

Page 22: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

Images in MATLAB

MATLAB can import/export several image formats BMP (Microsoft Windows Bitmap) GIF (Graphics Interchange Files) HDF (Hierarchical Data Format) JPEG (Joint Photographic Experts

Group) PCX (Paintbrush) PNG (Portable Network Graphics) TIFF (Tagged Image File Format) XWD (X Window Dump) MATLAB can also load raw-data or

other types of image data

• Data types in MATLAB– Double (64-bit double-precision

floating point)– Single (32-bit single-precision

floating point)– Int32 (32-bit signed integer)– Int16 (16-bit signed integer)– Int8 (8-bit signed integer)– Uint32 (32-bit unsigned integer)– Uint16 (16-bit unsigned integer)– Uint8 (8-bit unsigned integer)

22 Multimedia Systems ([email protected])

Page 23: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

Images in MATLAB• Binary images : {0,1}

• Intensity images : [0,1] or uint8, double etc.

• RGB images : m-by-n-by-3

• Indexed images : m-by-3 color map

• Multidimensional images m-by-n-by-p (p is the number of layers)

23 Multimedia Systems ([email protected])

Page 24: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

Image import and export Read and write images in Matlab

>> I=imread('cells.jpg');

>> imshow(I)

>> size(I)

ans = 479 600 3 (RGB image)

>> Igrey=rgb2gray(I);

>> imshow(Igrey)

>> imwrite(lgrey, 'cell_gray.tif', 'tiff')

Alternatives to imshow>>imagesc(I)

>>imtool(I)

>>image(I)

24 Multimedia Systems ([email protected])

Page 25: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

Images and Matrices How to build a matrix (or image)?

>> A = [ 1 2 3; 4 5 6; 7 8 9 ]; A = 1 2 3

4 5 67 8 9

>> B = zeros(3,3) B = 0 0 0

0 0 00 0 0

>> C = ones(3,3) C = 1 1 1

1 1 11 1 1

>>imshow(A) (imshow(A,[]) to get automatic pixel range)

25 Multimedia Systems ([email protected])

Page 26: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

Image format conversion Convert between intensity/indexed/RGB to binary format:

dither() Convert between intensity format to indexed format:

gray2ind() Convert between indexed format to intensity format:

ind2gray() Convert between indexed format to RGB format:

ind2rgb() Convert a regular matrix to intensity format by scaling:

mat2gray() Convert between RGB format to intensity format:

rgb2gray() Convert between RGB format to indexed format:

rgb2ind()

Multimedia Systems ([email protected])26

Page 27: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

Colormaps I = imread (‘lena.bmp’); imshow (I); colormap (cool);

Autumn, bone, colorcube, cool, copper, flag, gray, hot, hsv, jet, pink, prism, spring, summer, white, winter

Multimedia Systems ([email protected])27

Page 28: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

Video mov = aviread(filename) mov = aviread(filename, index)

fileinfo = aviinfo(filename)

Multimedia Systems ([email protected])28

Page 29: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

Digital Audio

Multimedia Systems ([email protected])29

Page 30: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

Audio [road,fs]=wavread('road.wav'); The array road now contains the stereo sound data and fs

is the sampling frequency.  This data is sampled at the same rate as that on a music

CD (fs=44,100 samples/second).

The left and right channel signals are the two columns of the road array:

 left=road(:,1); right=road(:,2);

Multimedia Systems ([email protected])30

Page 31: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

time=(1/44100)*length(left); t=linspace(0,time,length(left)); plot(t,left) xlabel('time (sec)'); ylabel('relative signal strength')

soundsc(left,fs)       % plays left channel as mono  soundsc(right,fs)    % plays right channel mono (sound

nearly the same)  soundsc(road,fs)     % plays stereo (ahhh…)

Multimedia Systems ([email protected])31

Page 32: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

Multimedia Systems ([email protected])32

Page 33: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

Multimedia Systems ([email protected])33

Page 34: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

Multimedia Systems ([email protected])34

Page 35: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

Multimedia Systems ([email protected])35

Page 36: Introduction to MATLAB. Outline 2  Introduction  MATLAB Environment  Getting Help  Vectors, Matrices, and Linear Algebra  Plotting  User Defined

Multimedia Systems ([email protected])36