% [ 2006-distant-speaker/README.m ]
%
% Experiments comparing signals captured by a microphone close and
% distant from a speaker.
%
% You'll see that in the distant microphone case,
% different frequencies arrive with different delays.
% 
% This experiment is carried on two subjects (seq01 and seq03 from the
% AV16.3 Corpus. Unfortunately seq02 does not include a lapel, so it
% cannot be used here.).
%
% The main function is "crosscorr_analysis.m".  The wrapper function
% "crosscorr_analysis_wrapper.m" makes the usage simpler.
%
% By Guillaume Lathoud, 2006  (email address in "my_email_address.jpg")

%%%

% Theoretical cross-correlation between a lapel signal and a distant
% microphone signal, in the case of a spherical wave emitted by a
% point source and travelling through a free field.

recording_name  = 'seq01-1p-0000';
data_dir        = fullfile( 'DATA', recording_name );
framelength_sec = 0.016;
output_filename = fullfile( 'DATA', sprintf( [ recording_name '-theoretical_crosscorr_analysis_%04d.mat' ], round( framelength_sec * 1000 ) ) );
theoretical_crosscorr_analysis_wrapper( recording_name, data_dir, framelength_sec, output_filename );

close all
do_pause = 0;
show_crosscorr_result( output_filename, do_pause );

%%%

% Cross-correlation analysis on recording 'seq01-1p-0000'

recording_name  = 'seq01-1p-0000';
data_dir        = fullfile( 'DATA', recording_name );
framelength_sec = 0.016;
mic_list        = 1:16;
output_filename = fullfile( 'DATA', sprintf( [ recording_name '-crosscorr_analysis_%04d.mat' ], round( framelength_sec * 1000 ) ) );

% Do the c.c. analysis

crosscorr_analysis_wrapper( recording_name, data_dir, framelength_sec, mic_list, output_filename );

% Show the result in a graphical way
% (this also dumps '.eps' files)

close all
show_crosscorr_result( output_filename ); % All speaker locations & all microphones

% If you don't want any pause (only to get the .eps files)

close all
do_pause = 0;
show_crosscorr_result( output_filename, do_pause ); % All speaker locations & all microphones

% If you only want to see some of it

close all
do_pause = 1;
loc_ind = [ 1 16 ];  % Only some speaker locations
mic_ind = [ 1  4 ];  % Only some microphones
show_crosscorr_result( output_filename, do_pause, loc_ind, mic_ind );

% Show a summary figure
% y = RMS of normalized narrowband Time Of Flight across all bands,
% x = difference of distance between lapel and distant microphone

close all
show_crosscorr_summary( output_filename );


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Let's repeat with another subject
%
% Warning: the ground-truth speech segmentation for seq03
% is slightly less precise than for seq01.
%
% -> more silences included inside ground-truth speech segments
%    (but not the reverse).

recording_name  = 'seq03-1p-0000';
data_dir        = fullfile( 'DATA', recording_name );
framelength_sec = 0.016;
mic_list        = 1:16;
output_filename = fullfile( 'DATA', sprintf( [ recording_name '-crosscorr_analysis_%04d.mat' ], round( framelength_sec * 1000 ) ) );

% Do the c.c. analysis

crosscorr_analysis_wrapper( recording_name, data_dir, framelength_sec, mic_list, output_filename );

% ... then apply any of the above to show the result

% For example, to get all .eps files you can use:

close all
do_pause = 0;
show_crosscorr_result( output_filename, do_pause ); % All speaker locations & all microphones

close all
show_crosscorr_summary( output_filename );

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Let's repeat with larger windows.
%
% Keep in mind that:
% -> theoretically it is meaningless to estimate the cross-correlation
%    between non-stationary signals.
% -> speech is considered stationary only up to 20 or 30 ms, no more

recording_name  = 'seq01-1p-0000';
data_dir        = fullfile( 'DATA', recording_name );
framelength_sec = 0.032;
mic_list        = 1:16;
output_filename = fullfile( 'DATA', sprintf( [ recording_name '-crosscorr_analysis_%04d.mat' ], round( framelength_sec * 1000 ) ) );
crosscorr_analysis_wrapper( recording_name, data_dir, framelength_sec, mic_list, output_filename );

close all
do_pause = 0;
show_crosscorr_result( output_filename, do_pause ); % All speaker locations & all microphones

close all
show_crosscorr_summary( output_filename );

%%%

