function plot_tri_sample( fs, shiftint )
% PLOT_TRI_SAMPLE Plots reconstructed 1 Hz triangle sampled at fs.
%   The frequency fs must be an INTEGER. The result will be a sinc
%   interpolated reconstruction of the signal.
%
%     plot_tri_sample( fs )
%     plot_tri_sample( fs, shiftint )
%
%   To shift the triangle waveform's phase, adjust shiftint. Otherwise,
%   it can be omitted (shiftint = Inf by default).
%
%   This script uses the INTERPFT function, but it can be made to
%   use FFT and IFFT directly without much modification.
%
%   Script also plots a ZOH+filter reconstruction that uses LSIM.

%
% 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 %%%%%%%%%%%%%%%%%%%%%%%%%%% 

    persistent fontsize
    fontsize = 14;

    if( nargin < 2 )
        % Specifies phase shift for triangle wave
        shiftint = Inf;     % (set to Inf for no shift)
    end
        
    % One period of basic triangle wave
    triangle = -4*triang( fs*1000 )' + 2;
    
    % Give two periods and play with phase
    triangle = [ triangle(floor(end/shiftint+1):end) ...
                 triangle ...
                 triangle(1:floor(end/shiftint)) ];
    
    % Sample at the deisred frequency
    striwave = downsample( triangle, 1000 );
    ts = -1:1/fs:(1-1/fs);         

    %%% Ideal Sinc Interpolation Plot
    subplot(121);
    
    t = (-1):1/(fs*1000):(1-1/(fs*1000));
    triinterp = interpft( striwave, length(ts)*1000 );
    
    plot( t, triangle, '--', t, triinterp, '-', ts, striwave, 'o' );
    
    xlim([-1 1]);
    ylim([-2 2]);
    
    % Setup grid to show sampling
    grid on;
    set( gca, 'XTick', [ ts 1 ], 'YTick', [-2 0 2] );
    xlabels = cell( 1, length(ts)+1 );
    xlabels{1} = -1;
    xlabels{fs+1} = 0;
    xlabels{end} = 1;
    set( gca, 'XTickLabel', xlabels );

    % Title and Label
    xlabel( 'Time (s)', 'Interpreter', 'latex', 'FontSize', fontsize );
    title([ '\quad ' num2str(fs) ' S/s Ideal Reconstruction' ], ...
            'Interpreter', 'latex', 'FontSize', fontsize );
        
        
    %%% ZOH Plot
    subplot(122);
    
    % There must be a better way to do this...
    triup = zeros(1, length(striwave)*1000 );
    for i = 1:length(striwave)
        triup( 1, ((i-1)*1000+1):(i*1000) ) = striwave(i)*ones(1,1000);
    end
    
    % Very simple second-order filter 
    sys = tf( [1], [1/(pi*fs) 1] );
    sys = sys*sys;
    % (subtract min(t) to keep lsim from complaining)
    triupfilt = lsim( sys, triup, t - min(t) ); 

    % Delay from CT filter makes it impractical to plot original
    % waveform
    %plot( t, triangle, '--', t, triup, '-', t, triupfilt, '-' );
    plot( t, triup, '-', t, triupfilt, '-' );

    xlim([-1 1]);
    ylim([-2 2]);
    
    % Setup grid to show sampling
    grid on;
    set( gca, 'XTick', [ ts 1 ], 'YTick', [-2 0 2] );
    xlabels = cell( 1, length(ts)+1 );
    xlabels{1} = -1;
    xlabels{fs+1} = 0;
    xlabels{end} = 1;
    set( gca, 'XTickLabel', xlabels );

    % Title
    xlabel( 'Time (s)', 'Interpreter', 'latex', 'FontSize', fontsize );
    title([ '\qquad ' num2str(fs) ' S/s ZOH+Filter Reconstruction' ], ...
            'Interpreter', 'latex', 'FontSize', fontsize );
    
end %of plot_tri_sample

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