Vectorize a moving average calculation

View: New views
3 Messages — Rating Filter:   Alert me  

Vectorize a moving average calculation

by babelproofreader :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I would like to calculate various moving averages, FIR filters etc. which rely on previously calculated values of said averages and filters e.g. an exponential moving average is of the form alpha*ValueToday + (1-alpha)*ValueYesterday. How does one use vectorized code to calculate such things?

Re: Vectorize a moving average calculation

by Joshua Stults :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

I don't think you can really vectorize a calculation like that, but
it's pretty easy to recast it as a sparse matrix solve if you can
afford to fit all of the coefficients into memory.  For your problem
you just end up with a bi-diagonal system, so you need about 2*n of
extra storage beyond just storing your solution vector.  It's ~2
orders of magnitude faster than a for-loop too, and for more complex
operators you can use the built-in iterative solvers.  For your
problem you'd have alpha down the main diagonal and (1-alpha) down the
first sub-diagonal, if A is your sparse matrix, your solution is just

x = A\b;

I wrote up an example in my blog a few days ago on this exact thing,
it's in the last half of this post:
http://j-stults.blogspot.com/2008/10/is-octave-slow.html

Hope that helps.

Cheers!
Josh

On Mon, Nov 3, 2008 at 4:01 PM, babelproofreader
<babelproofreader@...> wrote:

>
> I would like to calculate various moving averages, FIR filters etc. which
> rely on previously calculated values of said averages and filters e.g. an
> exponential moving average is of the form alpha*ValueToday +
> (1-alpha)*ValueYesterday. How does one use vectorized code to calculate such
> things?
> --
> View this message in context: http://www.nabble.com/Vectorize-a-moving-average-calculation-tp20303346p20303346.html
> Sent from the Octave - General mailing list archive at Nabble.com.
>
> _______________________________________________
> Help-octave mailing list
> Help-octave@...
> https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
>
_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

Re: Vectorize a moving average calculation

by Bugzilla from ozzy.lash@gmail.com :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Mon, Nov 3, 2008 at 3:01 PM, babelproofreader
<babelproofreader@...> wrote:
>
> I would like to calculate various moving averages, FIR filters etc. which
> rely on previously calculated values of said averages and filters e.g. an
> exponential moving average is of the form alpha*ValueToday +
> (1-alpha)*ValueYesterday. How does one use vectorized code to calculate such
> things?

I think the filter function will do what you want.  For your example
you should be able to use:

y=filter(alpha,[1,-(1-alpha)],x)
_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave