Spectral Operations¶
spectrakit.ops.spectral_subtract ¶
Subtract background from spectrum.
Computes spectrum - factor * background. Useful for background
subtraction, solvent subtraction, or difference spectroscopy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spectrum
|
ndarray
|
Spectrum or batch, shape |
required |
background
|
ndarray
|
Spectrum to subtract, shape |
required |
factor
|
float
|
Scaling factor for background before subtraction. |
1.0
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Difference spectrum, same shape as spectrum. |
Raises:
| Type | Description |
|---|---|
SpectrumShapeError
|
If shapes are incompatible (different number of wavelength points). |
EmptySpectrumError
|
If spectrum has zero elements. |
Source code in src/spectrakit/ops/subtract.py
spectrakit.ops.spectral_average ¶
Compute the mean spectrum from a batch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
intensities
|
ndarray
|
Spectral batch, shape |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Mean spectrum, shape |
Raises:
| Type | Description |
|---|---|
SpectrumShapeError
|
If input is not 2-D. |
Source code in src/spectrakit/ops/average.py
spectrakit.ops.spectral_interpolate ¶
spectral_interpolate(
intensities: ndarray,
wavenumbers: ndarray,
new_wavenumbers: ndarray,
kind: str = "linear",
) -> np.ndarray
Interpolate spectra onto a new wavenumber axis.
Useful for aligning spectra measured on different instruments or resampling to a uniform grid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
intensities
|
ndarray
|
Spectral intensities, shape |
required |
wavenumbers
|
ndarray
|
Original wavenumber axis, shape |
required |
new_wavenumbers
|
ndarray
|
Target wavenumber axis, shape |
required |
kind
|
str
|
Interpolation method ( |
'linear'
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Interpolated intensities, shape |
Raises:
| Type | Description |
|---|---|
SpectrumShapeError
|
If intensities is not 1-D or 2-D. |
EmptySpectrumError
|
If intensities has zero elements. |
Source code in src/spectrakit/ops/interpolate.py
spectrakit.ops.spectral_correlate ¶
spectral_correlate(
query: ndarray,
reference: ndarray,
*,
mode: str = "full",
normalize: bool = True,
) -> np.ndarray
Compute cross-correlation between spectral signals.
Wraps :func:scipy.signal.correlate with optional L2 normalization
so the peak value equals the cosine similarity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
ndarray
|
Query spectrum, shape |
required |
reference
|
ndarray
|
Reference spectrum, shape |
required |
mode
|
str
|
Correlation mode passed to |
'full'
|
normalize
|
bool
|
If |
True
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Cross-correlation array. Shape depends on mode: |
ndarray
|
|
ndarray
|
|
ndarray
|
|
Raises:
| Type | Description |
|---|---|
SpectrumShapeError
|
If query is not 1-D or 2-D, or reference is not 1-D. |
SpectrumShapeError
|
If spectral widths do not match. |
EmptySpectrumError
|
If inputs have zero elements. |
ValueError
|
If mode is not one of |
Examples:
>>> import numpy as np
>>> from spectrakit import spectral_correlate
>>> a = np.array([0.0, 1.0, 0.0])
>>> spectral_correlate(a, a, mode="same").shape
(3,)
Source code in src/spectrakit/ops/correlate.py
spectrakit.ops.spectral_align ¶
spectral_align(
intensities: ndarray,
reference: ndarray,
*,
max_shift: int | None = None,
fill_value: float | None = None,
) -> tuple[np.ndarray, int | np.ndarray]
Align spectra to a reference using cross-correlation.
Finds the integer shift that maximizes cross-correlation between each spectrum and the reference, then applies the shift.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
intensities
|
ndarray
|
Spectrum or batch to align, shape |
required |
reference
|
ndarray
|
Reference spectrum, shape |
required |
max_shift
|
int | None
|
Maximum allowed shift in points. |
None
|
fill_value
|
float | None
|
Value for positions exposed by the shift.
If |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Tuple of |
int | ndarray
|
|
tuple[ndarray, int | ndarray]
|
|
Raises:
| Type | Description |
|---|---|
SpectrumShapeError
|
If intensities is not 1-D or 2-D, or reference is not 1-D. |
SpectrumShapeError
|
If spectral widths do not match. |
EmptySpectrumError
|
If inputs have zero elements. |
ValueError
|
If max_shift < 0. |
Examples:
>>> import numpy as np
>>> from spectrakit import spectral_align
>>> ref = np.zeros(50); ref[25] = 1.0 # peak at 25
>>> shifted = np.zeros(50); shifted[30] = 1.0 # peak at 30
>>> aligned, shift = spectral_align(shifted, ref)
>>> shift
-5