Presentation is loading. Please wait.

Presentation is loading. Please wait.

Master in Optical Fiber Communications and Photonic Technologies Foundations of Digital Transmission - Fall quarter Introduction to Matlab.

Similar presentations


Presentation on theme: "Master in Optical Fiber Communications and Photonic Technologies Foundations of Digital Transmission - Fall quarter Introduction to Matlab."— Presentation transcript:

1 Master in Optical Fiber Communications and Photonic Technologies Foundations of Digital Transmission - Fall quarter Introduction to Matlab

2 Copyright 2001 OCG 2 What Is MATLAB?  MATLAB is an interactive program for scientific and engineering numeric calculation.  It integrates computation, visualization, and programming in an easy-to-use environment where problems and solutions are expressed in familiar mathematical notation.  Typical uses include:  Math and computation  Algorithm development  Modeling, simulation, and prototyping  Scientific and engineering graphics

3 Copyright 2001 OCG 3 Command Window  Command Window is used to enter variables and run functions and M-files.  Navigation within MATLAB is done using regular UNIX commands  cd (change directory)  pwd (show the path)  ls (list contents of directory)  whos (list of the variable stored in the memory)  In latest version, most of this is also available using a graphical interface

4 Copyright 2001 OCG 4 Command Window Getting help from the command window help (show the help document for a given function) By the way you can use this help only if you know the name of the function to be used. If you do not know it, just use the windows help, which is much more easy and complete. Here you can find also several examples. P.S. If you want to have an help for your own functions, you can write it at the beginning of the function as : % your own help. After this statement, the matlab file will start.

5 Copyright 2001 OCG 5 Variables  MATLAB does not require any type declarations!  Real scalar:>> x=1  Complex scalar:>> x=1+2i  Row vector:>> x=[1 2 3]  Column vector:>> x=[1; 2; 3]  2x2 Matrix:>> x=[1 2; 3 4] You can define global variables by putting in front the variable the statement global.

6 Copyright 2001 OCG 6 Complex numbers  Some useful operations on complex numbers:  Complex scalar >> x = 3+4j  Real part of x >> real(x) ->3  Imaginary part of x >> imag(x) ->4  Magnitude of x >> abs(x) ->5  Angle of x >> angle(x) ->0.9273  Complex conjugate of x >> conj(x) ->3 - 4i

7 Copyright 2001 OCG 7 Generating vectors >> x=[a:step:b]  Generate a vector that takes on the values a to b in increments of step >> x=linspace(a,b,n)  generates a row vector x of n points linearly spaced between a and b >> x=logspace(a,b,20)  generates a logarithmically spaced vector x of n points between 10^a and 10^b.

8 Copyright 2001 OCG 8 Generating matrices  Matrix building functions: >> A=zeros(m,n)  returns an m-by-n matrix of zeros >> A=ones(m,n)  returns an m-by-n matrix of 1s >> A=eye(m,n)  returns an m-by-n matrix with 1's on the diagonal and 0's elsewhere

9 Copyright 2001 OCG 9 Generating random matrices >> A=rand(m,n)  returns an m-by-n matrix of random numbers whose elements are uniformly distributed in the interval (0,1) >> A=randn(m,n)  returns an m-by-n matrix of random numbers whose elements are normally distributed with mean 0 and variance 1 >> A=randint(m,n,range)  generates an m-by-n integer matrix. The entries are uniformly distributed and independently chosen from the range:  [0, range-1] if range is a positive integer  [range+1, 0] if range is a negative integer

10 Copyright 2001 OCG 10 Accessing matrix elements  Elements of a matrix are accessed by specifying the row and column >> A=[1 2 3; 4 5 6; 7 8 9]; >> x=A(1,3)  Returns the element in the first row and third column >> y=A(2,:)  Returns the entire second row [4 5 6]  “:” means “take all the entries in the column” >> B=A(1:2,1:3)  Returns a submatrix of A consisting of rows 1 and 2 and all three columns [1 2 3; 4 5 6]

11 Copyright 2001 OCG 11 Arithmetic matrix operation  The basic arithmetic operations on matrices are:  + addition  - subtraction  * multiplication  / division  ^ power  ’ conjugate transpose  This operation are to be meant in the “matrix sense” MATRIX-wise operation

12 Copyright 2001 OCG 12 element-by-element operations  MATLAB provides element-by-element operations by prepending a ‘.’ before the operator .* multiplication ./ division .^ power .’ transpose (unconjugated)  BE CAREFUL when operating on matrix: the meaning of the two operations.* and * are completely different element-wise operation

13 Copyright 2001 OCG 13 Relational operations  MATLAB defines the following relational operations:  < less than  <= less than or equal to  > greater than  >= greater than or equal to  == equal to  ~= not equal to

14 Copyright 2001 OCG 14 Logical operations  MATLAB defines the following logical operations:  & and  | or  ~ not

