How to Draw a Random Signal Shape in TikZ
At a glance
- 1. Choose signal levels
Select the integer range that defines the possible vertical values of the signal.
- 2. Seed the generator
Set a seed when you need the same random waveform on every compilation.
- 3. Build horizontal steps
Draw each interval at the current level, then generate a new level at its right endpoint.
- 4. Add axes and labels
Use ordinary TikZ nodes and lines to annotate the resulting waveform.
One simple way to represent a random signal in TikZ is as a piecewise-constant staircase: the signal remains at one level for a fixed time interval and then moves to a possibly equal randomly chosen level. TikZ can generate the levels during compilation with \pgfmathrandominteger.
The example below produces a signal with 12 intervals and integer amplitudes from to . It also includes a fixed random seed, so the result is reproducible.
Complete minimal example
\documentclass[tikz,border=5pt]{standalone}
\usepackage{tikz}
\begin{document}
% Use a fixed seed for a reproducible waveform.
\pgfmathsetseed{314159}
% Choose the initial signal level.
\pgfmathrandominteger{\level}{-2}{2}
\begin{tikzpicture}[x=0.75cm,y=0.8cm]
% Axes
\draw[->] (-0.3,0) -- (12.8,0) node[right] {$t$};
\draw[->] (0,-2.8) -- (0,2.8) node[above] {$s(t)$};
% Light reference lines
\draw[gray!25] (0,-2) -- (12,-2);
\draw[gray!25] (0,-1) -- (12,-1);
\draw[gray!25] (0,1) -- (12,1);
\draw[gray!25] (0,2) -- (12,2);
% Random staircase signal
\draw[very thick,blue]
(0,\level)
\foreach \x in {1,...,12} {
-- (\x,\level)
\pgfmathrandominteger{\nextlevel}{-2}{2}
\global\let\level\nextlevel
-- (\x,\level)
};
\end{tikzpicture}
\end{document}
This requires only the tikz package. Compile it with pdfLaTeX, LuaLaTeX, or XeLaTeX.
How the waveform is constructed
The command
\pgfmathrandominteger{\level}{-2}{2}
assigns a random integer between and to the macro \level. The limits are inclusive, so the possible values are .
The path starts at (0,\level). On each iteration of the \foreach loop, it does three things:
-- (\x,\level)draws a horizontal segment at the current level.\pgfmathrandominteger{\nextlevel}{-2}{2}chooses the next level.-- (\x,\level)draws the vertical transition to that new level.
The command
\global\let\level\nextlevel
makes the new value become the current value for the next loop iteration. The \global is useful here because each \foreach iteration has a local group; without it, the updated level may not survive until the next iteration.
The potential vertical transitions occur at , while the horizontal portions represent the signal between successive time samples.
Making the result reproducible
A random generator normally produces a different-looking sequence when its internal state changes. For figures in a paper or lecture notes, reproducibility is usually preferable. Set the seed before generating any values:
\pgfmathsetseed{314159}
Changing 314159 to another integer gives another sequence. Keeping the same seed gives the same sequence when the document is compiled under the same PGF/TikZ setup.
If reproducibility does not matter, omit \pgfmathsetseed. The waveform will still be generated by TikZ’s pseudo-random-number generator; it is not a source of cryptographically secure randomness.
Changing the number of samples and amplitude
The loop controls the number of intervals:
\foreach \x in {1,...,20} {
...
}
This creates 20 intervals instead of 12. The random range controls the amplitude:
\pgfmathrandominteger{\nextlevel}{-5}{5}
For a binary signal, use only two possible levels. One convenient method is to generate a random integer from to and transform it:
\pgfmathrandominteger{\bit}{0}{1}
\pgfmathtruncatemacro{\nextlevel}{2*\bit-1}
Here \nextlevel is either or .
Different interval widths
The example uses equally spaced samples. To give each random level a different duration, generate a duration as well as a level and use it when updating the horizontal coordinate. For example, the following loop generates candidate widths of one, two, or three units, but does not yet use them to position a path:
\foreach \x in {1,...,8} {
\pgfmathrandominteger{\duration}{1}{3}
\pgfmathrandominteger{\nextlevel}{-2}{2}
% A complete implementation must accumulate the current x-coordinate
% before drawing the next segment.
}
For variable widths, it is usually clearer to maintain the current horizontal coordinate in a macro and update it after every segment. The fixed-width version is less error-prone when the goal is simply to illustrate a random signal.
A continuous random-looking signal
A staircase is appropriate for sampled or digital data. If the desired drawing is a jagged continuous curve, use random points and connect them with straight segments. For example:
\documentclass[tikz,border=5pt]{standalone}
\usepackage{tikz}
\begin{document}
\pgfmathsetseed{271828}
\begin{tikzpicture}[x=0.7cm,y=0.8cm]
\draw[->] (-0.2,0) -- (12.5,0) node[right] {$t$};
\draw[->] (0,-3) -- (0,3) node[above] {$s(t)$};
\pgfmathrandominteger{\level}{-2}{2}
\draw[very thick,red] (0,\level)
\foreach \x in {1,...,12} {
\pgfmathrandominteger{\level}{-2}{2}
-- (\x,\level)
};
\end{tikzpicture}
\end{document}
This second version connects random samples diagonally, so it depicts a piecewise-linear signal rather than a zero-order-hold or staircase signal. Use the first version when the signal should remain constant between samples.
Common mistakes
Using plot[smooth] for a digital signal. Smoothing changes the shape and introduces values that were not part of the sampled signal. Use explicit horizontal and vertical segments for a staircase.
Forgetting the initial level. The path needs a starting coordinate such as (0,\level) before the loop begins.
Updating the wrong macro. Generate a new value in \nextlevel, then copy it to \level. This distinguishes the old level used for the horizontal segment from the new level used after the jump.
Expecting statistical independence from visual randomness. TikZ’s generator is useful for illustrations and test figures, but it is not intended for scientific random-number simulation. Generate experimental data externally when statistical properties matter.
Omitting the seed in a published figure. Without a fixed seed, a later compilation may not preserve the exact waveform. Record the seed alongside the source code if the figure must be regenerated exactly.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.