% nyquistRate

% sampling rate of our "analog" signal (cycles/s = Hz)
fan = 12600; % designed to have many factors
% time (s)
t = 0:1/fan:100;
t = t(t~=100);

fsam = 0.5;

%% The loop
shg
while true
for fsig = 0:0.01:0.75
    %% sampling rate (samples/s = Hz)
%      fsam = 0.8;
    %% Create the signal
    % frequency (cycles/s = Hz)
    % voltage (V)
    x = cos(2*pi*fsig*t);

    %% Sample the signal
    off = 0;
    % off = fan/fsam/2;
    range = 1+off:fan/fsam:fan*10;
%     if(max(abs(round(range)-range)) < 0.01)
        range = round(range);
%     else
%         error('not a multiple')
%     end
    samples = x(range);
    sampleTimes = t(range);

    %% "reconstruct" the signal
    frecon = fsig;
    if 2*fsig > 3*fsam
        error('jay:unsupportedFreq','simulations does not work at this frequency.')
    elseif 2*fsig > 2*fsam
        frecon = (fsam - (2*fsig - fsam))/2; % "reflection" in frequency domain        
    elseif 2*fsig > fsam
        frecon = (2*fsig - 2*fsam)/2; % "reflection" in frequency domain
    end
    xrecon = cos(2*pi*frecon*t); % uses same t as x
    %% Plot original signal and samples
    plot(t,x,'k-');
    hold on
    plot(sampleTimes,samples,'bo');
    plot(t,xrecon,'r--');
    hold off

    xlim([-1 11])
    ylim([-1.1 1.1])
    pause(0.1)
end
end