EE-597 Matlab Pointers
Much of the work assigned in EE-597 will involve Matlab processing of (and
subsequent listening to) audio signals. The following tidbits may help.
Speed Improvements:
Avoid "for" loops whenever possible by vectorizing your code.
Most Matlab commands are capable of operating on vector arguments.
For example, rather than pointwise multplying two vectors via
for i=1:M
z(i) = x(i)*y(i);
end;
take advantage of the pointwise multiplication command ".*"
z = x.*y;
If you have to use a "for" loop, your code will run much faster
if you initialize vectors before entering the loop. If this is not done,
Matlab may need to allocate memory at every iteration of the loop, slowing
things down tremendously. As an example, the "for" loop
above should have been initialized with
z = zeros(1,M);
Importing, Exporting, and Playing Sound:
-
The commands "wavread" and "wavwrite" can be used to
import and export .wav files. Note: for "wavwrite"
to work properly, the input file must not have entries with absolute value
one or greater.
-
The command "sound" can be used to play a data vector at a specified
sampling rate. On Windows 95/98/NT machines, the command "wavplay"
may
also be used.
-
When comparing the sound of two or more files, be sure that all files have
the same "loudness"; the ear/brain often mistakes "louder" as "better".
Matching signal variances is a rough way to accomplish this.
Miscellaneous:
Make sure that you do not create a variable whose name is used for a Matlab
function -- this leads to strange behavior that is often hard to trace.
When in doubt, type "help varname".
Sometimes it is convenient to know to what extent Matlab has finished processing
your sound file. The "printf" line below displays the percentage
of loop completion.
cnt=0;
for i=1:M,
cnt_cur = floor(100*(i/M));
if cnt_cur>cnt, cnt=cnt_cur; fprintf('%3d%%\b\b\b\b',cnt);
end;
% main processing goes here
end;
Matlab is capable of interpreting infinity and uses "inf" for
this purpose. This might be useful in conjunction with, for example,
the "erfc" function.
Getting Help:
-
To find instructions on how to use a particular command, type "help
command_name".
-
If you are looking for the name for a Matlab command, make a guess and
type "lookfor guess". If this doesnt work, type "help"
to look for likely subtopics, then type "help subtopic".
-
Some relatively good Matlab tutorials can be found at the Univ.
of British Columbia, and Vanderbilt.
Anything Else?
-
Do you have suggestions for this page? Are there "tricks" you have
learned or stumbling blocks that deserve mentioning? If so, email
me.
EE-597 Home