Baseline Correction¶
spectrakit.baseline.baseline_als ¶
baseline_als(
intensities: ndarray,
lam: float = DEFAULT_LAMBDA,
p: float = DEFAULT_P,
max_iter: int = DEFAULT_MAX_ITER,
tol: float = DEFAULT_TOL,
return_info: bool = False,
) -> np.ndarray | ConvergenceInfo
Estimate baseline using Asymmetric Least Squares smoothing.
Iteratively fits a smooth baseline by penalizing deviations asymmetrically: points above the baseline are penalized less (weight p) than points below (weight 1-p).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
intensities
|
ndarray
|
Spectral intensities, shape (W,) or (N, W). |
required |
lam
|
float
|
Smoothness parameter (lambda). Larger = smoother. Typical range: 1e4 to 1e9. |
DEFAULT_LAMBDA
|
p
|
float
|
Asymmetry parameter. Smaller values push baseline lower. Typical range: 0.001 to 0.05. |
DEFAULT_P
|
max_iter
|
int
|
Maximum number of iterations. |
DEFAULT_MAX_ITER
|
tol
|
float
|
Convergence tolerance on weight change. |
DEFAULT_TOL
|
return_info
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
ndarray | ConvergenceInfo
|
Estimated baseline (same shape as input), or |
ndarray | ConvergenceInfo
|
class: |
Raises:
| Type | Description |
|---|---|
SpectrumShapeError
|
If input is not 1-D or 2-D. |
EmptySpectrumError
|
If input has zero elements. |
Examples:
>>> corrected = intensities - baseline_als(intensities)
>>> info = baseline_als(intensities, return_info=True)
>>> print(info.iterations, info.converged)
Source code in src/spectrakit/baseline/als.py
spectrakit.baseline.baseline_snip ¶
baseline_snip(
intensities: ndarray,
max_half_window: int = DEFAULT_MAX_HALF_WINDOW,
decreasing: bool = True,
) -> np.ndarray
Estimate baseline using the SNIP algorithm.
Iteratively clips peaks by comparing each point to the average of its neighbors at increasing window sizes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
intensities
|
ndarray
|
Spectral intensities, shape (W,) or (N, W). |
required |
max_half_window
|
int
|
Maximum half-window size. Controls how broad the features that get clipped can be. |
DEFAULT_MAX_HALF_WINDOW
|
decreasing
|
bool
|
If True, iterate from max_half_window down to 1. |
True
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Estimated baseline, same shape as intensities. |
Raises:
| Type | Description |
|---|---|
SpectrumShapeError
|
If input is not 1-D or 2-D. |
EmptySpectrumError
|
If input has zero elements. |
Source code in src/spectrakit/baseline/snip.py
spectrakit.baseline.baseline_polynomial ¶
baseline_polynomial(
intensities: ndarray,
degree: int = DEFAULT_DEGREE,
max_iter: int = DEFAULT_MAX_ITER,
tol: float = DEFAULT_TOL,
return_info: bool = False,
) -> np.ndarray | ConvergenceInfo
Estimate baseline using iterative polynomial fitting.
Fits a polynomial, then iteratively removes points above it (peaks) and refits until convergence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
intensities
|
ndarray
|
Spectral intensities, shape (W,) or (N, W). |
required |
degree
|
int
|
Polynomial degree. Higher = more complex baselines. |
DEFAULT_DEGREE
|
max_iter
|
int
|
Maximum iterations for peak-removal loop. |
DEFAULT_MAX_ITER
|
tol
|
float
|
Convergence tolerance (fraction of points changed). |
DEFAULT_TOL
|
return_info
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
ndarray | ConvergenceInfo
|
Estimated baseline (same shape as input), or |
ndarray | ConvergenceInfo
|
class: |
Raises:
| Type | Description |
|---|---|
SpectrumShapeError
|
If input is not 1-D or 2-D. |
EmptySpectrumError
|
If input has zero elements. |
Source code in src/spectrakit/baseline/polynomial.py
spectrakit.baseline.baseline_rubberband ¶
Estimate baseline using the rubberband (convex hull) method.
Computes the lower convex hull of the spectrum and interpolates between the hull vertices to form the baseline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
intensities
|
ndarray
|
Spectral intensities, shape (W,) or (N, W). |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Estimated baseline, same shape as intensities. |
Raises:
| Type | Description |
|---|---|
SpectrumShapeError
|
If input is not 1-D or 2-D. |
EmptySpectrumError
|
If input has zero elements. |