Unformatted text preview:

ELEG-212 Signals and Communications 1/7 Lab 0: Introduction to MATLAB 1 Overview MATLAB is a technical computing environment for high-performance numeric computation and visualization. MATLAB integrates numerical analysis, matrix computation, signal processing, and graphics into one easy-to-use compiler-less environment. Unlike traditional programming languages such as Fortran and C, MATLAB is optimized for vector and matrix operations. In fact, the name MATLAB stands for matrix laboratory. MATLAB has a rich family of application-specific solution packages called toolboxes, which are comprehensive collections of MATLAB functions related to particular problems. MATLAB version 6.1 has more than 16 toolboxes including the Signal Processing Toolbox, Statistics Toolbox and Communications Toolbox. Except for some built-in functions, MATLAB is an open source software package. Thus you can not only directly apply the built-in and toolbox functions, but also learn to use MATLAB more efficiently by examining the code for these functions and modifying it to fit specific tasks and applications. This is a key feature that has led to the widespread use of MATLAB. The specific objectives of this lab include: 1. Learning to use the help system to study basic MATLAB commands and syntax. 2. Writing your own functions and m-file's in MATLAB. 3. Learning some advanced programming techniques for MATLAB, i.e., vectorization. Before undertaking the following tasks, please make sure that you have read the MATLAB overview in the Signal Processing First book. Numerous other MATLAB primers are available on the Internet. A list of commonly used commands is given in the appendix of this document. You should also make use of the help command within MATLAB. Utilizing help function gives specific help on the given command. 2 Procedures 2.1 Basic Commands The following exercises will begin your orientation in MATLAB. (a) View the MATLAB introduction by typing intro at the MATLAB prompt. This short introduction will demonstrate some of the basics of using MATLAB. (b) Explore the MATLAB help capability. Type each of the following lines to read about these commands: help help plot help colon help ops help zeros help ones lookfor filter %<--- keyword search If the lines scroll past the bottom of the screen, it is possible to force MATLAB to display only one screen-full of information at a time by issuing the command more on. (c) Use MATLAB as a calculator. Try the following: pi*pi - 10ELEG-212 Signals and Communications 2/7 sin(pi/4) ans ^ 2 %<--- "ans" holds the last result (d) Variable names can store values and matrices in MATLAB. Try the following: xx = sin( pi/5 ); cos( pi/5 ) %<--- assigned to what? yy = sqrt( 1 - xx*xx ) ans (e) Complex numbers are natural in MATLAB. Notice that the names of some basic operations are unexpected, e.g., abs for magnitude. Try the following: zz = 3 + 4i conj(zz) abs(zz) angle(zz) real(zz) imag(zz) help zprint %- requires DSP First Toolbox exp( sqrt(-1)*pi ) exp(j*[ pi/4 -pi/4 ]) (f) Plotting is easy in MATLAB, for both real and complex numbers. The basic plot command will plot a vector yy versus a vector xx. Try the following: xx = [-3 -1 0 1 3 ]; yy = xx.*xx - 3*xx; plot( xx, yy ) zz = xx + yy*sqrt(-1); plot( zz ) %<---- complex values can be plotted Drop the semicolons, if you want to display the values in the x, y, and z vectors. Use help arith to learn how the operation xx.*xx works; compare to matrix multiply. When unsure about a command, use help. 2.2 MATLAB Array Indexing (a) Make sure that you understand the colon notation. In particular, explain what the following MATLAB code will produce jkl = 2 : 4 : 17 jkl = 99 : -1 : 88 ttt = 2 : (1/9) : 4 tpi = pi * [ 2 : (-1/9) : 0 ] (b) Extracting and/or inserting numbers in a vector is very easy to do. Consider the following definition: xx = [ ones(1,4), [2:2:11], zeros(1,3) ] xx(3:7) length(xx) xx(2:2:length(xx)) Explain the result echoed from the last three lines of the above code. (c) In the previous part, the vector x contains 12 elements. Observe the result of the following assignment: xx(3:7) = pi*(1:5) Now write a statement that will replace the odd-indexed elements of xx with the constant -77 (i.e., xx(1), xx(3), etc). Use a vector indexing and vector replacement.ELEG-212 Signals and Communications 3/7 2.3 MATLAB Script Files (a) Experiment with vectors in MATLAB. Think of a vector as a list of numbers. Try the following: kset = -3:11; kset cos( pi*kset/4 ) %<---comment: compute cosines Explain how the last example computes the different values of cosine without a loop. The text following the % is a comment; it may be omitted. If you remove the semicolon at the end of the first statement, all the elements of kset will be echoed to the screen. (b) Vectorization is an essential programming skill in MATLAB. Loops can be done in MATLAB, but they are not the most efficient way to get things done. It's better to avoid loops and use the vector notation instead. For example, the code below uses a loop to compute values of the sine function. Rewrite this computation without using the loop (as in the previous part). xx = [ ]; %<--- initialize the x vector to a null for k=0:7 xx(k+1) = sin( k*pi/4 ) %-- xx(0) would fail end x (c) Use the built-in MATLAB editor (on Windows or a Mac), or an external one such as emacs or notepad (on UNIX or DOS), to create a script file called funky.m containing the following lines: tt = -2 : 0.05 : 3; xx = sin( 2*pi*0.789*tt ); plot( tt, xx ), grid on %<--- plot a sinusoid title('TEST PLOT of SINUSOID') xlabel('TIME (sec)') (d) Run your script from MATLAB. To run the file funky that you created in part (c), try Funky %<---will run the commands in the file type funky %<---will type out the contents of % funky.m to the screen which funky %<--will show directory containing % funky.m (e) Add three lines of code to your script, so that it will plot a cosine on top of the sine. Use the hold function to add a plot of 0.5*cos( 2*pi*0.789*tt ) to the plot created in part (c). See help hold in MATLAB. 2.4 MATLAB Demos There are many demonstration files in MATLAB. Run the MATLAB demos from a menu by typing demo, and explore some of the different demos of basic MATLAB commands and plots. 2.5 MATLAB Sound (a) Run the MATLAB sound demo by typing xpsound at the MATLAB prompt. If you are unable to hear the sounds in the MATLAB demo then check the sound hardware on


View Full Document

UD ELEG 212 - Introduction to MATLAB

Download Introduction to MATLAB
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Introduction to MATLAB and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Introduction to MATLAB 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?