_linear_model

This module implements some common finite difference schemes

pynumdiff.linear_model._linear_model.chebydiff(x, dt, params, options=None)

Slide a smoothing derivative function across a times eries with specified window size.

Parameters
  • x (np.array (float)) – array of time series to differentiate

  • dt (float) – time step size

  • params (list (int)) –

    a list of 2 elements:

    • N: order of the polynomial

    • window_size: size of the sliding window (ignored if not sliding)

  • options (dict {'sliding': (bool), 'step_size': (int), 'kernel_name': (string)}, optional) –

    a dictionary consisting of 3 key value pairs:

    • ’sliding’: whether to use sliding approach

    • ’step_size’: step size for sliding

    • ’kernel_name’: kernel to use for weighting and smoothing windows (‘gaussian’ or ‘friedrichs’)

Returns

a tuple consisting of:

  • x_hat: estimated (smoothed) x

  • dxdt_hat: estimated derivative of x

Return type

tuple -> (np.array, np.array)

pynumdiff.linear_model._linear_model.lineardiff(x, dt, params, options=None)

Slide a smoothing derivative function across a time series with specified window size.

Parameters
  • x (np.array (float)) – array of time series to differentiate

  • dt (float) – time step size

  • params (list (int, float, int)) –

    a list of 3 elements:

    • N: order of the polynomial

    • gamma: regularization term

    • window_size: size of the sliding window (ignored if not sliding)

  • options (dict {'sliding': (bool), 'step_size': (int), 'kernel_name': (string)}, optional) –

    a dictionary consisting of 3 key value pairs:

    • ’sliding’: whether to use sliding approach

    • ’step_size’: step size for sliding

    • ’kernel_name’: kernel to use for weighting and smoothing windows (‘gaussian’ or ‘friedrichs’)

Returns

a tuple consisting of:

  • x_hat: estimated (smoothed) x

  • dxdt_hat: estimated derivative of x

Return type

tuple -> (np.array, np.array)

pynumdiff.linear_model._linear_model.polydiff(x, dt, params, options=None)

Fit polynomials to the time series, and differentiate the polynomials.

Parameters
  • x (np.array (float)) – array of time series to differentiate

  • dt (float) – time step size

  • params (list (int)) –

    a list of 2 elements:

    • N: order of the polynomial

    • window_size: size of the sliding window (ignored if not sliding)

  • options (dict {'sliding': (bool), 'step_size': (int), 'kernel_name': (string)}, optional) –

    a dictionary consisting of 3 key value pairs:

    • ’sliding’: whether to use sliding approach

    • ’step_size’: step size for sliding

    • ’kernel_name’: kernel to use for weighting and smoothing windows (‘gaussian’ or ‘friedrichs’)

Returns

a tuple consisting of:

  • x_hat: estimated (smoothed) x

  • dxdt_hat: estimated derivative of x

Return type

tuple -> (np.array, np.array)

pynumdiff.linear_model._linear_model.savgoldiff(x, dt, params, options=None)

Use the Savitzky-Golay to smooth the data and calculate the first derivative. It wses scipy.signal.savgol_filter. The Savitzky-Golay is very similar to the sliding polynomial fit, but slightly noisier, and much faster

Parameters
  • x (np.array (float)) – array of time series to differentiate

  • dt (float) – time step size

  • params (list (int)) –

    a list of three elements:

    • N: order of the polynomial

    • window_size: size of the sliding window, must be odd (if not, 1 is added)

    • smoothing_win: size of the window used for gaussian smoothing, a good default is window_size, but smaller for high frequnecy data

Returns

a tuple consisting of:

  • x_hat: estimated (smoothed) x

  • dxdt_hat: estimated derivative of x

Return type

tuple -> (np.array, np.array)

pynumdiff.linear_model._linear_model.spectraldiff(x, dt, params, options=None)

Take a derivative in the fourier domain, with high frequency attentuation.

Parameters
  • x (np.array (float)) – array of time series to differentiate

  • dt (float) – time step size

  • params (list (float) or float) – the high frequency cut off

  • options (dict {'even_extension': (bool), 'pad_to_zero_dxdt': (bool)}, optional) –

    a dictionary consisting of 2 key value pairs:

    • ’even_extension’: if True, extend the time series with an even extension so signal starts and ends at the same value.

    • ’pad_to_zero_dxdt’: if True, extend the time series with extensions that smoothly force the derivative to zero. This allows the spectral derivative to fit data which does not start and end with derivatives equal to zero.

Returns

a tuple consisting of:

  • x_hat: estimated (smoothed) x

  • dxdt_hat: estimated derivative of x

Return type

tuple -> (np.array, np.array)