Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Sliding Time Window Function

51 views
Skip to first unread message

northern_flicker

unread,
Oct 14, 2011, 5:58:57 AM10/14/11
to
Dear Forum

This problem is quite similar to a moving average, but it has two differences, which I am struggling to overcome. It is applicable to a heterogeneous time series with dependent x and y values, i.e., {time, x, y}, where the temporal variations in x and y are of interest.

1) It would be preferable to have a sliding window based on a time interval rather than the number of data, e.g., 0.5 s, 5 min., 50 years, etc.
2) Instead of a moving average I would like to apply some expression over the moving time interval that uses both x and y, e.g.,

Total[Cos(x)*Sin(y)]/Total[Cos(x)*Cos(y)]

A fresh approach would be much appreciated

Vince Virgilio

unread,
Oct 16, 2011, 7:09:35 AM10/16/11
to
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

0 new messages