Closed Loop FRF identification with 20-sim (3-point method)

This page is a guide to frequency response identification with the use of 20-sim and MATLAB. The identification method used is the '3-point method'.

20-sim-4C actions

1. Design controller

First of all it is important to create a suitable controller for identification. This should be a weak controller which makes the system (roughly) follow the reference profile. The reason you don't want a stiff controller for identification is that a stiff controller would suppress the error of the system. Thus you would identify your controller instead of your plant.

If you know your system you can use 20-sim to create a model of it. That way you can run simulations for your controller to see if it creates the desired behavior. Once you are done designing your controller, select the blocks that belong to the controller, including reference profile, summation point etc. create a subsystem out of them by selecting "Edit -> Implode"

Implode selection

Imploded selection

In the sub-model 'Controller' the previously selected components can be found. In the sub-model it is now possible to add a noise source to the output of the controller, which is required for the identification process.

Note I:
It is not possible to do simulations with the noise source in the model due to solver issues.

Note II:
For the fourth order systems of the ACE7 practical the controller sub-model needs to have an extra output to enable and disable the motors. Possible in-/outputs of this sub-model are the lines that go in and out the 'Controller' block. However it is possible to add an extra port to the sub-model with one of the following methods:
  • Select the submodel in the main window of 20sim. Then, click on the 'Interface' tab in the lower left of the screen. There you can add an input or output by right clicking.
  • Another way to add inputs or outputs is by right clicking the submodel block in the main window and select 'edit interface'. In this window it is possible to add or delete inputs and outputs. You can also open this menu by clicking on 'Tools' in the top bar in 20sim, and then going to the interface editor.
The direction of this new port should be Output and can be given the name 'Enable'. Connect this output to a constant which is set to the value 1.

Add gate to the sub-model
Controller sub-model with enable and noise source

2. Measure the frequency response of the system in 20-sim 4C

Once you created a suitable controller for your system, start 20-sim 4C by going to the 20-sim simulator window.

Next click 'Tools -> Real-Time Toolbox -> C-Code generation'.

In the pop-up window select 20-sim 4C 2.0 and for the sub-model select the controller sub-model you just created.

In 20-sim 4C setup the target as you usually would and connect the software in/outputs with the hardware pins.

An important window to setup is the 'Log' screen. When the controller is run with logging 20-sim automatically generates a file with the values of selected logging parameters. Go to the 'Log' screen and start by selecting the desired parameters.For 3-point closed-loop identification this would be the noise output D, the difference between reference and actual position known as error E, and the sum of the controller output and the noise output U.
From these parameters you can create the Sensitivity frequency response and the Process Sensitivity frequency response of your system. From which the frequency response of the plant can be derived.

After setting up the logging variables, select a finish time that would be sufficient to gather enough information to create reliable frequency response data. Selecting a longer measurement time will generally result in a better coherence function as you will have more data that can be relevant. (especially for lower frequencies!)
Before you run the model do not forget to setup where your logging file needs to be saved! Finally you can choose to 'run the model with logging'runwithlogging.png

MATLAB actions

Analyzing the measurement file is preferably done in MATLAB. In order to be able to do so the measurement data has to be imported to MATLAB


import wizard

Next be sure to select the correct delimiter (comma) and to import the data as column vectors. To check if the import was successful look in your work space

Workspace with imported variables

