1#
2# This script demonstrates the use of assignment operators and
3# sequential expression evaluation to track data points as they
4# are read in.
5#
6# We use the '=' and ',' operators to track the running total
7# and previous 5 values of a stream of input data points.
8#
9# Ethan A Merritt - August 2007
10#
11# Define a function to calculate average over previous 5 points
12#
13set title \
14    "Demonstrate use of assignment and serial evaluation operators\n" \
15    . "to accumulate statistics as successive data lines are read in\n"
16set key invert box center right reverse Left
17set xtics nomirror
18set ytics nomirror
19set border 3
20
21samples(x) = $0 > 4 ? 5 : ($0+1)
22avg5(x) = (shift5(x), (back1+back2+back3+back4+back5)/samples($0))
23shift5(x) = (back5 = back4, back4 = back3, back3 = back2, back2 = back1, back1 = x)
24
25#
26# Initialize a running sum
27#
28init(x) = (back1 = back2 = back3 = back4 = back5 = sum = 0)
29
30#
31# Plot data, running average and cumulative average
32#
33
34datafile = 'silver.dat'
35set xrange [0:57]
36
37set style data linespoints
38
39plot sum = init(0), \
40     datafile using 0:2 title 'data' lw 2 lc rgb 'forest-green', \
41     '' using 0:(avg5($2)) title "running mean over previous 5 points" pt 7 ps 0.5 lw 1 lc rgb "blue", \
42     '' using 0:(sum = sum + $2, sum/($0+1)) title "cumulative mean" pt 1 lw 1 lc rgb "dark-red"
43
44pause -1 "Hit return to continue"
45
46reset
47