polynomial_fit

Methods based on fitting data with polynomials

pynumdiff.polynomial_fit.polydiff(x, dt_or_t, params=None, options=None, degree=None, window_size=None, step_size=1, kernel='friedrichs', axis=0)

Fit polynomials to the data, and differentiate the polynomials.

Parameters:
  • x (np.array[float]) – data to differentiate. May contain NaN values (missing data); NaNs are excluded from fitting and imputed by polynomial interpolation. May be multidimensional; see axis.

  • dt_or_t (float or array[float]) – This function supports variable step size. This parameter is either the constant \(\Delta t\) if given as a single float, or data locations if given as an array of same length as x.

  • params (list[int]) – (deprecated, prefer degree and window_size)

  • options (dict) – (deprecated, prefer step_size and kernel) a dictionary consisting of {‘sliding’: (bool), ‘step_size’: (int), ‘kernel_name’: (str)}

  • degree (int) – degree of the polynomial

  • window_size (int) – size of the sliding window, if not given no sliding

  • step_size (int) – step size for sliding

  • kernel (str) – name of kernel to use for weighting and smoothing windows (‘gaussian’ or ‘friedrichs’)

  • axis (int) – data dimension along which differentiation is performed

Returns:

  • x_hat (np.array) – estimated (smoothed) x

  • dxdt_hat (np.array) – estimated derivative of x

pynumdiff.polynomial_fit.savgoldiff(x, dt, params=None, options=None, degree=None, window_size=None, smoothing_win=None, axis=0)

Use the Savitzky-Golay to smooth the data and calculate the first derivative. It uses 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]) – data to differentiate. May be multidimensional; see axis.

  • dt (float) – step size

  • params (list) – (deprecated, prefer degree, window_size, and smoothing_win)

  • options (dict) – (deprecated)

  • degree (int) – degree of the polynomial

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

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

  • axis (int) – data dimension along which differentiation is performed

Returns:

  • x_hat (np.array) – estimated (smoothed) x

  • dxdt_hat (np.array) – estimated derivative of x

pynumdiff.polynomial_fit.splinediff(x, dt_or_t, params=None, options=None, degree=3, s=None, num_iterations=1, axis=0)

Find smoothed data and derivative estimates by fitting a smoothing spline to the data with scipy.interpolate.UnivariateSpline. Variable step size is supported with equal ease as uniform step size.

Parameters:
  • x (np.array[float]) – data to differentiate. May contain NaN values (missing data); NaNs are excluded from fitting and imputed by spline interpolation. May be multidimensional; see axis.

  • dt_or_t (float or array[float]) – This function supports variable step size. This parameter is either the constant \(\Delta t\) if given as a single float, or data locations if given as an array of same length as x.

  • params (list) – (deprecated, prefer degree, cutoff_freq, and num_iterations)

  • options (dict) – (deprecated, prefer num_iterations) a dictionary of {‘iterate’: (bool)}

  • degree (int) – polynomial degree of the spline. A kth degree spline can be differentiated k times.

  • s (float) – positive smoothing factor used to choose the number of knots. Number of knots will be increased until the smoothing condition is satisfied: \(\sum_t (x[t] - \text{spline}[t])^2 \leq s\)

  • num_iterations (int) – how many times to apply smoothing

  • axis (int) – data dimension along which differentiation is performed

Returns:

  • x_hat (np.array) – estimated (smoothed) x

  • dxdt_hat (np.array) – estimated derivative of x