Taking subarrays from numpy array with given stride/stepsize

前端 未结 2 1538
不知归路
不知归路 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)
    

提交回复
热议问题