From: Dr. David Kirkby on
Does anyone know a simple way to create the n-point moving average of
a set of numbers in a file in a shell script?


I would think awk might be able to do it in some way, though I don't
have a clue how.

dave
From: Seebs on
On 2010-04-13, Dr. David Kirkby <david.kirkby(a)onetel.net> wrote:
> Does anyone know a simple way to create the n-point moving average of
> a set of numbers in a file in a shell script?

I am not sure this is the right tool for the job.

> I would think awk might be able to do it in some way, though I don't
> have a clue how.

awk -v points=3 -f thisfile.awk:

{ a[NR % points] = $1;
total = 0;
for (i = 0; i < points; ++i) { total = total + a[i]; }
print (total / points);
}

Not especially pretty or feature-laden, but it works. You get bonus
points if you can explain both what the first line does and why it
works. :)

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam(a)seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
From: pk on
Seebs wrote:

> On 2010-04-13, Dr. David Kirkby <david.kirkby(a)onetel.net> wrote:
>> Does anyone know a simple way to create the n-point moving average of
>> a set of numbers in a file in a shell script?
>
> I am not sure this is the right tool for the job.
>
>> I would think awk might be able to do it in some way, though I don't
>> have a clue how.
>
> awk -v points=3 -f thisfile.awk:
>
> { a[NR % points] = $1;
> total = 0;
> for (i = 0; i < points; ++i) { total = total + a[i]; }
> print (total / points);
> }

IIUC, shouldn't it be more like

awk -v points=3 -f thisfile.awk:

{ a[NR % points] = $1;
if(NR>=points) {
total = 0;
for (i = 0; i < points; ++i) { total = total + a[i]; }
print (total / points);
}
}

From: Seebs on
On 2010-04-14, pk <pk(a)pk.invalid> wrote:
> IIUC, shouldn't it be more like
>
> awk -v points=3 -f thisfile.awk:
>
> { a[NR % points] = $1;
> if(NR>=points) {
> total = 0;
> for (i = 0; i < points; ++i) { total = total + a[i]; }
> print (total / points);
> }
> }

Maybe! A lot of people don't have a clear definition of what they mean
by a "moving average", I just figured I'd illustrate the simplest case...

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam(a)seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
From: Dr. David Kirkby on
On Apr 14, 6:17 pm, Seebs <usenet-nos...(a)seebs.net> wrote:
> On 2010-04-14, pk <p...(a)pk.invalid> wrote:
>
> > IIUC, shouldn't it be more like
>
> > awk -v points=3 -f thisfile.awk:
>
> > { a[NR % points] = $1;
> >   if(NR>=points) {
> >     total = 0;
> >     for (i = 0; i < points; ++i) { total = total + a[i]; }
> >     print (total / points);
> >   }
> > }
>
> Maybe!  A lot of people don't have a clear definition of what they mean
> by a "moving average", I just figured I'd illustrate the simplest case...
>
> -s
> --
> Copyright 2010, all wrongs reversed.  Peter Seebach / usenet-nos...(a)seebs.nethttp://www.seebs.net/log/<-- lawsuits, religion, and funny pictureshttp://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!



Thank you for that. It works perfectly. In my case, the number of data
points is much greater than the likely number of points I want to
average over, so extra test added is not strictly necessary.

Dave