Skip to content

Tutorial โ€‹

A walkthrough covering the most common operations: construct a series, compute a spectrum, resample, filter, plot. Most steps need only TimeseriesTools; the spectrum plot uses TimeseriesMakie and a Makie backend, and filtering/resampling load their respective extensions on demand.

Construct a time series โ€‹

julia
using TimeseriesTools, Unitful

t = range(0, 10; step = 0.01) * u"s"
x = Timeseries(sin.(2ฯ€ * 5 * ustrip.(t)) .+ 0.3randn(length(t)), t) * u"V"
โ”Œ 1001-element ToolsArray{Unitful.Quantity{Float64, ๐‹ยฒ ๐Œ ๐ˆโปยน ๐“โปยณ, Unitful.FreeUnits{(V,), ๐‹ยฒ ๐Œ ๐ˆโปยน ๐“โปยณ, nothing}}, 1} โ”
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ dims โ”ค
  โ†“ ๐‘ก Sampled{Unitful.Quantity{Float64, ๐“, Unitful.FreeUnits{(s,), ๐“, nothing}}} (0.0:0.01:10.0) s ForwardOrdered Regular Points
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  0.0 s    0.0395782 V
  0.01 s   0.451425 V
  0.02 s   1.06739 V
  0.03 s   1.03727 V
  0.04 s   1.21711 V
  โ‹ฎ       
  9.96 s  -0.771344 V
  9.97 s  -0.715027 V
  9.98 s  -1.42258 V
  9.99 s   0.0982456 V
 10.0 s    0.05493 V

The result is a UnivariateTimeseries with a ๐‘ก (time) dimension. Units flow through naturally: unit(eltype(x)) is V, unit(eltype(lookup(x, ๐‘ก))) is s.

julia
samplingrate(x), duration(x)
(100.0 sโปยน, 10.0 s)

Compute and plot a power spectrum โ€‹

julia
using CairoMakie, TimeseriesMakie

S = powerspectrum(x, 0.5)         # second argument: minimum frequency to resolve
fig = Figure()
ax = Axis(fig[1, 1])
plotspectrum!(ax, S)
fig

See Spectra and spectrograms for powerspectrum/energyspectrum options and the wavelet path.

Resample, upsample, downsample โ€‹

resample evaluates an interpolant onto any target grid. upsample is the dense-target sugar; downsample (filter-then-decimate) is the only safe way to reduce a sampling rate.

@example
using DataInterpolations    # enables interpolation/resampling

y_dense = upsample(x, 4)            # 4ร— the sampling density
y_grid  = resample(x, 0.05u"s")     # explicit period
@example
using DSP                           # enables downsample
y_slow  = downsample(x, 5)          # anti-alias filter, then decimate by 5
samplingrate(y_slow)

See Data wrangling: imputation, interpolation, and resampling for the full table, including impute (NaN/missing fill) and N-dimensional joint fits.

Filter โ€‹

@example
xb = bandpass(x, [4u"Hz", 6u"Hz"])  # extracts the 5 Hz tone

See Filtering and analytic signals for bandpass/highpass/ lowpass, the Hilbert transform, instantaneous frequency/phase/amplitude, and phasestitch.

Next steps โ€‹