% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/preproc_norm_eretic.R
\name{norm_eretic}
\alias{norm_eretic}
\title{Normalise Spectra Using ERETIC Signal}
\usage{
norm_eretic(
  X,
  integr = FALSE,
  ppm = NULL,
  pos = NULL,
  search = c(10, 16),
  width = 0.2,
  warn_absent = TRUE,
  noise_win = c(9.8, 10.2),
  snr_factor = 10
)
}
\arguments{
\item{X}{Numeric matrix or vector. NMR spectra with spectra in rows.
If \code{ppm} is not provided, it is inferred from \code{colnames(X)}.}

\item{integr}{Logical. If \code{TRUE}, returns the ERETIC integral per spectrum.
If \code{FALSE}, returns the normalised spectra.}

\item{ppm}{Numeric vector of chemical shift positions. If \code{NULL}, inferred from \code{colnames(X)}.}

\item{pos}{Numeric scalar or \code{NULL}. If \code{NULL} (default), ERETIC position is discovered
within \code{search}. If provided, the ERETIC window is centered on \code{pos}.}

\item{search}{Numeric vector of length 2. Ppm window used to discover ERETIC when \code{pos = NULL}.}

\item{width}{Numeric scalar. Full integration window width in ppm (default 0.2 gives \code{pos ± 0.1}).}

\item{warn_absent}{Logical. If \code{TRUE}, warn when ERETIC integral is zero/invalid for individual spectra.}

\item{noise_win}{Numeric vector of length 2. Ppm window for estimating noise intensity.}

\item{snr_factor}{Numeric scaler. Minimum Signal-to-Noise Ratio required for detecting the ERETIC peak.}
}
\value{
If \code{integr = TRUE}, numeric vector of ERETIC integrals.
If \code{integr = FALSE}, numeric matrix of normalised spectra.
}
\description{
Normalises 1D NMR spectra using an ERETIC reference signal. By default
the ERETIC position is discovered in a search window and the spectra are
normalised by the integral in a narrow window around the detected position.
Power users can supply \code{pos} to enforce a fixed ERETIC position.
}
\details{
Binning/normalisation history is recorded in \code{attr(X, "m8_prep")} if present.
Ppm axis values are stored in \code{attr(X, "m8_axis")$ppm}.
}
\examples{
set.seed(123)

n <- 1000
ppm <- seq(0, 14, length.out = n)

gauss <- function(x, c, h, w = 0.05) {
  h * exp(-((x - c)^2) / (2 * w^2))
}

heights <- c(100, 80, 60, 40, 20)

spectra <- sapply(heights, function(h)
  rnorm(n, 0, 0.01) + gauss(ppm, 12, h)
)

spectra <- t(spectra)   # 5 spectra × 1000 variables

plot_spec(spectra, ppm, shift = c(11, 13))

X_norm <- norm_eretic(spectra, ppm=ppm)
plot_spec(X_norm, ppm, shift=c(11, 13))
}
