# how to determine piecewise constant function representing binary data

I like to define function which represent binary data, but i stuck in this rather simple task ... my NWME is:

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\pgfmathdeclarefunction{data}{2}{%
\pgfmathifthenelse{(or(\x<#1, \x>#2))}{1}{0}%
}
\begin{tikzpicture}
\draw [red,  thick] (0,0) --
plot [domain= 0:80, samples=160, smooth]  (\x,data(20,40));
\end{tikzpicture}
\end{document}


This example gives error: "giving up on this path. ..." It seems that I do not understand howto use pgfmath for drawing such a simple function.

Drawing of this data function by hand is simple, but latter i like add a noise to this function.

-

Here, I complete the excellent cmhuges's answer to show how to use the declare function key:

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}
\tikzset{declare function={
data(\a,\b,\x)=or(\x<\a, \x>\b)?1:0;
}}
\draw [red,thick] (0,0) -- plot [domain= 0:80,samples=160,smooth]  (\x,{data(20,40,\x)});
\end{tikzpicture}
\end{document}

-

Your pgfmath is ok, but you need to use

\draw [red,  thick] plot [domain= 0:80, samples=160, smooth]  (\x,{data(20,40)});


Note the {} in (\x,{data(20,40)}). Here's a complete MWE:

% arara: pdflatex
\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\pgfmathdeclarefunction{data}{2}{%
\pgfmathifthenelse{(or(\x<#1, \x>#2))}{1}{0}%
}
\begin{tikzpicture}
\draw [red,  thick] plot [domain= 0:80, samples=160, smooth]  (\x,{data(20,40)});
\end{tikzpicture}
\end{document}


As a side note, you might like to explore the pgfplots package (which is based on tikz) as it eases some of the notation.

-
Huh, how simple, if know how. this sketch i will add to block scheme of communication channel, so I like to stay with pure tikz. And one more question, I need function data different from picture to picture. Is possible to include it definition inside tikzpicture? – Zarko Jul 4 '14 at 11:11
@Zarko yes; see, for example, PGF Math Error: Could not parse input – cmhughes Jul 4 '14 at 12:28
thank you for the link. The similar solution also gives Poul in his answer. But now i thinking, how to combine pgfplots and pure tikz picture. Graph given in my MWE is actually inside callout node, which described signals in scheme. I will experiment in my first spare time. – Zarko Jul 4 '14 at 15:04