% freqinst2.m
%
% P. Flandrin

clf

Ns = 256; % nombre d'Žchantillons du signal
f0 = 0.01;
f1 = 8*f0;
a = .7;
sig = fmconst(Ns,f0);
sig1 = a*fmconst(Ns,f1);
sig(Ns/2:Ns) = sig(Ns/2:Ns) + a*sig1(Ns/2:Ns);

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))-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

