Re: Sliding Time Window Function
- To: mathgroup at smc.vnet.net
- Subject: [mg122134] Re: Sliding Time Window Function
- From: Vince Virgilio <blueschi at gmail.com>
- Date: Sun, 16 Oct 2011 07:05:53 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <j79151$i46$1@smc.vnet.net>
- Reply-to: comp.soft-sys.math.mathematica at googlegroups.com
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. Vince