## MATLAB FOR DSP

Here a few programming tips that should help you get started writing MATLAB programs for DSP. For more basic information about MATLAB, refer to MATLAB Primer.

MATLAB is an interpreted language. This means that the source code is not compiled but is interpreted on the fly. You can use MATLAB interactively by typing commands at the prompt. You can also write MATLAB functions by creating m-files, which are text files that describe input, processsing, and output. You can view the text of an accessible m-file at any time by typing type <function> at the MATLAB prompt. Looking at the m-files that implement the various core MATLAB functions is a good way to pick up some ideas on efficient MATLAB programming.

AVOID FOR LOOPS

You may be tempted to write MATLAB code the way you would write a program in Fortran or C. Although you can get by with this, don't do it. Because MATLAB is an interpreted language, it must interpret every line in a for loop each time it goes through the loop. This makes MATLAB painfully slow at doing loops. For example, to generate a sine wave you can write:
for i=0:999, x(i+1) = sin(2*pi*i/100);
However, this is significantly slower than
x = sin(2*pi*[0:999]/100);
In general, you should try to think of ways to let MATLAB process whole vectors at one time rather than using loops. Most MATLAB functions will operate on a vector as easily as on a scalar. With some experience, you will find that most (but not all) for loops can be eliminated.

Here is a function that generates a triangular waveform in one line:

function x = tri(T,sT,Ns)
% TRI   Generate samples of a triangular waveform with period T seconds,
%               sampling frequency sT samples/second, and Ns samples.

x=2*abs(round([0:Ns-1]/sT/T)-[0:Ns-1]/sT/T);

VECTORS AS SIGNALS

In MATLAB it is convenient to use a vector to represent a sampled signal (a sequence). MATLAB vectors and matrices start with index 1 rather than 0. If a sequence is intended to start at some other time than the origin, you will have to use some other bookkeeping method to keep up with its starting time. One place where the MATLAB starting index sometimes causes confusion is in the use of the plot function. If no X axis is specified, then MATLAB will default to beginning the X axis labeling with the number 1. The same holds true for the discrete-time signal plotting function stem.

COLON OPERATOR

Colon notation is a convenient part of MATLAB that lets you specify a portion of a vector or matrix. In many cases, this notation will allow you to avoid for loops.

The colon notation works by specifying an index range with a start, a skip, and a final value. Therefore, a regularly spaced vector of integers (or reals) is obtained via
myvec = start:skip:end
If you use myvec = start:end
it will increment by 1.

MATRIX OPERATIONS

MATLAB assumes matrix notation by default. A*B'' is interpreted as a matrix multiply of A and B. If both A and B have dimensions 1x3, then MATLAB will return an error stating that the inner matrix dimensions must agree. If you want to do a point-by-point operation that combines the elements of A and B, then use point'' notation. A pointwise operator is indicated by preceding the standard symbol by .''. Thus, to multiply A and B pointwise, type A.*B''.

POLYNOMIALS

MATLAB uses a simple polynomial representation that is very useful for DSP. In DSP, we often need to represent rational polynomials of the form:

$$H(z) = \frac{B(z)}{A(z)} = \frac{\sum_{l=0}^M b_lz^{-l}}{\sum_{k=0}^N a_kz^{-k}}$$

In MATLAB the polynomials $$A(z) = \sum_{k=0}^M a_kz^{-k}$$ and $$B(z) = \sum_{l=0}^N b_lz^{-l}$$ are represented by vectors b and a containing their coefficients. Thus a=[1 -1.5 0.99] represents the polynomial $$1 -1.5z^{-1} + 0.99z^{-2}$$ We can find the roots of $$A(z)$$ with the command roots(a). A partial fraction expansion of can be found using the MATLAB function residuez. The function filter interprets the coefficients as a difference equation and implements it as a filter. The function freqz also uses this notation to find the frequency response from a rational polynomial system function.

PLOTTING

Some plotting functions are particularly useful for DSP. The function stem allows you to plot a discrete sequence as a lollipop'' plot. The function strips will plot an extremely long plot by breaking it up into horizontal strips. Furthermore, the subplot function is useful for making comparisons. It allows you to plot multiple plots on one figure. One final useful plotting routine is zplane, which generates a pole-zero plot either by specifying the pole and zero locations or the polynomial coefficients.

Stanley J. Reeves
sjreeves@eng.auburn.edu