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 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.
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''.
Matlab uses a simple polynomial representation that is very useful for DSP. In DSP, we often need to represent polynomials of the form:
In Matlab the polynomials and are represented by vectors b and a containing their coefficients. Thus a=[1 -1.5 0.99] represents the polynomial . 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.
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.