function wa_sns_model( par )

% Use wideband activeness only, to discriminate between speech and non-speech.
% This function simply prepares the automatic threshold from the posteriors.
% -> no MFCC, no GMM, so this is virtually costless
%  
% See IDIAP RR 06-26 for detailed documentation.
%
% The final binary decision (speech/non-speech) is taken using
% automatic threshold selection, as in IDIAP RR 05-52.
%
% By Guillaume Lathoud, 2006.
% lathoud@idiap.ch
  
  
  if nargin < 1
    error( [ mfilename ' needs at least 1 input argument.' ] );
  end
  
  dbstop error
  dbstop warning

  % Check for required parameters

  required_fields = { 'in_filename', 'far_t', 'out_dir' };
  check_param( required_fields, fieldnames( par ) );

  % Define default parameters

  par_default = [];
  
  par_default.verbose = 1;
  
  par = fill_default( par, par_default );

  if par.verbose
    disp( ' ' );
    disp( [ mfilename ' parameters:' ] );
    disp( par );
  end

  
  % Load input and process each recording

  load( par.in_filename, 'result' );
  if par.verbose
    disp( [ mfilename ' loaded "' par.in_filename '".' ] );
  end
  n_rec = numel( result.list );
  
  for i_rec = 1:n_rec 
    
    % ( 1 ) Define output filename
    
    [ p, n ] = fileparts( par.in_filename );

    out_filename = fullfile( par.out_dir, [ mfilename '-' n '.mat' ] );
    
    if exist( out_filename, 'file' )

      if par.verbose

	disp( ' ' );
	disp( [ mfilename ' skipped recording ' sprintf( '%d/%d', i_rec, n_rec ) ': "' result.list( i_rec ).name '".' ] );
	disp( [ '( output file already exists: "' out_filename '" )' ] );

      end

    else
      
      % Lock the output file by creating it
      fid = fopen( out_filename, 'w' );
      fclose( fid );
      
      % ( 2 ) Find automatic thresholds
      
      if par.verbose
	disp( ' ' );
	disp( [ mfilename ': processing recording ' sprintf( '%d/%d', i_rec, n_rec ) ': "' result.list( i_rec ).name '"...' ] );
      end

      % We create a "model_list" structure that has a format
      % compatible with "classify_cluster_sns", even if it does not
      % contain any MFCC-based GMM.
      
      % Full covariance GMM fitting
	
      model_list = create_empty_model;
      model_list( 1 ).par = par;
      model_list( 1 ).BIC = -Inf;

      % Extract the log-posterior probability of wideband activity
      % (defined in IDIAP RR 05-52).
      log_post_wa = result.list.sphloc_mat( 6,: );
      
      post = exp( log_post_wa(:).' );
      
      [ sorted_post, sorted_ind ] = sort( post );
      
      est_frr = cumsum( sorted_post );
      est_frr = est_frr / sum( sorted_post );
      
      est_far = fliplr( cumsum( 1 - fliplr( sorted_post(:).' ) ) );
      est_far = est_far / sum( 1 - sorted_post );
      
      [ tmp, min_ind ] = min( abs( est_far - par.far_t ) );
      
      model_list( 1 ).wa_est_far_point = est_far( min_ind );
      model_list( 1 ).wa_est_frr_point = est_frr( min_ind );
      model_list( 1 ).wa_threshold     = sorted_post( min_ind );
      
      % Store final decision
      model_list( 1 ).wa_decision = ( post > model_list( 1 ).wa_threshold );
      
      % Write out 
      
      model_list = model_list( 1 );

      save( out_filename, 'model_list', 'par' );
      
      if par.verbose
	disp( [ mfilename ' wrote out "' out_filename '".' ] );
      end
      
    end % End of check for output filename
    
  end % End of loop through recordings
  
  if par.verbose
    disp( ' ' );
    disp( [ mfilename ': done.' ] );
  end
  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function model_list = create_empty_model
  
  model_list( 1 ).n_components    = 0;
  model_list( 1 ).mvn_model       = [];
  model_list( 1 ).D = 0;
  model_list( 1 ).N = 0;
  model_list( 1 ).M = 0;
  model_list( 1 ).n_diag = 0;
  model_list( 1 ).n_full = 0;
  model_list( 1 ).K = 0;
  
  model_list( 1 ).log_post_speech = [];

  model_list( 1 ).wa_est_far_point = NaN;
  model_list( 1 ).wa_est_frr_point = NaN;
  model_list( 1 ).wa_threshold     = NaN;
  
  model_list( 1 ).speech_est_far_point = NaN;
  model_list( 1 ).speech_est_frr_point = NaN;
  model_list( 1 ).speech_threshold     = NaN;
  
  model_list( 1 ).wa_speech_est_far_point = NaN;
  model_list( 1 ).wa_speech_est_frr_point = NaN;
  model_list( 1 ).wa_speech_threshold     = NaN;
  
  model_list( 1 ).speech_weight = 0;
  
  