diff --git a/src/SAR1Q5.m b/src/SAR1Q5.m new file mode 100644 index 0000000000000000000000000000000000000000..4a0a6fd4626175e3c46d792df1071ec6868fa479 --- /dev/null +++ b/src/SAR1Q5.m @@ -0,0 +1,84 @@ +%Q1_5 +% Chargement du son +fichier = fullfile('wav', 'single_tone_piano1'); +[signal, Fs] = audioread(fichier); +% Mono si besoin +if size(signal,2) == 2 + signal = mean(signal, 2); +end + +% Extraire 1 seconde de son +duration = 1; +N = min(length(signal), round(duration*Fs)); +signal = signal(1:N); + +% FFT +Nfft = length(signal); +Y = fft(signal); +f = (0:Nfft-1)*(Fs/Nfft); + +% Spectre moitié +Y_half = Y(1:floor(Nfft/2)); +f_half = f(1:floor(Nfft/2)); + +% Détection du fondamentale +[~, idx_fundamental] = max(abs(Y_half)); +f0 = f_half(idx_fundamental); + +% Récupérer les indices des 8 premières harmoniques +harmonics_freqs = f0 * (1:8); +harmonic_indices = zeros(1,8); + +for k = 1:8 + [~, idx] = min(abs(f - harmonics_freqs(k))); + harmonic_indices(k) = idx; +end + +% Construire un nouveau spectre : garder seulement les harmoniques +Y_harmonics = zeros(size(Y)); +for idx = harmonic_indices + Y_harmonics(idx) = Y(idx); % Fréquence positive + if idx ~= 1 % éviter de doubler le DC + Y_harmonics(end - idx + 2) = conj(Y(idx)); % Fréquence négative conjuguée + end +end + +% Reconstruction via IFFT +synth_ifft = real(ifft(Y_harmonics)); + +% Normalisation +synth_ifft = synth_ifft / max(abs(synth_ifft)); + +% Appliquer enveloppe ADSR +t = (0:length(synth_ifft)-1)' / Fs; + +% ADSR paramètres +T_attack = 0.05; +T_decay = 0.1; +T_sustain = 0.7; +T_release = 0.2; + +n_attack = round(T_attack * Fs); +n_decay = round(T_decay * Fs); +n_release = round(T_release * Fs); +n_sustain = length(t) - (n_attack + n_decay + n_release); + +if n_sustain < 0 + error('Durée insuffisante pour l’ADSR définie.'); +end + +env_attack = linspace(0,1,n_attack)'; +env_decay = linspace(1,T_sustain,n_decay)'; +env_sustain = T_sustain * ones(n_sustain,1); +env_release = linspace(T_sustain,0,n_release)'; + +envelope = [env_attack; env_decay; env_sustain; env_release]; + +% Appliquer l’enveloppe +synth_ifft = synth_ifft .* envelope; + +% Écoute +sound(synth_ifft, Fs); + +% Sauvegarde si besoin +audiowrite('synth_ifft_ADSR.wav', synth_ifft, Fs); \ No newline at end of file