[Numpy-discussion] view 1d array as overlapping segments?
Davide
davide.lasagna@polito...
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.
Parameters
----------
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).
Returns
-------
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[0],
len_blocks)
# 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
> segments?
>
> nonoverlapping:
> l: segment length
> k: overlap
> u is the 1d array
> v is a 2d array
>
> v[i] = u[l*i:(l+1)*i]
>
> overlapping:
> v[i] = u[l*i:(l+1)*i+k]
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
More information about the NumPy-Discussion
mailing list