MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Sliding Time Window Function

I have something which might suit. I use it for sliding window averages over non-uniformly sampled time series.

binWin[seq_, f_, intervals_, g_:Identity] :=
Module[{sorted = SortBy[seq, f], order = Ordering@intervals[[All, 1]]},
    Function[{tLo, tHi},
        sorted = Drop[sorted, LengthWhile[sorted, f@# < tLo &] ];
        g @ TakeWhile[sorted, f@# < tHi &]
    ] @@@ intervals[[order]] // #[[Ordering@order]]&

'seq' is the list of (x,y) pairs, where y may be scalar or vector. 'f' picks the sort-by or key field (say, time). Usually, it is 'First'. 'intervals' is a sequence of time (key) intervals which may overlap. And 'g' is your reduction, which is applied to each window (bin). binWin will take unordered series and window specs and return the windowed results ordered according to the lower bound of the intervals.

I think it's fairly performant. But I'd appreciate any improvements.


  • Prev by Date: Re: How to create this kind of list
  • Next by Date: Re: How to create this kind of list
  • Previous by thread: Sliding Time Window Function
  • Next by thread: Help with manipulate and 3D plotting