GnuPlot: Frequency Spectrum

In the web pages on a sine wave and advanced sine wave, we plotted a mathematical function; in this example, we'll plot a set of data that is stored in a separate file.

We start off in the usual way. Next, we turn off the upper and right-hand borders with the set border command, which uses a binary set of flags (bits 0 and 1 refer to the bottom and left flags).

There are 21 samples in our data set with x ranging from 0 to 20. For the tickmarks on the X axis, we don't want them to appear on the top because we've turned that border off. We turn off the top axis tickmarks using the nomirror modifier. We don't want any tickmarks on the Y axis so we unset them.

Plotting the Spectrum with Impulses

For the first graph, we plot three curves, all of them referring to the same data set according to the using modifier. The pair of numbers indicates the columns to read from a file called spectrum.tsv (tab separated values). The first number is the X axis, which should come from column one of the data set, and the second number states that the Y axis should be read from column two.

The first curve is actually a set of impulses which means vertical lines. The second curve is a series of asterisks with a colour given by an RGB value; in this case pure red. The final curve provides a smoothed envelope for the impulses. The result of this plot is shown in Figure 3a.


# Clear up any existing plots.
clear
# Reset all variables to default values.
reset

# We don't need a key.
set key off

# Draw only the left-hand and bottom borders.
set border 3

# There are 21 sample points.
set xrange [0:20]

# Show tickmarks at increments of one, and don't show (mirror) them
# at the top of the graph.
set xtic 1 nomirror

# The largest value in the data set is 11.
set yrange [0:11]
# Don't show any tickmarks on the Y axis.
unset ytics

# Make some suitable labels.
set title "Frequency spectrum"
set xlabel "Frequency"
set ylabel "Power"

# Draw three curves in this graph.
plot "spectrum.tsv" using 1:2 with impulses lt 1, \
	"spectrum.tsv" using 1:2 with points pt 3 lc rgb "#FF0000", \
	"spectrum.tsv" using 1:2 smooth csplines lt 2

Figure 3a: Smooth frequency spectrum

Figure 3a: Smooth frequency spectrum

Plotting the Spectrum with Boxes

If we want to plot the same data using rectangular columns (like a histogram), we can plot with boxes instead of impulses. Gnuplot does have a histogram option, as described here, but the same effect can be achieved here with boxes.

We can produce a gap between the bars by making them narrower than their ‘slot’, hence boxwidth 0.8 relative. And rather than drawing only an outline of the bar, we'll have it filled-in with a solid color by setting the style to fill solid 1.0. The plot command then generates the graph with boxes.


# ______________________________________________________________________
# For the next graph, we want a histogram.
set style data boxes

# We want a small gap between solid (filled-in) bars.
set boxwidth 0.8 relative
set style fill solid 1.0

# Plot the histogram (one curve).
plot 'spectrum.tsv' using 1:2 with boxes

Figure 3a: Histogram frequency spectrum

Figure 3b: Frequency histogram

and here's the contents of spectrum.tsv:
1	0
2	1
3	3
4	10
5	4
6	0
7	0
8	0
9	0
10	0
11	2
12	6
13	1
14	0
15	0
16	4
17	1
18	0
19	0
20	1

Home About Me
Copyright © Neil Carter

Content last updated: 2012-02-16