
clc; clear; close all;
%%
% Generate all combination between wide-band and localized signals

%%
make_wav_pairs()
%%

pwd;
pathname ='spectro_all_mixtures';
if ~exist('spectro_all_mixtures','dir')
    mkdir('spectro_all_mixtures');
end
addpath('spectro_all_mixtures')

%%
data = load('signal_lists.mat');
%%
resampling_fs = 8000;
sig_len =16384;
signal_params = generate_signal_parameters(resampling_fs, sig_len);

%% DGT parameters
approx_win_len = 512;
win_len = approx_win_len;
win_type='hann';
params = get_params(win_len, win_type);

dgt_params = generate_dgt_parameters(win_type, approx_win_len, params.hop, params.nbins, sig_len);

%% DGT operators

[dgt, idgt] = get_stft_operators(dgt_params, signal_params);


%% All mix
close all
target_name ={'car','plane','train'};
per_name = {'beeps','bird','clicks','pop'};

gamma=0.7;
deb=0;
close all;

for k=1:length(data.wide_band_sources_files)
    sig_wd = load_wideband_signal(k, resampling_fs,  sig_len);
    
    for l=1:length(data.localized_sources_files)
        %%
        
        %figure;
        sig_loc = load_localized_signal(l, resampling_fs,  sig_len,deb);
        signals = generate_mix_signal(sig_wd, sig_loc, gamma);
        
        tf_mat_target = compute_dgt(signals.wideband, dgt );
        tf_mat_per = compute_dgt(signals.localized, dgt );
        tf_mat_mix = compute_dgt(signals.mix, dgt );
        
        [alpha, seuil, radius] = set_smooth_mask_params(target_name{k}, per_name{l}, win_type);
        
        sdr_mix = sdr(signals.wideband, signals.mix);
        
        [original_mask, mask_after_imclose, mask_after_imopen,...,
            mask] = generate_mask(tf_mat_target, tf_mat_per, alpha, seuil, radius);
        
        
        subplot(221);
        plot_spectrogram(tf_mat_target, dgt_params, signal_params, dgt )
        title(target_name{k})
        
        subplot(222);
        plot_spectrogram(tf_mat_per, dgt_params, signal_params, dgt )
        title(per_name(l));
        
        subplot(223);
        plot_spectrogram(tf_mat_mix, dgt_params, signal_params, dgt )
        title(['Mix SDR= ',num2str(sdr_mix),'dB']);
        
        subplot(224);
        plot_spectrogram(mask, dgt_params, signal_params, dgt )
        title(['mask - ', num2str(win_type)]);
        
        saveas(gcf,fullfile(pathname, [target_name{k},'_' num2str(l), '_',num2str(win_type) '.png']));
        %           figure;
        %     plot_spectrogram((1-mask).*tf_mat_per, dgt_params, signal_params, dgt );
        %     title(['masked spectro - ', target_name{k}, per_name(l)])
        %saveas(gcf,fullfile(pathname, ['masked_spectro_', target_name{k},'_' num2str(l), '_',num2str(win_type) '.png']));
        
    end
    
    
    
end