recording_name  = 'seq01-1p-0000';
data_dir        = fullfile( 'DATA', recording_name );
framelength_sec = 0.064;
mic_list        = 1:16;
output_filename = fullfile( 'DATA', sprintf( [ recording_name '-crosscorr_analysis_%04d.mat' ], round( framelength_sec * 1000 ) ) );
crosscorr_analysis_wrapper( recording_name, data_dir, framelength_sec, mic_list, output_filename );

close all
do_pause = 0;
show_crosscorr_result( output_filename, do_pause ); % All speaker locations & all microphones

close all
show_crosscorr_summary( output_filename );

%%%

recording_name  = 'seq01-1p-0000';
data_dir        = fullfile( 'DATA', recording_name );
framelength_sec = 0.128;
mic_list        = 1:16;
output_filename = fullfile( 'DATA', sprintf( [ recording_name '-crosscorr_analysis_%04d.mat' ], round( framelength_sec * 1000 ) ) );
crosscorr_analysis_wrapper( recording_name, data_dir, framelength_sec, mic_list, output_filename );

close all
do_pause = 0;
show_crosscorr_result( output_filename, do_pause ); % All speaker locations & all microphones

close all
show_crosscorr_summary( output_filename );

%%%

recording_name  = 'seq01-1p-0000';
data_dir        = fullfile( 'DATA', recording_name );
framelength_sec = 0.256;
mic_list        = 1:16;
output_filename = fullfile( 'DATA', sprintf( [ recording_name '-crosscorr_analysis_%04d.mat' ], round( framelength_sec * 1000 ) ) );
crosscorr_analysis_wrapper( recording_name, data_dir, framelength_sec, mic_list, output_filename, 0 );

% -> crashes (memory issue, I guess)

close all
do_pause = 0;
show_crosscorr_result( output_filename, do_pause ); % All speaker locations & all microphones

close all
show_crosscorr_summary( output_filename );


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Localization study: does the elevation bias
% depend on the true elevation?
%
% ( this is a cleanup of /home/speech/lathoud/localization/multidetloc/s060802_el_bias_study.m )

% First choose which recording you want to run the analysis on

recording_name = 'seq01-1p-0000';

recording_name = 'seq03-1p-0000';

% First run multisource detection-localization
% ( IDIAP RR 06-26 )
% This is an exact copy of the code in http://mmm.idiap.ch/Lathoud/2006-multidetloc

FAST_detect_locate_wrapper( recording_name, 1:8 );  % Array #1
FAST_detect_locate_wrapper( recording_name, 9:16 );   % Array #2

for i_array = 1:2

  array_name = sprintf( '-array_%d', i_array );

  p = [];
  p.in_filename = [ 'DATA/FAST_detect_locate_wrapper-' recording_name array_name '-result.mat' ];
  p.n_comp_list = 2:10;
  p.far_t       = 0.01;
  p.out_dir     = 'DATA';
  sbmfcc_sns_model( p );
  
  classify_cluster_sns( [ 'DATA/sbmfcc_sns_model-FAST_detect_locate_wrapper-' recording_name array_name '-result.mat' ] );
  
  eval_detloc_el( [ 'DATA/classify_cluster_sns-sbmfcc_sns_model-FAST_detect_locate_wrapper-' recording_name array_name '-result.mat' ] );
  
  % Second look at the estimated elevation angle of the speaker mouth,
  % and compare it with the true speaker mouth.
  %
  % This also dumps '.eps' files
  
  input_filename    = [ 'DATA/eval_detloc_el-classify_cluster_sns-sbmfcc_sns_model-FAST_detect_locate_wrapper-' recording_name array_name '-result.mat' ];
  output_figure_dir = 'DATA';
  
  el_bias_study( input_filename, i_array, output_figure_dir );

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Analysis of the power decay versus the speaker distance

% First choose which recording you want to run the analysis on

recording_name = 'seq01-1p-0000';

recording_name = 'seq03-1p-0000';

% Second, choose a window length

framelength_sec = 0.016;
%framelength_sec = 0.032;

% Third, run the analysis

data_dir        = fullfile( 'DATA', recording_name );
mic_list        = 1:16;
output_filename = fullfile( 'DATA', sprintf( [ recording_name '-powerdecay_analysis_%04d.mat' ], round( framelength_sec * 1000 ) ) );

powerdecay_analysis_wrapper( recording_name, data_dir, framelength_sec, mic_list, output_filename );

% Third, show the result

show_powerdecay_summary( fullfile( 'DATA', sprintf( [ recording_name '-powerdecay_analysis_%04d.mat' ], round( framelength_sec * 1000 ) ) ) );
