Generate DSP Applications with MATLAB Compiler
This example shows how to use the MATLAB
® Compiler™ to create a standalone application from a MATLAB
® function that uses System objects from DSP System Toolbox™.
In this example, you start with the function RLSFilterSystemIDCompilerExampleApp that uses RLS filter for system identification. You generate an executable application from this function using MATLAB Compiler and then run the application. The advantage of generating such standalone applications is that they can be run even on systems that do not have MATLAB installed. These only need an installation of MATLAB Runtime.
System Identification Algorithm
Recursive Least-Squares (RLS) filters are adaptive filters that can be used to identify an unknown system.
RLSFilterSystemIDCompilerExampleApp uses RLS filters to identify a system that has a variable cutoff frequency. The system is a lowpass FIR filter implemented using
dsp.VariableBandwidthFIRFilter. The RLS filter is implemented using
For more information on the algorithm and setup, follow the example: System Identification Using RLS Adaptive Filtering.
To verify the behavior of
RLSFilterSystemIDCompilerExampleApp, run the function in MATLAB. It takes an optional input which is number of iteration steps. The default value is 300 iterations.
A user interface (UI) comes up which has two parameters that you can control:
Cutoff Frequency (Hz) - Cutoff frequency of the lowpass filter to be identified, specified as a scalar in the range [0, 5000] Hz.
RLS Forgetting Factor - Forgetting factor for the RLS filter used for system identification, specified as a scalar in the range [0, 1].
When the simulation finishes or when you click on
Stop Simulation button, you will see a plot of the changes you made to these parameters and how it affected the mean-squared error (MSE) of the RLS filter.
Create a Temporary Directory for Compilation
Once you are satisfied with the function's simulation in MATLAB, you can compile the function. Before compiling, create a temporary directory in which you have write permissions. Copy the main MATLAB function and the associated helper files into this temporary directory.
compilerDir = fullfile(tempdir,'compilerDir'); % Name of temporary directory if ~exist(compilerDir,'dir') mkdir(compilerDir); % Create temporary directory end copyfile(which('RLSFilterSystemIDCompilerExampleApp'),compilerDir,'f'); copyfile(which('HelperRLSFilterSystemIdentificationSim'),compilerDir,'f'); copyfile(which('HelperCreateParamTuningUI'),compilerDir,'f'); copyfile(which('HelperUnpackUIData'),compilerDir,'f'); curDir = cd(compilerDir);
Compile the MATLAB Function into a Standalone Application
In the temporary directory you just created, run
mcc (MATLAB Compiler) command on the MATLAB function
mcc invokes the MATLAB Compiler which compiles the MATLAB function into a standalone executable that is saved in the current directory. Use the
mcc (MATLAB Compiler) function from MATLAB Compiler to compile
RLSFilterSystemIDCompilerExampleApp into a standalone application. Specify the '-m' option to generate a standalone application, '-N' option to include only the directories in the path specified through the '-p' option.
mcc('-mN', 'RLSFilterSystemIDCompilerExampleApp', ... '-p', fullfile(matlabroot,'toolbox','dsp'));
This step takes a few minutes to complete.
Run the Deployed Application
system command to run the generated standalone application. Note that running the standalone application using the system command uses the current MATLAB environment and any library files needed from this installation of MATLAB. To deploy this application on a machine which does not have MATLAB installed, refer to the Relocate Code Generated from MATLAB Code to Another Development Environment
if ismac status = system(fullfile('RLSFilterSystemIDCompilerExampleApp.app', ... 'Contents', 'MacOS', 'RLSFilterSystemIDCompilerExampleApp')); else status = system(fullfile(pwd, 'RLSFilterSystemIDCompilerExampleApp')); end
Similar to the MATLAB example System Identification Using RLS Adaptive Filtering, running this executable application also launches a UI. The UI allows you to tune parameters and the results are reflected in the simulation instantly. For example, move the slider for the 'Cutoff frequency (Hz)' to the left while the simulation is running. You will see a drop in the plot for cutoff frequency and a corresponding fluctuation in the MSE of RLS filter. You can use the buttons on the UI to pause or stop the simulation.
Clean up Generated Files
After generating and deploying the executable, you can clean up the temporary directory by running the following in the MATLAB command prompt: