% x99 - data setup for 2 parabolic tracks
% with random missed target events and
% Poisson distributed numbers of false detections.

if( K == 1)

  LOADED = 99;

  N = 20;			% N data samples

% generate false detections
%
  false_alarm_rate = 2;		% average number of false alarms per unit time

  n_false_detect = zeros(1,N);

  n_false_detect(1) = 2;	% to allow initialization up to K=4

  for i=2:N
    n_false_detect(i) = n_poisson( false_alarm_rate);
    %
    % limit to 3 to reduce trellis size
    %
    if( n_false_detect(i) > 3)
      n_false_detect(i) = 3;
    %
    % at least 1 at window truncation boundaries
    %
    elseif( (mod(i,5) == 1) & (n_false_detect(i) < 1))
      n_false_detect(i) = 1;
    end
  end

% generate missed target events
%
  Pmiss = 0.2				% probability of a missed target event

  n1 = rand(1,N) > Pmiss;		% n1 == 1 when target 1 is not missed
  n2 = rand(1,N) > Pmiss;		% n2 == 1 when target 2 is not missed

  n1(1) = 1;				% to allow initialization up to K=4
  n2(1) = 1;

  n1(2) = 1;				% really need measurements at first two time instants
  n2(2) = 1;				%  to initialize Kalman effectively

% fudge n1 and n2 on/Off
%
  n1 = [ones(1,5), zeros(1,N-10), ones(1,5)];
%  n2 = [ones(1,5), n2(6:N)];
  n2 = ones(1,N);

  n1, n2, n_false_detect

  MM = n1 + n2 + n_false_detect;	% measurement counter array

  MM_max = max(MM);

  M = MM_max + 1;			% M states

  XY_offset = [0, cumsum(MM(1:N-1))];	% XY measurement offset indices

% apriori costs for missed target events
%
% Cmiss(n+1,K) is -log( P(n target events)) apriori cost
%
  Cmiss = get_Cmiss( Pmiss, MM_max);

% apriori costs for false detections
%
% Cfalse(m+1) is -log(P(m false detections))

  Cfalse = get_Cfalse( false_alarm_rate, MM_max);

% generate XY measurements
%
  dt = 4/(N-1);			% time will go from 0 to 4
   t = [0:N-1]*dt;

  xy1_orig = [ t; 2*(t.*(1-t/4))];

  xy2_orig = [ t; 4-2*(t.*(1-t/4))];

  if(TEST)
    Kn = 0.0;
  else
    Kn = 0.01;	% measurement noise variance
  end

  xy1 = xy1_orig + sqrt(Kn) * randn( size(xy1_orig));
  xy2 = xy2_orig + sqrt(Kn) * randn( size(xy2_orig));



  missed_cost = log(16);	% volume is x,y = 0..4,0..4

  XY = zeros(2,sum(MM));

  for i = 1:N
    ii = XY_offset(i);
    if( n1(i) ~= 0)
      ii = ii + 1;
      XY(:,ii) = xy1(:,i);		% target #1
    end
    if( n2(i) ~= 0)
      ii = ii + 1;
      XY(:,ii) = xy2(:,i);		% target #2
    end
    for j = 1:n_false_detect(i)		% false detections
      if( TEST)
        XY(:,ii+j) = HUGE_VAL * ones(2,1);	% for testing
      else
        XY(:,ii+j) = 4 * rand(2,1);	% uniform distribution over 0..4
      end
    end
  end

  zstate = 0;	% initial state unknown

  z = [1:N];	% time index for dummy z data

else % K > 1
  
  if( LOADED ~= 99)
    error( 'initialize with K = 1 first');
  end

  if( K > MM_max)
    error( 'K > MM_max not allowed!');
  end

  setup_combos;	% setup global variables: perms, combos, unique_perms, MP, M

end
