fd
EECS20: Introduction to Real-Time Digital Systems
Lab01: Introduction to Matlab
EECS20: Introduction to Real-Time Digital Systems
©1996 Regents of the University of California.
By K. H. Chiang, William T. Huang, Brian L. Evans.
The last three exercises are copied from Labs for Signals and Systems by Virginia Stonick and Kevin Bradley.
URL: http://www-inst.eecs.berkeley.edu/~ee20
News: ucb.class.ee20
Half of the laboratory assignments will use Matlab. Matlab stands for ``Matrix Laboratory'' and is a general-purpose programming language. Matlab interprets commands like Lisp does, instead of compiling source code like C and Fortran require. Matlab, unlike Lisp, has an extensive sets of routines, or toolboxes, that perform matrix, signal processing, image processing, controls, optimization, and statistics computations.
By building your own expertise in Matlab, you will be able to attack numerical problems in engineering quickly by using its high-powered routines and by avoiding the compile-link-debug cycle in C and Fortran. Since Matlab is available on a variety of different platforms, your Matlab scripts can run without modification on several different kinds of computers.
Here are a few key points to keep in mind when you're using Matlab:
>> rowvector = [1 2 3] rowvector = 1 2 3 >> columnvector = [1; 2; 3] columnvector = 1 2 3 >> matrix2x2 = [1 2; 3 4] matrix2x2 = 1 2 3 4
>> [1 2 3 4 5] + [1 1 1 1 1] ans = 2 3 4 5 6
>> fid = fopen('x.dat','wb') >> fwrite(fid, x, 'double') >> fclose(fid)However, Matlab is similar enough to C to cause some extremely annoying errors. One such error is that array and vector indexing begins with 1 and not 0.
>> x = [1:2:9] x = 1 3 5 7 9 >> x(0) ??? Index exceeds matrix dimensions. >> x(1) ans = 1
>> A = [1 2 1; 2 1 3; 4 0 5]; >> y = [3; 2; 1]; >> x = A \ y x = 0 1.4000 0.2000Unfortunately, its lack of data structures will make handling of multidimensional arrays messy [this problem is addressed in
http://www.mathworks.com/1109.html
.]
>> eigvalues = eig( [ 1 2; 2 1 ] ) eigvalues = -1.0000 3.0000 >> [eigvectors, eigenValueMatrix] = eig( [ 1 2; 2 1 ] ) eigvectors = 0.7071 0.7071 -0.7071 0.7071 eigenValueMatrix = -1.0000 0 0 3.0000Don't worry if you don't know what an eigenvalue or eigenvector is.
>> c = x + yThe WRONG WAY would be to reimplement the add operation by defining a new function called add in a file called
add.m
:
function c = add(x, y) %% Function to add two vectors x and y together len = max( [ length(x) length(y) ] ); for i = 1:len c(i) = x(i) + y(i); end;In Matlab, you would access the new function by typing
>> add( [1 1], [2 2] )Matlab will automatically try to load functions that it does not recognize by searching for the file function.m on its search path. But in this case you are reinventing the wheel and doing a bad job of it too.
A short and coherent tutorial on vectorization can be found at http://www.mathworks.com/1109.html
.
http://www.mathworks.com/technical.html#general
.
In addition, you can have further fun by running the Matlab demo:
>> demoEnjoy the pretty pictures. Drive those Pentiums into the ground.
%
sign
[so that you can figure out what you did when you look at your code at a future
date]. Use help
and lookfor
when necessary.
half
that removes every other element from
an arbitrary length vetor, creating a shorter vector made of only the
odd-numbered elements of the original vector. Use only matrix and vector
manipulations; do not use loops.
double
that creates a longer vector by
adding an additional element, 0, between neighboring elements in the original
vector. Then replace each of these new 0 elements with the average of its
neighboring elements.
Use only matrix and vector manipulations; do not use loops.
half(double(x))
and
double(half(x))
?
find and replace
elementwise
colon operator
matrix multiplication
argument error [trig functions] dimension error
transposition