% freqinst1b.m
%
% P. Flandrin

clf

Ns = 200; % nombre d'Žchantillons du signal
f0 = 0.02;
df = 0.15;
sig1 = fmconst(Ns,f0);
[y,iflaw] = fmsin(Ns,f0-df,f0+df,2*Ns);
sig2 = y.*amgauss(Ns,Ns/2,10*sqrt(Ns));
sig = zeros(Ns,1);
sig(1:Ns/2) = sig1(1:Ns/2);
sig(Ns/2+1:Ns) = sig2(Ns/2+1:Ns);
sig = sig + hilbert(randn(size(sig)))/5;

dt = 1;
clear zr;
clear zi;
zr = real(sig(1));
zi = imag(sig(1));

fi = [f0 -diff(unwrap(angle(sig')))/(2*pi)];

subplot(2,1,1)
plot(real(sig))
amp = 1.2*max(max(real(sig)),abs(min(real(sig))));
axis([1 Ns -2*amp amp])
hold on
plot([1 Ns],[0 0])
title(['signal reel'])
hold off

pause

for k = 1:dt:Ns

	subplot(2,1,1)
	plot(real(sig))
	amp = 1.2*max(max(real(sig)),abs(min(real(sig))));
	axis([1 Ns -2*amp amp])
	hold on
	plot([k k],[real(sig(k)) real(sig(k))],'or')
	plot([k k],[0 real(sig(k))],'r')
	plot([1 Ns],[0 0])
	plot(abs(sig(1:k)),'r')
	plot(-abs(sig(1:k)),'r')
	plot(5*(fi(1:k)-fi(1))-1.5*amp,'k')
	title(['signal rŽel'])
	set(gca,'YTick',[]);set(gca,'XTick',[])
	hold off

	subplot(2,1,2)
	zr = [zr real(sig(k))];
	zi = [zi imag(sig(k))];
	z = zr + i*zi;
	plot(z)
	axis('square')
	axis([-amp amp -amp amp])
	hold on
	plot(z(max(size(z))),'or')
	plot(z(max(size(z))-1),'+r')
	plot([0 zr(max(size(z)))],[0 zi(max(size(z)))],'r')
	plot([0 zr(max(size(z))-1)],[0 zi(max(size(z))-1)],'r')
	plot([zr(max(size(z))-1) zr(max(size(z)))],[zi(max(size(z))-1) zi(max(size(z)))],'r')
	plot([-amp amp],[0 0]);
	plot([0 0],[-amp amp]);
	title('signal analytique')
	set(gca,'YTick',[]);set(gca,'XTick',[])
	hold off

	pause(1/100)

end

