function [state, cov, dist, lh, model_prob] = immTracking(track_id, max_id, fcn, prev_state, prev_cov, meas, meas_cov, dt)
persistent trackers
if isempty(trackers)
trackers = cell(1, max_id);
for i=1:length(trackers)
trackers{i} = trackingIMM();
end
end
if isequal(trackers{track_id}.State, zeros(6,1))
trackers{track_id} = trackingIMM('State', prev_state, 'StateCovariance', prev_cov);
end
switch(fcn)
case 1
tracker = clone(trackers{track_id});
[state, cov] = predict(tracker, dt);
case 2
tracker = clone(trackers{track_id});
[state, cov, dist, lh, model_prob] = correct_filter(tracker, meas, meas_cov, dt);
case 3
tracker = trackers{track_id};
[state, cov, dist, lh, model_prob] = correct_filter(tracker, meas, meas_cov, dt);
case 4
trackers{track_id} = tracking_IMM();
end
end
function [state, cov, dist, lh, model_prob] = correct_filter(tracker, meas, meas_cov, dt)
predict(tracker, dt);
tracker.MeasurementNoise = meas_cov;
dist = distance(tracker, meas);
lh = likelihood(tracker, meas);
[state, cov] = correct(tracker, meas, 'rectangular');
model_prob = tracker.ModelProbabilities;
end