Taking subarrays from numpy array with given stride/stepsize
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]: aOut[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]])
Starting in Numpy 1.20
, we can make use of the new sliding_window_view
to slide/roll over windows of elements.
And coupled with a stepping [::3]
, it simply becomes:
from numpy.lib.stride_tricks import sliding_window_view# values = np.array([1,2,3,4,5,6,7,8,9,10,11])sliding_window_view(values, window_shape = 5)[::3]# array([[ 1, 2, 3, 4, 5],# [ 4, 5, 6, 7, 8],# [ 7, 8, 9, 10, 11]])
where the intermediate result of the sliding is:
sliding_window_view(values, window_shape = 5)# array([[ 1, 2, 3, 4, 5],# [ 2, 3, 4, 5, 6],# [ 3, 4, 5, 6, 7],# [ 4, 5, 6, 7, 8],# [ 5, 6, 7, 8, 9],# [ 6, 7, 8, 9, 10],# [ 7, 8, 9, 10, 11]])
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 arraya - 1D arrayframelen - Samples per frameframeadv - Samples between starts of consecutive frames Set to framelen for non-overlaping consecutive framesModified from Divakar's 10/17/16 11:20 solution:https://stackoverflow.com/questions/40084931/taking-subarrays-from-numpy-array-with-given-stride-stepsizeCAVEATS:Assumes array is contiguousOutput 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")# Outputnrows = ((a.size-framelen)//frameadv)+1oshape = (nrows, framelen)# Size of each element in an = a.strides[0]# Indexing in the new object will advance by frameadv * element sizeostrides = (frameadv*n, n)return np.lib.stride_tricks.as_strided(a, shape=oshape, strides=ostrides, writeable=False)