Skip to content

hertzforge.decimate

decimate

Peak-keeping decimation: fewer points, with every peak kept.

A record of a million samples is too many to draw point for point, and keeping every n-th sample can skip the one large peak that was the reason to look at the record. Peak-keeping decimation cuts the samples into equal slices and keeps each slice's smallest and largest value at their own abscissa positions, so no resonance, dropout or shock is lost. It is the same method as pyqtgraph's peak downsampling, which the 2-D plot uses.

The report and the 3-D waterfall both use this module. It works on all of an object's records at once, because the waterfall passes it hundreds of them. It needs only numpy.

This thinning is for drawing, never for a record anything is computed from: its points sit at uneven times, and an anti-alias filter ahead of it would take off the peaks it exists to keep. A record taken to a lower sample rate for analysis goes through core.resample, which filters first.

A plot zoomed into part of a record asks for that part again. Inside a drawing_window, peak_decimate and peak_decimate_rows keep the samples in the window thinned to the window's own budget, which is every sample once the window holds fewer, and thin the rest as before, so a curve drawn from the answer is the samples where the plot looks and the record's outline everywhere else.

Functions:

Name Description
drawing_window

Thin for a plot that shows low to high of the abscissa and

watching

A list that gains an entry each time a thinning in the block

peak_decimate

x and y thinned to at most budget points that keep every

peak_decimate_rows

peak_decimate for every record of one object at once.

shared_peak_indices

The samples of one shared abscissa to keep, at most budget of

envelope_rows

Every record of one object thinned onto the same bins, at most

Functions:

drawing_window

drawing_window(low: float, high: float, points: int) -> Iterator[None]

Thin for a plot that shows low to high of the abscissa and can draw points across it.

Every peak_decimate and peak_decimate_rows in the block keeps the samples from low to high, and the sample on either side so the curve runs on past the plot's edges, thinned to points (all of them when they are as few), and the rest thinned to the caller's budget. A context variable carries the window, so it reaches only the thread that set it.

watching

watching() -> Iterator[list[int]]

A list that gains an entry each time a thinning in the block leaves samples out: empty when everything was drawn whole.

peak_decimate

peak_decimate(x: ArrayLike, y: ArrayLike, budget: int) -> tuple[ndarray, ndarray]

x and y thinned to at most budget points that keep every extreme.

The samples are cut into budget // 2 slices, and each slice keeps its smallest and largest value. y must be real: a caller with complex data takes the component it wants first, because the extremes of a complex value are not defined. Input at or under the budget is returned as given.

NaN marks a gap (a specification is NaN outside its band) and is never kept as an extreme. A slice that is all NaN keeps its first point, so the gap is still drawn as a gap.

peak_decimate_rows

peak_decimate_rows(x: ArrayLike, rows: ArrayLike, budget: int) -> list[tuple[ndarray, ndarray]]

peak_decimate for every record of one object at once.

rows is a (records, samples) array on the shared abscissa x. The result is one (x, y) pair per record, and the records no longer share an abscissa: each keeps its own extremes at their own positions. The whole block is decimated in one vectorized pass.

shared_peak_indices

shared_peak_indices(rows: ArrayLike, budget: int) -> ndarray

The samples of one shared abscissa to keep, at most budget of them, so that every row keeps its extremes.

peak_decimate_rows gives each row its own positions, which suits curves drawn on their own. Rows that share an abscissa on the page, a measurement and the limits shaded about it, must keep one set of positions between them, or a band is shaded between points that are not above each other. This keeps, for every slice of the samples, the smallest and largest value of every row, with the slices as fine as the budget allows, and the first and last sample so the extent is unchanged. The first missing sample of each gap is kept too, while the gaps fit a quarter of the budget, so a thinned curve breaks where its data does instead of being drawn across the gap. Rows with fewer than budget samples keep them all.

Parameters:

Name Type Description Default
rows array_like

(rows, samples) values on one abscissa; NaN marks a gap.

required
budget int

The most samples to keep, at least two for each row and two for the ends.

required

Returns:

Type Description
ndarray

Sorted sample indices.

Raises:

Type Description
ValueError

If the budget cannot hold one slice's extremes for every row.

envelope_rows

envelope_rows(x: ArrayLike, rows: ArrayLike, bins: int) -> tuple[ndarray, ndarray]

Every record of one object thinned onto the same bins, at most bins of them: two points a bin, the bin's least and greatest value in the order they occurred, at the bin's first and last sample time.

peak_decimate_rows keeps each record's extremes at their own positions, so a page carrying two dozen records carries two dozen abscissas. An envelope on common bins lets a report's time-history page share one abscissa among its curves, and draws the same picture, since a bin is far narrower than a pixel at any zoom the page offers. Input with at most two points a bin is returned as given.

Returns (axis, envelope): the shared abscissa and a (records, points) array.