chemotools.adaptation.functions.subtract_reference#

chemotools.adaptation.functions.subtract_reference(X: ndarray, reference: float | ndarray) → ndarray[source]#

Subtract a reference spectrum from every row of X.

A common operation in spectroscopy for blank or solvent subtraction, background removal, or single-beam to double-beam correction.

Parameters:
  • X (np.ndarray of shape (n_samples, n_features)) – Input spectra.

  • reference (float or np.ndarray) –

    Reference to subtract. Accepted shapes:

    • scalar float — subtracted from every element.

    • (1, n_features) — shared reference broadcast across all samples.

    • (n_samples, 1) — per-sample scalar subtracted from every feature.

    • (n_samples, n_features) — per-sample full spectrum.

    1-D inputs are rejected; use .reshape(1, -1) for a shared spectrum or .reshape(-1, 1) for a per-sample scalar.

Returns:

X_corrected – X - reference.

Return type:

np.ndarray of shape (n_samples, n_features)

Examples

>>> import numpy as np
>>> from chemotools.adaptation.functions import subtract_reference
>>> X = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
>>> ref = np.array([[0.5, 0.5, 0.5]])
>>> subtract_reference(X, ref)
array([[0.5, 1.5, 2.5],
       [3.5, 4.5, 5.5]])