estimate_energy_in_mask.m 2.59 KB
Newer Older
Marina Kreme's avatar
Marina Kreme committed
1
2
3
function estimated_energy = estimate_energy_in_mask(x_mix, mask, dgt_params, signal_params, dgt,fig_dir)

%%estimated_energy = estimate_energy_in_mask(x_mix, mask, dgt_params, signal_params, dgt,fig_dir)
Marina Kreme's avatar
doc    
Marina Kreme committed
4
% Functions that estimates energy in time-frequency mask
Marina Kreme's avatar
Marina Kreme committed
5
6
7
8
9
10
11
12
13
%
% Inputs:
%     - x_mix:mixtures of signals with wideband and localized spectrograms
%     - mask:labeled mask
%     - dgt_params : Discrete Gabor Transform parameters(hop, nbins,win, ect..)
%     - signal_params:Signals parameters(sig_len, fs)
%     - dgt: Gabor transform operator
%     - fig_dir: firgures directory
% Outputs:
Marina Kreme's avatar
doc    
Marina Kreme committed
14
%     -estimated_energy: Estimated energy in each sub-region.
Marina Kreme's avatar
Marina Kreme committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
%
%
% Author: Marina KREME
%%
x_tf_mat = dgt(x_mix);
x_tf_mat(mask>0)= nan;
e_f_mean = nanmean(abs(x_tf_mat).^2, 2);
%%
n_areas = length(unique(mask))-1;
estimated_energy = zeros(n_areas,1);
e_mat = e_f_mean*ones(1, size(x_tf_mat,2));
e_mat(mask==0) = 0;

for k_area = 1:n_areas
    mask_k = (mask==k_area);
    estimated_energy(k_area) = sum(e_mat(:).*mask_k(:));
    
end

%%


dynrange = 100;
c_max = nanmax(db(x_tf_mat(:)));
clim = [c_max - dynrange, c_max];


fs = signal_params.fs;
figure;
plot_spectrogram(x_mix, dgt_params, signal_params, dgt, dynrange, clim)
%plotdgtreal(S_mix, dgt_params.hop, dgt_params.nbins, fs);
title('Mix');
set(gca, 'FontSize', 20, 'fontName','Times');
saveas(gcf,fullfile(fig_dir, 'mix.pdf'));



%
figure;
plotdgtreal(sqrt(e_mat), dgt_params.hop, dgt_params.nbins, fs, 'clim', clim);
%title(['Mask filled with average energy (total: ', num2str(estimated_energy,8),')']);
set(gca, 'FontSize', 20, 'fontName','Times');
Marina Kreme's avatar
doc    
Marina Kreme committed
57
saveas(gcf,fullfile(fig_dir, 'mask_filled__with_energy.pdf'));
Marina Kreme's avatar
Marina Kreme committed
58
59
60
61
62
63
64
65
66


%

x_tf_mat(mask>0) = sqrt(e_mat(mask>0));
figure;
plotdgtreal(x_tf_mat, dgt_params.hop, dgt_params.nbins, fs, 'clim', clim);
%title(['Mix filled with average energy (total: ', num2str(estimated_energy,8),')']);
set(gca, 'FontSize', 20, 'fontName','Times');
Marina Kreme's avatar
doc    
Marina Kreme committed
67
saveas(gcf,fullfile(fig_dir, 'filled_mix_with_average_energy.pdf'));
Marina Kreme's avatar
Marina Kreme committed
68
69
70
71
72
73
74
75
76
77


%
figure;
plot(db(e_f_mean) / 2);
xlabel('Frequency index')
ylabel('Average energy')
grid on;
title('Average energy per frequency bin in mix');
set(gca, 'FontSize', 20, 'fontName','Times');
Marina Kreme's avatar
doc    
Marina Kreme committed
78
saveas(gcf,fullfile(fig_dir, 'average_energy_per_freq_bin.pdf'));
Marina Kreme's avatar
Marina Kreme committed
79
80
81
82
83
84
85
86
87
88
89
90
91


%%
e_f_mean_check = mean(abs(x_tf_mat).^2, 2);
figure;
plot(db(e_f_mean) / 2); hold on;
plot(db(e_f_mean_check) / 2, '--')
xlabel('Frequency index')
ylabel('Average energy')
title('Average energy per frequency bin in mix')
legend('Before filling', 'After filling')
grid on;
set(gca, 'FontSize', 20, 'fontName','Times');
Marina Kreme's avatar
doc    
Marina Kreme committed
92
saveas(gcf,fullfile(fig_dir, 'average_energy_per_freq_bin_mix.pdf'));
Marina Kreme's avatar
Marina Kreme committed
93
94
95


end