\typeout{ }
\typeout{This is psDSP, the LaTeX style file for most of the PStricks macros}
\typeout{ used in the book "Signal Processing for Communications"}
\typeout{ edited by PPUR-CRC.}
\typeout{by Paolo Prandoni, May 2007 }
\typeout{contact: paolo.prandoni@epfl.ch}
\typeout{ }


% required packages:
\usepackage{calc}
\usepackage{fp}
\usepackage{pst-xkey}
\usepackage{fmtcount}
\usepackage{ifthen}

% turn off FP messages
\FPmessagesfalse

\makeatletter

%% General plot sizes %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\psDSPlargeWidth{0.9\textwidth}
\def\psDSPlargeHeight{\psDSPlargeWidth * \real{0.42}}
\def\psDSPmediumWidth{\psDSPlargeWidth * \real{0.7}}
\def\psDSPmediumHeight{\psDSPmediumWidth * \real{0.5}}
\def\psDSPsmallWidth{\psDSPlargeWidth * \real{0.43}}
\def\psDSPsmallHeight{\psDSPsmallWidth * \real{0.7}}
\edef\MRspecHeight{2.5cm }

\def\psDSPctLineWidth{2pt}

%% Axis labels:
\newlength{\psDSPlabelSpaceX}\setlength{\psDSPlabelSpaceX}{6mm}
\newlength{\psDSPlabelSpaceY}\setlength{\psDSPlabelSpaceY}{4mm}
\newlength{\psDSPlabelSpaceF}\setlength{\psDSPlabelSpaceF}{6mm}
%\newlength{\psDSPzpSpacer}\setlength{\zpSpacer}{1mm}

%% Plot font (was {\fontfamily{phv}\selectfont\footnotesize} )
\newcommand{\selectPlotFont}{\footnotesize}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Discrete-time signal plots   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcounter{psDSPminN}
\newcounter{psDSPmaxN}
\newcounter{psDSPnumDtPts}
\newcounter{psDSPtmpInt}

\newlength{\psDSPunitX}
\newlength{\psDSPunitY}
\newlength{\psDSPtmpLen}
\newlength{\psDSPtmpLenY}

