TP-traitement-audio-visuel/TP12/exercice_2.m
2023-06-22 20:47:16 +02:00

89 lines
3 KiB
Matlab
Executable file

clear;
close all;
taille_ecran = get(0, 'ScreenSize');
L = taille_ecran(3);
H = taille_ecran(4);
% Lecture d'un fichier audio :
[y, f_ech] = audioread('Musiques/Au clair de la lune.wav');
% Passage dans le domaine fréquentiel :
n_fenetre = 1024; % Largeur de la fenêtre (en nombre de points)
n_decalage = 512; % Décalage entre deux fenêtres (en nombre de points)
fenetre = 'hann'; % Type de la fenêtre
[Y, valeurs_t, valeurs_f] = TFCT(y, f_ech, n_fenetre, n_decalage, fenetre);
S = 20 * log10(abs(Y) + eps);
% figure('units', 'normalized', 'outerposition', [0 0 1 1]);
% imagesc(valeurs_t, valeurs_f, S);
% axis xy;
% xlabel('Temps (s)');
% ylabel('Frequence (Hz)');
% title('$\mathbf{S}$', 'Interpreter', 'Latex');
% drawnow;
% export_fig(gcf, "/work/lfainsin-matlab/TP12/exo2_sonogramme.png", '-png', '-painters', '-m2');
% NMF :
R = 25;
D_0 = rand(size(S, 1), R);
A_0 = rand(R, size(S, 2));
[D, A] = nmf(abs(Y), D_0, A_0, 30, 1, "exo2", valeurs_t, valeurs_f, R);
% Sauvegarde de chaque composante :
figure('units', 'normalized', 'outerposition', [0 0 1 1]);
for r = 1:R
Y_r = (D(:, r) * A(r, :)) ./ (D * A) .* Y;
[y_r, ~] = ITFCT(Y_r, f_ech, n_decalage, fenetre);
y_r = min(max(y_r, -1), 1);
audiowrite(['/work/lfainsin-matlab/TP12/exo2_composante_' int2str(r) '_' int2str(R) '.wav'], y_r, f_ech);
S_r = 20 * log10(abs(Y_r) + eps);
imagesc(valeurs_t, valeurs_f, S_r);
axis xy;
xlabel('Temps (s)');
ylabel('Frequence (Hz)');
title("$\mathbf{S_{r" + int2str(r) + "}}$", 'Interpreter', 'Latex');
drawnow;
export_fig(gcf, "/work/lfainsin-matlab/TP12/exo2_mask_" + int2str(r) + "_" + int2str(R) + ".png", '-png', '-painters', '-m2');
end
% % Figures :
% figure('units', 'normalized', 'outerposition', [0 0 1 1]);
% imagesc(1:R, valeurs_f, 20 * log10(abs(D)));
% set(gca, 'xtick', 1:R)
% caxis([-40 10]);
% axis xy;
% xlabel('Composantes');
% ylabel('Frequence (Hz)');
% title('$\mathbf{D}$', 'Interpreter', 'Latex');
% drawnow;
% export_fig(gcf, "/work/lfainsin-matlab/TP12/exo2_dico_" + int2str(R) + ".png", '-png', '-painters', '-m2');
%
% figure('units', 'normalized', 'outerposition', [0 0 1 1]);
% imagesc(valeurs_t, 1:R, A);
% set(gca, 'ytick', 1:R)
% axis xy;
% xlabel('Temps (s)');
% ylabel('Composantes');
% title('$\mathbf{A}$', 'Interpreter', 'Latex');
% drawnow;
% export_fig(gcf, "/work/lfainsin-matlab/TP12/exo2_activ_" + int2str(R) + ".png", '-png', '-painters', '-m2');
%
% figure('units', 'normalized', 'outerposition', [0 0 1 1]);
% prod = 20 * log10(abs(Y_final) + eps);
% imagesc(valeurs_t, valeurs_f, prod);
% axis xy;
% xlabel('Temps (s)');
% ylabel('Frequence (Hz)');
% title('$\mathbf{S}$', 'Interpreter', 'Latex');
% drawnow;
% export_fig(gcf, "/work/lfainsin-matlab/TP12/exo2_sonoprod_" + int2str(R) + ".png", '-png', '-painters', '-m2');
Y_final = D * A;
[y_final, ~] = ITFCT(Y_final, f_ech, n_decalage, fenetre);
y_final = min(max(y_final, -1), 1);
audiowrite(['/work/lfainsin-matlab/TP12/exo2_final_' int2str(R) '.wav'], y_final, f_ech);