15 Copyright 2001 OCG 15 Math functions  The following functions operate element-wise, also when applied to a matrix: sincostan asinacosatan sinhcoshtanh explog(natural log)log10 abssqrtsign

16 Copyright 2001 OCG 16 M-files  MATLAB is an interpretive language  M-files are text files containing MATLAB scripts  Scripts are sequences of commands typed by an editor  The instructions are executed by typing the file name in the command window at the MATLAB prompt  All the variables used in the m-file are placed in MATLAB’s workspace that contains all the variables defined in the MATLAB session

17 Copyright 2001 OCG 17 M-files Debug  In Matlab you can debug your m-file, like in other programming languages  To do it, you need to open your matlab file from the window command.  Afterwards, you can operate exactly like for other languages and you can use usual command as: step in step out break point etc. etc

18 Copyright 2001 OCG 18 Flow control  If statements if expression statements else statements end  Example If n<0 a=a-1; else a=a+1; end

19 Copyright 2001 OCG 19 Flow control  For  Repeats a group of statements a fixed, predetermined number of times. a=0; for n = 1:10 a=a+1; end

20 Copyright 2001 OCG 20 Function in Matlab To simplify your matlab file structure, you can use functions. An example of how to use Matlab functions is the following: Main Matlab Program SegEqRx = SegEqNew + 1*SegRx(DimC*TTaps+1:LRx-DimC*TTaps); clear SegEqNew; clear SegEqOld; [SAMPLE_CENTRAL, BIT_DETECTED] = =syncronizer(SegEqRx,PNSEQ_VECTOR,NS_BIT,NBIT_PNSEQ); … Function declaration: function [ SAMPLE_CENTRAL, BIT_DETECTED] =syncro(SIGNAL,PNSEQ_VECTOR,NS_BIT,NBIT_PNSEQ); Main of the function

21 Copyright 2001 OCG 21 Creating a plot >> plot(x,y)  produces a graph of y versus x, where x and y are two vectors x=linspace(0,2*pi,100); plot(x,sin(x));

22 Copyright 2001 OCG 22 Line styles and colors  It is possible to specify color, line styles, and markers when you plot your data using the plot command plot(x,y,'color_style_marker')  color_style_marker is a string containing from one to four characters constructed from a color, a line style, and a marker type:  Color strings: 'c', 'm', 'y', 'r', 'g', 'b', 'w', and 'k'. These correspond to cyan, magenta, yellow, red, green, blue, white, and black  Linestyle strings are '-' for solid, '--' for dashed, ':' for dotted, '-.' for dash-dot, and 'none' for no line  The marker types are '+', 'o', '*' and 'x'

23 Copyright 2001 OCG 23 Axis lables and titles  xlabel('string')  labels the x-axis of the current axes  ylabel('string')  labels the y-axis of the current axes  Title(‘string’)  add a title to a graph at the MATLAB command prompt or from an M-file

24 Copyright 2001 OCG 24 The figure function  MATLAB directs graphics output to a figure window  Graphics functions automatically create new figure windows if none currently exist >>figure  creates a new window and makes it the current figure >>figure(h)  make an existing figure current by passing its handle (the number indicated in the window title bar), as an argument to figure >>clf  Clear current figure window

25 Copyright 2001 OCG 25 Adding plots  Setting hold to on, MATLAB doesn’t remove the existing graph and adds the new data to the current graph x=linspace(0,2*pi,100); plot(x,sin(x)); hold on; plot(x,cos(x)); xlabel('x'); ylabel('Sine of x');

26 Copyright 2001 OCG 26 Adding plots With more graphics functionalities: x=linspace(0,2*pi,100); plot(x,sin(x),’-ob’); hold on; grid on; plot(x,cos(x),’->r’); xlabel('x'); ylabel('Sine of x'); legend('sin(x)','cos(x)',3)

27 Copyright 2001 OCG 27 Basic plotting commands  Plot  Graph 2-D data with linear scales for both axes  Loglog  Graph with logarithmic scales for both axes  Semilogx  Graph with a logarithmic scale for the x-axis and a linear scale for the y-axis  Semilogy  Graph with a logarithmic scale for the y-axis and a linear scale for the x-axis

28 Copyright 2001 OCG 28 Specialized plots  bar(x,Y)  draws a bar for each element in Y at locations specified in x, where x is a monotonically increasing vector defining the x-axis intervals for the vertical bars >> bar((1:1:10),(1:1:10))

29 Copyright 2001 OCG 29 Specialized plots  Stem  displays data as lines (stems) terminated with a marker symbol at each data value x=linspace(0,2*pi,10); stem(x,sin(x));

30 Copyright 2001 OCG 30 Specialized plots  Stairs  Stairstep plots are useful for drawing time-history plots of digitally sampled data systems x=linspace(0,2*pi,20); stairs(x,sin(x));


Download ppt "Master in Optical Fiber Communications and Photonic Technologies Foundations of Digital Transmission - Fall quarter Introduction to Matlab."

Similar presentations


Ads by Google