Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!fu-berlin.de!uni-berlin.de!news.dfncis.de!not-for-mail From: =?windows-1252?Q?Hans-Bernhard_Br=F6ker?= Newsgroups: comp.graphics.apps.gnuplot Subject: Re: SMA (Simple Moving Average) inside gnuplot, ONLY AS LAST resort Date: Fri, 07 Nov 2014 16:04:01 +0100 Lines: 44 Message-ID: References: <63da4eac-2fda-4782-b6da-46b93921e95c@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.dfncis.de BUf/BIC5/YzHW1i06EldgwqrKSU9LznSNwzL1G478RtVxdv3pMq8OJ3x7u Cancel-Lock: sha1:miEXUVu4u0B84XYkoFugB8Zg/CI= User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:31.0) Gecko/20100101 Thunderbird/31.2.0 In-Reply-To: <63da4eac-2fda-4782-b6da-46b93921e95c@googlegroups.com> Xref: csiph.com comp.graphics.apps.gnuplot:2635 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)