% RotatingPhasor - rotating phasor with sampling

% Parameters
fs = 25; % sampling rate in frames per second
dur= 20;  % signal duration in seconds

ff  = .5;  % frequency of rotating phasor (try 4.95, 5.05, 2.5)
phi = pi/4;  % initial phase of phasor
A   = 1;  % amplitude

% Prepare for plot
TitleString = sprintf('Rotating Phasor: f_d = %5.2f', ff/fs);
figure(1)

subplot(2,1,1)
axis('square')
axis([-A A -A A]);
title(TitleString)
xlabel('Real')
ylabel('Imag')

subplot(2,1,2)
axis([0 dur -A A])
xlabel('Time (s)')

cc = exp(i*2*pi*(0:0.01:1));
ccx = A*real(cc);
cci = A*imag(cc);

tti=[];
xxi=[];

for tt = 0:1/fs:dur
    tic; % establish time-reference
    
    % rotating phasor
    subplot(2,1,1)
    plot(ccx, cci, ':', ...
        [0 A*cos(2*pi*ff*tt+phi)], [0 A*sin(2*pi*ff*tt+phi)], '-ob');
    axis('square')
    axis([-A A -A A]);
    title(TitleString)
    xlabel('Real')
    ylabel('Imag')
    
    % plot sinusoid
    subplot(2,1,2)
    tti = [tti tt];
    xxi = [xxi A*cos(2*pi*ff*tt+phi)];
    plot(tti, xxi,':o');
    axis([0 dur -A A])
    xlabel('Time (s)')
    
    drawnow % force plots to be redrawn
    
    te = toc;
    
    % pause until the next sampling instant, if possible
    if ( te < 1/fs)
        pause(1/fs-te)
    end
end

