%
% Pitch detection in a signal
%

% The Caruso signal does not exist anymore

%E = ReadSignal('Caruso');
%[clean,bb,st] = CPDeNoise(E,7,'Sine');
%sound(E)
%sound(clean)
%EE = E(25000:25511);
%save carpit EE

xx = (0:511)/511;
load carpit EE
sound(EE)

% coiflet filter + transform

QMF = MakeONFilter('Coiflet',5);
wc = FWT_PO( EE, 5, QMF);

clf;
figure(1); plot(xx,EE);xlabel('t'); hold on
axis([0,1,-20000,25000]); title('Speech signal');
figure(2), plot(wc);

% attempt to clean from noise
%wc(257:512)=0;
%for i=1:512
% if abs(wc(i))<5000
%	wc(i)=0;
% end
%end
%figure(4), plot(wc)

% plot the multiresolution decomposition

figure(3), PlotMultiRes(wc,5,0,QMF); hold on

%EC=IWT_PO(wc,5,QMF);
%figure(5), plot(xx,EC)

% find the pitch = local maxima above 11000

sd(1) = 0;
for i=2:length(EE)-1,
   sd(i) = EE(i-1)<EE(i) & EE(i) > EE(i+1) & EE(i) > 11000;
end
pp = find(sd);

figure(1),
for k = 1:length(pp)
 plot ([xx(pp(k)),xx(pp(k))],[-20000,25000],'r--');
end

hold off;
figure(3);
for k = 1:length(pp)
 plot ([xx(pp(k)),xx(pp(k))],[-20000,25000],'r--');
end
hold off;
