function statsObject = timeseg_stats( timeseg )
  
  nChannels = timeseg.nChannels;

  % For each channel
  % active segment duration
  asd = {};
  
  % For each channel
  % non-active segment duration
  nsd = {};

  % For all channels
  asd_total = [];
  nsd_total = [];
  
   for a = 1:nChannels
    m = timeseg.seg{ a };
    m = [ m( 2, : ) - m( 1, : ); m( 3, : ) ];
     
    as = find( m( 2,: ) );
    asd{ a } = m( 1, as );
    asd_total = [ asd_total, asd{ a } ];
    
    ns = find( ~m( 2, : ) );
    nsd{ a } = m( 1, ns );
    nsd_total = [ nsd_total, nsd{ a } ];
  end
  
  if nargout < 1
    % Show when asked for
    for a = 1:nChannels
    
      [ n, x ] = hist( asd{ a }, unique( asd{ a } ) );
      figure; plot( x, cumsum( n ) ); 
      title( sprintf( 'active segment duration for channel %d', a ) );
    
      [ n, x ] = hist( nsd{ a }, unique( nsd{ a } ) );
      figure; plot( x, cumsum( n ) ); 
      title( sprintf( 'non-active segment duration for channel %d', a ) );
    
    end
    
    [ n, x ] = hist( asd_total, unique( asd_total ) );
    figure; plot( x, cumsum( n ) );
    title( sprintf( 'active segment duration for all channels' ) );
    
    [ n, x ] = hist( nsd_total, unique( nsd_total ) );
    figure; plot( x, cumsum( n ) ); 
    title( sprintf( 'non-active segment duration for all channels' ) );
    
  else
    % Return value when asked for
    statsObject.nChannels = nChannels;
    statsObject.asd = asd;
    statsObject.nsd = nsd;
    statsObject.asd_total = asd_total;
    statsObject.nsd_total = nsd_total;
  end
  