I am relatively new to using Kalman Filtering. Currently I am trying to understand it and how to implement it in Matlab. I found a website with some nice examples that I would like to rewrite in Matlab using the unscentedKalmanFilter() function. The website is Kalman Filter examples and I am trying to rebuild the first example where depending on some measurments with noise the weight of a gold bar is estimated.
I've come up with some code but I doubt that my state transistion function $f$ is correct. From what I understand one needs the measurement function $y=h(x)$ calculating the state $x$ from the measurement $y$ and the state transition function $x_{n+1}=f(x_n,u)$ where $x_{n+1}$ is the prediction for the next state from the input $u$ and the current state $x_{n}$.
As the gold bar weight is directly measured the measurement function should simply be $h(x)=x+v_{k}$ with $v_k$ being the additive noise term. For the state transition function I don't have an idea. In the example they write $\hat x_{n}=\hat x_{n-1}+α_n(y_n−\hat x_{n-1})$ where $y_n$ is the last measurement and $α_n$ a factor. However I don't think the state transition function $f$ should include the measurement.
So maybe someone could enlighten me. Here is the Matlab code I have so far:
initialStateGuess = [1030];
n=length(initialStateGuess);
h=@(x)[x(1)];% Measurement function
f=@(x)[x(1)];% State transition function
ukf = unscentedKalmanFilter(...
f,... % State transition function
h,... % Measurement function
initialStateGuess,...
'HasAdditiveMeasurementNoise',true,...
'HasAdditiveProcessNoise',true);
yMeas = [1030 989 1017 1009 1013 979 1008 1042 1012 1011];% Measurement Values
R = var(yMeas);%0.2; % Variance of the measurement noise v[k]
ukf.MeasurementNoise = R;
ukf.ProcessNoise = 60;
Nsteps = length(yMeas); % Number of time steps
xCorrectedUKF = zeros(Nsteps,n); % Corrected state estimates
PCorrected = zeros(Nsteps,n,n); % Corrected state estimation error covariances
e = zeros(Nsteps,1); % Residuals (or innovations)
for k=1:Nsteps
% Let k denote the current time.
%
% Residuals (or innovations): Measured output - Predicted output
e(k) = yMeas(k) - vdpMeasurementFcn(ukf.State); % ukf.State is x[k|k-1] at this point
[xCorrectedUKF(k,:), PCorrected(k,:,:)] = correct(ukf,yMeas(k));
predict(ukf);
end
plot(xCorrectedUKF')
hold on
plot(yMeas)
plot(1010*ones(1,Nsteps))
legend('Kalman FIlter state','Measurement Values','True Value')
Edit:
As can be seen in the above picture the Matlab code I wrote does work. As more measurements come in the state gets closer to the true value. However the variance value for the Process Noise seems to be quite relevant to achieve a fast convergence. Even after playing around with it for a bit I don't manage to achieve that fast convergence as in the example:
So I wonder why my code convergeces so slowliy?
Edit2:
As suggested I have tried to set the process noise to 0 and the measurement noise to the variance of the measurement values. However this results in a very slow settling time. Might this have to da with me using an unscented Kalman filter? I thought that would be a good choise as I can use it also for other, nonlinear problems.