# add noise to piecewise constant function

Is it possible to add noise to the following function, which is piecewise constant?

Data:

time    energy
0       0
1       0
2       0
3       0
4       0
5       0
6       0
7       0
8       0
9       0
10      1.8
12      1.8
13      1.8
14      1.8
15      1.8
16      0.8
17      0.8
18      0.8
19      0.8
20      0.8
21      0.8
22      0.8
23      0.8
24      0.8
25      0.3
26      0.3
27      0.3
28      0.3
29      0.3
30      0.3
31      0.3
32      0.3
33      0.3
34      0.3
36      0.3
37      0.3
38      0.3
39      0.3
40      0.3
41      0.3
42      0.3
43      0.3
44      0.3
45      0.3
46      0.3


Code:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}

{energy.dat}
{\table}
\begin{tikzpicture}
\begin{axis}[
ymin=0,
minor tick num=4,
enlarge x limits=false,
axis on top,
every axis plot post/.append style=
{mark=none},
const plot,
legend style={
area legend,
at={(0.5,-0.15)},
anchor=north,
legend columns=-1}]
table[x=time,y=energy] from \table
\closedcycle;
\legend{energy}
\end{axis}
\end{tikzpicture}

\end{document}

-
A modification of the solutions to Adding vertical noise to a path should work here. – Andrew Swann Dec 3 '12 at 13:36

You can add some noise to the values of a table by setting y expr=\thisrow{energy}+0.1*rnd instead of y=energy (for example).

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots, pgfplotstable}

\begin{document}

{
time    energy
0       0
1       0
2       0
3       0
4       0
5       0
6       0
7       0
8       0
9       0
10      1.8
12      1.8
13      1.8
14      1.8
15      1.8
16      0.8
17      0.8
18      0.8
19      0.8
20      0.8
21      0.8
22      0.8
23      0.8
24      0.8
25      0.3
26      0.3
27      0.3
28      0.3
29      0.3
30      0.3
31      0.3
32      0.3
33      0.3
34      0.3
36      0.3
37      0.3
38      0.3
39      0.3
40      0.3
41      0.3
42      0.3
43      0.3
44      0.3
45      0.3
46      0.3
}
{\table}
\begin{tikzpicture}
\begin{axis}[
ymin=0,
minor tick num=4,
enlarge x limits=false,
axis on top,
every axis plot post/.append style=
{mark=none},
const plot,
legend style={
area legend,
at={(0.5,-0.15)},
anchor=north,
legend columns=-1}]