Taking subarrays from numpy array with given stride/stepsize

前端 未结 2 1546
不知归路
不知归路 2020-11-22 02:45

Lets say I have a Python Numpy array a.

a = numpy.array([1,2,3,4,5,6,7,8,9,10,11])

I want to create a matrix of sub sequences

相关标签:
2条回答
  • 2020-11-22 03:19

    Modified version of @Divakar's code with checking to ensure that memory is contiguous and that the returned array cannot be modified. (Variable names changed for my DSP application).

    def frame(a, framelen, frameadv):
    """frame - Frame a 1D array
    a - 1D array
    framelen - Samples per frame
    frameadv - Samples between starts of consecutive frames
       Set to framelen for non-overlaping consecutive frames
    
    Modified from Divakar's 10/17/16 11:20 solution:
    https://stackoverflow.com/questions/40084931/taking-subarrays-from-numpy-array-with-given-stride-stepsize
    
    CAVEATS:
    Assumes array is contiguous
    Output is not writable as there are multiple views on the same memory
    
    """
    
    if not isinstance(a, np.ndarray) or \
       not (a.flags['C_CONTIGUOUS'] or a.flags['F_CONTIGUOUS']):
        raise ValueError("Input array a must be a contiguous numpy array")
    
    # Output
    nrows = ((a.size-framelen)//frameadv)+1
    oshape = (nrows, framelen)
    
    # Size of each element in a
    n = a.strides[0]
    
    # Indexing in the new object will advance by frameadv * element size
    ostrides = (frameadv*n, n)
    return np.lib.stride_tricks.as_strided(a, shape=oshape,
                                           strides=ostrides, writeable=False)
    
    0 讨论(0)
  • 2020-11-22 03:34

    Approach #1 : Using broadcasting -

    def broadcasting_app(a, L, S ):  # Window len = L, Stride len/stepsize = S
        nrows = ((a.size-L)//S)+1
        return a[S*np.arange(nrows)[:,None] + np.arange(L)]
    

    Approach #2 : Using more efficient NumPy strides -

    def strided_app(a, L, S ):  # Window len = L, Stride len/stepsize = S
        nrows = ((a.size-L)//S)+1
        n = a.strides[0]
        return np.lib.stride_tricks.as_strided(a, shape=(nrows,L), strides=(S*n,n))
    

    Sample run -

    In [143]: a
    Out[143]: array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
    
    In [144]: broadcasting_app(a, L = 5, S = 3)
    Out[144]: 
    array([[ 1,  2,  3,  4,  5],
           [ 4,  5,  6,  7,  8],
           [ 7,  8,  9, 10, 11]])
    
    In [145]: strided_app(a, L = 5, S = 3)
    Out[145]: 
    array([[ 1,  2,  3,  4,  5],
           [ 4,  5,  6,  7,  8],
           [ 7,  8,  9, 10, 11]])
    
    0 讨论(0)
提交回复
热议问题