This example shows how to use a recursive least-squares (RLS) filter to identify an unknown system modeled with a lowpass FIR filter. Transfer function estimation is used to compare the frequency response of the unknown and estimated systems. This example allows you to dynamically tune key simulation parameters using a graphical user interface (GUI). The example also shows you how to use MATLAB Coder to generate code for the algorithm and accelerate the speed of its execution.
Required MathWorks™ products:
DSP System Toolbox™
Optional MathWorks™ products:
Adaptive system identification is one of the main applications of adaptive filtering. This example showcases system identification using an RLS filter. The example's workflow is depicted below:
The unknown system is modeled by a lowpass FIR filter. The same input is fed to the FIR and RLS filters. The desired signal is the output of the unidentified system. The estimated weights of the RLS filter therefore converges to the coefficients of the FIR filter. The input signal and the output signals of the unknown and estimated systems are passed to a transfer function estimator. The desired and estimated frequency transfer functions are then visualized on a scope. The learning curve of the RLS filter (the plot of the mean square error (MSE) of the filter versus time) is also visualized.
The lowpass FIR filter used in this example is modeled using a dspdemo.TunableFIRFilterdspdemo.TunableFIRFilter System object. This object allows you to tune the filter's cutoff frequency while preserving the FIR structure. Tuning is achieved by multiplying each filter coefficient by a factor proportional to the current and desired cutoff frequencies. For more information on this object, type help dspdemo.TunableFIRFilterhelp dspdemo.TunableFIRFilter. Refer to  for details of the algorithm.
HelperRLSFilterSystemIdentificationSimHelperRLSFilterSystemIdentificationSim is the function containing the algorithm's implementation. It instantiates, initializes and steps through the objects forming the algorithm.
The function HelperRLSFilterSystemIdentificationHelperRLSFilterSystemIdentification wraps around HelperRLSFilterSystemIdentificationSimHelperRLSFilterSystemIdentificationSim and iteratively calls it, providing continuous adapting to the unidentified FIR system. It also plots the following:
The desired versus estimated frequency transfer functions.
The desired versus estimated filter coefficients.
The learning curve of the RLS filter.
Plotting occurs when the 'plotResults' input to the function is 'true'.
Execute HelperRLSFilterSystemIdentificationExecute HelperRLSFilterSystemIdentification to run the simulation and plot the results on scopes. Note that the simulation runs for as long as the user does not explicitly stop it.
The plots below are the output of running the above simulation for 100 time-steps:
The fast convergence of the RLS filter towards the FIR filter can be seen through the above plots.
HelperRLSFilterSystemIdentification launches a Graphical User Interface (GUI) designed to interact with the simulation. The GUI allows you to tune parameters and the results are reflected in the simulation instantly. For example, moving the slider for the 'Cutoff Frequency' to the right while the simulation is running, increases the FIR filter's cutoff frequency. Similarly, moving the slider for the 'RLS Forgetting Factor' tunes the forgetting factor of the RLS filter. The plots reflects your changes as you tune these parameters. For more information on the GUI, please refer to HelperCreateParamTuningGUIHelperCreateParamTuningGUI.
There are also two buttons on the GUI - the 'Reset' button resets the states of the RLS and FIR filters to their initial values, and 'Stop simulation' ends the simulation. If you tune the RLS filter's forgetting factor to a value that is too low, you will notice that the RLS filter fails to converge to the desired solution, as expcted. You can restore convergence by first increasing the forgetting factor to an acceptable value, and then clicking the 'Reset' button. The interaction between the GUI and the simulation is performed using UDP. Using UDP enables the GUI to control either the simulation or, optionally, a MEX-file (or standalone executable) generated from the simulation code as detailed below. If you have a MIDI controller, it is possible to synchronize it with the GUI. You can do this by choosing a MIDI control in the dialog that is opened when you right-click on the sliders or buttons and select "Synchronize" from the context menu. The chosen MIDI control then works in accordance with the slider/button so that operating one control is tracked by the other.
MATLAB Coder can be used to generate C code for the function HelperRLSFilterSystemIdentificationSimHelperRLSFilterSystemIdentificationSim as well. In order to generate a MEX-file for your platform, execute the following:
currDir = pwd; % Store the current directory address mexDir = [tempdir 'RLSFilterSystemIdentificationExampleMEXDir']; % Name of % temporary directory mkdir(mexDir); % Create temporary directory cd(mexDir); % Change directory codegen('HelperRLSFilterSystemIdentificationSim', ... '-o','HelperRLSFilterSystemIdentificationSimMEX');
By calling the wrapper function HelperRLSFilterSystemIdentificationHelperRLSFilterSystemIdentification with 'true' as an argument, the generated MEX-file HelperRLSFilterSystemIdentificationSimMEX can be used instead of HelperRLSFilterSystemIdentificationSim for the simulation. In this scenario, the GUI is still running inside the MATLAB environment, but the main processing algorithm is being performed by a MEX-file. Performance is improved in this mode without compromising the ability to tune parameters.
Creating MEX-Files often helps achieve faster run-times for simulations. In order to measure the performance improvement, let's first time the execution of the algoriothm in MATLAB without any plotting:
clear HelperRLSFilterSystemIdentificationSim disp('Running the MATLAB code...') tic nTimeSteps = 100; for ind = 1:nTimeSteps HelperRLSFilterSystemIdentificationSim(); end tMATLAB = toc;
Running the MATLAB code...
Now let's time the run of the corresponding MEX-file and display the results:
clear HelperRLSFilterSystemIdentificationSim disp('Running the MEX-File...') tic for ind = 1:nTimeSteps HelperRLSFilterSystemIdentificationSimMEX(); end tMEX = toc; disp('RESULTS:') disp(['Time taken to run the MATLAB System object: ', num2str(tMATLAB),... ' seconds']); disp(['Time taken to run the MEX-File: ', num2str(tMEX), ' seconds']); disp(['Speed-up by a factor of ', num2str(tMATLAB/tMEX),... ' is achieved by creating the MEX-File']);
Running the MEX-File... RESULTS: Time taken to run the MATLAB System object: 12.5755 seconds Time taken to run the MEX-File: 0.52907 seconds Speed-up by a factor of 23.769 is achieved by creating the MEX-File
The temperory directory previously created can be deleted through:
cd(currDir); clear HelperRLSFilterSystemIdentificationSimMEX; rmdir(mexDir, 's');
 Mitra, S.K., "Digital Signal Processing - A Computer-based Approach", Section 8.7.2, "Tunable FIR Digital Filters", McGraw-Hill, 2nd Edition.