Converting MATLAB to Python using ChatGPT. An example for a Kalman filter simulation.

preview_player
Показать описание
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');
Рекомендации по теме
Комментарии
Автор

Firstly, it's kind of strange watching this on my Mac and seeing a Windows desktop!

It's doing a good job of converting from one format to another and that can be difficult, and I guess I'll have to do this myself and learn SWIFT to continue with iPhone programming. I dread that for the amount of work I put in to the original apps, and then to convert to a language I don't quite understand yet (not having tried to learn it as yet).

What I think people fear will happen is how they placed it out on Star Trek The Next Generation where they'd ask the computer to create a program which did XYZ. There's a big leap there, where the system has to firstly figure out the problem, secondly an implementation in whatever language, be it maths alone, and then create something which fits within the choose programming language framework.

There's still an innovation step to go through where someone is creating the basic concept before it is written in to code by the AI. I think that's the most important takeaway from this demonstration.

johnsim