function angle_gt = static_gt_2_angle_gt( p, static_gt, array_ind )

% FUNCTION ANGLE_GT = STATIC_GT_2_ANGLE_GT( P, STATIC_GT, ARRAY_IND )
%
% Expand static speaker ground-truth into frame-level ground-truth.
% The result is a binary matrix (nspeaker x nframes).
% ( NaN values when speaker is silent ).
%
% input: 3 arguments
%
% P: a structure with FRAMELENGTH_SEC, FRAMESHIFT_SEC and
%    FRAMELIST fields.
%
%   P.FRAMELENGTH_SEC: the span of one time frame, in seconds (e.g. 0.032).
%   P.FRAMESHIFT_SEC:  the time shift between two consecutive time frames, in seconds (e.g. 0.016).
%   P.FRAMELIST:       a N x 2 array of integer frame indices (column 1: beginning, column 2: end).
%
% STATIC_GT: structure for static speaker ground-truth (AV16.3 corpus)
%
% ARRAY_IND: 1 or 2 = which microphone array
%
% output: a structure
% ANGLE_GT.FRAME_IND:  vector of nframes indices
% ANGLE_GT.SPEAKER_IND: vector of speaker ids
% ANGLE_GT.AZ_MAT:     matrix of azimuth values in radians, filled with NaNs.
%                      Row 1 corresponds to speaker ANGLE_GT.SPEAKER_IND( 1 ), etc.
% ANGLE_GT.EL_MAT:     matrix of elevation values in radians, filled with NaNs.
%                      Row 1 corresponds to speaker ANGLE_GT.SPEAKER_IND( 1 ), etc.
  
  if nargin < 3
    error( 'static_gt_2_angle_gt needs 3 input arguments' );
  end
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  % Transform spatial gt into angles
  
  % Move spatial ground-truth to array's referent
  
  X = static_gt.array( array_ind ).Pmat * static_gt.p3d;
  
  % Transform to spherical coordinates
  
  [ th, ph ] = cart2sph( X(1,:), X(2,:), X(3,:) );
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  % List speaker ids
  
  speaker_ind = unique( static_gt.speaker_id );
  nspeakers = length( speaker_ind );
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
  % Create frame-level ground-truth
  
  frame_ind = [];
  for a = 1:size( p.framelist, 1 )
    frame_ind = [ frame_ind  p.framelist( a,1 ):p.framelist( a,2 ) ];
  end
  nframes = length( frame_ind );
  
  t = p.framelength_sec / 2 + ( frame_ind - 1 ) * p.frameshift_sec;

  az_mat = repmat( NaN, nspeakers, nframes );
  el_mat = repmat( NaN, nspeakers, nframes );
    
  nseg = size( static_gt.sp_seg, 2 );
  for a = 1:nseg

    % Find the speaker active in this segment
    
    ind1 = find( speaker_ind == static_gt.speaker_id( a ) );
    
    % Find time frames in this segment
    
    t1 = static_gt.sp_seg( 1, a );
    t2 = static_gt.sp_seg( 2, a );
    ind2 = find( ( t1 <= t ) & ( t <= t2 ) );
    
    % Activate this angle for all frames in this segment
    
    az_mat( ind1, ind2 ) = th( static_gt.pos_ind( a ) );
    el_mat( ind1, ind2 ) = ph( static_gt.pos_ind( a ) );
    
  end
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  % Return values
  
  angle_gt.frame_ind    = frame_ind;
  angle_gt.speaker_ind  = speaker_ind;
  angle_gt.az_mat       = az_mat;
  angle_gt.el_mat       = el_mat;
  