% sons non periodiques (timbale et cloche)
%-----------------------------------------

% partiels d'une timbale
%-----------------------

clear all; close all;

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

f = f0*[2.40  5.52   8.65   11.79  14.93  ...
        3.83  7.02   10.17  13.32  16.47 ...
        5.14  8.42   11.62  14.80  17.96 ...
        6.38  9.76   13.02  16.22  19.41 ...
        7.59  11.06  14.37  17.62  20.83 ...
        8.77  12.34  15.70  18.98  22.22];

n = length(f);
am = f.^(-2.5);
N = 1:n;

% ajout des partiels
y = zeros(1,length(th));
for k = 1:n
    z = sin(2*pi*f(k)*th);
    y = y + am(k)*z;    
end
tfen = 1:round(.12*Fe);
plot(th(tfen),y(tfen));

% et d'une enveloppe quasi constante
tr = [0 .001 T*.99 T];
yr = [0  1  1 0];
y = y.*envelop(tr,yr,Fe);
y = .5*y/(max(abs(y)));
sound(y,Fe)
%wavwrite(y,Fe,'../SONS/105timbale.wav');
pause(T+2)

% ou d'une enveloppe decroisante
y = y.*exp(-1.8*th);
y = .98*y/(max(abs(y)));
sound(y,Fe)
%wavwrite(y,Fe,'../SONS/106timbale.wav');
pause(T+2)


% partiels d'une cloche
%----------------------

f0 = 233; T = 4;
th = 0:h:T;

f = [0.5  1 1.188 1.530 2.0000 2.470 2.607 2.650 2.991 3.367 4.137 4.487 4.829 5.385 5.863 6.709 8.077 8.547 9.017 9.530 11.026 12.393]*f0;
am = [350 950 500 150 700 100 250 370 1000 180 300 100  150  300  100   100  50   20   10   35   5   15];
n = length(f); N = 1:n;

% ajout des partiels
y = zeros(1,length(th));
for k = 1:n
    z = sin(2*pi*f(k)*th);
    y = y + am(k)*z;    
end

% et d'une enveloppe quasi constante
tr = [0 .001 T*.99 T];
yr = [0  1  1 0];
y = y.*envelop(tr,yr,Fe);
y = .5*y/(max(abs(y)));
sound(y,Fe)
%wavwrite(y,Fe,'../SONS/107cloche.wav');
pause(T+2)

% ou d'une enveloppe decroisante
tr = [0 .005 .2 1.3 T*.8 T];
yr = [0  .6    1  .4 .1 0];
env = envelop(tr,yr,Fe);
y = y.*env;
y = .98*y/(max(abs(y)));
sound(y,Fe)
%wavwrite(y,Fe,'../SONS/108cloche.wav');
