function [filtered_data, P_old] = kf(x_state, z, P_old, Q, R, F, G, H)
% function call from kalmanFilter.m function, line 38
% 
% returns a Kalman-filtered data sample
%  acceptes the state vector (x_state), observation (z), process noise (Q), 
% measurement or  observation noise (R), transition model (F), process
% noise vector (G), observation model (H)
%

% predicted state stage
% x_predict = x_state * F + G * AWGN(0, Q)
% Predict the state vector and the error covariance matrix using
% previous estimate of state vector and the error covariance matrix
%
x_predict = F * x_state + G' * normrnd(0, sqrt(Q));
P_new = F * P_old * F' + Q;    

% Computing the Kalman Gain:          
%
K = P_new * H * inv(H' * P_new * H + R);

% Correcting the observation using the innovations process to generate
% the 'filtered' estimate
%
p = length(G);
e = z - H' * x_predict;
x_estimate = x_predict + (K * e);
P_old = (eye(p, p) - K * H') * P_new;    

% Filtering the observation, z 
%
filtered_data = H' * x_estimate;
% disp('computed one filtered data in kf.m line 33');