[SciPy-user] Tutorial on advanced slicing with the DCT

Travis Oliphant oliphant at ee.byu.edu
Wed May 18 21:41:55 EDT 2005


I just checked in a module implementing the Discrete Cosine Transform 
into the image library of scipy. 

The main reason I'm advertising on the scipy users list is that it shows 
an example of using slices to accomplish generic indexing.

The DCT can be implemented using the FFT on a specifically sliced 
version of the sequence.   Basically the DCT can be implemented using an 
FFT of xtilde where

xtilde[:N/2] = x[::2]
xtilde[N/2:] = x[N::-2]

But, I wanted to do this kind of slicing on an arbitrary axis.   So, if 
the user-provided axis was -2 I wanted the first of these to be:

xtilde[...,:N/2,:] = x[...,:N/2,:]

or if axis=0 this should be

xtilde[:N/2,...] = x[:N/2,...]

In order to do that I used a tuple of slice objects on each side

xtilde[slice0] = xtilde[slice1]

where slice0 is a tuple of slice objects with slice?[axis] set to the 
correct slicing behavior. 

slice objects can be contructed using sl = slice(arg1, arg2, arg3) where

x[sl] will act equivalently to x[arg1:arg2:arg3]

I thought that the solution provided a good example for how to do these 
sorts of things.  If others have even better solutions, please let me know.

Best regards,

-Travis O.




More information about the SciPy-User mailing list