Commit 7a4defb3 authored by Marina Kreme's avatar Marina Kreme
Browse files

tff toolbox

parent bf42dfd8
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
function [signal_params, dgt_params, w,dgt,idgt, signals, alpha, seuil,...,
r, mask,dgt_mix]= get_cuicui_mix_icassp()
%% This function allows you to reproduce ICASSP dataset.
%% Signal parameters
%make_wav_pairs();
sig_len = 2^(13);
fs = 8000;
signal_params.fs = fs;
signal_params.sig_len = sig_len;
%% dgt parameters
approx_win_duration =0.02;
win_type ='hann';
win_len = 2^(round(log2(approx_win_duration*fs)));
dgt_params.hop = win_len/4 ;
dgt_params.nbins=4*win_len;
dgt_params.sig_len = sig_len;
dgt_params.win_type = win_type;
dgt_params.win_len = win_len;
%% window and DGT operator
L=dgtlength(dgt_params.sig_len, dgt_params.hop, dgt_params.nbins);
w = gabwin(dgt_params.win_type, dgt_params.hop, dgt_params.nbins, dgt_params.win_len);
dgt = @(x) dgtreal(x,w, dgt_params.hop, dgt_params.nbins, dgt_params.sig_len);
wd = {'dual', dgt_params.win_type};
idgt= @(x)idgtreal(x, wd, dgt_params.hop,dgt_params.nbins, dgt_params.sig_len);
compute_stft = @(x,w,dgt_params,L) dgtreal(x,w, dgt_params.hop, dgt_params.nbins,L);
%% signals and their mixtures
ind_wideband = 1;
ind_localized = 2;
deb = 0.2;
[x_engine, x_bird] = load_pairs(ind_wideband, ind_localized,...,
fs, sig_len, deb);
%%
x_target = x_engine;
x_perturbation = x_bird;
gamma=0.7;
x_target = x_target/max(abs(x_target));
x_perturbation = x_perturbation/max(abs(x_perturbation));
x_target = gamma*x_target;
x_perturbation = (1-gamma)*x_perturbation;
x_mix = x_target +x_perturbation;
signals.target = x_target;
signals.noise = x_perturbation;
signals.mix = x_mix;
%% Spectogramm of signals
dgt_Xref = compute_stft(signals.target,w, dgt_params,L);
dgt_Xper = compute_stft(signals.noise, w, dgt_params,L);
dgt_mix = dgt_Xref +dgt_Xper;
%% mask
alpha = 2; seuil =0.02; r = 3;
mask = and(abs(dgt_Xper) < alpha*abs(dgt_Xref),abs(dgt_Xper)<seuil);
se = strel('disk',r);
mask =imclose(mask,se);
figure; imagesc(mask);
%% plot figures
pwd;
pathname ='figures';
if ~exist('figures','dir')
mkdir('figures');
end
addpath('figures')
figure;
plot_spectrogram(mask, dgt_params, signal_params, dgt);
title('mask')
saveas(gcf,fullfile(pathname,['mask','.png']));
figure;
plot_spectrogram(dgt_Xref, dgt_params, signal_params, dgt)
title('target signal')
saveas(gcf,fullfile(pathname,['engine','.png']));
figure;
plot_spectrogram(dgt_Xper, dgt_params, signal_params, dgt)
title('perturbation signal')
saveas(gcf,fullfile(pathname,['bird','.png']));
figure;
plot_spectrogram(dgt_mix, dgt_params, signal_params, dgt)
title('perturbation signal')
saveas(gcf,fullfile(pathname,['engine_bird','.png']));
function dataset = get_dataset()
% This function is used to get the data of the experiments.
%The data is stored in a structure array.
%
% Author: Marina KREME
wide_dir = './data/data_8000Hz_16384samples/wide_band_sources/';
loc_dir = './data/data_8000Hz_16384samples/localized_sources/';
n_wide_dir = dir([wide_dir, '*.wav']);
n_loc_dir = dir([loc_dir, '*.wav']);
dataset = struct('wideband',struct(),'localized',struct());
for x =1: length(n_wide_dir)
wideband_file = [n_wide_dir(x).folder, filesep,n_wide_dir(x).name];
wideband_name = n_wide_dir(x).name(1:end-4);
dataset.wideband.(wideband_name)=wideband_file;
end
for x =1: length(n_loc_dir)
localized_file = [n_loc_dir(x).folder, filesep,n_loc_dir(x).name];
localized_file_name = n_loc_dir(x).name(1:end-4);
dataset.localized.(localized_file_name)=localized_file;
end
function [signals, dgt_params, signal_params, mask, mask_area, dgt,...,
idgt] = get_mix(loc_source, wideband_src, gamma, win_dur, hop_ratio,...,
nbins_ratio, win_type, alpha, thres, radius, fig_dir)
%%
% Function that generates :
% - the mixture of a wide-band spectrogram signal and a localized signal.
% - the mask from the two mixtures
%
% Inputs:
% - loc_source(str): signal with localized spectrogram
% - wideband_src(str): signal with wideband spectrogram
% - gamma: integer (belong to ]0,1[)
% - win_dur: window duration (must be between 12 and 20 ms)
% - hop_ratio, nbins_ration: real
% - win_type (str): analysis window (hann or gauss)
% -alpha, thres, radius(real): smoothing parameter for the mask
% -fig_dir: directory
%
% Outputs:
% -signals(struct): contains wideband, localized and mixtures signals
% - dgt_params (struct): contains dgt parameters (hop, n_bins, win_type, win_len, win)
% - signal_params (struct): contains the length of signal and sampling frequency
% - mask : binary mask
% - mask_are: binary mask area
% - dgt, idgt: Operator of Gabor transform and its inverse
%
% Author : Marina KREME
%%
dataset = get_dataset();
[x_loc, fs_loc]= audioread(dataset.localized.(loc_source));
[x_wb, fs_wb]= audioread(dataset.wideband.(wideband_src));
if length(x_loc)~=length(x_wb)
warning('Arrays are not equal');
end
if fs_loc~=fs_wb
error('The sampling frequencies must be the same.')
end
fs= fs_loc;
sig_len = length(x_loc);
signal_params = generate_signal_parameters(fs, sig_len);
%% build mix signals
signals = generate_mix_signal(x_wb, x_loc, gamma);
%% Build dgt
approx_win_len = 2.^round(log2(win_dur*fs));
hop = approx_win_len* hop_ratio;
nbins = approx_win_len * nbins_ratio;
dgt_params = generate_dgt_parameters(win_type, approx_win_len, hop, nbins, sig_len);
%% generat mask
[dgt, idgt] = get_stft_operators(dgt_params, signal_params);
tf_mat_wb = compute_dgt(signals.wideband, dgt );
tf_mat_loc = compute_dgt(signals.localized, dgt );
[mask, original_mask, mask_after_imclose, mask_after_imopen,...,
] = generate_mask(tf_mat_wb, tf_mat_loc, alpha, thres, radius);
mask_area = get_mask_area(mask);
%%
figure;
plot_mask(mask, hop, nbins, fs);
title(['Mask Mix : mask-area = ',num2str(mask_area)]);
set(gca, 'FontSize', 20, 'fontName','Times');
saveas(gcf,fullfile(fig_dir, 'mask_mix.pdf'));
%%
figure;
% plot_spectrogram(signals.wideband, dgt_params, signal_params, dgt)
% title(['Wideband source - ', wideband_src]);
subplot(231)
plot_spectrogram(signals.localized, dgt_params, signal_params, dgt)
title(['Localized source - ',loc_source]);
subplot(232)
plot_spectrogram(signals.mix, dgt_params, signal_params, dgt)
title('Mix');
subplot(233)
plot_mask(original_mask, hop, nbins, fs);
title('Original mask');
subplot(234)
plot_mask(mask_after_imclose, hop, nbins, fs);
title('Smooth mask- after imclose');
subplot(235)
plot_mask(mask_after_imopen, hop, nbins, fs);
title('Smooth and final mask - after impoen');
m = ~mask;
gabmul = gen_gabmul_operator(dgt, idgt,m);
x_est = gabmul(signals.wideband);
subplot(236)
plot_spectrogram(x_est, dgt_params, signal_params, dgt)
title('Filtered wb')
saveas(gcf,fullfile(fig_dir, 'mix_spectro_mask.pdf'));
end
\ No newline at end of file
function params = get_params(win_len, win_type)
% Function that generates input data for the function that generates dgt parameters
% Inputs:
% -win_len(int): analysis window length
% -win_type(str): analysis window type (hann/gauss)
% Outputs:
% -hop(int): hop size
% -nbins(int): frequency bins
% -win_type, win_len
%
% Author : Marina KREME
%%
switch win_type
case 'gauss'
hop = win_len/4;
nbins = win_len*4;
case 'hann'
hop = win_len/8;
nbins = win_len*2;
end
params.win_type = win_type;
params.win_len = win_len;
params.hop = hop;
params.nbins = nbins;
end
\ No newline at end of file
function x_loc = load_localized_signal(ind_localized, resampling_fs, sig_len, deb)
if nargin ==0
deb=0;
end
data = load('signal_lists.mat');
n = size(data.localized_sources_files, 1);
if ind_localized > n
error('%s: The ind_localized value you entered is outside the value range. Please try another value.', upper(mfilename));
end
localized_file = [data.localized_sources_files(ind_localized).folder, ...
filesep, ...
data.localized_sources_files(ind_localized).name];
x_loc= wave_read(localized_file, resampling_fs, sig_len, deb);
end
function [x_wideband,x_localized] = load_pairs(ind_wideband, ind_localized, resampling_fs, sig_len, deb)
% [x_wideband,x_localized] = load_pairs(ind_wideband, ind_localized, resampling_fs, sig_len, deb)
% Load data from .mat file. Return the target and the perturbation signals.
%
% Inputs:
% - ind_wideband:(int)index of signals that have a wideband spectrogram
% - ind_localized: (int) index of signals that have a well-localized spectrogram.
% - resampling_fs : desired sampling frequency.
% - sig_len: (int). signal length
% - deb: Start point of the signal in ms. deb =0 by default
%
% Outputs:
% - x_wideband: target signal
% - x_localized: perturbation signal (localized in time-frequency)
%
% Author : A. Marina KREME
% e-mail : ama-marina.kreme@lis-lab.fr/ama-marina.kreme@univ-amu.fr
% Created: 2020-28-01
%%
if nargin < 4
deb=0;
end
data = load('signal_lists.mat');
m = size(data.wide_band_sources_files, 1);
n = size(data.localized_sources_files, 1);
if ind_wideband > m || ind_localized > n
error('%s: The ind_wideband or ind_localized value you entered is outside the value range. Please try another value.', upper(mfilename));
end
wideband_file = [data.wide_band_sources_files(ind_wideband).folder, ...
filesep, ...
data.wide_band_sources_files(ind_wideband).name];
localized_file = [data.localized_sources_files(ind_localized).folder, ...
filesep, ...
data.localized_sources_files(ind_localized).name];
x_localized= wave_read(localized_file, resampling_fs, sig_len, deb);
x_wideband= wave_read(wideband_file, resampling_fs, sig_len, deb);
end
function x_wd = load_wideband_signal(ind_wideband,resampling_fs, sig_len, deb)
if nargin == 3
deb=0;
end
data = load('signal_lists.mat');
n = size(data.wide_band_sources_files, 1);
if ind_wideband > n
error('%s: The ind_wideband value you entered is outside the value range. Please try another value.', upper(mfilename));
end
wideband_file = [data.wide_band_sources_files(ind_wideband).folder, ...
filesep, ...
data.wide_band_sources_files(ind_wideband).name];
x_wd= wave_read(wideband_file, resampling_fs, sig_len, deb);
end
\ No newline at end of file
function make_wav_pairs()
%% MAKE_WAV_PAIRS: make_wav_pairs()
% function that generates two structures.
% One containing the wideband sounds and the other one with the well localized sounds
%
% Author : A. Marina KREME
% e-mail : ama-marina.kreme@lis-lab.fr/ama-marina.kreme@univ-amu.fr
% Created: 2020-28-01
%%
wide_dir = './data/data_8000Hz_16384samples/wide_band_sources/';
loc_dir = './data/data_8000Hz_16384samples/localized_sources/';
wide_band_sources_files= dir([wide_dir, '*.wav']); % set the path to the .wav file
localized_sources_files = dir([loc_dir, '*.wav']);
save('signal_lists.mat','wide_band_sources_files', 'localized_sources_files');
end
clc; clear; close all;
%%
[signal_params, dgt_params, w,dgt, signals, alpha, seuil,...,
r, mask]= get_cuicui_mix_icassp();
\ No newline at end of file
function [alpha, thres, radius] = set_smooth_mask_params(wideband_src, loc_source, win_type)
% This function allow us to generate parameters for smooth binary mask
% Inputs:
% - wideband_src : signal with wide-bande spectrogram
% - loc_source : signal with localized spectrogram
% - win_type : window - (gauss or hann)
%
%
% Outputs:
% - alpha : real
% - thres :threshold
% - radius : integer
%
% Author: Marina KREME
switch win_type
case 'gauss'
switch wideband_src
case 'car'
switch loc_source
case 'beeps'
alpha=9;
thres=1e-5;
radius=1;
case 'bird'
alpha=1 ;
thres=1e-4;
radius=4;
case 'clicks'
alpha=1;
thres=0.0002;
radius=3;
case 'finger_snaps'
alpha=0.01;
thres=0.0001;
radius=1;
case 'modulations'
alpha=1;
thres=0.0002;
radius=3;
case 'pop'
alpha=0.1;
thres=0.0001;
radius=1;
end
case'plane'
switch loc_source
case 'beeps'
alpha=1;
thres=0.00002;
radius=2;
case 'bird'
alpha=0.3;
thres=0.0001;
radius=1;
case 'clicks'
alpha=1;
thres=0.0002;
radius=4;
case 'finger_snaps'
alpha=0.01;
thres=0.0001;
radius=1;
case 'modulations'
alpha=1;
thres=0.0002;
radius=3;
case 'pop'
alpha=0.01;
thres=0.0001;
radius=1;
end
case 'train'
switch loc_source