Using a Kalman filter to predict samples between measurements (cross posted from stack overflow)
I have a dataset where measurements were taken at 10 Hz, and I am trying to use a Kalman filter to add predicted samples in between the measurements, so that my output is at 100 Hz. I have it working ok when the velocity is linear, but when the direction changes, the filter takes a while to catch up. I am new to Kalman models, so am very likely making some mistakes in my settings. See image

for an example, the red is measured data, with stepping in between measurements. The blue is the Kalman corrected.

std::vector measurements is a dummy data array I am testing with.

The main Kalman code is based on this:

https://github.com/hmartiro/kalman-cpp/blob/master/kalman.cpp

Am I correct in feeding the prediction back into the filter for every loop? Or is this line:

yPos &lt;&lt; kf.state().transpose(); wrong?

my code is:

int main(int argc, char* argv[]) {

  int n = 3; // Number of states
  int m = 1; // Number of measurements

  double dt = 1.0/30; // Time step

  Eigen::MatrixXd matA(n, n); // System dynamics matrix
  Eigen::MatrixXd matC(m, n); // Output matrix
  Eigen::MatrixXd matQ(n, n); // Process noise covariance
  Eigen::MatrixXd matR(m, m); // Measurement noise covariance
  Eigen::MatrixXd matP(n, n); // Estimate error covariance

  // Discrete motion, measuring position only
  matA &lt;&lt; 1, dt, 0, 0, 1, dt, 0, 0, 1;
  matC &lt;&lt; 1, 0, 0;

  // Reasonable covariance matrices
  matQ &lt;&lt; 0.001, 0.001, .0, 0.001, 0.001, .0, .0, .0, .0;
  matR &lt;&lt; 0.03;
  matP &lt;&lt; .1, .1, .1, .1, 10000, 10, .1, 10, 100;

  // Construct the filter
  KalmanFilter kf(dt,matA, matC, matQ, matR, matP);

  // List of noisy position measurements (yPos)
  std::vector&lt;double&gt; measurements = {
     10,11,13,13.5,14,15.2,15.6,16,18,22,20,21,19,18,17,16,17.5,19,21,22,23,25,26,25,24,21,20,18,16
  };

  // Best guess of initial states
  Eigen::VectorXd x0(n);
  x0 &lt;&lt; measurements[0], 0, 0;
  kf.init(dt,x0);

  // Feed measurements into filter, output estimated states
  double t = 0;
  Eigen::VectorXd y(m);


  for(int i = 0; i &lt; measurements.size(); i++) { //ACTUAL MEASURED SAMPLE

      yPos &lt;&lt; measurements[i];

      kf.update(yPos);

      for (int ji = 0; ji &lt; 10; ji++)  // TEN PREDICTED SAMPLES
      {
          t += dt;       

         kf.update(yPos);


          yPos &lt;&lt; kf.state().transpose(); //USE PREDICTION AS NEW SAMPLE

      }
  }

  return 0;
}


Thank you.
I'm sorry that it was not very clear to me what you were asking. But I think I have developed some sense on it as follows: 

You are physically capturing 10 noisy sensor measurements per second, which I call it as the physical measurements. But for some reasons, you want to fill in between those true-physical data samples with new synthetic data, generated mathematically at a rate of 100 samples per second (10 times more dense than available with the physical sensor measurement)

The solution to this problem has different names, based on your interpretation and which method you choose to follow, such as a sampler, a  predictor, an estimator, a curve fitting or an interpolation...

It seems, you want to use a Kalman filter (an estimator actually) to generate those new synthetic samples in between the true physical measurements. And your question is whether you should feed those synthetically generated new sampes back into the Kalman filter (for residual calculations) as if the data were generated from a true physical sensor, so that the filter can use it in residual calculations to get rid of the noise present in it and refine the state estimates?

That's what I understand from your question set up and my answer to it is no you cannot. Those new data cannot replace true phsyical measurements. They do not involve new information that's called as the innovations process of a random process (that you're trying to estimate), hence they cannot be used to make refined computations or decisions alone.

In fact, the Kalman filter will only propagate the current state $X(t)$ into the next phsyical measurement point $X(t+T_s)$ by using the system dynamic matrix alone. (i.e., it will solve the differential equation numerically based on the initial condition provided from the previous state estimate, and sample this solution at the requested high data rate)