function seg = simplify_seg( in_seg, small_time )

  % Deal with parameters
  
  if nargin < 1
    error( 'simplify_seg needs at least one input argument!' );
  end

  if ~exist( 'small_time', 'var' )
    small_time = 1e-10;
  end
  
  seg = in_seg;
  
  if isempty( seg )
    if ~strcmp( warning, 'off' )
      disp( 'simplify_seg: WARNING! Empty input parameter "in_seg".' );
    end
    return;
  end
  
  if ~ismember( size( seg, 1 ), [2 3] )
    disp( 'simplify_seg: input parameter "in_seg" must be a 2-row or 3-row matrix' );
  end

  % Merge consecutive segments with same activity
  
  if size( seg,1 ) == 2
  
    % seg is a list of speech segments only
    
    ind = find( abs( seg( 2, 1:end-1 ) - seg( 1, 2:end ) ) < small_time );

  else
    
    % seg is a list of speech and silence segments
    
    ind = find( ( abs( seg( 2, 1:end-1 ) - seg( 1, 2:end ) ) < small_time ) & ...
		( seg( 3, 1:end-1 ) == seg( 3, 2:end ) ) );
    
  end
    
  for a = fliplr( ind )
    tmp = seg( :, a );
    tmp( 2 ) = seg( 2, a+1 );
    seg = [ seg( :, 1:a-1 ), tmp, seg( :, a+2:end ) ];
  end
  
  % Remove empty segments
  seg = seg( :, find( abs( seg( 2,: ) - seg( 1,: ) ) > small_time ) );
  