function [data] = tilt(datafile)

dt=1/35;
x=[0;
   0];

P=[1 0;
   0 1];

R=[17];

Q=[.001 0;
   0  .003];

A=[0 -1;
   0 0];

C=[1 0];


fid=mopen(datafile, 'r')
if(fid == -1)
  error('cannot open datafile');
end


num_lines= 0;
done = 0;
while (done == 0)

  [num_read, time, r_rate, p_rate, y_rate, x_accel, y_accel, z_accel, roll, pitch, yaw] = mfscanf(fid, "%f %f %f %f %f %f %f %f %f %f");

  if(num_read < 0)            // end of file
    break;
  end

  if(num_read == 10)

//  state update
    rate = r_rate*180/%pi - x(2);  // unbias gyro
    Pdot=A*P + P*A'+ Q;            // derivative of covariance matrix

    x(1) = x(1) + rate*dt;         // update angle estimate
    P = P + Pdot*dt;               // update covariance matrix

// kalman_update

    E=C*P*C' + R;                  // error estimate

    K=P*C'*inv(E);                 // Kalman filter gains
    P=P - K*C*P;                   // update covariance matrix
    x=x+K*(roll-x(1));             // update state estimate


    num_lines = num_lines + 1;
    data(num_lines, 1) = time;
    data(num_lines, 2) = roll;
    data(num_lines, 3) = r_rate*180/%pi;
    data(num_lines, 4) = x(1);
    data(num_lines, 5) = x(2);
  end
end

mclose(fid);

disp(P);

endfunction;