%% This is the package dspblocks
%%
%% (c) Paolo Prandoni <paolo.prandoni _at_ epfl.ch>
%%
%% This program can be redistributed and/or modified under the terms
%% of the LaTeX Project Public License Distributed from CTAN archives
%% in directory macros/latex/base/lppl.txt.
%%
%% DESCRIPTION:
%%   `dspblocks' is a companion package to dsptricks; it contains a
%%     set of macros to draw the types of block diagrams commonly
%%     used in DSP theory
%%
%% v1.1, November 2023
%%

% required packages:
\RequirePackage{calc}
\RequirePackage{fp}
\RequirePackage{pst-xkey}
\RequirePackage{fmtcount}
\RequirePackage{ifthen}
%
\ProvidesPackage{dspblocks}[2023/11/08 package for signal processing graphics]


% turn off FP messages
\FPmessagesfalse

\makeatletter

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

\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=1.5pt]}%
\def\BDupsmp#1{[mnode=circle] $#1 \uparrow$}%
\def\BDdwsmp#1{[mnode=circle] $#1 \downarrow$}%
\def\BDsampler{\raisebox{-14.6pt}{
        \psset{xunit=1em,yunit=1em,linewidth=1.2pt}%
        \pspicture(-2.6,-1.8)(1,1.8)%
          \psline(-3,0)(-1.6,0)(0.7,1.2)
          \psline(0.8,0)(1,0)
          \psarc[linewidth=1pt]{<-}(-1.2,0){1.4em}{-10}{55}
        \endpspicture}}
\newcommand{\BDsamplerFramed}[1][1em]{
  \newskip\tmpLen \tmpLen=#1%
  \raisebox{-1.5\tmpLen}{%
    \psframebox[linewidth=1.2pt]{%
      \psset{unit=#1,linearc=0}%
      \pspicture(-3.1,-2)(3,2)%
      \psline(-2.8,-0.2)(-1.6,-0.2)(1.2,1.4)%
      \psline(1.1,-0.2)(1.8,-0.2)%
      \psarc[linewidth=1pt]{<-}(-1.6,0){2}{-10}{55}%
      \endpspicture}}}
\newcommand{\BDlowpass}[1][1em]{%
  \newskip\tmpLen \tmpLen=#1%
  \raisebox{-1.5\tmpLen}{%
    \psframebox[linewidth=1.2pt]{%
      \psset{unit=#1,linearc=0}%
      \pspicture(-3.1,-2)(3,2)%
      \psline{->}(-2.8,-1)(2.8,-1)%
      \psline{->}(0,-1.8)(0,2)%
      \psline[linewidth=1.5pt](-1,-1)(-1,0.8)(1,0.8)(1,-1)%
      \endpspicture}}}
\newcommand{\BDsinc}[1][1em]{%
  \newskip\tmpLen \tmpLen=#1%
  \raisebox{-1.5\tmpLen}{%
    \psframebox[linewidth=1.2pt]{%
      \psset{unit=#1,linearc=0}%
      \pspicture(-3.1,-2)(3,2)%
      \psline{->}(-2.8,-.5)(2.8,-.5)%
      \psline{->}(0,-1.8)(0,2)%
      \psplot[linewidth=1pt, plotpoints=500,linejoin=1]{-2.5}{2.5}{x \dspSinc{0}{0.5} 1.5 mul .5 sub}%
      \endpspicture}}}
\newcommand{\BDclip}[1][1em]{
  \newskip\tmpLen \tmpLen=#1%
  \raisebox{-1.5\tmpLen}{%
    \psframebox[linewidth=1.2pt]{%
      \psset{unit=#1,linearc=0,linewidth=2pt}%
      \pspicture(-3,-2)(3,2)%
      \psline[linecolor=gray](-2,-0)(2,-0)%
      \psline(-1.5,-1.7)(0,-1.7)(0,1.7)(1.5,1.7)%
      \endpspicture}}}

\newenvironment{dspBlocks}[2]{%
  \begin{psmatrix}[colsep=#1,rowsep=#2,emnode=R] }{%
  \end{psmatrix}
}

\newcommand{\BDConnH}[5][]{\ncline[#1]{->}{#2,#3}{#2,#4}\taput{#5}}
\newcommand{\BDConnHNext}[2]{\FPupn\n{#2 1 add 0 trunc}\ncline{->}{#1,#2}{#1,\n}}
\newcommand{\BDConnV}[5][]{\ncline[#1]{->}{#2,#3}{#4,#3}\tlput{#5}}

\makeatother 
.
