function dsp_aliasing_example
%
% Shows the PHASE effects of aliasing.
%
%   Output: dsp_aliasing_example.eps
%

%
% Copyright (c) 2008 by Theodore P. Pavlic
%
% This work is licensed under the Creative Commons
% Attribution-Noncommercial 3.0 United States License. To view a copy of
% this license, visit http://creativecommons.org/licenses/by-nc/3.0/us/
% or send a letter to Creative Commons, 171 Second Street, Suite 300,
% San Francisco, California, 94105, USA.
%

% Upper-case    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
% Lower-case    a b c d e f g h i j k l m n o p q r s t u v w x y z
% Digits        0 1 2 3 4 5 6 7 8 9
% Exclamation   !           Double quote "          Hash (number) #
% Dollar        $           Percent      %          Ampersand     &
% Acute accent  '           Left paren   (          Right paren   )
% Asterisk      *           Plus         +          Comma         ,
% Minus         -           Point        .          Solidus       /
% Colon         :           Semicolon    ;          Less than     <
% Equals        =           Greater than >          Question mark ?
% At            @           Left bracket [          Backslash     \
% Right bracket ]           Circumflex   ^          Underscore    _
% Grave accent  `           Left brace   {          Vertical bar  |
% Right brace   }           Tilde        ~

%%%%%%%%%%%%%%%%%%%%%%%%%%% Start of Script %%%%%%%%%%%%%%%%%%%%%%%%%%% 

%%%% Do sampling

% "Continuous-time" vector
t = 0:1/1000:10;

% Sample data at 10 Hz
ts = 0:1/10:10;

% Sample a 0.5 Hz, a 9.5 Hz, and a 10.5 Hz sine wave
ybaseband = sin(2*pi*0.5*ts);
yalias1 = sin(2*pi*9.5*ts);
yalias2 = sin(2*pi*10.5*ts);

% Sample the sum of 9.5 Hz and 10.5 Hz sine waves
ysum = yalias1 + yalias2;

% Find the continuous-time versions
ybasebandcont = sin(2*pi*0.5*t);
yalias1cont = sin(2*pi*9.5*t);
yalias2cont = sin(2*pi*10.5*t);
ysumcont = sin(2*pi*9.5*t) + sin(2*pi*10.5*t);

%%%% Plot results

figure(1); clf;

% "Continuous-time" version of 0.5 Hz
subplot(231);
h = plot( t, ybasebandcont, '-', ts, ybaseband, 'o' );
format_axis;
format_title( '0.5 Hz in continuous time' );
xlim( [ 0 0.5 ] ); ylim([-1 1]);
grid on;
set( gca, 'XTick', ts( ts <= 0.5 ), 'YTick', -1:1 );
legfig = legend( h(2), '10 S/s Samples', 'Location', 'SouthEast' );
set( legfig, 'Interpreter', 'latex' );

% "Continuous-time" version of 9.5 Hz
subplot(232);
h = plot( t, yalias1cont, '-', ts, yalias1, 'o' );
format_axis;
format_title( '9.5 Hz in continuous time' );
xlim( [ 0 0.5 ] ); ylim([-1 1]);
grid on;
set( gca, 'XTick', ts( ts <= 0.5 ), 'YTick', -1:1 );
legfig = legend( h(2), '10 S/s Samples' );
set( legfig, 'Interpreter', 'latex' );

% "Continuous-time" version of 10.5 Hz
subplot(233);
h = plot( t, yalias2cont, '-', ts, yalias2, 'o' );
format_axis;
format_title( '10.5 Hz in continuous time' );
xlim( [ 0 0.5 ] ); ylim([-1 1]);
grid on;
set( gca, 'XTick', ts( ts <= 0.5 ), 'YTick', -1:1 );
legfig = legend( h(2), '10 S/s Samples', 'Location', 'SouthEast' );
set( legfig, 'Interpreter', 'latex' );


% "Continuous-time" sum
subplot(212);
h = plot( t, ysumcont, '-', ts, ysum, 'o' );
format_axis;
format_title( '9.5 Hz + 10.5 Hz sine waves in continuous time' );
xlim( [ 0 2 ] ); ylim([-2 2]);
grid on;
set( gca, 'XTick', ts( ts <= 2 ), 'YTick', -2:2 );
legfig = legend( h(2), '10 S/s Samples' );
set( legfig, 'Interpreter', 'latex' );


%%%% Save plot

set( gcf, 'PaperType', 'usletter', ...
          'PaperOrientation', 'portrait', ...
          'PaperPosition', [0.25 2.5 7.75 4.75] );
saveas( gcf, 'dsp_aliasing_example.eps', 'epsc2' );

%%%% Helper functions

    % Sets up plotter axes
    function format_axis( axishandle )
        if nargin < 1
            axishandle = gca;
        end
        
        xlim( [0 5] );
        ylim( [-1 1] );
        
        xlabel( axishandle, 'Time (s)', 'Interpreter', 'latex' );
        
        set( gca, 'GridLineStyle', '-.' );
    end

    % Formatted title
    function format_title( axishandle, titlestr )
        
        if nargin < 2
            titlestr = axishandle;
            axishandle = gca;
        end
            
        title( titlestr, 'Interpreter', 'latex' );
    end

end

%%%%%%%%%%%%%%%%%%%%%%%%%%% End of Script %%%%%%%%%%%%%%%%%%%%%%%%%%% 
