Question About $ Q $ Matrix (Model Process Covariance) in Kalman Filter I am implementing my own discrete Kalman filter to estimate velocity from acceleration and position measurements (using Matlab ).

I think I managed to deal with the $R$ matrix (measurements noise covariance matrix) in this way:

M = [x;x2dot];
R = cov(M);


What I am not sure about is the matrix $Q$. 

In all the examples I found on the web and on this website also, the values inside that matrix are really really small. Moreover, I found here this statement:


  if you select an overly large Q, then it doesn’t seem like the Kalman
  filter would be well-behaved.


The fact is that, in my model, to obtain acceptable results I used values in the order of $1e6$ on the diagonal of the $Q$ matrix.

In this way I got the estimated displacement equal to the measured one and the estimated acceleration equal to the measured one. The velocity is in the right range of values but it still looks noisy.

So the question is: is there a limit for the values to be used in $Q$? 

And also, are there guidelines to choose those values?

EDIT

Last question, does the $Q$ matrix have to be diagonal or it can be full also?
I'm the author of the textbook linked to above. This is a new account and thus I am not allowed to reply to that answer.

Anyway, the Gaussian chapter covers the definition of a covariance matrix. In general though your Q matrix will be full, not a diagonal, because there is correlation between the state variables. For example, suppose you are tracking position and velocity in x-axis for a car. Process noise (which Q is modelling) might be things like wind, bumps in the road, and so on. If there is a change in velocity due to this noise, there will also be a change in position. They are correlated, and so the off-diagonal elements will be non-zero for those terms.

The Kalman math chapter tells you how to compute the Q matrix for various scenarios. Admittedly there is more work to be done on that section. It's one of the more opaque subjects as most sources either gloss over it or just throw an integral at you and expect you to figure out the rest.

Finally, the companion FilterPy project contains code to compute the full Q matrix for you given common scenarios. The code is in common/discretization. For analytically difficult situations there is a routine to find the result numerically following an idea from Golub and Van Loan; the textbook is lacking on this topic, however.