Main Content

MATLAB Performance Optimization

Code Profiling

You can use the profile function to analyze code processing. After determining which portions of code are consuming the most processing time, consider techniques such as vector-based processing and MATLAB® code generation to accelerate simulation run times.

Vector-Based Processing

To shorten the overall run time of MATLAB functions that include System objects use vector-based processing for large vectors of data.

With vector-based processing, function call overhead is reduced because the program processes multiple samples during a single execution call to a System object™. Processing larger vectors of data, specifically vectors with thousands of data points, in a single function call improves performance of MATLAB functions by reducing the frequency of System object setup overhead.

MATLAB Code Generation

Accelerate your MATLAB algorithms by generating a MEX function using the function codegen (MATLAB Coder).

RTL-SDR System Object Code Generation

This example outlines the steps to follow when generating a MEX file. When generating and running code that uses an RTL-SDR System object, the considerations are similar to those made for any MATLAB code generation. Write a function to serve as the main function for the code. Considering these points:

  • Define the System object inputs.

  • If you will make multiple calls to the object, declare the object as persistent so that it retains memory between calls. Add conditional logic to create and load the object into memory only once.

  • Release each object from memory when it is no longer needed.

This MATLAB function serves as the main function for the code that codegen (MATLAB Coder) compiles into a MEX function. In this example, a Boolean input is used as a release flag to release the object from memory.

function x = sdrrtlrx(releaseFlag)

persistent rx

if isempty(rx)
    % The System object is created only if it isn't already in memory.
    rx = comm.SDRRTLReceiver('OutputDataType', 'double');
end

if releaseFlag
    % When finished, release the object and all resources used by the object.
    release(rx)
    x = complex(zeros(rx.SamplesPerFrame,1));
else
    % Execute the RTL-SDR receiver System object that was created earlier.
    x = rx();
end
Before performing code generation, execute the function from the command line.

Note

To successfully run the function you must connect the RTL-SDR hardware to your system. For more information, see Installation and Setup.

  • Executing sdrrtlrx(false) the first time creates the RTL-SDR receiver System object and loads the object into memory.

  • Subsequent executions of sdrrtlrx(false) run the RTL-SDR receiver System object that exists in memory.

  • Executing sdrrtlrx(true) releases the RTL-SDR receiver System object and all resources used by the interface.

After confirming the function runs as expected in MATLAB, perform code generation, declaring the input argument types:

codegen sdrrtlrx -args false
Executing this command produces a library of files in a codegen subdirectory that contains the executable MEX file, along with all intermediate and supporting files. By default, codegen creates an output MEX file with _mex appended to the function name. For more information, see codegen. Run this codegen example, by appending _mex to the function name:
sdrrtlrx_mex(false)

RTL-SDR System Object Code Generation of an FM Broadcast Receiver

This example presents a function that configures objects to build an FM stereo receiver. The example then shows how to use the codegen (MATLAB Coder) function to generate a MEX file version of the function to run.

function fmrx(duration, station)
% This function builds an FM stereo receiver using MATLAB(R) and Communications 
% Toolbox(TM), and enables you to receive signals in real time using the 
% RTL-SDR USB radio hardware and the Communications Toolbox Support Package 
% for RTL-SDR Radio. Function inputs specify the duration in seconds to play the FM 
% signal and the FM station to tune the radio to.

% Initialize run-time parameters.
centerFreq = station*1e6; % FM radio station
samplesPerFrame = 3840;
rfSampleRate = 240000;

freqDeviation = 75000;
filterTimeConst = 7.5e-5;

audioSampleRate = 48000;

radioTime = 0;
frontEndFrameTime = samplesPerFrame/rfSampleRate;

%%
% Create an RTL-SDR receiver System object.
fmReceiver = comm.SDRRTLReceiver('OutputDataType','double',...
    'SampleRate',rfSampleRate,'SamplesPerFrame', samplesPerFrame,...
    'CenterFrequency',centerFreq);
%%
% Create an FM broadcast receiver System object.
fmBroadcastDemod = comm.FMBroadcastDemodulator(...
    'SampleRate', rfSampleRate,...
    'FrequencyDeviation', freqDeviation, 'FilterTimeConstant', filterTimeConst,...
    'AudioSampleRate', audioSampleRate, 'Stereo', true);
%%
% Create an audio device writer System Object(TM).
player = audioDeviceWriter('SampleRate',audioSampleRate);

%%
% Loop until timer expires, receiving and playing the specified FM station.
while radioTime < duration
    %%
    % Receive baseband samples from the specified FM station.
    [rcv,~,lost,~] = fmReceiver();
    %%
    % Demodulate FM broadcast signals and play the decoded audio.
    audioSig = fmBroadcastDemod(rcv);
    player(audioSig);
    %%
    % Update radio time. If there were lost samples, add those too.
    radioTime = radioTime + frontEndFrameTime + double(lost)/rfSampleRate;
end

release(fmReceiver)
release(fmBroadcastDemod)
release(player)

Before performing code generation, execute the function from the command line.

Note

To successfully run the function you must connect the RTL-SDR hardware to your system. For more information, see Installation and Setup.

Executing fmrx(10,98.5):

  1. Creates System objects for the RTL-SDR receiver tuned to the specified station, the FM broadcast receiver, and the audio device writer.

  2. Loads the objects into memory.

  3. Runs in a loop for the specified duration.

  4. Releases the objects from memory.

After confirming the function runs as expected in MATLAB, perform code generation, declaring the input argument types.

% Specify input parameters.
duration = 100;
station = 98.5;
% Perform code generation.
codegen fmrx -args {duration,station}

% Run the MEX file.
fmrx_mex(duration, station)

Related Topics