% bruit de quantification d'un son pur
%-------------------------------------

clear all

Fe = 22050;
f0 = 440; T = 1;
h = 1/Fe; th = 0:h:T;

% son pur + enveloppe
x = sin(2*pi*f0*th);
tr = T*[0 .02 .98 1];
yr = [0 1 1 0];
x = x.*envelop(tr,yr,Fe);

% quantification sur 4,6,8,16 bits
x4  = (round(2^3*x+.5)-.5)/2^3;
x6  = (round(2^5*x+.5)-.5)/2^5;
x8  = (round(2^7*x+.5)-.5)/2^7;
x16 = (round(2^15*x+.5)-.5)/2^15;

% normalisation
x4  = .9*x4/max(abs(x4));
x6  = .9*x6/max(abs(x6));
x8  = .9*x8/max(abs(x8));
x16 = .9*x16/max(abs(x16));

plot(th,x4,th,x6,th,x8,th,x16);
disp('son pur quantifie sur 16 bits');
sound(x16,Fe); pause(2);
disp('son pur quantifie sur 8 bits');
sound(x8,Fe); pause(2);
disp('son pur quantifie sur 6 bits');
sound(x6,Fe); pause(2);
disp('son pur quantifie sur 4 bits');
sound(x4,Fe); pause(2);
