% filtrages des harmoniques du Do-2
% et visualisation des enveloppes
%----------------------------------

clear all; close all;

[x,Fe,nbits] = wavread('pianodo3.wav');
n = length(x);
T = n/Fe;
x = .95*x/max(abs(x));
sound(x,Fe); 
pause(T+1);
%wavwrite(x,Fe,'../SONS/221do3complet.wav');

x0 = x;
f = 110*2^(3/12);
h = 1/Fe; n = length(x);
t = (n-1)*h;
th = 0:h:t;

% filtrage des 3 premiers harmoniques
%------------------------------------
N = 2*ceil(10*Fe/f);
b = fir1(N,3.3*f/Fe*2);
x = [x0 ; zeros(N,1)];
x = filter(b,1,x);
N = floor(N/2);
x = x(N+1:N+n);
x = .98*x/max(abs(x));
sound(x,Fe); 
pause(T+1);
%wavwrite(x,Fe,'../SONS/222do3h123.wav');
% visualisation de l'enveloppe
env = calculenv(x,Fe,f);
env = env/max(abs(env))*max(abs(x));
subplot(3,1,1);
plot(th,x,th,env,th,-env);
axis([0 1.2 -1 1]);

% filtrage du premier harmonique
%-------------------------------
N = 2*ceil(10*Fe/f);
b = fir1(N,[.8 1.3]*f/Fe*2);
x = [x0 ; zeros(N,1)];
x = filter(b,1,x);
N = floor(N/2);
x = x(N+1:N+n);
x = .98*x/max(abs(x));
sound(x,Fe); 
pause(T+1);
%wavwrite(x,Fe,'../SONS/223do3h1.wav');
% visualisation de l'enveloppe
env = calculenv(x,Fe,f);
env = env/max(abs(env))*max(abs(x));
subplot(3,1,2);
plot(th,x,th,env,th,-env);
axis([0 1.2 -1 1]);

% filtrage du troisieme harmonique
%---------------------------------
b = fir1(N,[2.8 3.3]*f/Fe*2);
x = [x0 ; zeros(N,1)];
x = filter(b,1,x);
N = floor(N/2);
x = x(N+1:N+n);
x = .98*x/max(abs(x));
sound(x,Fe);
%wavwrite(x,Fe,'../SONS/224do3h3.wav');
% visualisation de l'enveloppe
env = calculenv(x,Fe,f);
env = env/max(abs(env))*max(abs(x));
subplot(3,1,3);
plot(th,x,th,env,th,-env);
axis([0 1.2 -1 1]);
