Skip to contents

Aligns rows of an NMR spectral matrix to a reference spectrum by maximizing cross-correlation. Optionally uses the row-wise median spectrum as reference. Useful for minor spectral misalignments.

Usage

alignSegment(seg, idx_ref = 1, clim = 0.7, med = TRUE, norm = FALSE)

Arguments

seg

Numeric matrix. Each row is a 1D NMR spectrum segment.

idx_ref

Integer. Row index to use as reference spectrum. Ignored if med = TRUE.

clim

Numeric. Minimum cross-correlation threshold. Segments with lower similarity are not shifted.

med

Logical. If TRUE, the row-wise median spectrum is used as the reference.

norm

Logical. If TRUE, rows are scaled to the [0, 1] range using min-max scaling before alignment.

Value

A numeric matrix of aligned spectra with the same dimensions as seg. If med = TRUE, the output excludes the median row.

Details

Each spectrum is aligned to the reference by computing the cross-correlation and applying a lag-based circular shift. The shift is applied only if the cross-correlation exceeds clim. This function is intended for fine-tuning alignment across short ppm windows (i.e., segments).

Examples

set.seed(1)
x <- matrix(rnorm(1000), nrow = 10)
x[5, ] <- x[1, ] + 0.1  # Simulate minor shift
aligned <- alignSegment(x, clim = 0.6)