Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.graphics.apps.gnuplot > #2635

Re: SMA (Simple Moving Average) inside gnuplot, ONLY AS LAST resort

From Hans-Bernhard Bröker <HBBroeker@t-online.de>
Newsgroups comp.graphics.apps.gnuplot
Subject Re: SMA (Simple Moving Average) inside gnuplot, ONLY AS LAST resort
Date 2014-11-07 16:04 +0100
Message-ID <cc45b4F2lb3U1@mid.dfncis.de> (permalink)
References <63da4eac-2fda-4782-b6da-46b93921e95c@googlegroups.com>

Show all headers | View raw


Am 07.11.2014 um 15:38 schrieb Kalin Kozhuharov:

> It is almost always a better idea to compute SMA in something like
> Perl before feeding your data to gnuplot, trust me. I just switched
> to such implementation myself ;-)

> But if you like hacking and inline messy code, here is a bash script
> that will generate gnuplot code for SMA with 2 to 53 bins (due to
> alphabet limitations; may be extended).

Hmm... that approach appears overly complicated.  A simple moving 
average is, ultimately, really just a simple moving sum.  Which in turn 
can be built most easily by adding a "delayed copy" to the data itself. 
  I.e. for the simple case of a moving window of 3 steps, one would 
transform a file

	a1
	a2
	a3
	a4
	...
	a{n}
into

	a1	0
	a2	0
	a3	0
	a4	a1
	a5	a2
	...	...
	a{n}	a{n-3}
	0	a{n-2}
	0	a{n-1}
	0	a{n}

Say you have a little shell script that does this (using awk, or even 
just echo and /bin/paste), taking as arguments the input data file and 
the delay length, then you can compute the running average using 
gnuplot's builtin functions by doing this:

	plot mavg=0, '< delaydata foo.dat 3' \
                      using 0:((mavg = mavg + $1 - $2)/3)

Back to comp.graphics.apps.gnuplot | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

SMA (Simple Moving Average) inside gnuplot, ONLY AS LAST resort Kalin Kozhuharov <me.kalin@gmail.com> - 2014-11-07 06:38 -0800
  Re: SMA (Simple Moving Average) inside gnuplot, ONLY AS LAST resort Hans-Bernhard Bröker <HBBroeker@t-online.de> - 2014-11-07 16:04 +0100

csiph-web