function show_crosscorr_summary( crosscorr_result_filename, loc_ind, mic_ind )

% Show the result of "crosscorr_analysis_wrapper.m"
% in a graphical way:
% 
%   one summary picture with the RMS delay as a function of the
%   distance between the speaker and the distant microphone.
%
%   y = RMS of normalized narrowband Time Of Flight across all bands,
%   x = difference of distance between lapel and distant microphone
%
% Refer to "crosscorr_analysis_wrapper.m" and "crosscorr_analysis.m"
% for more details.
%
% By Guillaume Lathoud, 2006  (email address in "my_email_address.jpg")

  if nargin < 1
    error( [ mfilename ' needs 1 input argument.' ] );
  end
  
  if ~exist( 'loc_ind', 'var' )
    loc_ind = [];
  end

  if ~exist( 'mic_ind', 'var' )
    mic_ind = [];
  end

  % load the result
  disp( [ mfilename ' is loading "' crosscorr_result_filename '"...' ] );
  load( crosscorr_result_filename, 'out' );
  
  % Accumulate "x" and "y" data, as defined above
  x = [];
  y = [];
  
  n_loc = numel( out.loc );
  
  disp( sprintf( [ mfilename ': iterating through the %d speaker locations...' ], n_loc ) );
  
  if isempty( loc_ind )
    i_loc_list = 1:n_loc;
  else
    i_loc_list = intersect( 1:n_loc, loc_ind );
  end

  for i_loc = i_loc_list

    n_mic = numel( out.loc( i_loc ).distantmic );

    if isempty( mic_ind )
      i_mic_list = 1:n_mic;
    else
      i_mic_list = intersect( 1:n_mic, mic_ind );
    end

    for i_mic = i_mic_list

      ref_delay = out.loc( i_loc ).distantmic( i_mic ).fullband_delaymax;

      % x: difference of speaker distance between lapel and distant microphone, in meters

      x = [ x out.p.d_diff( out.loc( i_loc ).distantmic( i_mic ).i_mic, i_loc ) ];

      % y: RMS of normalized narrowband Time Of Flight across all bands,

      y = [ y sqrt( mean( ( out.loc( i_loc ).distantmic( i_mic ).narrowband_delaymax - ref_delay ) .^ 2 ) ) ];

    end

  end


  [ hfig hplot ] = my_figure( x, y, out.recording_name );

  % Graphical output to files
  [ p, n, e ] = fileparts( crosscorr_result_filename );
  
  % Dump eps to file (b/w)
  figure( hfig ); colormap( 0.1 + 0.9 * ( colormap( 'gray' ) .^ 3 ) );
  set( hplot, 'color', 'k' );
  out_filename = fullfile( p, [ n '-RMS_nnTOF-bw.eps' ] );
  print( hfig, '-depsc2', out_filename );
  disp( [ mfilename ': wrote out "' out_filename '".' ] );
  
  % Dump eps to file (color)
  figure( hfig ); colormap( jet );
  set( hplot, 'color', 'b' );
  out_filename = fullfile( p, [ n '-RMS_nnTOF-color.eps' ] );
  print( hfig, '-depsc2', out_filename );
  disp( [ mfilename ': wrote out "' out_filename '".' ] );
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  % 1st to 4th order polynomial regression

  order_list = 1:4;
  

  [ hfig  hplot ] = my_figure( x, y, out.recording_name );

  v = axis;

  regress_list = do_regress( x, y, order_list, [ v(1) v(2) ] );

  hold on;

  %%%

  % First, show 1st order alone

  h_regress1 = plot( regress_list( 1 ).x, regress_list( 1 ).y, 'r--' );
  col_regress1 = get( h_regress1, 'color' );

  h_legend = legend( h_regress1, sprintf( 'order %d (cc %.3f)', regress_list( 1 ).order, regress_list( 1 ).corrcoef ) );

  v = get( h_legend, 'position' );  v( 1 ) = 0.14; set( h_legend, 'position', v );

  v = axis; v( [ 1 3 ] ) = max( 0, v( [ 1 3 ] ) ); axis( v );

  % Graphical output to files
  [ p, n, e ] = fileparts( crosscorr_result_filename );
  
  % Dump eps to file (b/w)
  figure( hfig ); colormap( 0.1 + 0.9 * ( colormap( 'gray' ) .^ 3 ) );
  set( hplot, 'color', 'k' );
  set( h_regress1, 'color', 'k' );
  out_filename = fullfile( p, [ n '-RMS_nnTOF-regress_1-bw.eps' ] );
  print( hfig, '-depsc2', out_filename );
  disp( [ mfilename ': wrote out "' out_filename '".' ] );
  
  % Dump eps to file (color)
  figure( hfig ); colormap( jet );
  set( hplot, 'color', 'b' );
  set( h_regress1, 'color', col_regress1 );
  out_filename = fullfile( p, [ n '-RMS_nnTOF-regress_1-color.eps' ] );
  print( hfig, '-depsc2', out_filename );
  disp( [ mfilename ': wrote out "' out_filename '".' ] );

  %%%

  % Second, show all four orders

  [ hfig, hplot ] = my_figure( x, y, out.recording_name );

  hold on;

  linestyle_list = { 'b-', 'r--', 'k-.', 'k:' };

  h_regress = [];
  h_legend_str = {};
  col_regress = {};

  for a = 1:numel( regress_list )

    h_regress( a ) = plot( regress_list( a ).x, regress_list( a ).y, linestyle_list{ a } );

    h_legend_str{ a } = sprintf( 'order %d (cc %.3f)', regress_list( a ).order, regress_list( a ).corrcoef );

    col_regress{ a } = get( h_regress( a ), 'color' );

  end

  h_legend = legend( h_regress, h_legend_str{:} );
  
  v = get( h_legend, 'position' );  v( 1 ) = 0.14; set( h_legend, 'position', v );
  
  v = axis; v( [ 1 3 ] ) = max( 0, v( [ 1 3 ] ) ); axis( v );
  
  % Graphical output to files
  [ p, n, e ] = fileparts( crosscorr_result_filename );
  
  % Dump eps to file (b/w)
  figure( hfig ); colormap( 0.1 + 0.9 * ( colormap( 'gray' ) .^ 3 ) );
  set( hplot, 'color', 'k' );
  for a = 1:numel( h_regress )
    set( h_regress( a ), 'color', repmat( mean( col_regress{ a } ), 1, 3 ) )
  end
  out_filename = fullfile( p, [ n '-RMS_nnTOF-regress' sprintf( '_%d', [ regress_list.order ] ) '-bw.eps' ] );
  print( hfig, '-depsc2', out_filename );
  disp( [ mfilename ': wrote out "' out_filename '".' ] );
  
  % Dump eps to file (color)
  figure( hfig ); colormap( jet );
  set( hplot, 'color', 'b' );
  for a = 1:numel( h_regress )
    set( h_regress( a ), 'color', col_regress{ a } );
  end
  out_filename = fullfile( p, [ n '-RMS_nnTOF-regress' sprintf( '_%d', [ regress_list.order ] ) '-color.eps' ] );
  print( hfig, '-depsc2', out_filename );
  disp( [ mfilename ': wrote out "' out_filename '".' ] );


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function  [ hfig, hplot ] = my_figure( x, y, recording_name )

  if nargin < 3
    error( [ mfilename ' needs 3 input arguments.' ] );
  end

  hfig = figure;

  hplot = plot( x, y, '.' );

  set( gca, 'FontSize', 20 );
  
  a_title = sprintf( recording_name );
  title( strrep( a_title, '_', '\_' ) );
  
  xlabel( 'difference of distance (meters)' );
  ylabel( 'RMS of normalized narrowband TOF (samples)' );
  

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function regress_list = do_regress( x, y, order_list, x_range, npoints )

  if nargin < 4
    error( [ mfilename ' needs at least 4 input arguments.' ] );
  end

  if ~exist( 'npoints', 'var' )
    npoints = 1000;
  end

  for a = 1:numel( order_list )
    
    regress_list( a ).order = order_list( a );
    regress_list( a ).p     = polyfit( x, y, order_list( a ) );
    regress_list( a ).x     = x_range( 1 ) + diff( x_range ) * ( 0:npoints-1 ) / ( npoints-1 );
    regress_list( a ).y     = polyval( regress_list( a ).p, regress_list( a ).x );
    
    tmp = corrcoef( y, polyval( regress_list( a ).p, x ) );

    regress_list( a ).corrcoef = tmp( 2 );
    
  end
  