Sei sulla pagina 1di 11

EE-338 ECE 340 Discrete-Time Signals and Systems Introduction to MATLAB Introduction: MATLAB is a high-level language developed by MathWorks,

Inc. for scientific computing. It is used extensively in research, engineering/computing science and other math/computing intensive exercises such as developing algorithms, performing numerical computation, analyzing and visualizing data. MATLAB has many toolboxes (standard functions) that can be used in user-developed programs. We will be using the Signal Processing Toolbox in this lab. The Basics: The basic object in MATLAB is the data matrix and the manipulation of these objects generally follows the rules of matrix algebra. Lines of program/code can be entered one at a time at the command prompt or as groups of commands from a script file. A MATLAB script file is just a text file with the .m extension and can be produced with any word processor. In the command window, the prompt for input is >>. Objective: This document is designed as an introduction to MATLAB. 1. Starting MATLAB MATLAB can be run by clicking on Start->Run->Open; then type matlab Or By double clicking the following icon on the desktop:

2. MATLAB environment. When MATLAB opens, you should see a window similar to Fig.1 (comments explain the various features).

Page 1 of 9

Present Working Directory Browse to change directories/folders

Shows all the variables currently in use

Shows the history of commands used: you can copy from here or double click on the command to run it

Command prompt

Fig. 1 MATLAB initialized display.

Exercise 1: Defining and indexing an array


To enter a matrix, the square brackets [ ] are used. Suppose we want to define a matrix/array A=[1 4 9 16 25], we would type the following at the command prompt. (Note: Type only the line immediately following >> and press enter).

TIP: Adding a ; (semi-colon) after any command in MATLAB will ensure that the returning answer is not displayed. What a lot of programmers who switch from C/C++ find frustrating is that MATLAB allows only positive non-zero indexing into an array. I.e. for the array A defined above there is no element A(0), instead the first element is A(1)=1.

You can also define multidimensional arrays in MATLAB. For example, to define the following matrix: Page 2 of 9

1 2 A= 3 4 Type:

The semicolon inside the brackets delimits the rows of the matrix. A two dimensional array is indexed first by the row and then by the column. Hence Row index Column index

Exercise 2: Defining a row vector


To input a row vector, the colon operator can be used:

You can also define the step size when using the colon operator. So x=[0:2:10] would result in a vector from 0, increasing in steps of 2, to 10.

Page 3 of 9

To convert x from a row vector to a column vector (transpose), we could type: >> y=x'; To find the dot product of two row vectors x and y, we could type: >> z=x * y'; Note: A number of variables are predefined in MATLAB. e.g.: ans, i, j, pi, inf, eps, NaN.

Exercise 3: Adding/Subtracting and multiplying two matrices


Only the same sized matrices/arrays can be added/subtracted or a constant can be added to/subtracted from all the elements of a matrix.

Try adding Z=[2 3 1] to A and see what happens. Multiplication (A*B; * and not x ) is governed by the laws of array multiplication. Thus, a size [N x M] (N-rows and M-cols) matrix can only be multiplied by a [M x any_size] matrix.

Exercise 4: The . Implication


Often we need to perform operations on each individual element of a matrix. For example, if we wanted to find the square of each element of matrix A, we cannot type:

Page 4 of 9

From the answer you see that A*A resulted in matrix multiplication and not squaring of each element. However, if we were to place a . in front of the * the multiplication operation would become: each-individual element of A multiplied by the corresponding element in A which would result in the square of each element.

The . operator can also be used in any other operation as well (e.g. addition/exponential).

Exercise 5: for loop


We could also compute the square of each element by using a for-loop. Notice the format of the loop.

Opening of first for loop Opening of second for loop Closing of second for loop Closing of first for loop

See what happens if you miss an end statement. What kind of error does MATLAB throw at you?

Page 5 of 9

Exercise 6: Other useful commands


>> clear will delete all variables in the workspace. >> clc will clear the screen of previous commands, useful when you have too much clutter in the command window >> clf will clear the current figure >> helpwin opens MATLAB help in a separate window >> help opens MATLAB help in the command window >> help plot help or helpwin is usually followed by the specific command that you wish to learn about. If you dont know the keyword for the command then simply use helpwin and search for the information you want. A useful resource for most of your questions is the extensive documentation/examples available at the Mathworks website: http://www.mathworks.com/access/helpdesk/help/helpdesk.html

To do on your own: 1. Defining a complex variable: A complex number can be entered as follows: >> x=2-4j; The magnitude of a complex variable can be computed by using the function abs: >> mag_x=abs(x); The angle of a complex variable can be computed by using the function angle: >> ang_x=angle(x); 2. Defining signals: Most functions take vector inputs. For example, to define r=exp(-0.1x), for x=[1,2 ..100], we can type: >> x=[1:100]; >> r=exp(-0.1*x); 3. Visualization functions: Two commonly used visualization functions for signals are stem and plot. Stem is used for discrete signals while plot is used for continuous signals. Try generating a stem graph for the signal r defined above. >> stem(r); >> xlabel(Label for the x-axis); % The percentage symbol is used for commenting the code and should be
Page 6 of 9