For a three point identification measurement the following MATLAB file can be used. This file determines the sensitivity, process sensitivity and finally the plant. This file can be adjusted for other identification methods as well. When this script is run it will display the number of samples, number of frames, frequency resolution and the frame time in the MATLAB command window. Make sure the number of frames over which is averaged is between 10 and 20 in order to achieve good results. (Thus adjust the window length accordingly)
D=...; %Data of disturbance
U=...; %Data of Sum(controller+disturbance)
E=...; %Data of error
%% FRF setup
% Setup NFFT
fs = ...;  % Sample frequency
nfft=2^13; % window length
noverlap=nfft/2; % overlap between widows
wmax=fs/2; % maximum frequency
wmin=fs/nfft; % minimum frequency
% Some calculations on measurement
nsamp=length(D); % number of samples
nframes=nsamp/nfft; % number of frames (avaraging)
frametime=nfft/fs; % timelength of frame
fres=1/frametime; % frequency resolution
% Display FFT settings
disp('- - -')
disp('N samples (x1.000.000) - N frames - Freq res - Frame time')
disp([nsamp*1e-6 nframes fres frametime]);
%% Calculate Sensitivity
% Measured Sensitivity
Sens_in = D;
Sens_out = U;
[S, fx1] = tfestimate(Sens_in, Sens_out, hanning(nfft), noverlap ,nfft, fs);
f = gcf();
set(f,'Position',[10 10 800 700]);
subplot(3,1,1); semilogx(fx1, db(S));
grid on;
title('Measured Sensitivity')
axis([wmin wmax min(db(S)) max(db(S))]);
ylabel('Magnitude [db]');
subplot(3,1,2); semilogx(fx1, angle(S)*180/pi);
grid on;
axis([wmin wmax -200 200]);
ylabel('Phase [degrees]');
[Scoh1, wcohS] = mscohere(Sens_in, Sens_out, hanning(nfft), noverlap, nfft,fs);
subplot(3,1,3); semilogx(wcohS, Scoh1);
axis([wmin wmax 0 1]);
grid on;
xlabel('Frequency [Hz]');
%% Calculate Process Sensitivity
% Process Sensitivity
PS_in = D;
PS_out = E;
[PSmin, fx1] = tfestimate(PS_in, PS_out, hanning(nfft), noverlap ,nfft, fs);
f = gcf();
set(f,'Position',[10 10 800 700]);
subplot(3,1,1); semilogx(fx1, db(PSmin));
grid on;
title('Process Sensitivity')
axis([wmin wmax min(db(PSmin)) max(db(PSmin))]);
ylabel('Magnitude [db]');
subplot(3,1,2); semilogx(fx1, angle(PSmin)*180/pi);
grid on;
axis([wmin wmax -200 200]);
ylabel('Phase [degrees]');
[PSmincoh1, wcohPSmin] = mscohere(PS_in, PS_out, hanning(nfft), noverlap, nfft,fs);
subplot(3,1,3); semilogx(wcohPSmin, PSmincoh1);
axis([wmin wmax 0 1]);
grid on;
xlabel('Frequency [Hz]');
%% Calculate PLANT
% Plant
H = PS./S;
subplot(2,1,1); semilogx(fx1, db(H));
grid on;
title('Measured Plant')
subplot(2,1,2); semilogx(fx1, angle(H)*180/pi);
grid on;
axis([wmin wmax -200 200]);
ylabel('Phase [degrees]');
xlabel('Frequency [Hz]');
%% Save data to C:\Identification\MeasuredPlant.txt
freq = fx1;
mag = db(H);
phase = angle(H)*180/pi;
data = [freq mag phase];

This script produces the plots of the measured sensitivity, process sensitivity and the plant. In these plots it is important to analyze the coherence plot. When the coherence is relatively low (<0.8) it indicates there is no linear relation between the input and output, thus only the measured frequency range with a high coherence can be regarded as reliable. A possible solution to increase the coherence is taking a longer measurement.

Measurement results

If the results from the script are regarded reliable and a valid identification of the system, the measured plant can be 'fitted' in 20-sim again to determine the approximated transfer function. The MATLAB script creates a folder in C:\ named Identification. The file in this folder holds the frequency data of the measured plant with the frequency vector in Hz, the magnitude in dB's and the angle in degrees. (the format 20-sim requires)

A clear description on how to curve fit a measured system in 20-sim can be found on the 20-sim website under the section video's. (
Before starting to fit a curve, be sure your 20-sim frequency unit is set to Hz.