MataveID is a basic system identification toolbox for both GNU Octave and MATLAB®. MataveID is based on the power of linear algebra and the library is easy to use. MataveID using the classical realization and polynomal theories to identify state space models from data. There are lots of subspace methods in the "old" folder and the reason why I'm not using these files is because they can't handle noise quite well.
I'm building this library because I feel that the commercial libraries are just for theoretical experiments. I'm focusing on real practice and solving real world problems.
Papers:
MataveID contains realization identification, polynomal algorithms and subspace algorithms. They can be quite hard to understand, so I highly recommend to read papers in the "reports" folder about the algorithms if you want to understand how they work, or read the literature.
Literature:
I have been using these books for creating the .m files. All these books have different audience. Some techniques are meant for researchers and some are meant for practical engineering.
Applied System Identification
This book include techniques for linear mechanical systems such as vibrating beams, damping, structural mechanics etc. These techniques comes from NASA and the techniques are created by Jer-Nan Juang. This is a very practical book. The book uses the so called realization theory methods for identify dynamical models from data.
Advantages:
Easy to read and very practical
Include mechanical model buildning
Include impulse, frequency, stochastic, closed loop and recursive identification
These techniques are applied onto Hubble Telescope, Space Shuttle Discovery and Galileo spacecraft
Disadvantages:
Do not include nonlinear system identification and subspace methods
Do not include filtering
MATLAB files from this book is export controlled from NASA = Difficult to download
This book is not produced anymore. I have the PDF.
System Modeling & Identification
This book covering techniques for all types of systems, linear and nonlinear, but it's more a general book for system identfication. Professor Rolf Johansson book contains lots of practice, but also theory as well. More theory and less practice compared to Applied System Identification from Jer-Nan Juang. This book uses both the realization theory methods and subspace methods for identify dynamical systems from data. Also this book includes filters as well such as Uncented Kalman Filter. Can be purchased from https://kfsab.se/sortiment/system-modeling-and-identification/
Advantages:
Easy to read and somtimes practical
Include filtering, statistics and other types of modeling techniques
Include impulse, frequency, stochastic, closed loop, nonlinear and recursive identification
Include both realization theory, subspace and nonlinear system identification methods
Disadvantages:
Do not include closed loop identification
Some methods are difficult to understand how to apply with MATLAB-code. Typical univerity literature for students
Subspace Methods For System Identification
This book include techniques for all types of linear systems. It's a general book of linear system identification. The advantages of this book is that it include modern system identification techniques. The disadvantages about this book is that it contains only theory and no practice, but Professor Tohru Katayama, have made a great work for collecting all these subspace methods. Use this book if you want to have knowledge about the best subspace identification methods.
Advantages:
Include MATLAB code examples and lots of step by step examples
Include stochastic and closed identification
Include the latest methods for linear system identification
Include both realization theory and subspace system identification methods
Disadvantages:
Difficult to read and understand
Does not include impulse, frequency and nonlinear identification
Does not include filtering, statistics and other types of modeling techniques
Adaptive Control
This book is only for adaptive control. But there is one algorithm that are very useful - Recursive Least Squares. This is a very pratical book for applied adaptive control. It's uses the legacy SISO adaptive techniques such as pole placement, Self Tuning Regulator(STR) and Model Reference Adaptive Systems(MRAS) combined with Recursive Least Squares(RLS). If you wonder why only SISO and not MIMO, it's because adaptive control is very difficult to apply in practice and create a reliable controller for all types of systems. The more difficult problem is to solve, the more simplier technique need to be used.
Advantages:
The authors of the book explains which chapters are for pratcial engineering and theoretical researchers
Easy to read
Include both advanced and simple methods depending on which type of problem to solve
Disadvantages:
Only one system identification algorithm is taught
Only SISO model are applied
This book is made for adaptive control and have only one chapter that contains system identification
Examples
Multivariable Output-Error State Space
MOESP is an algorithm that identify a linear state space model. It was invented in 1992. It can both identify SISO and MISO models.
Try MOESP or N4SID. They give the same result, but sometimes MOESP can be better than N4SID. It all depends on the data.
[sysd] =mi.moesp(u, y, k, sampleTime, delay, systemorder); % k = Integer tuning parameter such as 10, 20, 25, 32, 47 etc.
RLS is an algorithm that creates a SISO model from data. Here you can select if you want to estimate an ARX, OE model or an ARMAX model, depending on the number of zeros in the polynomal "nze". Select number of error-zeros-polynomal "nze" to 1, and you will get a ARX model or select "nze" equal to model poles "np", you will get an ARMAX model that also includes a kalman gain matrix K. I recommending that. This algorithm can handle data with noise. This algorithm was invented 1821 by Carl Friedrich Gauss, but it was until 1950 when it got its attention in adaptive control.
Use this algorithm if you have data from a open/close loop system and you want to apply that algorithm into embedded system that have low RAM and low flash memory. RLS is very suitable for system that have a lack of memory.
[sysd, K] =mi.rls(u, y, np, nz, nze, sampleTime, delay, forgetting);
Notice that there are sevral functions that simplify the use of rls.m
[sysd, K] =mi.oe(u, y, np, nz, sampleTime, delay, forgetting);
[sysd, K] =mi.arx(u, y, np, nz, sampleTime, ktune, delay, forgetting);
[sysd, K] =mi.armax(u, y, np, nz, nze, sampleTime, ktune, delay, forgetting);
Example RLS
This is a hanging load of a hydraulic system. This system is a linear system due to the hydraulic cylinder that lift the load. Here I create two linear first order models. One for up lifting up and one for lowering down the weight. I'm also but a small orifice between the outlet and inlet of the hydraulic cylinder. That's create a more smooth behavior. Notice that this RLS algorithm also computes a Kalman gain matrix.
Here we can se that the first model follows the measured position perfect. The "down-curve" should be measured a little bit longer to get a perfect linear model.
SINDy - Sparse Identification of Nonlinear Dynamics
This is a new identification technique made by Eurika Kaiser from University of Washington. It extends the identification methods of grey-box modeling to a much simplier way. This is a very easy to use method, but still powerful because it use least squares with sequentially thresholded least squares procedure. I have made it much simpler because now it also creates the formula for the system. In more practical words, this method identify a nonlinear ordinary differential equations from time domain data.
This is very usefull if you have heavy nonlinear systems such as a hydraulic orifice or a hanging load.
This example is a real world example with noise and nonlinearities. Here I set up a hydraulic motor in a test bench and measure it's output and the current to the valve that gives the motor oil. The motor have two nonlinearities - Hysteresis and the input signal is not propotional to the output signal. By using two nonlinear models, we can avoid the hysteresis.
Square Root Uncented Kalman Filter for parameter estimation
This is Uncented Kalman Filter that using cholesky update method (more stable), instead of cholesky decomposition. This algorithm can estimate parameters to very a complex function if data is available. This method is reqursive and there is a C code version in CControl as well. Use this when you need to estimate parameters to a function if you have data that are generated from that function. It can be for example an object that you have measured data and you know the mathematical formula for that object. Use the measured data with this algorithm and find the parameters for the formula.
title(sprintf('Parameter estimation for parameter w%i', k));
ylabel(sprintf('w%i', k));
gridon
legend('Estimated parameters', 'Parameter error')
end
Square Root Uncented Kalman Filter for state estimation
This is Uncented Kalman Filter that using cholesky update method (more stable), instead of cholesky decomposition. This algorithm can estimate states from a very complex model. This method is reqursive and there is a C code version in CControl as well. Use this when you need to estimate state to a model if you have data that are generated from that function. It can be for example an object that you have measured data and you know the mathematical formula for that object. Use the measured data with this algorithm and find the states for the model.
title(sprintf('State estimation for state x%i', k));
ylabel(sprintf('x%i', k));
gridon
legend('y', 'xhat', 'x')
end
Numerical algorithm for Subspace State Space System IDentification.
N4SID is an algoritm that identify a linear state space model. Use this if you got regular data from a dynamical system. This algorithm can handle both SISO and MISO. N4SID algorithm was invented 1994. If you need a nonlinear state space model, check out the SINDy algorithm. Try N4SID or MOESP. They give the same result, but sometimes N4SID can be better than MOESP. It all depends on the data.
[sysd, K] =mi.n4sid(u, y, k, sampleTime, ktune, delay, systemorder); % k = Integer tuning parameter such as 10, 20, 25, 32, 47 etc. ktune = kalman filter tuning such as 0.1, 0.01 etc
Example N4SID
Here I programmed a Beijer PLC that controls the multivariable cylinder system. It's a nonlinear system, but N4SID can handle it because it's not so nonlinear as a hydraulic motor. Cylinder 0 and Cylinder 1 affecting each other when the propotional control valves opens.
This is an extention from OKID. The idea is the same, but OCID creates a LQR contol law as well. This algorithm works only for closed loop data. It have its orgin from NASA around 1992 when NASA wanted to identify a observer, model and a LQR control law from closed loop data that comes from an actively controlled aircraft wing in a wind tunnel at NASA Langley Research Center. This algorithm works for both SISO and MIMO models.
Use this algorithm if you want to extract a LQR control law, kalman observer and model from a running dynamical system. Or if your open loop system is unstable and it requries some kind of feedback to stabilize it. Then OCID is the perfect choice.
This OCID algorithm have a particle filter that estimates the markov parameters.
[sysd, K, L] =mi.ocid(r, uf, y, sampleTime, alpha, regularization, systemorder);
A particle filter is another estimation filter such as Square Root Uncented Kalman Filter (SR-UKF), but SR-UKF assume that the noise is gaussian (normally distributed) and SR-UKF requries a dynamical model. The particle filter does not require the user to specify a dynamical model and the particle filter assume that the noise can be non-gaussian or gaussian, nonlinear in other words.
The particle filter is using Kernel Density Estimation algorithm to create the internal probability model, hence the user only need to specify one parameter with the following example. If you don't have a model that describes the dynamical behaviour, this filter is the right choice for you then.
Box-Jenkins is a special case when a system model sysd and a disturbance model sysh need to be found. The disturbance is difficult to know and with this Box-Jenkins algorithm, then the user can identify the disturbance model and create an observer of it by using the kalman gain matrices K1, K2. Notice that this Box-Jenkins algorithm using subspace methods, instead of classical polynomial methods.
The disturbance model can be used for:
Create a disturbance simulation with feedback control
Notice that Canny is quite slow, but gives very thin edges, which is good. But if you only want to have the edges and you don't care how thick they are.
Then Sobel is the right solution for you because Sobel is much faster than Canny.
>> G=mi.sobel(imread('way.jpg'));
>> G(G<255) =0; % Every pixel that are not white is going to be black
>> imshow(uint8(G));
Result
Principal Component Analysis
Principal Component Analysis can be used for dimension reduction and projection on maximum variance between classes.
title(sprintf('Reconstruction %iD with error %f', c, reconstruction_error), 'FontSize', 20)
CCA - Canonical Correlation Analysis
If N4SID won't work for you due to high noise measurement, then CCA is an alternative method to use. CCA returns a state space model and a kalman gain matrix K.
[sysd, K] =mi.cca(u, y, k, sampleTime, delay); % k = Integer tuning parameter such as 10, 20, 25, 32, 47 etc.
Density-based spatial clustering of applications with noise
This is a cluster algorithm that can identify the amount of clusters.
This algorithm requries two tuning parameters, epsilon and min_pts, which stands for radius and minimum points.
This algorithm does not work if you have varying densities, else this algorithm is considered to be one of the best clustering algorithms.
So, make sure that all your classes have the same amount of variance before you are using this algorithm due to its robustness against noise/outliers.
It exist an equivalent C-code dbscan inside CControl repository.
[idx] =mi.dbscan(X, epsilon, min_pts);
Example Density-based spatial clustering of applications with noise
ERA/DC - Eigensystem Realization Algorithm Data Correlations
ERA/DC was invented 1987 and is a successor from ERA, that was invented 1985 at NASA. The difference between ERA/DC and ERA is that ERA/DC can handle noise much better than ERA. But both algorihtm works as the same. ERA/DC want an impulse response. e.g called markov parameters. You will get a state space model from this algorithm. This algorithm can handle both SISO and MISO data.
Use this algorithm if you got impulse data from e.g structural mechanics.
#Feature from accelerated segmentation test
Use Feature from accelerated segmentation test(FAST) if you want to find corners inside an image. There is also an equivalent C-code FAST algorithm inside the CControl repository.
This filter away noise with a good old low pass filter that are being runned twice. Filtfilt is equal to the famous function filtfilt in MATLAB, but this is a regular .m file and not a C/C++ subroutine. Easy to use and recommended.
X = imread(fullfile('..', 'data', 'test_hough.png'));
% If the image is color
if(size(X, 3) >1)
X = rgb2gray(X);
end
% Plot the image
imshow(X);
% Do hough transform
p =0.3; % Percentage definition of a line e.g all lines shorter than p times longest line, should be classes as a non-line
epsilon =10; % Minimum radius for hough cluster
min_pts =2; % Minimum points for hough cluster
[N, K, M] =mi.hough(X, p, epsilon, min_pts);
% Plot the lines together with the image
[~, n] = size(X);
x = linspace(0, n);
holdon
for i =1:N
y = K(i)*x+ M(i);
plot(x, y)
end
Independent Component Analysis
Independent component analysis(ICA) is a tool if you want to separate independent signals from each other. This is not a filter algorithm, but instead of removing noise, it separate the disturbances from the signals. The disturbances are created from other signals. Assume that you have an engine and you are measuring vibration in X, Y and Z-axis. These axis will affect each other and therefore the signals will act like they are mixed. ICA separate the mixed signals into clean and independent signals.
% End clock time and check the difference how long it took
toc
These signals are what we want to find
This is how the signals look when we are measuring them
This is how the signals are reconstructed as they were independent
IDBode - Identification Bode
This plots a bode diagram from measurement data. It can be very interesting to see how the amplitudes between input and output behaves over frequencies. This can be used to confirm if your estimated model is good or bad by using the bode command from Matavecontrol and compare it with idebode.
K-means clustering is a tool that can identify the center of clusters. All you need to do is to specify how many cluster IDs you think there exist in your data. Use this algorithm if your data is gaussian and you know the numbers of clusters. All you want to know are the cetrums of the clusters.
Kernel Principal Component Analysis can be used for dimension reduction and projection on maximum variance between classes.
Kernel methods make PCA suitable for nonlinear data. Kernels has proven very good results in nonlinear dimension reduction.
[a, b, flag, iterations] =mi.logreg(x, y, 'sigmoid');
% Plot the logistic function: sigmoid
t = linspace(min(x), max(x), 1000);
p =1./(1+ exp(-(a*t+b)));
figure
plot(t, p)
title('sigmoid function', 'FontSize', 20)
Linear Support Vector Machine
This is the standard way to create a support vector machine. Even if it's only returning back a linear model, it's still very powerful and suits systems that need extreamly fast predictions such as embedded systems.
Notice that the Linear Support Vector Machine can only do two-class prediction only. But you can use multiple classes with the Linear Support Vector Machine by using multiple linear support vector machines. It's called One-VS-All method.
[w, b, accuracy, solution] =mi.lsvm(x, y, C, lambda)
% This is an example for more than 3 columns of X.
% Here we cannot plot this 9D table because nobody knows how to plot 9D plots.
% Data - Each column inside the table is a sensor
X = [% Class B table
532245673;
213334325;
724319424;
831157829;
912231532;
% Class A table
152323324352136434;
171813345410459977;
181363563395356555;
162024939423568777;
191552362045442232];
% Labels of the data for each class
y = [1; % Class B
1;
1;
1;
1;
-1; % Class A
-1;
-1;
-1;
-1];
% Tuning parameters
C =1; % For upper boundary limit
lambda =1; % Regularization (Makes it faster to solve the quadratic programming)
% Compute weigths, bias and find accuracy
[w, b, accuracy, solution] =mi.lsvm(X, y, C, lambda);
% Classify
x_unknown = [15; 5; 7; 2; 4; 6; 3; 5; 2];
class_ID = sign(w*x_unknown+b)
if(class_ID>0)
disp('x_unknown class B')
else
disp('x_unknown is class A')
end
Nonlinear Support Vector Machine with C code generation
This algorithm can do C code generation for nonlinear models. It's a very simple algorithm because the user set out the support points by using the mouse pointer. When all the supports are set ut, then the algorithm will generate C code for you so you can apply the SVM model in pure C code using CControl library.
All you need to have is two matrices, X and Y. Where the column length is the data and the row length is the amount of classes.
The nlsvm.m file will plot your data and then when you have placed out your support points, then the svm.m will generate C code for you that contains all the support points.
If you have let's say more than two variables, e.g Z matrix or even more. Then you can create multiple models as well by just using diffrent data as arguments for the svm function below. The C code generation is very fast and it's very easy to build a model.
% Plot how many each class got - Maximum N points per each class
figure
bar(point_counter_list);
xlabel('Class index');
ylabel('Points');
Here is an application with SVM for a hydraulical system. This little box explains whats happening inside the hydraulical system if something happen e.g
a motor or a valve is active. It can identify the state of the system.
NN - Neural Network
This generates a neural network back and an activation function.
This Neural Network is tranied by Support Vector Machine.
fprintf('The accuracy of this model is: %i\n', score/classes*100);
Output:
Training:NeuralNetworksuccesswithaccuracy:1.000000atclass:1Training:NeuralNetworksuccesswithaccuracy:0.733333atclass:2Training:NeuralNetworksuccesswithaccuracy:0.986667atclass:3Theaccuracy of this model is: 96.6667
Robust Principal Component Analysis
Robust principal component analysis(RPCA) is a great tool if you want to separate noise from data X into a matrix S. RPCA is a better tool than PCA because it using optimization and not only reconstructing the image using SVD, which PCA only does.
[L, S] =mi.rpca(X); % Start RPCA. Our goal is to get L matrix
figure(1)
imshow(uint8(X)) % Before RPCA
title('Before RPCA - Bob')
figure(2)
imshow(uint8(L)) % After RPCA
title('After RPCA - Bob')
Install
To install MataveID, download the folder "matave" and place it where you want it. Then the following code need to be written inside of the terminal of your MATLAB® or GNU Octave program.
In MATLAB, quadprog is in the Optimization Toolbox. I would recommend adding some code that checks for the Optimization Toolbox before making this call.
Dear Professor,
Thanks for your sharing your codes. I have a problem when I use OKID in practice.
Here is the problem:
error: svd: cannot take SVD of matrix containing Inf or NaN values
error: called from
okid >eradcokid at line 200 column 11
okid at line 142 column 13
OKID_motor at line 24 column 11
The reason is the following function has no output.
Ybar = y_part*inv(V'V + regularizationeye(size(V'*V)))*V';
Here is the output on terminal
Ybar =
Columns 1 through 37:
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
Columns 38 through 74:
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
...
...
...
How should I fix this problem?
Thanks again.
Regards
Dear professor, It's generous of you to share this project . But I have a question that what is the specific value of input(u) in README.md, such as command lsim(G, u, t). I'm a beginner, it's a little diffcult for me to guess the value of u. Can you upload a more detailed version of 'Typical use' at your convenience or just tell me the value of u?
I modified the code of the example only slightly so that some indexing issues don't stop MATLAB. Instead, we end up erroring out at line 159. See stack trace below. The code completes in Octave.
clc; clear; closeall;
% Load CSV data
X = csvread('..\data\MotorRotation.csv'); % Can be found in the folder "data"
t = X(:, 1);
u = X(:, 2);
y = X(:, 3);
sampleTime =0.02;
% Do filtering of y
y = filtfilt2(y', t', 0.1)';
% Sindy - Sparce identification Dynamics
activations = [111111111000000000000000000000000000000000000000000]; % Enable or disable the candidate functions such as sin(u), x^2, sqrt(y) etc...
lambda =0.05;
l = length(u);
h = floor(l/2);
s = ceil(l/2);
fx_up = sindy(u(1:h), y(1:h), activations, lambda, sampleTime); % We go up
fx_down = sindy(u(s:end), y(s:end), activations, lambda, sampleTime); % We go down% Simulation up
x0 = y(1);
u_up = u(1:h);
u_up = u_up(1:100:end)';
stepTime =1.2;
[x_up, t] = nlsim(fx_up, u_up, x0, stepTime, 'ode15s');
% Simulation down
x0 = y(s);
u_down = u(s:end)
u_down = u_down(1:100:end)';
stepTime =1.2;
[x_down, t] = nlsim(fx_down, u_down, x0, stepTime, 'ode15s');
% Compare figure
plot([x_upx_down])
holdon
plot(y(1:100:end));
legend('Simulation', 'Measurement')
ylabel('Rotation')
xlabel('Time')
gridon
Thank you for your open source, but there are some problems in running the example code:ss function
help ss
ss - Create state-space model, convert to state-space model
This MATLAB function creates a state-space model object representing the
continuous-time state-space model
sys = ss(A,B,C,D)
I have just tried to execute the RLS example under Octave 5.2.0. I get the following error:
Attribute "sampleTime" is not known in Octave transfer function object. When I code Gd.tsam = sampleTime in rls.m the next error occurs.
To me it seems like the code is not really usable with Octave although the first sentence in the description suggests so...