0

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.],

00__00__00
  • 4,834
  • 9
  • 41
  • 89

1 Answers1

1

You always pad L - 1 elements. This is too much in must cases, but in case of right-padding it does not matter because superfluous elements of the end of the array are ignored. However, in case of left padding this is noticable because all elements from the beginning end up in the array.

In the particular case you insert 29 elements but you need only 28, so the last item in the array is dropped from the sliding window.

The number of elements to insert should depend on both array size and window size like this:

L - (a.shape[0] - 1) % L - 1

This will evaluate to the number of elements required to make the array size an integer multiple of the window size (which can be 0 if they already match).

MB-F
  • 22,770
  • 4
  • 61
  • 116