I'm new to Kalman filtering and state estimation and I'd like some guidance on EKFs.
Currently, I'm trying to use a linear prediction model coupled with nonlinear measurements to estimate the state of an object. My state vector ($\bar{x}$) is:
\begin{bmatrix}x\\y\\v_x\\v_y\end{bmatrix}
and my state model is (with no process noise ($W_k$) or process noise covariance ($Q_k$)): $$ \bar{x}_{k_p} = A\bar{x}_{k-1} \ + B\begin{bmatrix} a_x \\ a_y \end{bmatrix} $$
$$ P_{k_p} = AP_{k-1}A^T $$ where $$ A = \begin{bmatrix} 1 & 0 & \Delta T & 0 \\ 0 & 1 & 0 & \Delta T \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} $$ and $$ B = \begin{bmatrix} \frac{1}{2}\Delta T^2 & 0 \\ 0 & \frac{1}{2}\Delta T^2 \\ \Delta T & 0 \\ 0 & \Delta T\end{bmatrix} $$
I have simulated some measurement data with Gaussian noise. The measurements are outputted as $\begin{bmatrix}r\\\theta\end{bmatrix}$, and this is the graph of the generated data:
To account for the polar coordinates, I was going to use a function $h(z)$ that transformed from polar to cartesian coordinates in my state update equation $\bar{x}_k = \bar{x}_{k_p} \ + K_k(h(\bar{z}_k)-\bar{x}_{k_p})$? This seemed like the simple thing to do, but this post does say that if I did so, my filter wouldn't work as the polar to cartesian transform isn't a linear transform.
To resolve this, I want to use an EKF to linearize the measurements, and after reading a few articles online, my gain calculation and update step are as follows:
$$ K_k = P_kJ(\bar{z}_k)^T \ (J(\bar{z}_k)P_kJ(\bar{z}_k)^T + R)^{-1} $$
$$ \bar{x}_k = \bar{x}_{k_p} \ + K_k(Z_k - H\bar{x}_{k_p}) $$
$$ P_k = (I - K_k J(\bar{z}_k))P_{k_p} $$
where $K_k$ is the Kalman gain, $\bar{z}_k$ is the measurement vector outputted by the sensor, and $J(\bar{z}_k)$ is a Jacobian to linearize the polar coordinate measurements. These are all at time $k$. In addition, my prediction equations are the same linear equations as shown above.
I used $x = r \cos(\theta)$ and $y = r \sin(\theta)$ to derive the following Jacobian: $$ J(r,\theta) = \begin{bmatrix} \frac{\partial x}{\partial r} & \frac{\partial x}{\partial \theta} \\ \frac{\partial y}{\partial r} & \frac{\partial y}{\partial \theta} \\ \frac{\partial v_x}{\partial r} & \frac{\partial v_x}{\partial \theta} \\ \frac{\partial v_y}{\partial r} & \frac{\partial v_y}{\partial \theta} \\ \end{bmatrix} = \begin{bmatrix} \cos(\theta) & -r\sin(\theta) \\ \sin(\theta) & r\cos(\theta) \\ 0 & 0 \\ 0 & 0 \end{bmatrix} $$
Would this Jacobian and model be the correct way to go about my problem? Thanks for your help!
P.S. I'm super new to this so if some of my lingo is wrong or weird, if I'm missing something or something makes no sense, or if you have some tips to better my understanding, please let me know. Thanks!
EDIT: I converted the polar values to the cartesian plane with $x = r\cos(\theta)$ and $y=r\sin(\theta)$, and then graphed the $x$ and 4y$ values. This is the result:
Obviously, something seems to be working, but I don't know/understand why, especially if the transform isn't linear. If someone could help my understanding of this as well, I'd truly appreciate it.