filmov
tv
Converting MATLAB to Python using ChatGPT. An example for a Kalman filter simulation.
Показать описание
Using a Kalman filter written from scratch on MATLAB I checked out ChatGPTs ability to convert from MATLAB to Python. It's pretty good though not quite perfect of course at present. I'm sure you'll all want to try your own examples but for reference here is my original MATLAB code:
-----------------------------------------------------------------------------------------------------------------------
% Finds Kalman Filter and shows the state estimates
x=[0 0]';% state vector
xh=[0 0]'; % Kalman estimate vector of state
Npoints = 200; % number of time steps
y=zeros(Npoints,1);
s=zeros(Npoints,1);
x1=zeros(Npoints,1);
x2=zeros(Npoints,1);
xh1=zeros(Npoints,1);
xh2=zeros(Npoints,1);
t=[1:1:200];
% Define the system
F = [0, 1; -0.8 1.5]; % system matrix
D = [0 1]'; % system process noise vector
H = [1 1]; % observation matrix
Q = D*D'*1; %Process noise covariance is unity
R=5 %Measurement noise variance - scalar
% Initialize the covariance matrix
P = eye(2)*0.1 % Covariance matrix for initial state error
%Generate Random Noise Variance =1
zeta=randn(Npoints,1);
%re-seed noise generator
rng(2);
% Additive uncorrelated white noise
rv=sqrt(10)*randn(Npoints,1);
% Loop through and perform the Kalman filter equations recursively
for k = 1:Npoints
% State equations of system
x=F*x+D*zeta(k);
y(k)=H*x;
s(k)=y(k)+rv(k);
% Store states for plotting later
x1(k)=x(1);
x2(k)=x(2);
%Update Kalman gain vector : one step ahead version
K=F*P*H'/(H*P*H'+R);
% Update the covariance from Riccati equation
P = F*P*F' + Q-K*H*P*F';
%Kalman Filter
xh=F*xh+K*[s(k)-H*xh];
%Store estimated states for plotting
xh1(k)=xh(1);
xh2(k)=xh(2);
end
%Plot results
figure
subplot(2,1,1)
plot(t,x1,'-r',t,xh1,'-.b')
legend('State 1','State 1 estimate','Location','northwest')
title('State 1 and KF Estimate')
xlabel({'Time(samples)'})
subplot(2,1,2)
plot(t,x2,'-r',t,xh2,'-.b')
legend('State 2','State 2 estimate','Location','northwest')
title('State 2 and KF Estimate')
xlabel({'Time(samples)'})
ax = gca;
set(gcf,'color','w');
-----------------------------------------------------------------------------------------------------------------------
% Finds Kalman Filter and shows the state estimates
x=[0 0]';% state vector
xh=[0 0]'; % Kalman estimate vector of state
Npoints = 200; % number of time steps
y=zeros(Npoints,1);
s=zeros(Npoints,1);
x1=zeros(Npoints,1);
x2=zeros(Npoints,1);
xh1=zeros(Npoints,1);
xh2=zeros(Npoints,1);
t=[1:1:200];
% Define the system
F = [0, 1; -0.8 1.5]; % system matrix
D = [0 1]'; % system process noise vector
H = [1 1]; % observation matrix
Q = D*D'*1; %Process noise covariance is unity
R=5 %Measurement noise variance - scalar
% Initialize the covariance matrix
P = eye(2)*0.1 % Covariance matrix for initial state error
%Generate Random Noise Variance =1
zeta=randn(Npoints,1);
%re-seed noise generator
rng(2);
% Additive uncorrelated white noise
rv=sqrt(10)*randn(Npoints,1);
% Loop through and perform the Kalman filter equations recursively
for k = 1:Npoints
% State equations of system
x=F*x+D*zeta(k);
y(k)=H*x;
s(k)=y(k)+rv(k);
% Store states for plotting later
x1(k)=x(1);
x2(k)=x(2);
%Update Kalman gain vector : one step ahead version
K=F*P*H'/(H*P*H'+R);
% Update the covariance from Riccati equation
P = F*P*F' + Q-K*H*P*F';
%Kalman Filter
xh=F*xh+K*[s(k)-H*xh];
%Store estimated states for plotting
xh1(k)=xh(1);
xh2(k)=xh(2);
end
%Plot results
figure
subplot(2,1,1)
plot(t,x1,'-r',t,xh1,'-.b')
legend('State 1','State 1 estimate','Location','northwest')
title('State 1 and KF Estimate')
xlabel({'Time(samples)'})
subplot(2,1,2)
plot(t,x2,'-r',t,xh2,'-.b')
legend('State 2','State 2 estimate','Location','northwest')
title('State 2 and KF Estimate')
xlabel({'Time(samples)'})
ax = gca;
set(gcf,'color','w');
Комментарии