diff --git a/docs/SARaudio.m b/docs/SARaudio.m index 6299c3cddd5f1c12fec0686f5fd3f9d757d53dc1..7f8fb4f174ef6175cc90a472068596a0454a79f9 100644 --- a/docs/SARaudio.m +++ b/docs/SARaudio.m @@ -1,6 +1,7 @@ +%{ clear all; close all; -%{ + [x,fe]=audioread('/homes/t24dherv/Documents/SAR_audio/single_tone_cello-a3.wav'); soundsc(x,fe); @@ -19,11 +20,19 @@ freq = -fe/2 : fe/n : fe/2-fe/n; freq2 = -fe2/2 : fe2/n2 : fe2/2-fe2/n2; -plot(freq,abs(y)) +plot(freq,10*log(abs(y))); +xlabel('Fréquence (Hz)'); +ylabel('Amplitude du signal (dB)'); +title('Single tone cello'); + figure; clf; -plot(freq2,abs(y2)); - +plot(freq2,10*log(abs(y2))); +xlabel('Fréquence (Hz)') +ylabel('Amplitude du signal (dB)') +title('Single tone guitar nylon string') +%} +%{ clear all; close all; @@ -43,13 +52,15 @@ n4 = length(y4); freq4 = -fe4/2 : fe4/n4 : fe4/2-fe4/n4; -plot(freq3,abs(y3)); -hold on -plot(freq4,abs(y4)); +plot(freq3,10*log(abs(y3)),freq4,10*log(abs(y4))); +legend('single-tone-piano1','single-tone-piano2'); +xlabel('Fréquence (Hz)'); +ylabel('Amplitude du signal (dB)'); + fe = 44100; -D = 2; +D = 5; t = linspace(0, D, D*fe); % Exemple d’amplitudes relatives (normalisées) @@ -58,13 +69,20 @@ f = [220.374, 442.041, 663.24, 885.538, 1108.34, 1331.46, 1556.2, 1782.88]; x_synth = zeros(size(t)); for k = 1:8 - x_synth = x_synth + A(k) * sin(2*pi*f(k)*t); + x_synth = x_synth + 10*log(A(k)) * sin(2*pi*f(k)*t); end x_synth = x_synth / max(abs(x_synth)); %soundsc(x_synth, fe); audiowrite('synth_piano.wav', x_synth, fe); +plot(t,x_synth) +xlabel('Temps (s)') +ylabel('Signal normalisé') +title('Signal engendré') + + + % Durées de chaque phase A = 0.1; Dd = 0.4; S = 1; R =0.5; @@ -103,29 +121,38 @@ xlabel('Temps (s)'); ylabel('Amplitude'); title('Enveloppe ADSR'); +%} +%{ A = [7287.97, 5565.18, 1088.64, 1414.52, 980.919, 671.72, 570.373, 101.402]; f = [220.374, 442.041, 663.24, 885.538, 1108.34, 1331.46, 1556.2, 1782.88]; fe = 44100; -D = 2; +D = 5; t = linspace(0, D, D*fe); N = D*fe; x_synth = zeros(size(t)); for k = 1:8 - x_synth = x_synth + A(k) * sin(2*pi*f(k)*t); + x_synth = x_synth + 10*log(A(k)) * sin(2*pi*f(k)*t); end +x_synth = x_synth / max(abs(x_synth)); + nu = zeros(size(t)); for k = 1:8 - nu = nu + A(k) * fftshift(fft(sin(2*pi*f(k)*t))); + nu = nu + 10*log(A(k)) * fftshift(fft(sin(2*pi*f(k)*t))); end -x_synth2 = ifft(nu) -plot(t,x_synth2) +x_synth2 = ifft(nu)/max(abs(ifft(nu))); +plot(t,x_synth2); +title('Nouveau signal') +xlabel('Temps (s)') +ylabel('Signal normalisé') +%} +%{ fe = 44100; % fréquence d’échantillonnage T = 1/440; % période = 1/f0 = La4 D = 100*T; @@ -148,34 +175,36 @@ figure; subplot(2,1,1); plot(f, (X_carre)); title('Spectre signal carré'); -xlabel('Fréquence (Hz)'); ylabel('Amplitude (dB)'); +xlabel('Fréquence (Hz)'); ylabel('Amplitude'); xlim([0 5000]); subplot(2,1,2); plot(f, (X_saw)); title('Spectre signal dent de scie'); -xlabel('Fréquence (Hz)'); ylabel('Amplitude (dB)'); +xlabel('Fréquence (Hz)'); ylabel('Amplitude'); xlim([0 5000]); +%} +%{ % Signal d’entrée : signal dent de scie fe = 44100; f0 = 10; D = 100/f0; t = linspace(0, D, D*fe); -x = sawtooth(2*pi*f0*t); +%x = sawtooth(2*pi*f0*t); z = square(2*pi*f0*t); % Filtrage passe-bas d’ordre 1 b = [0.5 0.5]; a = 1; -y = filter(b, a, x); +%y = filter(b, a, x); w = filter(b, a, z); % FFT N = length(x); -X = fftshift(abs(fft(x))/N); -Y = fftshift(abs(fft(y))/N); +%X = fftshift(abs(fft(x))/N); +%Y = fftshift(abs(fft(y))/N); Z = fftshift(abs(fft(z))/N); W = fftshift(abs(fft(w))/N); f = linspace(-fe/2, fe/2, N); @@ -183,28 +212,53 @@ f = linspace(-fe/2, fe/2, N); % Affichage figure; -plot(f, 20*log10(X), 'b'); hold on; -plot(f, 20*log10(Y), 'r'); -legend('Entrée (signal brut)', 'Sortie filtrée'); -xlabel('Fréquence (Hz)'); -ylabel('Amplitude (dB)'); -title('Spectre avant et après filtrage passe-bas'); -xlim([0 5000]); +%plot(f, 10*log(X), 'b'); hold on; +%plot(f, 10*log(Y), 'r'); +%legend('Entrée (signal brut)', 'Sortie filtrée'); +%xlabel('Fréquence (Hz)'); +%ylabel('Amplitude (dB)'); +%title('Spectre avant et après filtrage passe-bas'); +%xlim([0 5000]); figure; -plot(f, 20*log(Z), 'b'); hold on; -plot(f, 20*log(W), 'r'); +plot(f, 10*log(Z), 'b'); hold on; +plot(f, 10*log(W), 'r'); figure(2); legend('Entrée (signal brut)', 'Sortie filtrée'); xlabel('Fréquence (Hz)'); -ylabel('Amplitude (dB)'); +ylabel('Amplitude'); title('Spectre avant et après filtrage passe-bas'); xlim([0 5000]); soundsc(w,fe) audiowrite('square_filtre.wav', w, fe); +%} + +%{ +% Signal d’entrée : signal dent de scie +fe = 44100; +f0 = 10; +D = 100/f0; +t = linspace(0, D, D*fe); +%x = sawtooth(2*pi*f0*t); +z = square(2*pi*f0*t); + +% Filtrage passe-bas d’ordre 1 +b = [0.5 0.5]; +a = 1; +%y = filter(b, a, x); +w = filter(b, a, z); + +% FFT +N = length(x); +%X = fftshift(abs(fft(x))/N); +%Y = fftshift(abs(fft(y))/N); +Z = fftshift(abs(fft(z))/N); +W = fftshift(abs(fft(w))/N); +f = linspace(-fe/2, fe/2, N); + d = designfilt('lowpassiir','FilterOrder',3,'HalfPowerFrequency',1); @@ -213,8 +267,8 @@ y_2 = filter(d,z); Y_2 = fftshift(abs(fft(y_2))/N); figure; -plot(f, 20*log10(X), 'b'); hold on; -plot(f, 20*log10(Y_2), 'r'); +plot(f, 10*log10(X), 'b'); hold on; +plot(f, 10*log10(Y_2), 'r'); legend('Entrée (signal brut)', 'Sortie filtrée'); xlabel('Fréquence (Hz)'); ylabel('Amplitude (dB)'); @@ -224,16 +278,17 @@ xlim([0 5000]); soundsc(y_2,fe); %} - -load('signal_excitation.mat'); - %{ -plot(xcorr(xe1,xe1)) -plot(xcorr(xe2,xe2)) load('signal_excitation.mat'); + + +plot(xcorr(xe1,xe1)); +title('Autocorrélation du signal xe1'); +plot(xcorr(xe2,xe2)); +title('Autocorrélation du signal xe2'); y = simule_piece(xe1, fe); plot(xcorr(y,xe1)); -%} +title('Réponse impulsionnelle') [x_test,fe_test]=audioread('/homes/t24dherv/Documents/SAR_audio/single_tone_guitar_nylon_string_a3.wav'); soundsc(x_test,fe_test); @@ -244,4 +299,86 @@ y = conv(xe1,x_test(:,2)); %plot(y); -soundsc(y,fe); \ No newline at end of file +soundsc(y,fe); + +%} + +%{ +function y = effet_reverb(x,h); + + y = conv(x,h); + y = y/max(abs(y)); +end + +% Charger le signal source (guitare) +[x4,fe4]=audioread('/homes/t24dherv/Documents/SAR_audio/single_tone_piano2.wav'); + +% Charger la réponse impulsionnelle estimée +y = simule_piece(xe1, fe); + +% Mesurer le temps de calcul +tic; +w = effet_reverb(x, y); +t_exec = toc; + +% Afficher le temps mesuré +fprintf('Temps d_exécution de effet_reverb : %.6f secondes\n', t_exec); + +% Lecture et sauvegarde du signal réverbéré +soundsc(w, fe); +audiowrite('guitare_reverberee.wav', w, fe); + +%} + +fe = 44100; +f0 = 440; + +t = linspace(0, D, D*fe); + +function y = effet_reverb_FFT(x, h) + % Réverbération par convolution dans le domaine fréquentiel + + % Taille minimale de FFT + NFFT = max(length(x),length(h)); + + % Chronométrage + tic; + + % FFT des deux signaux + X = fft(x, NFFT); + H = fft(h, NFFT); + + % Multiplication dans le domaine fréquentiel + Y = X .* H; + + % Retour dans le domaine temporel + y = ifft(Y, NFFT); + y = real(y); % Supprimer éventuelles parties imaginaires + y = y / max(abs(y)); % Normalisation + + % Affichage du temps d'exécution + t_exec = toc; + fprintf('Temps d''exécution (FFT) : %.6f secondes\n', t_exec); +end + +% Charger le signal source (guitare) +[x4,fe4]=audioread('/homes/t24dherv/Documents/SAR_audio/single_tone_piano2.wav'); + +% Charger la réponse impulsionnelle estimée +y = simule_piece(xe1, fe); + +% Mesurer le temps de calcul +tic; +w = effet_reverb_FFT(x4, y); +t_exec = toc; + +% Afficher le temps mesuré +fprintf('Temps d_exécution de effet_reverb : %.6f secondes\n', t_exec); + +% Lecture et sauvegarde du signal réverbéré +soundsc(w, fe); +audiowrite('guitare_reverberee.wav', w, fe); + + + +plot(linspace(0,1, length(w)),w) \ No newline at end of file