def slidingWindow(s, span, stride=None, axis=0):
"""Sliding window.
"""
#s = np.ascontiguousarray(s)
s = np.require(s, requirements=['C', 'O'])
if stride is None:
stride = span
# catch some bad values since this is a common place for
# bugs to crop up in other routines
if span > s.shape[axis]:
raise ValueError('Span of %d exceeds input length of %d.' % (span, s.shape[axis]))
if span < 0:
raise ValueError('Negative span of %d is invalid.' % span)
if stride < 1:
raise ValueError('Stride of %d is not positive.' % stride)
nWin = int(np.ceil((s.shape[axis]-span+1) / float(stride)))
shape = list(s.shape)
shape[axis] = span
shape.insert(axis, nWin)
strides = list(s.strides)
strides.insert(axis, stride*strides[axis])
return npst.as_strided(s, shape=shape, strides=strides)
评论列表
文章目录