Documentation Center

  • Trial Software
  • Product Updates

When to Use System Objects Instead of MATLAB Functions

System Objects vs. MATLAB Functions

Many System objects have MATLAB® function counterparts. For simple, one-time computations use MATLAB functions. However, if you need to design and simulate a system with many components, use System objects. Using System objects is also appropriate if your computations require managing internal states, have inputs that change over time or process large streams of data.

Building a dynamic system with different execution phases and internal states using only MATLAB functions would require complex programming. You would need code to initialize the system, validate data, manage internal states, and reset and terminate the system. System objects perform many of these managerial operations automatically during execution. By combining System objects in a program with other MATLAB functions, your can streamline your code and improve efficiency.

Process Audio Data Using Only MATLAB Functions Code

This example shows how to write MATLAB function-only code for reading audio data.

The code reads audio data from a file, filters it, and then plays the filtered audio data. The audio data is read in frames. This code produces the same result as the System objects code in the next example, allowing you to compare approaches.

Locate source audio file.

fname = 'speech_dft_8kHz.wav';

Obtain the total number of samples and the sampling rate from the source file.

audioInfo = audioinfo(fname);
maxSamples = audioInfo.TotalSamples;
fs = audioInfo.SampleRate;

Define the filter to use.

b = fir1(160,.15);

Initialize the filter states.

z = zeros(1,numel(b)-1);

Define the amount of audio data to process at one time, and initialize the while loop index.

frameSize = 1024; 
nIdx = 1;

Define the while loop to process the audio data.

while nIdx <= maxSamples(1)-frameSize+1
    audio = audioread(fname,[nIdx nIdx+frameSize-1]);
    [y,z] = filter(b,1,audio,z);
    sound(y,fs);
    nIdx = nIdx+frameSize;
end

The loop uses explicit indexing and state management, which can be a tedious and error-prone approach. You must have detailed knowledge of the states, such as, sizes and data types. Another issue with this MATLAB-only code is that the sound function is not designed to run in real time. The resulting audio is very choppy and barely audible.

Process Audio Data Using System Objects

This example shows how to write System objects code for reading audio data.

The code uses System objects from the DSP System Toolbox™ software to read audio data from a file, filter it, and then play the filtered audio data. This code produces the same result as the MATLAB code shown previously, allowing you to compare approaches.

Locate source audio file.

fname = 'speech_dft_8kHz.wav';

Define the System object to read the file.

audioIn = dsp.AudioFileReader(fname,'OutputDataType','single');

Define the System object to filter the data.

filtLP = dsp.FIRFilter('Numerator',fir1(160,.15));

Define the System object to play the filtered audio data.

audioOut = dsp.AudioPlayer('SampleRate',audioIn.SampleRate);

Define the while loop to process the audio data.

while ~isDone(audioIn)
    audio = step(audioIn);    % Read audio source file
    y = step(filtLP,audio);   % Filter the data
    step(audioOut,y);         % Play the filtered data
end

This System objects code avoids the issues present in the MATLAB-only code. Without requiring explicit indexing, the file reader object manages the data frame sizes while the filter manages the states. The audio player object plays each audio frame as it is processed.

Was this topic helpful?