Timeseries

Timeseries transformations, filters, etc.

Classes:

Filter_IIR([ftype, buffer_size, coef_type, axis])

Simple wrapper around scipy.signal.iirfilter()

Kalman(dim_state, dim_measurement, …)

Kalman filter!!!!!

Integrate([decay, dt_scale])

class Filter_IIR(ftype='butter', buffer_size=256, coef_type='sos', axis=0, *args, **kwargs)[source]

Bases: autopilot.transform.transforms.Transform

Simple wrapper around scipy.signal.iirfilter()

Creates a streaming filter – takes in single values, stores them, and uses them to filter future values.

Parameters
  • ftype (str) – filter type, see ftype of scipy.signal.iirfilter() for available filters

  • buffer_size (int) – number of samples to store when filtering

  • coef_type ({‘ba’, ‘sos’}) – type of filter coefficients to use (see scipy.signal.sosfilt() and scipy.signal.lfilt())

  • axis (int) – which axis to filter over? (default: 0 because when passing arrays to filter, want to filter samples over time)

  • **kwargs – passed on to scipy.signal.iirfilter() , eg.

    • N - filter order

    • Wn - array or scalar giving critical frequencies

    • btype - type of band: ['bandpass', 'lowpass', 'highpass', 'bandstop']

Variables
  • coefs (np.ndarray) – filter coefficients, depending on coef_type

  • buffer (collections.deque) – buffer of stored values to filter

  • coef_type (str) – type of filter coefficients to use (see scipy.signal.sosfilt() and scipy.signal.lfilt())

  • axis (int) – which axis to filter over? (default: 0 because when passing arrays to filter, want to filter samples over time)

  • ftype (str) – filter type, see ftype of scipy.signal.iirfilter() for available filters

Methods:

process(input)

Filter the new value based on the values stored in Filter.buffer

process(input: float)[source]

Filter the new value based on the values stored in Filter.buffer

Parameters

input (float) – new value to filter!

Returns

the filtered value!

Return type

float

class Kalman(dim_state: int, dim_measurement: Optional[int] = None, dim_control: int = 0, *args, **kwargs)[source]

Bases: autopilot.transform.transforms.Transform

Kalman filter!!!!!

Adapted from https://github.com/rlabbe/filterpy/blob/master/filterpy/kalman/kalman_filter.py simplified and optimized lovingly <3

Each of the arrays is named with its canonical letter and a short description, (eg. the x_state vector x_state is self.x_state

References

Roger Labbe. “Kalman and Bayesian Filters in Python” - https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python Roger Labbe. “FilterPy” - https://github.com/rlabbe/filterpy

Methods:

_init_arrays([state])

Initialize the arrays!

predict([u, B, F, Q])

Predict next x_state (prior) using the Kalman filter x_state propagation equations.

update(z[, R, H])

Add a new measurement (z_measure) to the Kalman filter.

_reshape_z(z, dim_z, ndim)

ensure z is a (dim_z, 1) shaped vector

process(z, **kwargs)

Call predict and update, passing the relevant kwargs

residual_of(z)

Returns the residual for the given measurement (z_measure).

measurement_of_state(x)

Helper function that converts a x_state into a measurement.

Attributes:

alpha

Fading memory setting.

_init_arrays(state=None)[source]

Initialize the arrays!

predict(u=None, B=None, F=None, Q=None)[source]

Predict next x_state (prior) using the Kalman filter x_state propagation equations.

unp.array, default 0

Optional control vector.

Bnp.array(dim_state, dim_u), or None

Optional control transition matrix; a value of None will cause the filter to use self.B_control.

Fnp.array(dim_state, dim_state), or None

Optional x_state transition matrix; a value of None will cause the filter to use self.F_state_trans.

Qnp.array(dim_state, dim_state), scalar, or None

Optional process noise matrix; a value of None will cause the filter to use self.Q_proc_var.

update(z, R=None, H=None)[source]

Add a new measurement (z_measure) to the Kalman filter.

If z_measure is None, nothing is computed. However, x_post and P_post are updated with the prior (x_prior, P_prior), and self.z_measure is set to None.

z(dim_measurement, 1): array_like

measurement for this update. z_measure can be a scalar if dim_measurement is 1, otherwise it must be convertible to a column vector.

If you pass in a value of H_measure, z_measure must be a column vector the of the correct size.

Rnp.array, scalar, or None

Optionally provide R_measure_var to override the measurement noise for this one call, otherwise self.R_measure_var will be used.

Hnp.array, or None

Optionally provide H_measure to override the measurement function for this one call, otherwise self.H_measure will be used.

_reshape_z(z, dim_z, ndim)[source]

ensure z is a (dim_z, 1) shaped vector

process(z, **kwargs)[source]

Call predict and update, passing the relevant kwargs

Parameters
  • z ()

  • **kwargs ()

Returns

self.x_state

Return type

np.ndarray

residual_of(z)[source]

Returns the residual for the given measurement (z_measure). Does not alter the x_state of the filter.

measurement_of_state(x)[source]

Helper function that converts a x_state into a measurement.

xnp.array

kalman x_state vector

z_measure(dim_measurement, 1): array_like

measurement for this update. z_measure can be a scalar if dim_measurement is 1, otherwise it must be convertible to a column vector.

property alpha

Fading memory setting. 1.0 gives the normal Kalman filter, and values slightly larger than 1.0 (such as 1.02) give a fading memory effect - previous measurements have less influence on the filter’s estimates. This formulation of the Fading memory filter (there are many) is due to Dan Simon [1]_.

class Integrate(decay=1, dt_scale=False, *args, **kwargs)[source]

Bases: autopilot.transform.transforms.Transform

Methods:

process(input)

process(input)[source]