Skip to content
Snippets Groups Projects
Commit f03d9ab9 authored by DUBOC Marc's avatar DUBOC Marc
Browse files

Upload New File

parent 96a3ac68
Branches
No related tags found
No related merge requests found
PARTIE2.m 0 → 100644
%% ========================================================================
% PARTIE 2 — Synthese soustractive (Q 2.1 → Q 2.4)
% Poly « SAR – Traitements audio : partie signal » v1.0 (23 avril 2025)
% ------------------------------------------------------------------------
% Script sans caractere underscore, corrige pour l erreur designfilt.
% ------------------------------------------------------------------------
clear; close all; clc
Fe = 44100; % frequence d echantillonnage (Hz)
addpath(pwd) % acces aux fichiers wav
%% ========================================================================
%% Q 2.1 — Spectres carre et dent de scie
% -------------------------------------------------------------------------
dur = 0.1; f0 = 440;
t = (0:1/Fe:dur - 1/Fe).';
xCarre = square(2*pi*f0*t);
xScie = sawtooth(2*pi*f0*t);
figure('Name','Q2-1 Spectres'); hold on
plotSpectrum(xCarre,Fe,'b','Carre FFT');
plotSpectrum(xScie ,Fe,'r','Scie FFT');
nH = 20;
stem(f0*(1:2:(2*nH-1)), 20*log10(abs(4./(pi*(1:2:(2*nH-1))))), ...
'k.','DisplayName','Carre theorie');
stem(f0*(1:nH), 20*log10(abs(2./(pi*(1:nH))) ), ...
'm.','DisplayName','Scie theorie');
xlim([0 8*f0]); grid on; legend
xlabel('Frequence (Hz)'); ylabel('|X| (dB)');
title('Q 2.1 Spectres normalises');
%% ========================================================================
%% Q 2.2 — Filtre passe-bas ordre 1 : theorie et test
% -------------------------------------------------------------------------
dur = 0.1; t = (0:1/Fe:dur - 1/Fe).';
x = sawtooth(2*pi*f0*t);
b = [0.5 0.5]; a = 1;
[H,w] = freqz(b,a,4096,'whole',Fe);
figure('Name','Q2-2 Filtre ordre 1');
subplot(2,1,1)
plot(w,20*log10(abs(H))); grid on
title('|H| theorique cos(w/2)'); xlabel('f (Hz)'); ylabel('dB')
y = filter(b,a,x);
subplot(2,1,2); hold on
plotSpectrum(x,Fe,'k','Entree');
plotSpectrum(y,Fe,'g','Sortie');
xlim([0 6*f0]); grid on; legend
title('Spectre dB verification');
soundsc([x y],Fe); % gauche brut, droite filtre
%% ========================================================================
%% Q 2.3 — Ecoute comparative (sources + LPF)
% -------------------------------------------------------------------------
forms = {'square','sawtooth','triangle'};
fprintf('\n===== Q 2.3 ecoute comparative =====\n');
pause(0.4)
for k = 1:numel(forms)
dur = 0.5; t = (0:1/Fe:dur - 1/Fe).';
switch forms{k}
case 'triangle'
x = sawtooth(2*pi*f0*t,0.5).^2 .* sign(sin(2*pi*f0*t));
otherwise
x = feval(forms{k},2*pi*f0*t);
end
y = filter(b,a,x);
fprintf('-- %s non filtre ...\n',forms{k}); soundsc(x,Fe); pause(dur+0.25)
fprintf('-- %s filtre ...\n',forms{k}); soundsc(y,Fe); pause(dur+0.25)
end
fprintf('===== fin Q 2.3 =====\n');
%% ========================================================================
%% Q 2.4 — Variation Fc, ordre, FIR ou IIR
% -------------------------------------------------------------------------
dur = 1; t = (0:1/Fe:dur - 1/Fe).';
x = sawtooth(2*pi*110*t); % scie grave (La1)
% Parametres adaptes pour eviter l erreur designfilt
dFIR8 = designfilt('lowpassfir','FilterOrder',8, 'HalfPowerFrequency',0.30);
dFIR30 = designfilt('lowpassfir','FilterOrder',30, 'HalfPowerFrequency',0.15);
dIIR = designfilt('lowpassiir','FilterOrder',8, 'HalfPowerFrequency',0.20,...
'DesignMethod','butter');
bank = {dFIR8 ,'FIR N=8 Fc=0.30' ; ...
dFIR30,'FIR N=30 Fc=0.15' ; ...
dIIR ,'IIR Butter N=8 Fc=0.20'};
fprintf('\n===== Q 2.4 ecoute des filtres =====\n');
for k = 1:size(bank,1)
y = filter(bank{k,1},x);
fprintf('-- %s ...\n',bank{k,2});
soundsc(y,Fe); pause(dur+0.35)
end
figure('Name','Q2-4 Reponses en frequence'); hold on
for k = 1:size(bank,1)
[h,w] = freqz(bank{k,1},4096,Fe);
plot(w,20*log10(abs(h)),'DisplayName',bank{k,2});
end
grid on; xlabel('f (Hz)'); ylabel('dB'); legend
title('Filtres FIR et IIR influence de Fc et de l ordre');
fprintf('===== fin Q 2.4 =====\n');
%% ========================================================================
%% === Fonction locale =====================================================
function plotSpectrum(x,Fe,color,label)
N = length(x);
f = (-N/2:N/2-1)*Fe/N;
X = fftshift(fft(x .* hann(N)));
SdB = 20*log10(abs(X)/max(abs(X))+eps);
plot(f,SdB,color,'LineWidth',1.2,'DisplayName',label);
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment