To improve upon the basic plot of a sine wave, this time, we'll plot more than one curve on the graph, and give each a slightly different look, so we can tell them apart.

First of all, we clean up anything lying around, and give the
graph a title. This time, we want the individual points along the
graph to be visible, so we set `samples`

to be a smaller
number. We still want the x value to vary over the same range as in
the previous example, so `xrange`

remains 0 to 2pi
(radians).

We want tickmarks on the x axis, but the number would be rather
cluttered and aren't really useful, so they are turned off with the
command `set format x ""`

.

Note that we divide the range of x by one less than the number of samples so that the samples and tickmarks coincide (its the fenceposts problem; the first sample occurs at zero). For instance, to fit ten samples between zero and ten, the interval between samples is 1.1 because the ‘distance’ is 11. So we need to place our tickmarks 1.1 units apart.

The `yrange`

is configured to have 16 ticks,
corresponding to the 16 different values a four-bit sample can take
on. We then label the `ytics`

accordingly.

Whereas we placed the function to be plotted in the `plot`

command in the previous example, in this case, we've declared our
own function for it. This avoids repetition. In order to have the
sine wave placed properly against the samples, we have to manipulate
it a bit. We multiply by 7.51 so that when we round the values,
we'll end up with 16 integers, and add .5 so that the main curve
is better aligned with the sampled curve.

We can place our own text and symbols on the graph area using the
`set label`

and `set arrow`

commands, along
with the appropriate X and Y coordinates.

Next, we set the name of the output file, using the file type variable as before. This time, a Postscript file is generated.

Finally, the `plot`

command generates three curves.
The first is just the main sine wave. Note that the
`title`

keyword has been used to label the curve in the
key, and that we want a smooth curve so we use the ```
with
lines
```

modifier. For the second curve, we actually want a
series of unconnected sample markers, so we use `points`

,
set the type to asterisks with `pt 3`

, and select a blue
linecolour with `lc rgb blue`

. For the third curve we
draw the same function as for the second curve, but this time we
don't want a smooth interpolation between points, so we use
`steps`

. For both the second and third curves, we want to
mimic a digital aliasing effect, so we round the function samples to
zero with the `int()`

function.

The `plot`

command takes many arguments such as
`using`

, `with`

, and `smooth`

. It
also understands options like `linecolor`

(which can be
abbreviated to `lc`

, `linewidth`

(`lw`

), `pointtype`

(`pt`

), and so
on. See `help linestyle`

for more options. To generate a
sample graph showing all the available lines, points, colours,
amongst other things, set the appropriate terminal type and run the
command `test`

; bear in mind that the same option values
may well look different on different terminals.

```
# Delete previous plots.
clear
# Reset all configurations to their defaults.
reset
set title "Aliasing with 4-bit Sampling Resolution"
# We want 50 samples (half the default number) over
# one full sine wave.
set samples 50
set xrange [0:6.28]
# Show the tickmarks but don't show the numbers
# (it's too cluttered). Ticks is one less than samples
# because the first tick is at zero.
set xtics 0, 6.28/49, 6.28
set format x ""
# Show the tickmarks on the Y axis, but don't show
# the numbers.
set yrange[-7:8]
set ytics ("0000" -7, "0001" -6, "0010" -5, "0011" -4, "0100" -3, \
"0101" -2, "0110" -1, "0111" 0, "1000" 1, "1001" 2, "1010" 3, \
"1011" 4, "1100" 5, "1101" 6, "1110" 7, "1111" 8 )
# Define the function to plot
f(x) = (sin(x) * 7.51) + .5
# Draw a vertical arrow showing height of curve at this point.
# Place it halfway along 9th sample (counting from zero).
set arrow from (6.26/49)*8.5, -7 to (6.26/49)*8.5, 6
# Give the arrow a label.
set label "Level #14" at 0.35, 0
# Plot three curves with different styles.
plot f(x) title "Original signal" with lines, \
int(f(x)) title "Discrete samples" with points pt 3 lc rgb "blue", \
int(f(x)) title "Discrete signal" with steps lc rgb "blue"
```

Home | About Me | Copyright © Neil Carter |

Content last updated: 2012-02-16