% added at the beginning of every line of a comment. >> ylabel(Label for y-axis); >> title(Title of the graph); Function xlabel, ylabel and title are used to annotate graphs. Note that the text to be displayed is enclosed within single quotation marks. 4. Exponentiation: If we want to perform a operation such as 2^n, where n is a vector [1 2 3 4 5], we can proceed as follows: >> n=[1 2 3 4 5]; >>x=2.^n; See what happens if you dont put the . Before ^. 5. Script (.m files) As mentioned at the beginning of this tutorial, instead of executing a sequence of commands one by one at the command prompt, they can be executed as a group using MATLAB scripts. To open the MATLAB script editor, simply type edit at the command prompt. >> edit; The editor window with an untitled .m file opens. Type in the sequence of commands. Save the file with a meaningful name. To run the script simply type the name of the file at the command prompt.

TEST YOUR UNDERSTANDING OF THE BASICS Write a Matlab program that generates a matrix N=[1 2 3 ..20], using a for loop (i.e. you cannot type in all 20 numbers manually). Generate a stem plot of N. >> stem(N); Generate S=[1 8 278000] (third power of element of N) without using a for loop. Generate a continuous plot of S. >>plot(S);

Page 7 of 9

Elementary Math Functions


Trigonometric sin sinh asin asinh cos cosh acos acosh tan tanh atan atan2 atanh sec sech asec asech csc csch acsc acsch cot coth acot acoth Exponential. exp log log10 sqrt Complex. abs angle conj imag real Numeric. fix floor ceil Sine. Hyperbolic sine. Inverse sine. Inverse hyperbolic sine. Cosine. Hyperbolic cosine. Inverse cosine. Inverse hyperbolic cosine. Tangent. Hyperbolic tangent. Inverse tangent. Four quadrant inverse tangent. Inverse hyperbolic tangent. Secant. Hyperbolic secant. Inverse secant. Inverse hyperbolic secant. Cosecant. Hyperbolic cosecant. Inverse cosecant. Inverse hyperbolic cosecant. Cotangent. Hyperbolic cotangent. Inverse cotangent. Inverse hyperbolic cotangent.

Exponential. Natural logarithm. Common logarithm. Square root.

Absolute value. Phase angle. Complex conjugate. Complex imaginary part. Complex real part.

Round towards zero. Round towards minus infinity. Round towards plus infinity.

Page 8 of 9

round rem sign

Round towards nearest integer. Remainder after division. Signum function.

Arithmetic operators. + Plus Minus * Matrix multiplication .* Array multiplication ^ Matrix power .^ Array power \ Backslash or left division / Slash or right division ./ Array division Relational operators. == Equality <,> Relational operators & Logical AND | Logical OR ~ Logical NOT xor Logical EXCLUSIVE OR ~= Not equal < Less than (lt) > Greated than (gt) <= Less than or equal to (le) >= Greater than or equal to (ge)

Page 9 of 9

Very Nice MATLAB book for Signal processing, but may be too advanced and is quite rare. Digital Signal Processing Using MATLAB, 2e Ingle / Proakis Cengage Learning, 2007

Basic Books: (Any will work and are quite accessible in the Library) Computer Explorations in Signals and Systems Using MATLAB, 2e Buck / Singer / Daniel Prentice Hall, 2002 Computer-Based Exercises for Signal Processing Using MATLAB 5 Burrus / McClellan / Oppenheim / Parks / Schafer / Schuessler Prentice Hall, 1998 Applied Signal Processing: A MATLAB-Based Proof of Concept Dutoit / Marqus Springer, 2009

These books can be used too, but are less accessible. Discrete Systems and Digital Signal Processing with MATLAB ElAli CRC Press, Inc., 2004 Fundamentals of Digital Signal Processing Using MATLAB Schilling / Harris Thomson Engineering, 2005 Digital Signal and Image Processing using MATLAB Charbit / Blanchet ISTE Ltd, 2006 Digital Signal Processing Laboratory Using MATLAB Mitra WCB/McGraw-Hill, 1999 Digital Signal Processing: A MATLAB Based Tutorial Approach Leis Research Studies Press, 2002

Some Specialized Topics Fourier Analysis: Brief Notes in Advanced DSP: Fourier Analysis with MATLAB Grigoryan / Grigoryan CRC Press, Inc., 2009

Filter Design: Design and Analysis of Analog Filters: A Signal Processing Perspective with MATLAB Examples Paarmann Springer, 2001 Filter Design for Signal Processing Using MATLAB and Mathematica Lutovac / Tosc / Evans Prentice Hall, 2001 Adaptive Filtering Primer with MATLAB Poularikas / Ramadan Taylor & Francis, Inc., 2006

Potrebbero piacerti anche