MATLAB has certain built-in constants such as pi (π =
3.1415…) and i or j ((-1)1/2).
We will need these constants in this and in most of the coming assignments. Not
so much of a problem with pi, but one often uses i or
j as loop variables. Should this happen, the built-in values can be
restrored by way of recomputation as sqrt(-1), or by way
of removing them from the workspace by "clear i j". Note
that the latter would be the way to restore the number pi.
Note that we don't declare variables in MATLAB. Consequently, all variables
are in general assumed to store double-precision complex values. So, we can use
the built-in constants i and j almost anywhere in our
expressions and computations. For example, 3+j*4 and
2*exp(-j*pi/4) are two complex numbers expressed in
rectangular and polar coordinate systems, respectively. Given a complex number
x, we can get its real and imaginary parts, magnitude and phase by
real(x), imag(x),
abs(x), and angle(x), respectively.
We have so far learned that, to MATLAB, all variables are matrix objects,
and most operations on or between variables are matrix-algebraic ones. For
example, A*B requires the inner dimensions of A
and B be identical. However, not all computations one needs are
matrix-algebraic. For example, consider taking the weighted average of the set
of numbers. contained in a row vector named numbers. Let the weights be
stored in another row vector weights of the same size as
numbers. Then, the weighted sum can be computed by element-wise
multiplication and summing as in sum(numbers.*weights).
Although, in this case, the result could be obtained matrix-algebraically by
numbers*weights', where we transposed weights
making it a column vector, there are times when an element-wise operation is
inevitable. Take note of and always remember this
dot-operator method for element-wise operations. As
another example, consider squaring each and every element of a vector (or matrix
for that matter), say, x. This is accomplished by
x.^2.
On the other hand, any operation that is not matrix-algebraic by definition
will be performed in an element-wise manner. Some examples are sin,
cos, real, imag, abs, and angle.
Take cos(x) for example. This will return an array (vector
or matrix) the same size as x containing the cosines of the
corresponding elements of x. Similarly for the others.
We have seen MATLAB figures and plotting in the MATLAB tutorial and the
previous assignment. In addition to such visual output, MATLAB is equipped to
provide audio output as well. The command for this is sound, and we
call it by passing two arguments. The first is a vector corresponding to a
sampled audio signal. The second is the sampling frequency in Hertz (or we say,
sampling rate in samples/second). This second argument is optional, an if
omitted, it defaults to 8192 Hz. Note that knowledge of the sampling frequency
is essential in playing out the audio signal correctly as it determines the time
separation between adjacent samples, hence the duration of the signal. One point
to note is, the sample values should be in the interval from -1 to +1, otherwise
they are clipped. Other than that, this command is as powerful as to play cd
quality stereo music, provided that we have the right sound card. Read the help
on sound to learn more.
School of Computing and Engineering University of
Missouri - Kansas City |
Last updated: January 28, 2006
|