简体   繁体   English

将matlab系统标识转换为python

[英]convert matlab system identification to python

I have a piece of code in Matlab that I want to convert to Python.我在 Matlab 中有一段代码,我想将其转换为 Python。 The Matlab code is using the system identification toolbox which is provided here: Matlab 代码使用此处提供的系统识别工具箱:

Ts = 1;
Znl=iddata(Xdati(:,2),Xdati(:,1),Ts);

z=iddata(Xdati(:,1),Xdati(:,2),Ts); 
z1=z(1:floor(length(Xdati(:,1))/2));  
z2=z(floor(length(Xdati(:,1))/2)+1:1:floor(2*length(Xdati(:,1))/2));  

V = arxstruc(z1,z2,struc(0:2, 1:50,1:50)); % Find the best structure of ARX model that can be 
with degrees between 1 and 50.
nn = selstruc(V,'aic'); 
[NLHyp,NLValue,NLRegs,NoiseSigma,DetectRatio] = isnlarx(Znl,nn); 

if 2*max(nn)<length(z1.y)

sys=arx(z1,nn);
x0=findstates(sys,z);
ssmodel=idss(sys);
Unstable_System=[];
Unstable_System=find(abs(eig(ssmodel.A))>1);

To provide more explanation about the code, I have data where I encapsulate it as iddata, and split it to train and validation data.为了提供有关代码的更多解释,我将数据封装为 iddata,并将其拆分为训练和验证数据。 these splits will be used in order to estimate the best order to identify a linear ARX model.这些拆分将用于估计识别线性 ARX model 的最佳顺序。 Once identified, I want to detect nonlinearity in the system with these orders.一旦确定,我想用这些顺序检测系统中的非线性。 Then, I want to construct the ARX model, find the initial states, and convert it to a steady-state model.然后,我想构建 ARX model,找到初始状态,并将其转换为稳态 model。 Finally, I want to detect any abnormal behavior to identify if the system was unstable.最后,我想检测任何异常行为以识别系统是否不稳定。

I started the conversion to Python and found a package called SIPPY for linear ARX mdeoling.我开始转换到 Python 并找到了一个名为 SIPPY 的 package 用于线性 ARX mdeoling。 Here is the code I wrote in Python:这是我在 Python 中编写的代码:

T = pd.read_excel('./test_data.xlsx')
input_0 = np.array(T.iloc[:, 0])
output_0 = np.array(T.iloc[:, 1])

loss = []
na = list(range(0, 3))
nb = list(range(1, 51))
nk = list(range(1, 51))

final_model = system_identification(output_0, input_0, 'ARX', IC='AIC', na_ord=na, nb_ord=nb, delays=nk)

print(final_model.G)
print(final_model.Vn)
print(final_model.Yid)

This code will read the data (no need for iddata encapsulation) and output the best ARX model for a given range of orders.此代码将读取数据(无需 iddata 封装)和 output 最佳 ARX model 对于给定范围的订单。 This means it will perform as arxstruc(z1,z2,struc(0:2, 1:50,1:50)) , nn = selstruc(V,'aic');这意味着它将执行为arxstruc(z1,z2,struc(0:2, 1:50,1:50)) , nn = selstruc(V,'aic'); , and sys=arx(z1,nn); , 和sys=arx(z1,nn); . . However, when testing both on the same data to compare the output, I found that the best orders given by Matlab were [1 25 1] While python returns [2 35 1] .但是,在对相同数据进行测试以比较 output 时,我发现 Matlab 给出的最佳顺序是[1 25 1]而 Z23EEEB4347BDD26BFC6B7EE9A35 B755DDZ 返回[2 35 1] When I investigated the reason I found that the loss value is different from Matlab than Python, and since the output would be the order that achieves the minimum loss, it is logical to have different orders.当我调查原因时,我发现损失值与 Matlab 比 Python 不同,并且由于 output 将是实现最小损失的顺序。 So can anyone help me with that problem?那么有人可以帮我解决这个问题吗? What is the loss function used in Matlab? Matlab中使用的损失function是什么? and is there a package that simulates the system identification in Matlab and provide the same results in Python?是否有一个 package 模拟 Matlab 中的系统识别并在 Python 中提供相同的结果?

Here is some code that is on another StackOverflow post on Model Predictive Control and system identification.这是Model 预测控制和系统识别上另一个 StackOverflow 帖子上的一些代码。

系统识别

from gekko import GEKKO
import pandas as pd
import matplotlib.pyplot as plt

# load data and parse into columns
url = 'http://apmonitor.com/do/uploads/Main/tclab_dyn_data2.txt'
data = pd.read_csv(url)
t = data['Time']
u = data[['H1','H2']]
y = data[['T1','T2']]

# generate time-series model
m = GEKKO(remote=False) # remote=True for MacOS

# system identification
na = 2 # output coefficients
nb = 2 # input coefficients
yp,p,K = m.sysid(t,u,y,na,nb,diaglevel=1)

plt.figure()
plt.subplot(2,1,1)
plt.plot(t,u)
plt.legend([r'$u_0$',r'$u_1$'])
plt.ylabel('MVs')
plt.subplot(2,1,2)
plt.plot(t,y)
plt.plot(t,yp)
plt.legend([r'$y_0$',r'$y_1$',r'$z_0$',r'$z_1$'])
plt.ylabel('CVs')
plt.xlabel('Time')
plt.savefig('sysid.png')
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM