Patch Processing

Open in marimo

Patch-processing methods return new patches and are listed in the dascore.proc API.

import numpy as np

import dascore as dc

patch = dc.get_example_patch("terra15_das_1_trimmed.hdf5")

The examples use a short recording from a Terra15 interrogator; its traces carry a constant offset.

Filtering and detrending

Patch.demean removes the mean along a dimension and Patch.detrend removes a linear trend (or a constant one with type="constant"). Patch.pass_filter applies a zero-phase Butterworth filter; a None or ... bound makes it a low-pass or high-pass filter.

demeaned = patch.demean("time")
detrended = patch.detrend("time")
lowpass = detrended.pass_filter(time=(None, 500))
bandpass = detrended.pass_filter(time=(10, 500))
import matplotlib.pyplot as plt

_fig, (_raw_ax, _filt_ax) = plt.subplots(1, 2, figsize=(12, 4))
patch.viz.waterfall(ax=_raw_ax)
bandpass.viz.waterfall(ax=_filt_ax)
_raw_ax.set_title("Raw")
_filt_ax.set_title("Detrended, 10-500 Hz")
plt.tight_layout()
plt.show()

Filter bounds on time are in Hz; see Dimension arguments for other dimensions and units. Detrend or taper before filtering to reduce edge effects.

Shape and missing values

transpose changes dimension order, squeeze removes length-one dimensions, and dropna removes labels containing null values.

transposed = patch.transpose("time", "distance")

flat = patch.select(distance=0, samples=True)
squeezed = flat.squeeze()

data = np.array(patch.data)
data[0, :] = np.nan
without_nan = patch.new(data=data).dropna("time")

transpose() with no arguments rotates dimension order. squeeze() can name dimensions to remove and rejects any named dimension whose length is not one. dropna keeps coordinate and data indexing synchronized.

Decimate

Patch.decimate downsamples a dimension and applies an anti-aliasing filter by default.

waves = dc.examples.get_example_patch(
    "sin_wav",
    sample_rate=1000,
    frequency=[200, 10],
    channel_count=2,
)

iir = waves.decimate(time=10, filter_type="iir")
fir = waves.decimate(time=10, filter_type="fir")
aliased = waves.decimate(time=10, filter_type=None)

Use filter_type=None only when aliasing is intentional or filtering has already been performed.

iir.viz.wiggle(show=True);
aliased.viz.wiggle(show=True);

Here the filtered result removes the 200 Hz component before reducing the 1,000 Hz sample rate. The unfiltered result folds that energy into a lower apparent frequency.

Taper

Patch.taper applies a cosine taper to a fraction of a dimension. A tuple controls the two ends; None leaves one end unchanged.

ones = patch.new(data=np.ones_like(patch.data))
both_ends = ones.taper(time=0.1)
different_ends = ones.taper(distance=(0.1, 0.3))
end_only = ones.taper(distance=(None, 0.1))
different_ends.viz.waterfall(show=True);

A scalar applies the same fraction at both ends. Tuple values let asymmetric records taper more strongly at one boundary, and may also be expressed with compatible units where supported.

See the edge-effects recipe for tapering before filters.

Moving windows

Patch.window provides moving-window reductions. A window which reaches past either end of the data gives NaN, or is left out with ends="drop". See the windowing recipe for what else a window does.

event = dc.get_example_patch("example_event_1")
smoothed = event.window(time=50, samples=True, ends="drop").mean()
smoothed.viz.waterfall(show=True);

A window provides the reductions mean, median, min, max, std, and sum, and reduce for any other function. Specify a coordinate-unit window or set samples=True for an exact sample count.

A step as long as the window gives non-overlapping blocks, such as the mean of each pair of channels. align="middle" starts the blocks at the first sample and labels each at its centre; ends="partial" keeps a short last block, reduced over the samples it has.

pairs = event.window(distance=2, step=2, samples=True, align="middle", ends="partial")
stacked = pairs.mean()

Gain

Automatic gain control

Passing window to Patch.normalize divides every sample by a centered local norm. This raises weak arrivals but removes amplitude comparability across the record.

gained = event.normalize("time", norm="l2", window=0.1)
gained.viz.waterfall(scale=0.5, show=True);

norm may be l2 (RMS), l1 (mean absolute value), or max. Window length uses coordinate units unless samples=True. Reflected edge values keep the output shape and avoid the incomplete-window NaNs a reduction in a Patch.window produces.

Without window, normalize uses one norm for the complete trace. With a window, every sample receives its own local scale. This distinction matters when amplitudes themselves carry scientific meaning.

Power gain

Patch.pow_coord multiplies data by a coordinate raised to a power. Because the curve depends only on position, relative amplitudes remain comparable between traces.

spread_corrected = event.pow_coord(time=2)
both_dimensions = event.pow_coord(time=2, distance=1)

The coordinate count begins at one rather than zero, so the first sample retains its amplitude. Time powers of one or two are common geometric-spreading corrections; other dimensions can be corrected in the same call.

Whiten

See the whitening recipe for Patch.whiten, which balances a patch’s amplitude spectrum.