x_last = 0 p_last = 0 Q = 0.01 # 系统噪声 R = 0.5 # 测量噪声 def kalman(z_measure, x_last=0, p_last=0, Q=0.018, R=0.0542): x_mid = x_last p_mid = p_last + Q kg = p_mid / (p_mid + R) x_now = x_mid + kg * (z_measure - x_mid) p_now = (1 - kg) * p_mid p_last = p_now x_last = x_now return x_now, p_last, x_last path = 'C:/Users/calm夜/Desktop/作业1.xlsx' data_B = pd.read_excel(path, header=None) x = list(data_B.iloc[::, 0]) y=[] for i in range(len(x)): pred, p_last, x_last = kalman(x[i], x_last, p_last, Q, R) y.append(pred) ax1 = plt.subplot(1,2,1) ax2 = plt.subplot(1,2,2) plt.sca(ax1) plt.plot(x, color="g") # 测量值 plt.sca(ax2) plt.plot(y, color="r") # 预测值 plt.show() 把python代码转用matlab代码
时间: 2023-12-29 14:04:00 浏览: 79
kalman_filter_noise.rar_NOISE_kalman_filter_noise_信号误差_系统误差_系统误差
Sure! Here's the equivalent MATLAB code for your Python code:
```matlab
x_last = 0;
p_last = 0;
Q = 0.01; % 系统噪声
R = 0.5; % 测量噪声
path = 'C:/Users/calm夜/Desktop/作业1.xlsx';
data_B = xlsread(path);
x = data_B(:, 1);
y = zeros(size(x));
for i = 1:length(x)
x_mid = x_last;
p_mid = p_last + Q;
kg = p_mid / (p_mid + R);
x_now = x_mid + kg * (x(i) - x_mid);
p_now = (1 - kg) * p_mid;
p_last = p_now;
x_last = x_now;
y(i) = x_now;
end
subplot(1,2,1);
plot(x, 'g'); % 测量值
subplot(1,2,2);
plot(y, 'r'); % 预测值
```
Please note that in MATLAB, you will need to use the `xlsread` function to read data from an Excel file. Also, the `subplot` function is used to create subplots, and the color should be specified as a string ('g' for green and 'r' for red).
阅读全文