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:



    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:


    Anything Else?



    EE-597 Home