Examples

These examples demonstrate how to use the Kalman operator.

This following example uses the Kalman operator to track vehicle movement:


// Tracking an object movement in 2D plain with constant velocity
// An object is moving in 2D plane with constant velocity (1.5, 1.0). 
// We observe the noisy object position (x, y) as position vector [x,y]
// We estimate the state vector (x,y, dx, dy) made of estimated "real" position (x,y) and estimated
// velocity (dx, dy). We also predict the next position of the moving object.

use com.teracloud.streams.timeseries.modeling::Kalman;
composite KalmanMain {
  graph
  stream<list<float64> movingObjectInitialPosition> kalmanSrc=Beacon()
  {
    param
      iterations:50u;
    output
      kalmanSrc:movingObjectInitialPosition=[10.0,10.0];//this is the initial position
  }

//simulate a changing position (x,y) with a fixed velocity of (dx=1.5, dy=0.5)
  stream<list<float64> observedPosition> objectObservations=Functor(kalmanSrc)
  {
    logic
    state: {
      mutable float64 dx=1.5;
      mutable float64 dy=0.5;
      float64 observationNoise=0.3f; // our estimate of the noise covariance when measuring the position
      mutable list<float64> movingPosition;
  }
  onTuple kalmanSrc:
  {
    movingPosition=movingObjectInitialPosition;
    movingPosition[0]+=dx;  // move object at fixed velocity
    movingPosition[1]+=dy;  // move object at fixed velocity
    // add white noise to simulate noisy observations 
    movingPosition[0]+=sqrt(observationNoise)**(random() - 0.5);
    // add white noise to simulate noisy obeservations
    movingPosition[1]+=sqrt(observationNoise)**(random() - 0.5);  
    dx+=1.5; // increment to next position 
    dy+=0.5; // increment to next position 
  }
  output
  objectObservations:observedPosition=movingPosition;
}
// Now send observed position to Kalman and get estimated real position and velocity, and also the
// predicted next position
  stream<list<float64> observedPosition,list<float64> estimatedCurrentState, list<float64> predictedNextPosition>  KalmanTracking=Kalman(objectObservations)
  {
    param
      inputTimeSeries: observedPosition;
      stateVectorDimension:4u;
      initialState:[ 10.0, 10.0, 1.0,0.0]; //  initial state
      observationNoiseCovariance:0.3;
      stateNoiseCovariance:0.3;
      //state to state transition
      stateTransitionMatrix:[[1.0,0.0,1.0,0.0],[0.0,1.0,0.0,1.0],[0.0,0.0,1.0,0.0],[0.0,0.0,0.0,1.0]];
      observationMatrix:[[1.0,0.0,0.0,0.0],[0.0,1.0,0.0,0.0]];// observation to state transition
    output
      KalmanTracking: 
      estimatedCurrentState=predictedState(),
      predictedNextPosition=predictedTimeSeries();
 }

 () as SnkKey=FileSink(KalmanTracking)
 {
   param
     file: "KalmanTracking.dat";
     format: csv;
 }
}