Transformations

Open in JupyterLite

Transforms change a patch’s domain and usually its dimension names and units. They are available as Patch methods and in dascore.transform.

Fourier transform

Patch.dft changes a dimension such as time to its Fourier-domain counterpart, ft_time. real=True keeps only the nonnegative frequencies of real input.

import numpy as np

import dascore as dc

patch = dc.get_example_patch().set_units("m/s")
transformed = patch.dft(dim="time", real=True)

print(transformed.dims)
print(transformed.attrs.data_units)
('distance', 'ft_time')
1 m

The transformed coordinate uses inverse units and the data units change according to the transform normalization. Transform several dimensions by naming each one.

spatial_spectrum = patch.dft(dim="distance", real=True)
print(spatial_spectrum.get_coord("ft_distance"))
CoordRange( min: 0.000 1 / m max: 0.500 1 / m <0.500 1 / m> step: 0.00333 1 / m shape: (151,) dtype: float64 )

Patch.idft performs the inverse transform:

round_trip = patch.dft(dim="time").idft()
assert np.allclose(round_trip.data.real, patch.data)

Real transforms remember the information needed for their inverse. Numerical round trips may contain tiny floating-point differences, so compare them with a tolerance rather than exact equality.

See Fourier transforms in DASCore for naming, units, and normalization.

Short-time Fourier transform

Patch.stft shows frequency content through time; Patch.istft inverts it.

from dascore.units import percent, second

chirp = dc.get_example_patch("chirp", channel_count=3).set_units("m/s")
spectrogram = chirp.stft(time=1 * second, overlap=50 * percent)
inverse = spectrogram.istft()

spectrogram.abs().select(distance=0, samples=True).squeeze().viz.waterfall();

The window length uses the selected coordinate’s units. overlap may be expressed as a percentage, coordinate quantity, or supported numeric form. The inverse uses the transform metadata to reconstruct the original dimension.

Inspect the transformed coordinate to obtain frequency bins; select or filter in the transformed domain exactly as with other patch dimensions.

Like dft, stft drops non-dimensional coordinates associated with the transformed dimension.