oseiskar / simdkalman

Python Kalman filters vectorized as Single Instruction, Multiple Data
https://simdkalman.readthedocs.io/
MIT License
176 stars 36 forks source link

AssertionError #15

Closed chaotianjiao closed 3 years ago

chaotianjiao commented 3 years ago

Hi, I'm new to kalman, can you help me? This is my code:

from simdkalman.primitives import predict, update
import numpy as np

 A = np.eye(4)  # A 
 Q = R = np.eye(4) * 10 ** -5  # Q and R
 H = np.eye(4)  # H 
# init values
# where the res is an another functions's output, it is an numpy array ,shape is (4,2), like this:[[237 149] [162 223] [378 227] [260 
# 149]]  and res is time_varying array because the output in every frame is different
m = res
P = np.array([[9, 0, 0, 0], [0, 25, 0, 0], [0, 0, 4, 0], [0, 0, 0, 4]])  # P

m, P = predict(m, P, A, Q)
m, P = update(m, P, H, R, np.eye(4)*0.01)

But I always get the assert error:

Traceback (most recent call last): File "D:/PycharmProjects/first_ldw/test.py", line 64, in m,P= update(m, P, H, R, np.eye(4)0.01) File "D:\Python38\lib\site-packages\simdkalman\primitives.py", line 146, in update return _update(prior_mean, prior_covariance, observation_model, observation_noise, measurement)[:2] File "D:\Python38\lib\site-packages\simdkalman\primitives.py", line 52, in reshaped_func outputs = func([to_3d_array(a) for a in args], **kwargs) File "D:\Python38\lib\site-packages\simdkalman\primitives.py", line 95, in _update assert(measurement.shape[-2:] == (m,1)) AssertionError

Where should I modify my code ?
Thanks~

oseiskar commented 3 years ago

Hi! You need to think more carefully what you are trying to model: what is the dimension of the measurement (res): Is each measurement a 2D-vector (e.g., [237 149] would be one measurement), a 4D-vector ([[237], [162], [378], [260]] would be one measurement) or is it really a 4x2 matrix, in which case you cannot directly use a Kalman fitlter on it but have to first, e.g., flatten it into a vector of size 8. Depending on the answer, you need to modify the dimensions of the H matrix accordingly.

The fact that the value of the measurement is different on each "frame" does not require you to use "time-varying" parts of the Simdkalman library, unless the matrices A, H, R or Q also change on each frame.

Also take a moment to think about the hidden state, what is it's dimension and how is it related to the measurement. This other dimension of H and both dimensions of A (and the contents of the matrices A & H). If you simply set them to identity matrices (multiplied by some factor), it is proably not the optimal way to model the situation.

chaotianjiao commented 3 years ago

@oseiskar Hi ,Thanks for your reply ! the measurement(res) is a 4x2 matrix.Actually they are four points of a frame. We init it as res = np.array( [[237, 149], [162 ,223], [378 ,227], [260 , 149]]).It it the first frame's output. res[0] is the first point,res[0][0] is the first point's x-axis. It is really a 4x2 matrix. On each frame the four points are different, we want use kalman filter to track these points. A,H,R and Q are const, they will not change on each frame. So I use simdkalman.Kalmanfilter can solve my problem? And I want to know is there any good way to get best values of A and H ?

I will be very appreciated if you can answer my questions above. Thanks~