matlab tutorial (material available at jmarty/courses/matlabtutorial) 1 dr. jim martin associate...

29
Matlab Tutorial (material available at http://www.cs.clemson.edu/~jmarty/courses/ma tlabTutorial) 1 Dr. Jim Martin Associate Professor School of Computing Clemson University [email protected] http:// www.cs.clemson.edu/ ~jmarty Networking Lab’s Website: http://www.cs.clemson.edu/~jmarty/net Don’t worry Homer, the guys/girls in the Networking Lab will fix things!! WooHoo….I don’t know what this means There are no more /8 TCP/IP V4 Addresses!!

Upload: antony-turner

Post on 14-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Matlab Tutorial (material available at jmarty/courses/matlabTutorial) 1 Dr. Jim Martin Associate Professor School of Computing

Matlab Tutorial(material available at

http://www.cs.clemson.edu/~jmarty/courses/matlabTutorial)

1

Dr. Jim MartinAssociate ProfessorSchool of ComputingClemson [email protected]://www.cs.clemson.edu/~jmarty

Networking Lab’s Website: http://www.cs.clemson.edu/~jmarty/netlab/

Don’t worry Homer, the guys/girls in the Networking Lab will fix things!!

WooHoo….I don’t know what this means

There are no more /8 TCP/IP V4 Addresses!!

Page 2: Matlab Tutorial (material available at jmarty/courses/matlabTutorial) 1 Dr. Jim Martin Associate Professor School of Computing

2

• Objective is to give you some hands on experience with using Matlab for data analysis

Matlab Tutorial: Overview

Page 3: Matlab Tutorial (material available at jmarty/courses/matlabTutorial) 1 Dr. Jim Martin Associate Professor School of Computing

Matlab Overview

3

What Is MATLAB?

•MATLAB (MATrix LABoratory)

•high-performance language for technical computing

•computation, visualization, and programming in an easy-to-use environment

Typical uses include:

•Math and computation

•Algorithm development

•Modelling, simulation, and prototyping

•Data analysis, exploration, and visualization

•Scientific and engineering graphics

•Application development, including Graphical User Interface building

Page 4: Matlab Tutorial (material available at jmarty/courses/matlabTutorial) 1 Dr. Jim Martin Associate Professor School of Computing

What is Matlab?• Matlab is basically a high level language

which has many specialized toolboxes for making things easier for us

• How high?

Assembly

High Level Languages such as

C, Pascal etc.

Matlab

Page 5: Matlab Tutorial (material available at jmarty/courses/matlabTutorial) 1 Dr. Jim Martin Associate Professor School of Computing

Matlab Intro

5

A good choice for vision program development because:

• Easy to do very rapid prototyping• Quick to learn, and good documentation• A good library of image processing functions• Excellent display capabilities• Widely used for teaching and research in universities and industry• Another language to impress your boss with !

Page 6: Matlab Tutorial (material available at jmarty/courses/matlabTutorial) 1 Dr. Jim Martin Associate Professor School of Computing

Matlab Intro

6

Has some drawbacks:

•• Slow for some kinds of processes

•• Not geared to the web

•• Not designed for large-scale system development

Page 7: Matlab Tutorial (material available at jmarty/courses/matlabTutorial) 1 Dr. Jim Martin Associate Professor School of Computing

Matlab Intro

7

• MATLAB consists of:

• The MATLAB language• a high-level matrix/array language with control flow statements, functions, data

structures, input/output, and object-oriented programming features.

• The MATLAB working environment• the set of tools and facilities that you work with as the MATLAB user or programmer,

including tools for developing, managing, debugging, and profiling

• Handle Graphics• the MATLAB graphics system. It includes high-level commands for two-dimensional

and three-dimensional data visualization, image processing, animation, and presentation graphics.

• …(cont’d)

Page 8: Matlab Tutorial (material available at jmarty/courses/matlabTutorial) 1 Dr. Jim Martin Associate Professor School of Computing

Matlab Intro

8

• …

• The MATLAB function library. • a vast collection of computational algorithms ranging from elementary functions like

sum, sine, cosine, and complex arithmetic, to more sophisticated functions like matrix inverse, matrix eigenvalues, Bessel functions, and fast Fourier transforms as well as special image processing related functions

• The MATLAB Application Program Interface (API)• a library that allows you to write C and Fortran programs that interact with MATLAB.

It include facilities for calling routines from MATLAB (dynamic linking), calling MATLAB as a computational engine, and for reading and writing MAT-files.

Page 9: Matlab Tutorial (material available at jmarty/courses/matlabTutorial) 1 Dr. Jim Martin Associate Professor School of Computing

Matlab Intro

9

Some facts for a first impression

• Everything in MATLAB is a matrix !

• MATLAB is an interpreted language, no compilation needed (but possible)

• MATLAB does not need any variable declarations, no dimension statements, has no packaging, no storage allocation, no pointers

•Programs can be run step by step, with full access to all variables, functions etc.

Page 10: Matlab Tutorial (material available at jmarty/courses/matlabTutorial) 1 Dr. Jim Martin Associate Professor School of Computing

Matlab Screen• Command Window

– type commands

• Current Directory– View folders and m-files

• Workspace– View program variables– Double click on a variable

to see it in the Array Editor

• Command History– view past commands– save a whole session

using diary

Page 11: Matlab Tutorial (material available at jmarty/courses/matlabTutorial) 1 Dr. Jim Martin Associate Professor School of Computing

Variables• No need for types. i.e.,

• All variables are created with double precision unless specified and they are matrices.

• After these statements, the variables are 1x1 matrices with double precision

int a;double b;float c;

Example:>>x=5;>>x1=2;

Page 12: Matlab Tutorial (material available at jmarty/courses/matlabTutorial) 1 Dr. Jim Martin Associate Professor School of Computing

Array, Matrix• a vector x = [1 2 5 1]

x = 1 2 5 1

• a matrix x = [1 2 3; 5 1 4; 3 2 -1]

x = 1 2 3 5 1 4 3 2 -1

Page 13: Matlab Tutorial (material available at jmarty/courses/matlabTutorial) 1 Dr. Jim Martin Associate Professor School of Computing

Long Array, Matrix • t =1:10

t = 1 2 3 4 5 6 7 8 9 10• k =2:-0.5:-1

k = 2 1.5 1 0.5 0 -0.5 -1

• B = [1:4; 5:8]

x = 1 2 3 4 5 6 7 8

Page 14: Matlab Tutorial (material available at jmarty/courses/matlabTutorial) 1 Dr. Jim Martin Associate Professor School of Computing

Generating Vectors from functions• zeros(M,N) MxN matrix of zeros

• ones(M,N) MxN matrix of ones

• rand(M,N) MxN matrix of uniformly distributed

random

numbers on (0,1)

x = zeros(1,3)

x =

0 0 0

x = ones(1,3)

x =

1 1 1

x = rand(1,3)

x =

0.9501 0.2311 0.6068

Page 15: Matlab Tutorial (material available at jmarty/courses/matlabTutorial) 1 Dr. Jim Martin Associate Professor School of Computing

Matrix Index• The matrix indices begin from 1 (not 0 (as in C)) • The matrix indices must be positive integer

Given:

A(-2), A(0)

Error: ??? Subscript indices must either be real positive integers or logicals.

A(4,2)Error: ??? Index exceeds matrix dimensions.

Page 16: Matlab Tutorial (material available at jmarty/courses/matlabTutorial) 1 Dr. Jim Martin Associate Professor School of Computing

Concatenation of Matrices• x = [1 2], y = [4 5], z=[ 0 0]

A = [ x y]

1 2 4 5

B = [x ; y]

1 2

4 5

C = [x y ;z] Error:??? Error using ==> vertcat CAT arguments dimensions are not consistent.

Page 17: Matlab Tutorial (material available at jmarty/courses/matlabTutorial) 1 Dr. Jim Martin Associate Professor School of Computing

Operators (arithmetic)+ addition

- subtraction

* multiplication

/ division

^ power

‘ complex conjugate transpose

Page 18: Matlab Tutorial (material available at jmarty/courses/matlabTutorial) 1 Dr. Jim Martin Associate Professor School of Computing

Exercise

• Create a 4x4 matrix that contains random numbers

• Look up help for the random number generator (help rand)

18

Page 19: Matlab Tutorial (material available at jmarty/courses/matlabTutorial) 1 Dr. Jim Martin Associate Professor School of Computing

Example Plot

• X=rand(1,100) ;creates 100 RNs

• Y=(2*x) + 2

• Plot(x,y)

19

Page 20: Matlab Tutorial (material available at jmarty/courses/matlabTutorial) 1 Dr. Jim Martin Associate Professor School of Computing

ExerciseHow can you tell if a random number generator is good?

Properties of a U(0,1) RN Generator:–Uniformly distributed in the interval (0,1)–The RNs should be independent…i.e., uncorrelated–Many RNs should be generated before the cycle repeats (ideally it exhibits full period)–Reproducible and allow multiple streams–Consumes minimal cpu and memory resources

• Technique: plot sequential random numbers from the generator….–X=rand(1,1000); –Y=rand(1,1000);–Plot(x,y) ??

–Try a scatter plot instead…(help scatter)

20

Page 21: Matlab Tutorial (material available at jmarty/courses/matlabTutorial) 1 Dr. Jim Martin Associate Professor School of Computing

21

•A time series is a sequence of observations taken sequentially in time.

•Typically the observations are dependent•Time series analysis is concerned with techniques for the analysis of this dependence.•A time series Z1 … Zn of n successive observations can be regarded as a sample realization from an infinite population of such time series that could have been generated by a stochastic process.•A model that describes the probability structure of a sequence of observations is called a stochastic process.•A special class of stochastic processes called stationary processes assumes the process is in a particular state of statistical equilibrium.

•i.e., the statistical properties are unaffected by time.

Time Series Analysis

Page 22: Matlab Tutorial (material available at jmarty/courses/matlabTutorial) 1 Dr. Jim Martin Associate Professor School of Computing

22

•Today the majority of US internet bandwidth during prime time is video, and the majority of that is adaptively streamed long form content such as TV and movies•Cisco projects video will be over 90% of internet bandwidth by 2014 – the Internet will mainly become a video network•Content Delivery Networks (CDNs) make this possible by “edge caching” frequently viewed content•Adaptive streaming has been engineered for efficient edge caching, and to withstand network congestion and unreliable network bandwidth and latency

Netflix Tutorial

Page 23: Matlab Tutorial (material available at jmarty/courses/matlabTutorial) 1 Dr. Jim Martin Associate Professor School of Computing

23

•A segment is an independent, viewable period of video/audio/timing data..•Segment sizes of 2 seconds or 10 seconds are reasonable.•Segments are uniquely identified by an HTTP URL.•A client requests the segment, the bit rate, and optionally a specific byte range in the segment.•Clients can issue requests and receive segments over any number of concurrent TCP connections.•The video segment is sent back by the HTTP server in a ‘burst’.•The implementation of the client determines how frequently segments are requested, when bit rate adaptation occurs.

Netflix Tutorial

9

HTTP Server

DASH Client

Playback buffer queues segments• clientbuffersize: Capacity in time, • maxSegments: max queue size (segs)• maxSize: max buffer size (bytes)• Highwatermark:• Lowwatermark:

Representations

Player

Controller

HTTP Get Requests{SegmentNumber,BitRate}

Segment data

Controller parameters• Throughput Monitor Timescale• Segment size in seconds• Number of outstanding requests

Monitor Arrival Process

Rendering

Performance updates

… …

Page 24: Matlab Tutorial (material available at jmarty/courses/matlabTutorial) 1 Dr. Jim Martin Associate Professor School of Computing

24

•The figure shows the throughput consumed by a Netflix stream.•We see several levels of video quality•The input to the plotting program is dataset1.dat

Netflix Analysis Problem

500 1000 1500 2000 2500 30000

1

2

3

4

5

6

7

x 106 TCP Cx Dynamics

Time (seconds)

Bits

/sec

ond

Page 25: Matlab Tutorial (material available at jmarty/courses/matlabTutorial) 1 Dr. Jim Martin Associate Professor School of Computing

25

Estimating the mean

Page 26: Matlab Tutorial (material available at jmarty/courses/matlabTutorial) 1 Dr. Jim Martin Associate Professor School of Computing

26

>> load dataset1.dat>> mySize=size(dataset1)

mySize =

478 2

If we issue ‘mean dataset1’, should see

ans =

98.8750

Sample Mean in Matlab

Page 27: Matlab Tutorial (material available at jmarty/courses/matlabTutorial) 1 Dr. Jim Martin Associate Professor School of Computing

27

•Write a Matlab program that reads a data file and computes the sample mean.

• DO NOT use the Matlab ‘mean’ function…..compute the mean by:

• Load the data file• Find the size• For all entries

• Sum the throughput• Compute the sample mean

Exercise

Page 28: Matlab Tutorial (material available at jmarty/courses/matlabTutorial) 1 Dr. Jim Martin Associate Professor School of Computing

28

function mySampleMean()

load dataset1.datsampleSize = size(dataset1);numberSamples = sampleSize(1);totalThroughputCount = 0;averageThroughput = 0.0;for i = 1 : numberSamples totalThroughputCount = totalThroughputCount + dataset1(i,2);endaverageThroughput= totalThroughputCount / numberSamples;

fprintf(1,'mySampleMean: NumberSamples: %d, sample Throughput: %12.0f \n',numberSamples,averageThroughput);

return;

Exercise Solution

Page 29: Matlab Tutorial (material available at jmarty/courses/matlabTutorial) 1 Dr. Jim Martin Associate Professor School of Computing

29

•Using the same dataset, input the following to plot the PDF of the timeseries

load dataset1.datmyData=dataset1(:,2);myDataSize=size(dataset1)[N,X] = hist(myData,100);N=N/myDataSize(1);bar(X,N);axis([0 max(myData) 0 0.25]);

hold onylabel('PDF')xlabel('Distribution Values')title('distribution PDF')grid

Plotting the Distribution (pdf)

0 1 2 3 4 5 6 7 8 9 10

x 106

0

0.05

0.1

0.15

0.2

0.25

PD

F

Distribution Values