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

clear all; close all;

Fe = 22050;
f0 = 440; T = 2;
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,8,12,16 bits
x4  = (round(2^3 *x+.5)-.5)/2^3;
x8  = (round(2^7 *x+.5)-.5)/2^7;
x12 = (round(2^11*x+.5)-.5)/2^11;
x16 = (round(2^15*x+.5)-.5)/2^15;

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

plot(th,x4,th,x8,th,x12,th,x16);
sound(x16,Fe); pause(3);
sound(x12,Fe); pause(3);
sound(x8,Fe); pause(3);
sound(x4,Fe); pause(3);
wavwrite(x16,Fe,'../SONS/502sin16.wav');
wavwrite(x12,Fe,'../SONS/503sin12.wav');
wavwrite(x8,Fe,'../SONS/504sin8.wav');
wavwrite(x4,Fe,'../SONS/505sin4.wav');
