excess.org

Ian Ward

Software
CKAN contributor/tech lead
Urwid author
PyRF contributor
Speedometer author

Presentations
Contributing to Open Source
IASA E-Summit, 2014-05-16
Urwid Applications
2012-11-14
Urwid Intro
2012-01-22
Unfortunate Python
2011-12-19
Django 1.1
2009-05-16

Writing
Moving to Python 3
2011-02-17
Article Tags

Home

Ian Ward's email:
first name at this domain

wardi on OFTC, freenode and github

Locations of visitors to this page

Resample Numpy Array without Feature Loss

Posted on 2014-10-03.

For pyrf I needed to take data from a frequency plot, which could be any number of points, and present it as a spectrogram that fills the view size exactly. In the spectrogram I only care about the maximum values that appear in the range of frequencies represented by each pixel.

If I could just divide the number of source bins by an integer factor the solution would be simple:

return np.amax(data.reshape((-1, factor)), axis=1)

But I have to be able to handle any number of source bins and output that to any number of pixels.

Fortunately numpy is awesome.

I can use a numpy array to index another numpy array. In particular, I can get the first bin that applies to each of my output pixels with linspace:

bins = len(data)
indexes = np.linspace(0, bins, pixels, endpoint=False, dtype=int)
return data[indexes]

Truncation to ints causes the indexes to be biased to the left. We can add 0.5 to fix that:

bins = len(data)
indexes = np.linspace(0.5, bins + 0.5, pixels, endpoint=False, dtype=int)
return data[indexes]

Our indexes arrays elements now differ by bins // pixels or bins // pixels + 1. To cover all the bins except the + 1 we can create a matrix of index arrays and compute the maximums:

bins = len(data)
indexes = np.linspace(0.5, bins + 0.5, pixels, endpoint=False, dtype=int)
index_matrix = [indexes + i for i in range(bins // pixels))]
# FIXME: skipping some of the source data!
return np.amax(data[np.vstack(index_matrix)], axis=0)

To catch the + 1 elements we can subtract one from the following starting indexes, since it doesn't matter if we include a number we already included once in a max calculation. This works for resampling arrays from any size to any size without losing any peaks:

bins = len(data)
indexes = np.linspace(0.5, bins + 0.5, pixels, endpoint=False, dtype=int)
index_matrix = [indexes + i for i in range(bins // pixels))]
index_matrix.append(np.concatenate((indexes[1:], [bins])) - 1)
return np.amax(data[np.vstack(index_matrix)], axis=0)

But wait, sometimes the user resizes the window and we need to handle resampling a whole matrix instead of a single row.

Fortunately numpy is awesome again.

The ellipsis operator can be used to represent any number of dimensions in a matrix, so I don't have to loop over the rows, I can just write "..." and change the way I get the number of bins and now my code works for arrays and matrices:

bins = data.shape[-1]
indexes = np.linspace(0.5, bins + 0.5, pixels, endpoint=False, dtype=int)
index_matrix = [indexes + i for i in range(bins // pixels))]
index_matrix.append(np.concatenate((indexes[1:], [bins])) - 1)
return np.amax(..., data[np.vstack(index_matrix)], axis=len(data.shape) - 1)

Tags: Software Python