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


Groups > comp.graphics.apps.gnuplot > #2634 > unrolled thread

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

Started byKalin Kozhuharov <me.kalin@gmail.com>
First post2014-11-07 06:38 -0800
Last post2014-11-07 16:04 +0100
Articles 2 — 2 participants

Back to article view | Back to comp.graphics.apps.gnuplot


Contents

  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

#2634 — SMA (Simple Moving Average) inside gnuplot, ONLY AS LAST resort

FromKalin Kozhuharov <me.kalin@gmail.com>
Date2014-11-07 06:38 -0800
SubjectSMA (Simple Moving Average) inside gnuplot, ONLY AS LAST resort
Message-ID<63da4eac-2fda-4782-b6da-46b93921e95c@googlegroups.com>
Hello,

I have been thinking whether to post this or not for some time, but I decided to do it.
It is generally a bad practice to abuse tools for what they are not meant to be, so use that only in a pinch.
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).

https://github.com/thinrope/fixed_sensor_visualization/blob/master/SMA_gen.sh
I will probably leave it there for quite some time (I am not using it anymore), but just in case the revision link: https://github.com/thinrope/fixed_sensor_visualization/blob/bbd373c355648220b419d71facd743a536e3a341/SMA_gen.sh

It generates a code like that (for 3-bin and 6-bin):

sma_3_init(x) = (b3=b2=b1=x);
sma_3_shift(x) = (b3=b2,b2=b1,b1=x);
sma_3_samples(x) = $0 > 2 ? 3 : ($0+1);
sma_3_RO(x) = (b3+b2+b1)/sma_3_samples($0);
sma_3(x) = (sma_3_shift(x), sma_3_RO(x));

sma_6_init(x) = (e6=e5=e4=e3=e2=e1=x);
sma_6_shift(x) = (e6=e5,e5=e4,e4=e3,e3=e2,e2=e1,e1=x);
sma_6_samples(x) = $0 > 5 ? 6 : ($0+1);
sma_6_RO(x) = (e6+e5+e4+e3+e2+e1)/sma_6_samples($0);
sma_6(x) = (sma_6_shift(x), sma_6_RO(x));

Then you can use it like:
plot e=sma_6_init(0) "some_file" u 1:2 w p, "" u 1:(sma_6($2)) w l

I wrote this script while debugging 24-bin SMA for the code used to generate some radiation sensor time-charts (http://gamma.tar.bz/nGeigies/) and it was working for some time, but I finally moved away from it.

Hope I don't stir the pot too much, be happy and consider using the right tools for the job :-)

Kalin.

[toc] | [next] | [standalone]


#2635

FromHans-Bernhard Bröker <HBBroeker@t-online.de>
Date2014-11-07 16:04 +0100
Message-ID<cc45b4F2lb3U1@mid.dfncis.de>
In reply to#2634
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)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.graphics.apps.gnuplot


csiph-web