Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

signal

signal

Temporal signal processing — resampling, filtering, and basis functions.

Methods:

NameDescription
calc_bpmCalculate instantaneous BPM from beat to beat interval.
downsampleDownsample a Polars DataFrame/Series to a new target frequency or number of samples using averaging.
make_cosine_basisCreate basis functions for a discrete cosine transform.
upsampleUpsample a Polars DataFrame/Series to a new target frequency or number of samples using interpolation.

Methods

calc_bpm

calc_bpm(beat_interval, sampling_freq)

Calculate instantaneous BPM from beat to beat interval.

Parameters:

NameTypeDescriptionDefault
beat_interval(int) number of samples in between each beat (typically R-R Interval)required
sampling_freq(float) sampling frequency in Hzrequired

Returns:

NameTypeDescription
bpm(float) beats per minute for time interval

downsample

downsample(data, *, sampling_freq = None, target = None, target_type = 'samples', method = 'mean')

Downsample a Polars DataFrame/Series to a new target frequency or number of samples using averaging.

Parameters:

NameTypeDescriptionDefault
data(pl.DataFrame, pl.Series) data to downsamplerequired
sampling_freq(float) Sampling frequency of data in hertzNone
target(float) downsampling targetNone
target_typetype of target can be [samples,seconds,hz]‘samples’
method(str) type of downsample method [‘mean’,‘median’], default: mean‘mean’

Returns:

NameTypeDescription
out(pl.DataFrame, pl.Series) downsampled data (same type as input)

make_cosine_basis

make_cosine_basis(nsamples, sampling_freq, filter_length, unit_scale = True, drop = 0)

Create basis functions for a discrete cosine transform.

Based on the implementation in spm_filter and spm_dctmtx because scipy DCT can only apply transforms but not return the basis functions. Like SPM, this does not add a constant (i.e. intercept), but does retain the first basis (i.e. sigmoidal/linear drift).

Parameters:

NameTypeDescriptionDefault
nsamplesintnumber of observations (e.g. TRs)required
sampling_freqfloatsampling frequency in hertz (i.e. 1 / TR)required
filter_lengthintlength of filter in secondsrequired
unit_scaleboolassure that the basis functions are on the normalized range [-1, 1]; default TrueTrue
dropintindex of which early/slow bases to drop if any; default is to drop constant (i.e. intercept) like SPM. Unlike SPM, retains first basis (i.e. linear/sigmoidal). Will cumulatively drop bases up to and inclusive of index provided (e.g. 2, drops bases 1 and 2)0

Returns:

NameTypeDescription
outndarraynsamples x number of basis sets numpy array

upsample

upsample(data, *, sampling_freq = None, target = None, target_type = 'samples', method = 'linear')

Upsample a Polars DataFrame/Series to a new target frequency or number of samples using interpolation.

Parameters:

NameTypeDescriptionDefault
data(pl.DataFrame, pl.Series) data to upsample (Note: will drop non-numeric columns from DataFrame)required
sampling_freqSampling frequency of data in hertzNone
target(float) upsampling targetNone
target_type(str) type of target can be [samples,seconds,hz]‘samples’
method(str) [‘linear’, ‘nearest’, ‘zero’, ‘slinear’, ‘quadratic’, ‘cubic’] where ‘zero’, ‘slinear’, ‘quadratic’ and ‘cubic’ refer to a spline interpolation of zeroth, first, second or third order (default: linear)‘linear’

Returns: upsampled Polars DataFrame or Series (same type as input)