[Numpy-discussion] view 1d array as overlapping segments?
Mon Mar 7 09:14:44 CST 2011
Here is some *working* code i wrote once. It uses strides, look at the
docs for what it is.
from numpy.lib import stride_tricks
def overlap_array( y, len_blocks, overlap=0 ):
Make use of strides to return a two dimensional whose
rows come from a one dimensional array. Strides are
used to return rows that partially overlap.
y : a one dimensional array
len_blocks : the row length. The length of chunks from y.
overlap : number of elements that overlap. From 0 (no
overlap) to len_blocks-1 (almost full overlap).
x : a strided array
overlap = int(overlap)
len_blocks = int(len_blocks)
if not type(y) == np.ndarray:
raise ValueError( 'y must be a numpy.ndarray' )
if overlap >= len_blocks:
raise ValueError( 'overlap must be less than n_points' )
# compute shape and strides of the strided vector
strides = ( (len_blocks - overlap)*y.itemsize, y.itemsize )
shape = ( 1 + (y.nbytes - len_blocks*y.itemsize)/strides,
# create a strided array
return stride_tricks.as_strided( y, shape=shape, strides=strides )
On 03/07/2011 04:01 PM, Neal Becker wrote:
> reshape can view a 1d array as non-overlapping segments.
> Is there a convenient way to view a 1d array as a 2d array of overlapping
> l: segment length
> k: overlap
> u is the 1d array
> v is a 2d array
> v[i] = u[l*i:(l+1)*i]
> v[i] = u[l*i:(l+1)*i+k]
> NumPy-Discussion mailing list
More information about the NumPy-Discussion