pybaselines.utils.difference_matrix

pybaselines.utils.difference_matrix(data_size, diff_order=2, diff_format=None)[source]

Creates an n-order finite-difference matrix.

Parameters:
data_sizeint

The number of data points.

diff_orderint, optional

The integer differential order; must be >= 0. Default is 2.

diff_formatstr or None, optional

The sparse format to use for the difference matrix. Default is None, which will use the default specified in scipy.sparse.diags().

Returns:
diff_matrixscipy.sparse.spmatrix or scipy.sparse.sparray

The sparse difference matrix.

Raises:
ValueError

Raised if diff_order or data_size is negative.

Notes

The resulting matrices are sparse versions of:

import numpy as np
np.diff(np.eye(data_size), diff_order, axis=0)

This implementation allows using the differential matrices are they are written in various publications, ie. D.T @ D.

Most baseline algorithms use 2nd order differential matrices when doing penalized least squared fitting or Whittaker-smoothing-based fitting.