[Numpy-discussion] Downsampling array, retaining min and max values in window

Travis Oliphant oliphant.travis at ieee.org
Mon Jul 30 14:33:28 EDT 2007

Matthieu Brucher wrote:
> Hi,
> I think you should look into scipy.ndimage which has minimum_filter 
> and maximum_filter
> Matthieu
> 2007/7/24, Ludwig M Brinckmann < ludwigbrinckmann at gmail.com 
> <mailto:ludwigbrinckmann at gmail.com>>:
>     Hi there,
>     I have a large array, lets say 40000 * 512, which I need to
>     downsample by a factor of 4 in the y direction, by factor 3 in the
>     x direction, so that my resulting arrays are 10000 * 170 (or 171
>     this does not matter very much) - but of all the values I will
>     need to retain in the downsampled arrays the minimum and maximum
>     of the original data, rather than computing an average or just
>     picking every third/fourth value in the array.
>     So essentially I have a 4*3 window, for which I need the min and
>     max in this window, and store the result of applying this window
>     to the original array as my results.
>     What is the best way to do this?

order_filter(a, domain, order)
    Perform an order filter on an N-dimensional array.
      Perform an order filter on the array in.  The domain argument acts 
as a
      mask centered over each pixel.  The non-zero elements of domain are
      used to select elements surrounding each input pixel which are placed
      in a list.   The list is sorted, and the output for that pixel is the
      element corresponding to rank in the sorted list.
      in -- an N-dimensional input array.
      domain -- a mask array with the same number of dimensions as in.  Each
                dimension should have an odd number of elements.
      rank -- an non-negative integer which selects the element from the
              sorted list (0 corresponds to the largest element, 1 is the
              next largest element, etc.)

   Output: (out,)
      out -- the results of the order filter in an array with the same
             shape as in.

Run the order filter and then select out every 4th element in the first 
dimension and 3rd element


mask = numpy.ones(4,3)
out = scipy.signal.order_filter(in, mask, 0)
new = out[::4,::3]

More information about the NumPy-Discussion mailing list