\newlength{\psDSPdotSize}
\newlength{\psDSPlineWidth}
\newlength{\psDSPwidth}
\newlength{\psDSPheight}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%% Keyvals for ALL psDSP plots %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%   size=["small" | "medium" | "large" | DIM]
%                Plot size; if a dimension is specified, it is taken as
%                 the WIDTH of the plot; height is 0.4 times the width
%
%   width, height
%                 Custom plot size
%
%   dx=N, dx=N
%               tick increments on the X and Y axis
%
%   labelx=["true" | "false"]
%   labely=["true" | "false"]
%               self-explanatory
%
%   labelnudge=DIM
%               nudge horizontal labels by DIM
%
\define@key{psDSP}{dx}{\def\Dx{#1}}
\define@key{psDSP}{dy}{\def\Dy{#1}}
\define@key{psDSP}{labelx}{\def\labelx{#1}}
\define@key{psDSP}{labely}{\def\labely{#1}}
\define@key{psDSP}{labelnudge}{\def\labelnudge{#1}}
\define@key{psDSP}{sidegap}{\def\sideGap{#1}}
\define@key{psDSP}{size}[large]{%
  \ifthenelse{\equal{#1}{small}}{%
    %% Small figure
    \setlength{\psDSPwidth}{\psDSPsmallWidth}%
    \setlength{\psDSPheight}{\psDSPsmallHeight}%
    \setlength{\psDSPdotSize}{3.8pt}%
    \setlength{\psDSPlineWidth}{1pt}}{%
    \ifthenelse{\equal{#1}{medium}}{%
      %% Medium figure
      \setlength{\psDSPwidth}{\psDSPmediumWidth}%
      \setlength{\psDSPheight}{\psDSPmediumHeight}%
      \setlength{\psDSPdotSize}{4.2pt} %{3.2pt}%
      \setlength{\psDSPlineWidth}{1.1pt}}{ %{1pt}}{%
      \ifthenelse{\equal{#1}{large}}{%
        %% Large figure
        \setlength{\psDSPwidth}{\psDSPlargeWidth}%
        \setlength{\psDSPheight}{\psDSPlargeHeight}%
        \setlength{\psDSPdotSize}{4.2pt}%
        \setlength{\psDSPlineWidth}{1.1pt}}{%
        %% Custom
        \setlength{\psDSPwidth}{#1}%
        \setlength{\psDSPheight}{0.4\psDSPwidth}%
        \setlength{\psDSPdotSize}{4.2pt}%
        \setlength{\psDSPlineWidth}{1pt}}}}}
\define@key{psDSP}{width}{%
    \setlength{\psDSPwidth}{#1}}
\define@key{psDSP}{height}{%
    \setlength{\psDSPheight}{#1}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%% Set up DISCRETE TIME plot %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% \begin{psDTplot}[OPTIONS]{MIN_N}{MAX_N}{MIN_Y}{MAX_Y}
%
%   MIN_N, MAX_N  range for the discrete-time index
%   MIN_Y, MAX_Y  range for the signal values
%
% Specialized options:
%   sidegap=N     extra space (in number of discrete-time points)
%                  added to the left and right of the n-axis;
%                  set to 0.5 for small ranges
%
%
%
\newenvironment{psDTplot}[5][]{%
  \def\psDSPminN{#2}\def\psDSPmaxN{#3}%
  \def\Dx{0}\def\Dy{0}%
  \presetkeys{psDSP}{size=large,%
                     labelx=true,labely=true,labelnudge=0pt,%
                     sidegap=2}{}%
  \setkeys{psDSP}{#1}%
%
  %% Axes range, increment and labels %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
  %% X-axis tick and label increment
  \FPifeq\Dx{0} % Unless set by keyval...
    \FPupn\N{#2 #3 - 1 + 6 swap /}%
    \FPtrunc\Dx\N{0}%
  \fi
%
  % trick to eliminate axis labels
  \ifthenelse{\equal{\labelx}{false}}{%
    \FPupn\Dx{#2 abs #3 abs max 2 *}}{}
  \ifthenelse{\equal{\labely}{false}}{%
    \FPupn\Dy{#4 abs #5 abs max 2 *}}{}
%
  %% X-axis total length
  \FPupn\minN{\sideGap{} #2 -}%
  \FPupn\maxN{\sideGap{} #3 +}%
  \FPupn\numPts{#2 #3 - \sideGap{} 2 * + }%
%
  %% Y-axis total length
  \FPupn\rangeY{#4 #5 -}%
  %% Y-axis tick and label increment
  \FPifeq\Dy{0}%
    \FPtrunc\tmpY\rangeY{0}%
    \FPupn\tmpY{4 \tmpY{} /}%
    \FPtrunc\Dy\tmpY{2}%
  \fi
%
  %% Units and limits %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
  \setlength{\psDSPunitX}%
    {1mm*\ratio{\psDSPwidth-\psDSPlabelSpaceY}{{\numPts}mm}} %
  \setlength{\psDSPunitY}%
    {1mm*\ratio{\psDSPheight-\psDSPlabelSpaceX}{{\rangeY}mm}} %
  \psset{xunit=\psDSPunitX, yunit=\psDSPunitY}%
%
  %% Make room for axis labels %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \def\u{\strip@pt\psDSPunitX}%
  \def\g{\strip@pt\psDSPlabelSpaceY}%
  \FPupn\leftLim{\u{} \g{} / \minN{} -}%
  \def\u{\strip@pt\psDSPunitY}%
  \def\g{\strip@pt\psDSPlabelSpaceX}%
  \FPupn\bottomLim{\u{} \g{} / #4 -}%
%
  % Do it! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \begin{pspicture}(\leftLim,\bottomLim)(\maxN,#5)%
    %% Font
    \def\pshlabel##1{\selectPlotFont ##1 \hspace{-\labelnudge}}%
    \def\psvlabel##1{\selectPlotFont ##1}%
    %% Box
    \psaxes[axesstyle=frame,%
            yAxis=false,%
            ticks=none]%
            (\minN,#4)(\maxN,#5)
    %% X-axis
    \psaxes[Dx=\Dx,%
            ticks=x, tickstyle=full, ticksize=-3pt,%
            labelsep=3pt,labelpos=45]%
            (0,0)(\minN,0)(\maxN,0)
    %% Y-axes
    \psaxes[Dy=\Dy,%
            ticks=y, ticksize=3pt,%
            showorigin=false]%
            (\minN,0)(\minN,#4)(\maxN,#5)
    \psaxes[Dy=\Dy, ticksize=-3pt, labels=x]%
            (\maxN,0)(\maxN,#4)(\maxN,#5) }
{
  \end{pspicture}
}
%
%% Keyvals for discrete-time plots
%
\define@key[psset]{psDSP@DTfun}{nmin}{\def\minN{#1}}
\define@key[psset]{psDSP@DTfun}{nmax}{\def\maxN{#1}}
\pst@addfams{psDSP@DTfun}
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%% Plot discrete-time point
%
% \psDTplotPoint[PSTRICKS_OPTIONS]{N}{Y}
%
%   N         index
%   Y         value
%
\newcommand{\psDTplotPoint}[3][]{%
  \listplot[plotstyle=LineToXAxis, linestyle=solid,%
            showpoints=true, dotstyle=*,%
            linewidth=\psDSPlineWidth,%
            dotsize=\psDSPdotSize,#1]{#2 #3 #2 #3}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%% Plot discrete-time data
%
% \psDTplotFunction[PSTRICKS_OPTIONS]{DATA}
%
%   DATA        space-separated index-value pairs
%
\newcommand{\psDTplotData}[1][]{%
  \listplot[plotstyle=LineToXAxis, linestyle=solid,%
            showpoints=true, dotstyle=*,%
            linewidth=\psDSPlineWidth,%
            dotsize=\psDSPdotSize,#1]}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%% Plot discrete-time signal
%
% \psDTplotSignal[OPTIONS]{SIGNAL}
%
%   SIGNAL        PostScript code for the signal
%
% Specialized options (besides PsTricks directives):
%   nmin=N,
%   nmax=N       range for the plotted signal
%
\newcommand{\psDTplotSignal}[2][]{%
  \presetkeys[psset]{psDSP@DTfun}{nmin=\psDSPminN, nmax=\psDSPmaxN}{}%
  \setkeys*[psset]{psDSP@DTfun}{#1}%
  \setcounter{psDSPnumDtPts}{\maxN - \minN + 1}
  \psplot[plotstyle=LineToXAxis, linestyle=solid,%
          showpoints=true, dotstyle=*,%
          linewidth=\psDSPlineWidth, dotsize=\psDSPdotSize,%
          plotpoints=\value{psDSPnumDtPts},
          #1]%
          {\minN}{\maxN}{#2}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%% Plot continuous-time function over a discrete-time plot
%
% \psDTplotFunction[OPTIONS]{SIGNAL}
%
%   SIGNAL        PostScript code for the signal
%
% Specialized options (besides PsTricks directives):
%   nmin=N,
%   nmax=N       range for the plotted signal
%
\newcommand{\psDTplotFunction}[2][]{%
  \presetkeys[psset]{psDSP@DTfun}{nmin=\psDSPminN, nmax=\psDSPmaxN}{}%
  \setkeys*[psset]{psDSP@DTfun}{#1}%
  \psplot[linewidth=\psDSPlineWidth,%
          plotpoints=1500,%
          #1]%
          {\minN}{\maxN}{#2}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%% Plot continuous-time function from a data file over a discrete-time plot
%
% \psDTplotFile[PSTRICKS_OPTIONS]{FILE}
%
\newcommand{\psDTplotFile}[2][]{%
  \readdata{\data}{#2}%
  \listplot[linewidth=\psDSPlineWidth,%
            plotstyle=line,%
            #1]{\data}}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%% Set up DIGITAL FREQUENCY plot %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% \begin{psDFplot}[OPTIONS]{MIN_Y}{MAX_Y}
%
%   MIN_Y, MAX_Y  range for the signal values
%
% Specialized options:
%
%   reps=N        number of 2pi intervals (default 1)
%
%   fmin=F,
%   fmax=F        frequency range (default +/- REPS * pi)
%
%   pifrac=N      pi/N is the frequency axis tick
%
\define@key{psDSP@DF}{reps}{\def\reps{#1}}
\define@key{psDSP@DF}{fmin}{\def\fmin{#1}}
\define@key{psDSP@DF}{fmax}{\def\fmax{#1}}
\define@key{psDSP@DF}{pifrac}{\def\pifrac{#1}}
%
%
\newenvironment{psDFplot}[3][]{%
  \def\Dy{0}%
  \presetkeys{psDSP}{size=large,labelx=true,labely=true}{}%
  \presetkeys{psDSP@DF}{reps=1,fmin=-1000,fmax=1000,pifrac=1}{}%
  \setkeys{psDSP,psDSP@DF}{#1}%
%
  %% Frequency range %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \FPupn\psDSPminF{\psPi{} \reps{} * -1 *}%
  \FPupn\psDSPmaxF{\psPi{} \reps{} *}%
  \FPifgt\psDSPmaxF\fmax\FPset\psDSPmaxF\fmax\fi%
  \FPiflt\psDSPminF\fmin\FPset\psDSPminF\fmin\fi%
  \FPupn\rangeF{\psDSPminF{} \psDSPmaxF{} -}%
%
  %% Vertical range %%%%%%%%%%%%%
  \def\psDSPminY{#2}
  \def\psDSPmaxY{#3}
%
  %% Radians units for x-axis
  \FPupn\ticksPerPi{\pifrac{} \psPi{} /}%
  \FPupn\minFticks{\ticksPerPi{} \psDSPminF{} /}%
  \FPupn\maxFticks{\ticksPerPi{} \psDSPmaxF{} /}%
%
  %% Y-axis total length
  \FPupn\rangeY{#2 #3 -}%
  %% Y-axis tick and label increment
  \FPifeq\Dy{0}%
    \FPtrunc\tmpY\rangeY{0}%
    \FPupn\tmpY{4 \tmpY{} /}%
    \FPtrunc\Dy\tmpY{2}%
  \fi%
  % trick to eliminate y-axis labels
  \ifthenelse{\equal{\labely}{false}}{%
    \FPupn\Dy{#2 abs #3 abs max 2 *}}{}
%
  %% Units %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \setlength{\psDSPunitX}%
    {1mm*\ratio{\psDSPwidth-\psDSPlabelSpaceY}{{\rangeF}mm}} %
  \setlength{\psDSPunitY}%
    {1mm*\ratio{\psDSPheight-\psDSPlabelSpaceF}{{\rangeY}mm}} %
  \psset{xunit=\psDSPunitX, yunit=\psDSPunitY}%
%
  %% Make room for axis labels %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \def\u{\strip@pt\psDSPunitX}%
  \def\g{\strip@pt\psDSPlabelSpaceY}%
  \FPupn\leftLim{\u{} \g{} / \psDSPminF{} -}%
  \def\u{\strip@pt\psDSPunitY}%
  \def\g{\strip@pt\psDSPlabelSpaceF}%
  \FPupn\bottomLim{\u{} \g{} / #2 -}%
%
  \begin{pspicture}(\leftLim,\bottomLim)(\psDSPmaxF,#3)%
    %%% Font %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \def\pshlabel##1{\selectPlotFont ##1}%
    \def\psvlabel##1{\selectPlotFont ##1}%
    %%% X-axis %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \ifthenelse{\equal{\labelx}{false}}{%
      \psline[xunit=\ticksPerPi](\minFticks,#2)(\maxFticks,#2)%
      \psline[xunit=\ticksPerPi](\minFticks,#3)(\maxFticks,#3)%
    }{%
      \FPmul\axisSpan\psDSPminF\psDSPmaxF%
      \FPifneg\axisSpan % frequency range is across zero
        \psaxes[xunit=\ticksPerPi,%
                trigLabels=true,trigLabelBase=\pifrac,%
                ticks=x, tickstyle=full, ticksize=-3pt,%
                labelsep=4pt]%
                  (0, #2)(\minFticks,#2)(\maxFticks,#2)%
        \psaxes[xunit=\ticksPerPi,%
                trigLabels=true,trigLabelBase=\pifrac,%
                ticks=x, tickstyle=full, ticksize=-3pt,%
                labels=y]%
                  (0, #3)(\minFticks,#3)(\maxFticks,#3)%
      \else % frequency range is on half-axis
         \psaxes[xunit=\ticksPerPi,%
                 trigLabels=true,trigLabelBase=\pifrac,%
                 ticks=x, tickstyle=full, ticksize=-3pt,%
                 labelsep=4pt]%
                  (\minFticks,#2)(\maxFticks,#2)%
        \psaxes[xunit=\ticksPerPi,%
                trigLabels=true,trigLabelBase=\pifrac,%
                ticks=x, tickstyle=full, ticksize=-3pt,%
                labels=y]%
                  (\minFticks,#3)(\maxFticks,#3)%
      \fi%
    }%
    %%% Y-axes %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \FPmul\axisSpan{#2}{#3}%
    \FPifneg\axisSpan%
      \psaxes[Dy=\Dy,ticks=y, ticksize=3pt]%
                (\psDSPminF,0)(\psDSPminF,#2)(\psDSPmaxF,#3)%
      \psaxes[Dy=\Dy, ticksize=-3pt, labels=x]%
                (\psDSPmaxF,0)(\psDSPmaxF,#2)(\psDSPmaxF,#3)%
    \else
      \psaxes[Dy=\Dy,Oy=#2,ticks=y,ticksize=3pt]%
                (\psDSPminF,#2)(\psDSPmaxF,#3)%
      \psaxes[Dy=\Dy, ticksize=-3pt, labels=x]%
                (\psDSPmaxF,#2)(\psDSPmaxF,#3)%
    \fi%
}
{
  \end{pspicture}
}
%
%% Plotting Keyvals %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\define@key[psset]{psDSP@DFfun}{fmin}{\def\minF{#1}}
\define@key[psset]{psDSP@DFfun}{fmax}{\def\maxF{#1}}
\pst@addfams{psDSP@DFfun}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%% Plot frequency delta
%
% \psDFplotDelta[PSTRICKS_OPTIONS]{F}{Y}
%
%   F         normalized frequency (1 <-> pi)
%   Y         value
%
\newcommand{\psDFplotDelta}[3][]{%
  \psline[xunit=3.14,linewidth=1.5pt,#1]{->}(#2,0)(#2,#3)}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%% Plot frequency data
%
% \psDFplotFunction[PSTRICKS_OPTIONS]{DATA}
%
%   DATA        space-separated normalized_frequency-value pairs
%
\newcommand{\psDFplotData}[1][]{%
  \psline[xunit=3.14,linewidth=2pt,#1]}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%% Plot frequency function
%
% \psDFplotFunction[OPTIONS]{SIGNAL}
%
%   SIGNAL        PostScript code for the signal
%
% Specialized options (besides PsTricks directives):
%   fmin=F,
%   fmax=F       frequency range for the plotted function
%
% NOTE: frequency is non-normalized!
%
\newcommand{\psDFplotFunction}[2][]{%
  \presetkeys[psset]{psDSP@DFfun}{fmin=\psDSPminF, fmax=\psDSPmaxF}{}%
  \setkeys*[psset]{psDSP@DFfun}{#1}%
  \psplot[linewidth=2pt,%
          plotpoints=1500,%
          #1]%
          {\minF}{\maxF}{#2}}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%% Plot normalized-frequency function from a data file
%
% \psDFplotFile[PSTRICKS_OPTIONS]{FILE}
%
\newcommand{\psDFplotFile}[2][]{%
  \readdata{\data}{#2}%
  \listplot[xunit=3.14,%
            linewidth=2pt,%
            %plotstyle=curve,%
            #1]{\data}}
%
%
\newcommand{\DFylabel}[1]{%
  \selectPlotFont%
  \FPupn\hlabpos{\psDSPminF{} 1.1 mul}%
  \FPupn\vlabpos{\psDSPminY{} \psDSPmaxY{} add 0.5 mul}%
  \rput[b]{90}(\hlabpos,\vlabpos){#1}}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%% Set up CONTINUOUS TIME plot
%
% \begin{psDTplot}[OPTIONS]{MIN_T}{MAX_T}{MIN_Y}{MAX_Y}
%
%   MIN_T, MAX_T  range for the time index
%   MIN_Y, MAX_Y  range for the signal values
%
% Specialized options:
%   sidegap=T     extra space added to the left and right of the x-axis;
%                  set to 0.5 for small ranges
%
%
\newenvironment{psCTplot}[5][]{%
  \def\psDSPminT{#2}\def\psDSPmaxT{#3}%
  \def\Dx{0}\def\Dy{0}%
  \presetkeys{psDSP}{size=large,labelx=true,labely=true,sidegap=0}{}%
  \setkeys{psDSP}{#1}%
%
  %% Axes range, increment and labels %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
  %% X-axis total length
  \FPupn\minT{\sideGap{} #2 -}%
  \FPupn\maxT{\sideGap{} #3 +}%
  \FPupn\rangeT{#2 #3 - \sideGap{} 2 * + }%
  %% X-axis tick and label increment
  \FPifeq\Dx{0} % Unless set by keyval...
    \FPupn\N{#2 #3 - 1 + 6 swap /}%
    \FPtrunc\Dx\N{0}%
  \fi
%
  %% Y-axis total length
  \FPupn\rangeY{#4 #5 -}%
  %% Y-axis tick and label increment
  \FPifeq\Dy{0}%
    \FPtrunc\tmpY\rangeY{0}%
    \FPupn\tmpY{4 \tmpY{} /}%
    \FPtrunc\Dy\tmpY{2}%
  \fi
%
  % trick to eliminate axis labels
  \ifthenelse{\equal{\labelx}{false}}{%
    \FPupn\Dx{#2 abs #3 abs max 2 *}}{}
  \ifthenelse{\equal{\labely}{false}}{%
    \FPupn\Dy{#4 abs #5 abs max 2 *}}{}
%
  %% Units %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \setlength{\psDSPunitX}%
    {1mm*\ratio{\psDSPwidth-\psDSPlabelSpaceY}{{\rangeT}mm}} %
  \setlength{\psDSPunitY}%
    {1mm*\ratio{\psDSPheight-\psDSPlabelSpaceX}{{\rangeY}mm}} %
  \psset{xunit=\psDSPunitX, yunit=\psDSPunitY}%
%
  %% Make room for axis labels %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \def\u{\strip@pt\psDSPunitX}%
  \def\g{\strip@pt\psDSPlabelSpaceY}%
  \FPupn\leftLim{\u{} \g{} / \minT{} -}%
  \def\u{\strip@pt\psDSPunitY}%
  \def\g{\strip@pt\psDSPlabelSpaceX}%
  \FPupn\bottomLim{\u{} \g{} / #4 -}%
%
  % Do it! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \begin{pspicture}(\leftLim,\bottomLim)(\maxT,#5)%
    %% Font
    \def\pshlabel##1{\selectPlotFont ##1}%
    \def\psvlabel##1{\selectPlotFont ##1}%
    %%% X-axis %
    \FPmul\axisSpan\minT\maxT%
    \FPifneg\axisSpan %  range is across zero
      \psaxes[Dx=\Dx, ticks=x, tickstyle=full, ticksize=-3pt,%
              labelsep=3pt]%
                (0, #4)(\minT,#4)(\maxT,#4)%
      \psaxes[Dx=\Dx, ticks=x, tickstyle=full, ticksize=-3pt,%
              labels=y]%
                (0, #5)(\minT,#5)(\maxT,#5)%
    \else %  range is on half-axis
       \psaxes[Dx=\Dx, ticks=x, tickstyle=full, ticksize=-3pt,%
               labelsep=3pt]%
                (\minT,#4)(\maxT,#4)%
      \psaxes[Dx=\Dx, ticks=x, tickstyle=full, ticksize=-3pt,%
              labels=y]%
                (\minT,#5)(\maxT,#5)%
    \fi%
    %%% Y-axes %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \FPmul\axisSpan{#4}{#5}%
    \FPifneg\axisSpan%
      \psaxes[Dy=\Dy,ticks=y, ticksize=3pt]%
                (\minT,0)(\minT,#4)(\maxT,#5)%
      \psaxes[Dy=\Dy, ticksize=-3pt, labels=x]%
                (\maxT,0)(\maxT,#4)(\maxT,#5)%
    \else
      \psaxes[Dy=\Dy,Oy=#4,ticks=y,ticksize=3pt]%
                (\minT,#4)(\maxT,#5)%
      \psaxes[Dy=\Dy, ticksize=-3pt, labels=x]%
                (\maxT,#4)(\maxT,#5)%
    \fi%
}
{
  \end{pspicture}
}
%
%% Keyvals for discrete-time plots
%
\define@key[psset]{psDSP@CTfun}{tmin}{\def\minT{#1}}
\define@key[psset]{psDSP@CTfun}{tmax}{\def\maxT{#1}}
\pst@addfams{psDSP@CTfun}
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%% Plot continuous-time delta
%
%
% \psDFplotDelta[PSTRICKS_OPTIONS]{F}{Y}
%
%   F         normalized frequency (1 <-> pi)
%   Y         value
%
\newcommand{\psCTplotDelta}[3][]{%
  \psline[linewidth=1.5pt,#1]{->}(#2,0)(#2,#3)}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%% Plot continuous-time data
%
% \psDTplotFunction[PSTRICKS_OPTIONS]{DATA}
%
%   DATA        space-separated index-value pairs
%
\newcommand{\psCTplotData}[1][]{%
  \listplot[linewidth=\psDSPctLineWidth,#1]}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%% Plot continuous-time function
%
% \psCTplotFunction[OPTIONS]{SIGNAL}
%
%   SIGNAL        PostScript code for the signal
%
% Specialized options (besides PsTricks directives):
%   tmin=T,
%   tmax=T       range for the plotted signal
%
\newcommand{\psCTplotFunction}[2][]{%
  \presetkeys[psset]{psDSP@CTfun}{tmin=\psDSPminT, tmax=\psDSPmaxT}{}%
  \setkeys*[psset]{psDSP@CTfun}{#1}%
  \psplot[linewidth=\psDSPctLineWidth,%
          plotpoints=1500,%
          #1]%
          {\minT}{\maxT}{#2}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%% Plot continuous-time function from a data file
%
% \psCTplotFile[PSTRICKS_OPTIONS]{FILE}
%
\newcommand{\psCTplotFile}[2][]{%
  \readdata{\data}{#2}%
  \listplot[linewidth=\psDSPctLineWidth,%
            plotstyle=line,%
            #1]{\data}}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%% Set up POLE-ZERO plot
%
% \begin{psPZplot}[OPTIONS]{MAX_X}
%
%   MAX_X         range for both axes
%
% Specialized options:
%
%   cirlce=["true" | "false"]
%                 draw unit circle
%
%   unit=TEXT     use TEXT as the label for x=1
%
\define@key{psDSP@PZ}{circle}{\def\pzCircle{#1}}
\define@key{psDSP@PZ}{unit}{\def\pzLabel{#1}}
%
\newcommand{\psDSP@PZsetup}{%
    %%% Font %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \def\pshlabel##1{\selectPlotFont ##1}%
    \def\psvlabel##1{\selectPlotFont ##1}%
    %%%% Box %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \psaxes[axesstyle=frame,%
            yAxis=false,%
            ticks=none]%
            (-\zpSize,-\zpSize)(\zpSize,\zpSize)
    \psaxes[labels=none]%
            (0,0)(-\zpSize,-\zpSize)(\zpSize,\zpSize)
    \ifthenelse{\equal{\pzCircle}{true}}{%
    \pscircle[linewidth=1.8pt](0,0){\psDSPunitX}}{}
%
    \uput[-135](\zpSize\psDSPunitX,0){\selectPlotFont Re}
    \uput[-45](0,\zpSize\psDSPunitX){\selectPlotFont Im}
    \uput[-45](\psDSPunitX,0){\selectPlotFont \pzLabel}%
}
%
\newenvironment{psPZplot}[2][]{%
  \def\zpSize{#2}
  \def\Dx{1}\def\Dy{1}%
  \presetkeys{psDSP}{size=large}{}%
  \presetkeys{psDSP@PZ}{circle=true,unit=1}{}%
  \setkeys{psDSP,psDSP@PZ}{#1}%
%
  %% Axes range, increment and labels %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %% axis total length
  \FPupn\rangeX{\zpSize{} 2 *}%
  %% axis tick and label increment
  \FPifeq\Dx{0}%
    \FPtrunc\tmpX\rangeX{0}%
    \FPupn\tmpX{4 \tmpX{} /}%
    \FPtrunc\Dx\tmpX{2}%
  \fi%
  \FPifeq\Dy{0}%
    \FPset\Dy\Dx%
  \fi
  %% Units %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \setlength{\psDSPunitX}%
    {1mm*\ratio{\psDSPheight}{{\rangeX}mm}} %
  \psset{xunit=\psDSPunitX, yunit=\psDSPunitX}%
%
  \begin{pspicture}(-\zpSize,-\zpSize)(\zpSize,\zpSize)%
    \psDSP@PZsetup%
}
{
  \end{pspicture}
}
%
%% Keyvals for pole-zero plots
%
\define@key[psset]{psDSP@PZ}{type}{\def\pzType{#1}}
\define@key[psset]{psDSP@PZ}{label}{\def\pzLabel{#1}}
\define@key[psset]{psDSP@PZ}{labelpos}{\def\pzLabelpos{#1}}
\define@key[psset]{psDSP@PZ}{roctype}{\def\pzROCtype{#1}}
\pst@addfams{psDSP@PZ}
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%% Plot pole-zero point
%
% \psPZplotPoint[OPTIONS]{RE}{IM}
%
%     RE, IM    coordinates
%
%   Options:
%
%     type = ["pole" | "zero"]    circle or cross (default: pole)
%
%     label=["false" | TEXT ]     the point's label (default: false)
%     labelpos = ANGLE            label position (default: 45)
%
\newcommand{\psPZplotPoint}[3][]{%
  \presetkeys[psset]{psDSP@PZ}{type=pole,label=false}{}%
  \setkeys*[psset]{psDSP@PZ}{#1}%
  \ifthenelse{\equal{\pzType}{zero}}{%
    \psdot[dotstyle=o, dotsize=1.4ex, #1](#2,#3)%
  }{%
    \psdot[dotstyle=+, dotsize=1.4ex, dotangle=45, #1](#2,#3)%
  }
  \ifthenelse{\equal{\pzLabel}{false}}{%
  }{%
    \selectPlotFont%
    \ifthenelse{\equal{\pzLabel}{true}}{%
      \uput[\pzLabelpos](#2,#3){(#2, #3)}%
    }{%
      \uput[\pzLabelpos](#2,#3){\pzLabel}%
    }%
  }%
}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%% Plot pole-zero ROC
%
% \psPZplotROC[OPTIONS]{RADIUS}
%
%     RRADIUS    ROC's radius
%
%   Options:
%
%     roctype = ["causal" | "anticausal"]    default: causal
%
\newcommand{\psPZplotROC}[2][]{%
  \presetkeys[psset]{psDSP@PZ}{roctype=causal}{}%
  \setkeys*[psset]{psDSP@PZ}{#1}%
  \ifthenelse{\equal{\pzROCtype}{causal}}{%
    \pscustom[fillstyle=vlines,%
              hatchcolor=lightgray,%
              linecolor=lightgray]{%
      \psarc(0,0){#2\psDSPunitX}{0}{360}%
      \psline(\zpSize,0)(\zpSize,-\zpSize)%
                        (-\zpSize,-\zpSize)(-\zpSize,\zpSize)%
                        (\zpSize,\zpSize)(\zpSize,0)}
    %%% redo axis (alas)
    \psDSP@PZsetup %
  }{% Anticausal ROC
    \pscircle[fillstyle=vlines,hatchcolor=lightgray]{#2\psDSPunitX}%
  }%
}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%% Set up BLOCK DIAGRAM plot
%
% \begin{psdrawBDmatrix}[OPTIONS]{X_SPACING}{Y_SPACING}
%
%   X_SPACING
%   Y_SPACING         vertical and horizontal spacing
%
% Once in Block diagram mode the following commands are available
%  (they all create nodes)
%
%  \BDadd                 adder
%  \BDmul                 multiplier
%  \BDdelay               z^{-1} delay block
%  \BDdelayN{N}           z^{-N} delay block
%  \BDfilter{text}        filter box
%  \BDfilterMulti{text}   multiline filter box
%  \BDsplit               splitting point (dot)
%  \BDupsmp{K}            upsampler by K
%  \BDdwsmp{K}            downsampler by K
%
\newenvironment{psdrawBDmatrix}[2]{%
  \def\BDwidth{1.2pt}
  \psset{xunit=1mm,yunit=1mm,linewidth=\BDwidth}
  \def\BDadd{[mnode=circle] +}%
  \def\BDsub{[mnode=circle] -}%
  \def\BDmul{[mnode=circle] $\times$}%
  \def\BDdelay{\psframebox[framesep=.3]{$z^{-1}$}}%
  \def\BDdelayN##1{\psframebox[framesep=.3]{$z^{-##1}$}}%
  \def\BDfilter##1{\psframebox[framesep=.3]{##1}}%
  \def\BDfilterMulti##1{\psframebox%
    {\begin{tabular}{c} ##1 \end{tabular}}}%
  \def\BDsplit{[mnode=dot,linewidth=2pt]}%
  \def\BDupsmp##1{[mnode=circle] $##1 \uparrow$}%
  \def\BDdwsmp##1{[mnode=circle] $##1 \downarrow$}%
  \begin{psmatrix}[colsep=#1,rowsep=#2,emnode=R] }{%
  \end{psmatrix}
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%% Set up QAM constellation plot
%
% \begin{psQAMplot}[OPTIONS]{SIZE}
%
%   SIZE         number of points on the positive axis
%
% Specialized options:
%   decreg=[true|FALSE]     draw decision regions
%   labels=[all|NONE]       draw axis labels
%
%
\define@key{psDSP@QAM}{decreg}{\def\qamDecReg{#1}}
\define@key{psDSP@QAM}{labels}{\def\qamLabels{#1}}
%
\newenvironment{psQAMplot}[2][]{%
  \def\qamSize{#2}
  \def\Dx{1}\def\Dy{1}%
  \def\qamNoiseScale{0.2 }
  \def\psQAMdotSize{3pt}
  \presetkeys{psDSP}{size=large}{}%
  \presetkeys{psDSP@QAM}{decreg=false,labels=none}{}%
  \setkeys{psDSP,psDSP@QAM}{#1}%
%
  %% Axes range, increment and labels %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %% axis total length
  \FPupn\rangeX{\qamSize{} 2 *}%
  \FPclip\rangeX\rangeX
  %% Units %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \FPmul\extRange\rangeX{1.1}%
  \setlength{\psDSPunitX}%
    {0.5mm*\ratio{\psDSPheight}{{\extRange}mm}} %
  \psset{xunit=\psDSPunitX, yunit=\psDSPunitX}%

%
  \begin{pspicture}(-\extRange,-\extRange)(\extRange,\extRange)%
    %%% Font %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \def\pshlabel##1{\selectPlotFont ##1}%
    \def\psvlabel##1{\selectPlotFont ##1}%
    %%%% Box %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \psaxes[ticks=\qamLabels,linewidth=1.2pt]{->}%
            (0,0)(-\extRange,-\extRange)(\extRange,\extRange)
    \uput[-90](\extRange,0){\selectPlotFont Re}
    \uput[0](0,\extRange){\selectPlotFont Im}
    \ifthenelse{\equal{\qamDecReg}{true}}{%
      \FPsub\numRep\qamSize{1}%
      \FPclip\numRep\numRep%
      \multips(2,-\rangeX)(2,0){\numRep}{%
        \psline[linewidth=0.5pt](0,0)(! 0 2 \rangeX.0 mul)}%
      \multips(-2,-\rangeX)(-2,0){\numRep}{%
        \psline[linewidth=0.5pt](0,0)(! 0 2 \rangeX.0 mul)}%
      \multips(-\rangeX,2)(0,2){\numRep}{%
        \psline[linewidth=0.5pt](0,0)(! 2 \rangeX.0 mul 0)}%
      \multips(-\rangeX,-2)(0,-2){\numRep}{%
        \psline[linewidth=0.5pt](0,0)(! 2 \rangeX.0 mul 0)}%
    }{}
}
{
  \end{pspicture}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%% Plot QAM dot at given coordinates
%
\newcommand{\psQAMdot}[3][]{%
  \qdisk(#2,#3){0.5\psDSPdotSize}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%% Plot QAM point at given coordinates (Specify the type of point)
%
\newcommand{\psQAMpoint}[3][asterisk]{%
  \psdot[dotstyle=#1,dotsize=\psDSPdotSize](#2,#3)
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%% Plot NOISY QAM dot at given coordinates
%
% Specify point type. To change amount of noise deviation change the
% value of \qamNoiseScale
%
\newcommand{\psQAMnoisyPoint}[3][asterisk]{%
  \psdot[dotstyle=#1,dotsize=\psDSPdotSize](! #2 rand 100 mod 50 div 1 sub \qamNoiseScale mul add #3 rand 100 mod 50 div 1 sub \qamNoiseScale mul add)
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%% Plot entire QAM constellation
%
% Specify point drawing function and point type.
%
\newcommand{\psQAMconstellation}[2][asterisk]{%
  \FPupn\st{1 \qamSize{} 2 * - clip}%
  \multido{\ix=-\st+2}{\rangeX}{%
    \multido{\iy=-\st+2}{\rangeX}{%
      #2[#1]{\ix}{\iy}}}
}




\newcommand{\pstick}[2][0]{%
  \psline[linewidth=0.5pt,origin={#2,#1}](0,-4pt)}
\newcommand{\psticklab}[3][0]{%
  \psline[linewidth=0.5pt,origin={#2,#1}](0,-4pt)
  \uput{8pt}[-90](#2,#1){\selectPlotFont #3}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% PsTricks overrides
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Lollipop line style %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\let\beginplot@LineToXAxis\beginplot@line
\def\endplot@LineToXAxis{\psLineToXAxis@ii}
\let\beginqp@LineToXAxis\beginqp@line
\let\doqp@LineToXAxis\doqp@line
\let\endqp@LineToXAxis\endqp@line
\let\testqp@LineToXAxis\testqp@line
%
\def\psLineToXAxis@ii{%
\addto@pscode{\pst@cp \psline@iii \tx@LineToXAxis}%
\end@OpenObj}
%
\def\tx@LineToXAxis{LineToXAxis }
%
% Adapted from Line
\pst@def{LineToXAxis}<{%
NArray
n 0 eq not
  { n 1 eq { 0 0 /n 2 def } if
    ArrowA
    /n n 2 sub def
    CP 2 copy moveto pop 0 Lineto
    n { 2 copy moveto pop 0 Lineto } repeat
    CP
    4 2 roll
    ArrowB
    2 copy moveto pop 0
    L
    pop pop } if}>

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% triglabels font modification %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\define@key[psset]{pstricks-add}{trigLabelBase}%
  {\pst@getint{#1}{\psk@trigLabelBase}}
\psset{trigLabelBase=0}

\define@boolkey[psset]{pstricks-add}[Pst@]{trigLabels}[true]{%
\selectPlotFont
  \ifPst@trigLabels
    \def\pst@@@hlabel##1{\pshlabel{##1}}
    \def\pshlabel##1{%
      \ifnum\psk@trigLabelBase<2
        \def\de@nominator{\@ne}\else\def\de@nominator{\psk@trigLabelBase}\fi
      \def\pst@tempA{##1}
      \pst@abs{\pst@tempA}\pst@cntm
      \pst@mod{\pst@cntm}{\de@nominator}\pst@cntp % cntb=##1 modulo trigLabelBase
      \ifnum\@ne>\pst@cntp                  % 1 > modulo -> then we have pi/x
        \pst@cnto=\pst@cntm \divide\pst@cnto by \de@nominator
        %\psk@labelFontSize
  \ifnum\pst@tempA<0 -\fi%
        \ifnum\pst@cnto=\@ne                % #1 = trigLabelBase
          $\pi$                             % print pi
        \else\ifnum\pst@cnto=0 0
          \else
            \the\pst@cnto$\pi$              % print \pst@cnto/\de@nominator pi
          \fi
        \fi
      \else
        %\psk@labelFontSize
        \ifnum\pst@cntp=\@ne                % < 1 pi?
          \if\pst@cntm=\@ne
            $\pi / \de@nominator$   % pi/x
          \else\ifnum\pst@tempA=-1 -$\pi / \de@nominator$
             \else \ifnum\pst@tempA=1 $\pi / \de@nominator$
                 \else{\pst@tempA$\pi / \de@nominator$}  % (x pi)/y
          \fi\fi\fi
        \else
          \ifnum\pst@tempA=1 $\pi / \de@nominator$
          \else\ifnum\pst@tempA=\de@nominator $\pi$
            \else{\pst@tempA$\pi/\de@nominator$}
      \fi\fi\fi\fi%
    }%
  \else
    \def\pst@@@hlabel##1{%
      \edef\@xyDecimals{\psk@xDecimals}%
      \ifnum\psk@ticks<\tw@ % ticks=all|x
        \ifx\psk@xlogBase\@empty
          \pshlabel{\psk@labelFontSize\expandafter\@LabelComma##1..\@nil\psk@xlabelFactor}%
        \else%
          \pshlabel{\psk@labelFontSize\psk@xlogBase\textsuperscript{\expandafter\@stripDecimals##1..\@nil }}
        \fi%
      \fi%
    }
    \def\pshlabel##1{\psk@labelFontSize$##1$}%
  \fi
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\psxpoint}[3][black]{%
  \psline[linewidth=.5pt]{-}(#2,\pst@xticksizeB)(#2,\pst@xticksizeA)
  \rput[t](! #2 \pst@number\pslabelsep \pst@number\pst@xticksizeB add
  \pst@number\psyunit div neg ){\color{#1}#3}}

\makeatother 