function seg = intersect_seg( seg_list, small_time )

% Check input parameters
  
  if nargin < 1
    error( 'intersect_seg: needs at least one input argument' );
  end
  
  if nargout < 1
    error( 'intersect_seg: needs one output argument' );
  end
  
  if ~exist( 'small_time', 'var' )
    small_time = 1e-10;
  end
  
  seg_list = reshape( seg_list, 1, [] );

  seg = [];
  
  for a_seg = seg_list
    a_seg = a_seg{1};
    if ~isempty( a_seg )
      if size( a_seg, 1 ) ~= 3
	error( 'intersect_seg: all "seg_list" cells must be matrices with 3 rows !' );
      end
    end
  end
  
  if length( seg_list ) == 1
    if ~strcmp( warning, 'off' )
      disp( 'intersect_seg: WARNING! only one segmentation in input.' );
    end
    seg = seg_list{ 1 };
    return;
  end
  
  %%%
  
  % Now we can merge
  
  % ( 1 ) Build an oversegmentation

  t = [];
  for a_seg = seg_list
    a_seg = a_seg{1};
    t = [ t a_seg(1,:) a_seg(2,:)];
  end
    
  t = sort( unique( t ) );
  
  % "unique" is not enough to suppress duplicates
  % minor errors may appear (e.g. 1e-14)
  t = t( find( diff( [ -Inf t ] ) > small_time ) );
  
  if length( t ) < 2
    seg = [];
    return;
  end
  
  seg = [ t(1:end-1); t(2:end) ];
   
  % ( 2 ) Find activity for each segment
  %
  % A segment is active iff its middle is within an active segment 
  % for ALL input segmentations
  %
  % This is where we have a difference with "merge_seg"

  seg( 3,: ) = 1;
  
  seg_middle = mean( seg( 1:2, : ), 1 );
  nseg = length( seg_middle );
  
  for a = 1:nseg
  
    t = seg_middle( a );
    
    for a_seg = seg_list
      a_seg = a_seg{1};
      seg( 3, a ) = seg( 3, a ) & any( (a_seg(1,:) <= t) & (t <= a_seg(2,:)) & a_seg( 3,: ) );
    end
  
  end
  
  % ( 3 ) Merge consecutive segments with same activity tag
  
  for a = nseg-1:-1:1

    if seg( 3, a ) == seg( 3, a+1 ) & ( abs( seg( 2, a ) - seg( 1, a+1 ) ) < small_time )
      tmp = [ seg( 1,a ); seg( 2:3,a+1 ) ];
      seg = [ seg( :, 1:a-1), tmp, seg( :, a+2:end ) ];
    end
    
  end
  