The majority of people using Nutaq products also use MATLAB at some point during their development process. MATLAB is a very powerful tool that, when used properly, makes the development of new signal processing algorithms much more efficient. But, depending on the amount of data processed by the prototyped algorithm, the time consumed during processing can quickly become an issue. During the prototyping phase, developers want to quickly test the modifications they make to their algorithms. Anything that can be done to reduce the time required to execute their algorithms directly translates into an overall reduction in development time.

Using the MATLAB Profiler

A profiling tool is available in MATLAB to help you optimize your custom functions. You can find the MATLAB Profiler in the Desktop menu, as shown below.

Once the Profiler window is open, you only have to type the name of the function, just as you would normally do in the MATLAB command window. In the following window, the custom “myDemoFunction” function will be profiled.

Efficient MATLAB coding

Now that we know how to use the Profiler, let’s create some inefficient MATLAB code and then optimize it so it becomes efficient. Below is an M-file implementing a custom function that creates a vector containing a 10 million-point sine wave. This function is coded using a “C-like” for loop.

If we use the Profiler to compute the processing time of this function as shown below, we see that it takes roughly 11.5 seconds to compute.

One of the actions that can be taken to optimize this function is to create the 10 million-point buffer called “mySinus” prior to processing the loop (instead of resizing the destination array every iteration of the for loop). MATLAB uses contiguous memory to store arrays of data so constantly resizing means it always is looking for a memory block large enough to hold the entire new array.

The new optimized function is as follows:

To verify that the execution of this function yields the same results as the original one, a script that calls both functions in succession and then validates the results is shown below.

By executing this script with the Profiler, we obtain the following results:

As you can see, the signals generated by the functions are identical but their execution times are very different. Just by creating the destination buffer upfront instead of continuously resizing it in the loop, we saved 42% of the execution time.

It gets even better! MATLAB is designed to work with vectors. Before, I said “myDemoFunction” contained a “C-like” for loop. That is because for loops should be avoided as much as possible when coding in MATLAB. Data should be processed as vectors to take advantage of the way MATLAB handles data.

To show what can be gained by using a vector instead of a for loop, the following function was created:

A similar script to the one used to compare the performance of the original function with the initialized buffer version was created. It compares the original function to one that uses a vector to create the signal.

Executing this script using the Profiler, we obtain the results shown below. The generated data is still the same, but this time the gain in performance is much bigger. We saved 97% of the processing time by using a vector as an input to the sin() function (instead of processing it one sample at a time within a for loop).

Using vectors instead of for loops to process data in MATLAB makes the algorithms much more efficient. This can save a lot of development time as it shortens the time needed to test modifications to the algorithm under development.