[Numpy-discussion] fiddling the strides of an array

Tim Hochberg tim.hochberg at ieee.org
Mon Nov 20 15:08:45 EST 2006


A. M. Archibald wrote:
> 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

I don't know how much help this will be, but, if the input array can be 
mapped into a contiguous array using transpose, which looks to be the 
case with at least one of the problematic cases you show here, you could 
transpose so as to get a contiguous array and grab the buffer from that. 
As for how to decide what transposes to do, while it sounds like a fun 
problem, I don't have time to delve into it right now.

-tim




More information about the NumPy-Discussion mailing list