% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/utils_nmr_qc.R
\name{lw}
\alias{lw}
\title{Full Width at Half Maximum (FWHM) Estimation}
\usage{
lw(X, ppm = NULL, shift = c(-0.1, 0.1), sf)
}
\arguments{
\item{X}{Numeric matrix (spectra in rows) \emph{or} a named list as returned by
\code{\link{read1d}}/\code{read1d_proc} containing \code{X}, \code{ppm}, and \code{meta}.}

\item{ppm}{Numeric vector of chemical shift values (ppm) corresponding to columns of \code{X}.
If \code{NULL}, \code{ppm} is inferred in the following order:
\enumerate{
\item \code{attr(X, "m8_axis")$ppm} (if present),
\item numeric \code{colnames(X)} (if present).
}}

\item{shift}{Numeric vector of length 2. Chemical shift range containing the peak
(e.g., \code{c(-0.1, 0.1)} for TSP).}

\item{sf}{Spectrometer frequency in MHz. Either a single numeric (recycled across spectra)
or a numeric vector of length \code{nrow(X)} (one value per spectrum).
If \code{X} is a list input and \code{sf} is missing, \code{sf} is taken from
\code{X$meta$a_SFO1} when available. If \code{sf} is missing for a matrix input,
the function attempts to use \code{attr(X, "m8_meta")$a_SFO1} when present.}
}
\value{
Numeric vector of FWHM values in Hz (length \code{nrow(X)}).
}
\description{
Estimates the full width at half maximum (FWHM; line width) of a singlet-like peak
within a specified chemical-shift range for each spectrum.
}
\details{
For each spectrum, the function:
\enumerate{
\item extracts the region defined by \code{shift},
\item finds the peak apex within that region,
\item computes the half-height level relative to the local baseline (minimum in the window),
\item estimates the left and right half-height crossing points by linear interpolation,
\item converts the width from ppm to Hz using \code{sf} (MHz), i.e. \code{Hz = ppm * sf}.
}

If no valid half-height crossings can be found (e.g., very low SNR or truncated peak),
\code{NA} is returned for that spectrum.
}
\note{
The ppm axis may be increasing or decreasing; FWHM is computed as an absolute width
and is therefore independent of axis direction.
}
\examples{
# Simulated NMR peaks with different linewidths
ppm <- seq(-0.2, 0.2, length.out = 1000)

# generate peaks with increasing width
sds <- seq(0.01, 0.03, length.out = 10)

X <- t(sapply(sds, function(s)
  dnorm(ppm, mean = 0, sd = s)
))

sf <- 600  # spectrometer frequency in MHz

fwhm_vals <- lw(X, ppm = ppm, shift = c(-0.1, 0.1), sf = sf)

plot(sds, fwhm_vals,
  xlab = "Gaussian sd",
  ylab = "Estimated FWHM (Hz)",
  pch = 16)
}
