I wrote (with a little help from SO) a function to extract sliding windows with overlap L
from a serie a
, filling with fillval
either on the right or left when L/(L-overlap) is not integer,using numpy strides:
def strided_axis0_overlap(a, fillval, L,overlap,pad='left'): # a is 1D array
assert(overlap<L)
if pad=='left':
a_ext = np.concatenate(( np.full(L-1,fillval) ,a))
elif pad=='right':
a_ext = np.concatenate((a,np.full(L-1,fillval)))
n = a_ext.strides[0]
strided = np.lib.stride_tricks.as_strided
if pad=='left':
return strided(a_ext, shape=(a.shape[0],L), strides=(n,n))[[np.arange(0,len(a),L-overlap)],:]
elif pad=='right':
return strided(a_ext, shape=(a.shape[0],L), strides=(n,n))[[np.arange(0,len(a),L-overlap)],:]
It works fine, beside that for the padding. If I do
v=np.array(range(182)).T
strided_axis0_overlap(v,np.nan,5*6,0,pad='right')
I get the following which is expected:
array([[[ 0., 1., 2., ..., 27., 28., 29.],
[ 30., 31., 32., ..., 57., 58., 59.],
[ 60., 61., 62., ..., 87., 88., 89.],
...,
[ 120., 121., 122., ..., 147., 148., 149.],
[ 150., 151., 152., ..., 177., 178., 179.],
[ 180., 181., nan, ..., nan, nan, nan]]])
However, if I pad on the left
array([[[ nan, nan, nan, ..., nan, nan, 0.],
[ 1., 2., 3., ..., 28., 29., 30.],
[ 31., 32., 33., ..., 58., 59., 60.],
...,
[ 91., 92., 93., ..., 118., 119., 120.],
[ 121., 122., 123., ..., 148., 149., 150.],
[ 151., 152., 153., ..., 178., 179., 180.]]])
whereas I wanted the last window finishing with 182, and the first being like array([[[ nan, nan, nan, ..., nan, 0, 1.],