File: running_avg.dem

package info (click to toggle)
gnuplot 6.0.2%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,940 kB
  • sloc: ansic: 95,319; cpp: 7,590; makefile: 2,470; javascript: 2,328; sh: 1,531; lisp: 664; perl: 304; pascal: 191; tcl: 88; python: 46
file content (47 lines) | stat: -rw-r--r-- 1,394 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#
# This script demonstrates the use of assignment operators and
# sequential expression evaluation to track data points as they
# are read in.
#
# We use the '=' and ',' operators to track the running total
# and previous 5 values of a stream of input data points.
#
# Ethan A Merritt - August 2007
#
# Define a function to calculate average over previous 5 points
#
set title \
    "Demonstrate use of assignment and serial evaluation operators\n" \
    . "to accumulate statistics as successive data lines are read in\n"
set key invert box center right reverse Left
set xtics nomirror
set ytics nomirror
set border 3

samples(x) = $0 > 4 ? 5 : ($0+1)
avg5(x) = (shift5(x), (back1+back2+back3+back4+back5)/samples($0))
shift5(x) = (back5 = back4, back4 = back3, back3 = back2, back2 = back1, back1 = x)

#
# Initialize a running sum
#
init(x) = (back1 = back2 = back3 = back4 = back5 = sum = 0)

#
# Plot data, running average and cumulative average
#

datafile = 'silver.dat'
set xrange [0:57]

set style data linespoints

plot sum = init(0), \
     datafile using 0:2 title 'data' lw 2 lc rgb 'forest-green', \
     '' using 0:(avg5($2)) title "running mean over previous 5 points" pt 7 ps 0.5 lw 1 lc rgb "blue", \
     '' using 0:(sum = sum + $2, sum/($0+1)) title "cumulative mean" pt 1 lw 1 lc rgb "dark-red"

if (exists("MANUAL_FIGURES")) exit
pause -1 "Hit return to continue"

reset