function sc_hilbert(s);

% sc_hilbert(s);
% demo for the analytic signal
% s is either:
% 'cosine' plain cosine function
% 'AM' cosine with amplitude modulation
% 'FM' cosine with frequency modulation
% 'AM-FM' both
% 'wideband' well, not narrowband - Hilbert does not work well
% hitting a key displays the next sample

t = 0:9999;
switch s,
    case 'cosine',
        f=sqrt(2)*0.0001;
        x=cos(2*pi*f*t);
        y=sin(2*pi*f*t);
        Yl = [-1.5 1.5];
    case 'AM',
        f=sqrt(2)*0.0003;
        amp = 1+0.5*sin(2*pi*0.00005*t);
        x=cos(2*pi*f*t).*amp;
        y=sin(2*pi*f*t).*amp;
        Yl = [-1.5 1.5];
    case 'FM',
        f=sqrt(2)*0.0001;
        x=cos(2*pi*(f+4*1e-8*t).*t);
        y=sin(2*pi*(f+4*1e-8*t).*t);
        Yl = [-1.5 1.5];
    case 'AM-FM',
        f=sqrt(2)*0.0001;
        amp = 1+0.5*sin(2*pi*0.00005*t);
        x=cos(2*pi*(f+4*1e-8*t).*t).*amp;
        y=sin(2*pi*(f+4*1e-8*t).*t).*amp;
        Yl = [-1.5 1.5];
    case 'wideband',
        f=sqrt(2)*0.0002;
        x=0.5*cos(2*pi*f*t)+2*sin(2*pi*0.00001*t);
        %x=0.75*cos(2*pi*f*t)+0.75*cos(2*pi*sqrt(5)*0.0003*t)
        y=imag(hilbert(x));
        y(1) = 0.5;
        Yl = [-3 3];
end

subplot('Position',[0.3 0.1 0.5 0.6])
seg1 = [0 x(1)];
seg2 = [0 y(1)];
plot(seg1,seg2)
set(gca,'Xlim',[-2 2])
set(gca,'Ylim',[-2 2])
axis('square')
hold on
plot(0,0,'o','MarkerFaceColor','b','MarkerSize',6)
plot(x(1),y(1),'o','MarkerFaceColor','r','MarkerSize',6)
subplot('Position',[0.3 0.75 0.5 0.12])
plot(t(1),x(1),'o','MarkerFaceColor','r','MarkerSize',6)
set(gca,'Xlim',[0 10000])
set(gca,'Ylim',Yl)
hold on

for k=1:20,
     pause
    Ind = (k-1)*500+1:k*500;
    subplot('Position',[0.3 0.1 0.5 0.6])
    plot(seg1,seg2,'w')
    plot(x((k-1)*500+1),y((k-1)*500+1),'o','MarkerFaceColor','r','MarkerSize',6)
    seg1 = [0 x(k*500)];
    seg2 = [0 y(k*500)];
    plot(seg1,seg2) 
    plot(0,0,'o','MarkerFaceColor','b','MarkerSize',6)
    plot(x(Ind),y(Ind))
    plot(x(k*500),y(k*500),'o','MarkerFaceColor','r','MarkerSize',6)
    subplot('Position',[0.3 0.75 0.5 0.12])
    plot(t(Ind),x(Ind))
    plot(t(k*500),x(k*500),'o','MarkerFaceColor','r','MarkerSize',6)
end
subplot('Position',[0.3 0.1 0.5 0.6])
hold off
subplot('Position',[0.3 0.75 0.5 0.12])     
hold off

% plot(x(1:end-1),x(2:end),'.b')
% hold on
% plot(x(t-1),x(t),'.r')
% subplot('Position',[0.3 0.75 0.5 0.12])
% plot(t,x(t),'*')
% set(gca,'Xlim',[orig-1 orig+11]);
% set(gca,'Ylim',[-5 5])
