System Identification Using RLS Adaptive Filtering - MATLAB & Simulink - MathWorks Deutschland (2024)

This example uses:

  • DSP System ToolboxDSP System Toolbox
  • SimulinkSimulink
  • MATLAB CoderMATLAB Coder

Open Live Script

This example shows how to use a recursive least-squares (RLS) filter to identify an unknown system modeled with a lowpass FIR filter. Use the dynamic filter visualizer to compare the frequency response of the unknown and estimated systems. This example allows you to dynamically tune key simulation parameters using a user interface (UI). 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:

  • MATLAB Coder for generating C code from the MATLAB simulation

  • Simulink® for executing the Simulink version of the example

Introduction

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:

System Identification Using RLS Adaptive Filtering- MATLAB & Simulink- MathWorks Deutschland (1)

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 coefficients of the RLS filter and FIR filter are used by the dynamic filter visualizer to visualize the desired and estimated frequency response. The learning curve of the RLS filter (the plot of the mean square error (MSE) of the filter versus time) is also visualized.

Tunable FIR Filter

The lowpass FIR filter used in this example is modeled using a dsp.VariableBandwidthFIRFilter 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.

MATLAB Simulation

HelperRLSFilterSystemIdentificationSim is the function containing the algorithm's implementation. It instantiates, initializes and steps through the objects forming the algorithm.

The function RLSFilterSystemIDExampleApp wraps around HelperRLSFilterSystemIdentificationSim and iteratively calls it, providing continuous adapting to the unidentified FIR system. Using dsp.DynamicFilterVisualizer the application also plots the following:

  1. The desired versus estimated frequency transfer functions.

  2. The learning curve of the RLS filter.

Plotting occurs when the 'plotResults' input to the function is 'true'.

Execute RLSFilterSystemIDExampleApp 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:

System Identification Using RLS Adaptive Filtering- MATLAB & Simulink- MathWorks Deutschland (2)

System Identification Using RLS Adaptive Filtering- MATLAB & Simulink- MathWorks Deutschland (3)

The fast convergence of the RLS filter towards the FIR filter can be seen through the above plots.

RLSFilterSystemIDExampleApp launches a User Interface (UI) designed to interact with the simulation. The UI 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 UI, please refer to HelperCreateParamTuningUI.

There are also two buttons on the UI - 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 expected. You can restore convergence by first increasing the forgetting factor to an acceptable value, and then clicking the 'Reset' button. Use the UI 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 UI. 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.

System Identification Using RLS Adaptive Filtering- MATLAB & Simulink- MathWorks Deutschland (4)

Generating the MEX-File

MATLAB Coder can be used to generate C code for the function HelperRLSFilterSystemIdentificationSim as well. In order to generate a MEX-file for your platform, execute the following:

currDir = pwd; % Store the current directory addressaddpath(pwd)mexDir = [tempdir 'RLSFilterSystemIdentificationExampleMEXDir']; % Name of % temporary directoryif ~exist(mexDir,'dir') mkdir(mexDir); % Create temporary directoryendcd(mexDir); % Change directoryParamStruct = HelperRLSCodeGeneration();
Code generation successful: To view the report, open('codegen/mex/HelperRLSFilterSystemIdentificationSim/html/report.mldatx')

By calling the wrapper function RLSFilterSystemIDExampleApp with 'true' as an argument, the generated MEX-file HelperRLSFilterSystemIdentificationSimMEX can be used instead of HelperRLSFilterSystemIdentificationSim for the simulation. In this scenario, the UI 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.

Click here to call RLSFilterSystemIDExampleApp with 'true' as argument to use the MEX-file for simulation. Again, the simulation runs till the user explicitly stops it from the UI.

Simulation Versus MEX Speed Comparison

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 algorithm in MATLAB without any plotting:

clear HelperRLSFilterSystemIdentificationSimdisp('Running the MATLAB code...')
Running the MATLAB code...
ticnTimeSteps = 100;for ind = 1:nTimeSteps HelperRLSFilterSystemIdentificationSim(ParamStruct);endtMATLAB = toc;

Now let's time the run of the corresponding MEX-file and display the results:

clear HelperRLSFilterSystemIdentificationSimdisp('Running the MEX-File...')
Running the MEX-File...
ticfor ind = 1:nTimeSteps HelperRLSFilterSystemIdentificationSimMEX(ParamStruct);endtMEX = toc;disp('RESULTS:')
RESULTS:
disp(['Time taken to run the MATLAB System object: ', num2str(tMATLAB),... ' seconds']);
Time taken to run the MATLAB System object: 2.8408 seconds
disp(['Time taken to run the MEX-File: ', num2str(tMEX), ' seconds']);
Time taken to run the MEX-File: 0.51847 seconds
disp(['Speed-up by a factor of ', num2str(tMATLAB/tMEX),... ' is achieved by creating the MEX-File']);
Speed-up by a factor of 5.4792 is achieved by creating the MEX-File

Clean up Generated Files

The temporary directory previously created can be deleted through:

cd(currDir);clear HelperRLSFilterSystemIdentificationSimMEX;rmdir(mexDir, 's');

Simulink Version

rlsfiltersystemidentification is a Simulink model that implements the RLS System identification example highlighted in the previous sections.

System Identification Using RLS Adaptive Filtering- MATLAB & Simulink- MathWorks Deutschland (5)

