% Demo 1: Periodicity of DT signals
% 08/27/03 M. Haenggi
%
for w0=2*pi./[3 4 5 2.5 3.5 4.5 2.25 3.25 4.25 2.1 3.1 4.1 pi] % CT (angular) frequency [rad/s]
    f=w0/(2*pi) % frequency (cycles/s) [Hz]
    %
    % CT signal:
    t=0:0.01:20;
    xct=sin(w0*t);
    plot(t,xct,'--'); % use plot for CT signal
    grid on
    hold on % to ensure stem doesn't erase CT plot
    %
    % DT signal:
    n=0:20;
    xdt=sin(w0*n);
    stem(n,xdt,'r'); % use stem for DT signal (red)
    %
    % Determine period by converting f to a rational number m/N:
    ratio_s=maple('convert',f,'rational') % invoke maple to convert f to rational number
    ratio=sscanf(ratio_s,'%f/%f'); % extract numerator/denominator from maple string
    m=ratio(1) % numerator
    N=ratio(2) % denominator
    dt_period=N; % same as m/f
    %
    % convert to strings for display in graph title:
    ct_period_s=sprintf('%5.2f',1/f); 
    dt_period_s=sprintf('%2.0f',dt_period); % must be an integer
    freq_s=sprintf('%5.3f',f);
    dt_freq_s=sprintf('%5.3f',1/N);
    ct_s=['CT: \omega_0=' freq_s '*2\pi, Period T=' ct_period_s ' ;    '];
    dt_s=['DT: Period N=' dt_period_s ', \omega_0/m=' dt_freq_s '*2\pi'];
    title([ct_s  dt_s],'FontSize',16);
    xlabel('n','FontSize',16);
    ylabel('x[n]','FontSize',16);
    set(gca,'FontSize',14);
    hold off
    pause
end
