[Numpy-discussion] fiddling the strides of an array

A. M. Archibald peridot.faceted at gmail.com
Mon Nov 20 14:20:33 EST 2006


On 19/11/06, A. M. Archibald <peridot.faceted at gmail.com> wrote:

> Well, no, of course not to implement "fiddle". But the application I
> have in mind I don't think can be done with slicing...

If it helps to motivate what I'm doing, I'm trying to write (have
written) a function that will take an array and segment it along an
axis into overlapping pieces with a given length and number of entries
of overlap. Of course this is easy using reshape and stacking, but
there's no reason it needs to make a copy, or to occupy a large amount
of space. I can generate the strides that should be used for such a
segmented view of an array from the strides of the original array with
little difficulty, and the program works fine for one-dimensional
arrays. But if you try to feed it a high-dimensional array,
ndarray.__new__ often has a fit because it can't convert the array to
a (contiguous) buffer. I even considered lying to the system and
marking the array as contiguous, but even apart from being a bad idea
it doesn't seem to be possible from python.

I've attached the function; its test cases include several that
trigger the problem (at which point it spits out a warning and just
copies the array).

The function was suggested on the scipy list by a Matlab escapee as a
very handy function for signal processing. (Taking FFTs of successive
overlapped chunks, for example.)

A. M. Archibald
-------------- next part --------------
A non-text attachment was scrubbed...
Name: segmentaxis.py
Type: text/x-python
Size: 4806 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20061120/d057cb0b/attachment.py>


More information about the NumPy-Discussion mailing list