In this model, the unknown system is a lowpass FIR filter is modeled using the Variable Bandwidth FIR Filter block. Inside the System Identification subsystem, the Variable Bandwidth FIR Filter block takes in the input signal and provides the filtered signal at the output. The input signal along with this desired output signal are fed to the RLS Filter block which estimates the weights of the filter.

System Identification Using RLS Adaptive Filtering- MATLAB & Simulink- MathWorks Deutschland (6)

The coefficients of the lowpass filter are fed to the Filter Visualizer to visualize the magnitude response of the filter.

Double-click the System Identification subsystem to launch the mask designed to interact with the Simulink model. You can tune the cutoff frequency of the FIR filter and the forgetting factor of the RLS filter.

The model generates code when it is simulated. Therefore, it must be executed from a folder with write permissions.

See Also

Objects

  • dsp.RLSFilter | dsp.VariableBandwidthFIRFilter | timescope | dsp.DynamicFilterVisualizer

Blocks

  • Random Source | Variable Bandwidth FIR Filter | RLS Filter | Time Scope | Array Plot | Filter Visualizer

Related Topics

  • Inverse System Identification Using RLS Algorithm
  • System Identification of FIR Filter Using LMS Algorithm
  • System Identification of FIR Filter Using Normalized LMS Algorithm
System Identification Using RLS Adaptive Filtering
- MATLAB & Simulink
- MathWorks Deutschland (2024)

FAQs

What is the RLS adaptive filter algorithm? ›

Recursive least squares (RLS) is an adaptive filter algorithm that recursively finds the coefficients that minimize a weighted linear least squares cost function relating to the input signals.

What is the adaptive filter function in Matlab? ›

Adaptive filters are digital filters whose coefficients change with an objective to make the filter converge to an optimal state. The optimization criterion is a cost function, which is most commonly the mean square of the error signal between the output of the adaptive filter and the desired signal.

What is system identification method Matlab? ›

System identification uses the input and output signals you measure from a system to estimate the values of adjustable parameters in a given model structure. You can build models using time-domain input-output signals, frequency response data, time -series signals, and time-series spectra.

What is the algorithm of RLS? ›

4 Recursive Least Squares Algorithm. The RLS algorithms aim to minimize the sum of the squares of the difference between the desired signal and the filter output signal using the new samples of the incoming signal. The RLS algorithms compute filter coefficients in a recursive form at each iteration.

What is the difference between LMS and RLS filter? ›

Least mean squares (LMS) algorithms represent the simplest and most easily applied adaptive algorithms. The recursive least squares (RLS) algorithms, on the other hand, are known for their excellent performance and greater fidelity, but they come with increased complexity and computational cost.

What is an example of an adaptive filter? ›

Example application

To circumvent this potential loss of information, an adaptive filter could be used. The adaptive filter would take input both from the patient and from the mains and would thus be able to track the actual frequency of the noise as it fluctuates and subtract the noise from the recording.

Why do we need adaptive filter? ›

Prediction, where the function of the adaptive filter is to provide the optimum prediction (in some statistical sense) of the present value of an input signal, given a past record of the input In this application, the present value of the input signal serves as the desired response.

What are the two applications of adaptive filter? ›

The application depends on the adaptive filter configuration used. The classical configurations of adaptive filtering are system identification, prediction, noise cancellation, and inverse modeling.

What is Simulink used for in MATLAB? ›

Simulink is the platform for Model-Based Design that supports system-level design, simulation, automatic code generation, and continuous test and verification of embedded systems. Key capabilities include: A graphical editor for modeling all components of a system.

How do I identify a system in MATLAB? ›

You can start by opening System Identification Tool and following the workflow shown by the arrows. Start this importing the data. In this case, we'll import two data sets, data t time domain data set and data f frequency domain data set. Select the data set to work with and check the contents of this data set.

How to open System Identification in MATLAB? ›

In the Apps section, click System Identification. This action opens the System Identification app. Only one session can be open at a time. You can also start a new session by closing the current session using File > Close session.

What is the algorithm for adaptive median filter? ›

The adaptive median filter works in two levels denoted Level A and Level B as follows: Level A: A1= Zmed - Zmin A2= Zmed - Zmax If A1 > 0 AND A2 < 0, Go to level B Else increase the window size If window size <=Smax repeat level A Else output Zxy.

What is the adaptive traffic control algorithm? ›

ATCS algorithm adjusts traffic signal timings continuously based on the traffic demand at the intersections and anticipated arrivals from adjacent intersections.

What is the fast RLS algorithm? ›

The Fast Transversal RLS (FT-RLS) filter is designed to provide the solution to the filtering problem with performance equal to the standard recursive least-squares (RLS) algorithm. In addition, the FT-RLS filter provides this solution with reduced computational burden which scales linearly with the filter order.

What is adaptive filtering method? ›

An adaptive filter is a digital filter that has self-adjusting characteristics. It is capable of adjusting its filter coefficients automatically to adapt the input signal via an adaptive algorithm.

References

Top Articles
Latest Posts
Article information

Author: Corie Satterfield

Last Updated:

Views: 5800

Rating: 4.1 / 5 (62 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Corie Satterfield

Birthday: 1992-08-19

Address: 850 Benjamin Bridge, Dickinsonchester, CO 68572-0542

Phone: +26813599986666

Job: Sales Manager

Hobby: Table tennis, Soapmaking, Flower arranging, amateur radio, Rock climbing, scrapbook, Horseback riding

Introduction: My name is Corie Satterfield, I am a fancy, perfect, spotless, quaint, fantastic, funny, lucky person who loves writing and wants to share my knowledge and understanding with you.