% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/char_smooth.R
\name{char_smooth}
\alias{char_smooth}
\title{Smooth charcoal record to estimate low-frequency C_background}
\usage{
char_smooth(charcoal, pretreatment, smoothing, results = NULL, plot_data = 0L)
}
\arguments{
\item{charcoal}{Named list with elements \code{accI} (resampled CHAR,
length N), \code{ybpI} (resampled ages), and \code{ybp} (raw ages).}

\item{pretreatment}{Named list with element \code{yrInterp}.}

\item{smoothing}{Named list with elements:
\describe{
  \item{method}{Integer 1-5 selecting the smoothing method.}
  \item{yr}{Window width in years.}
}}

\item{results}{Named list (not used in R; kept for API symmetry).}

\item{plot_data}{0/1 flag; ignored in R (no diagnostic plots).}
}
\value{
The input \code{charcoal} list with an additional element
  \code{accIS}: the smoothed C_background series (length N).
}
\description{
Applies one of five smoothing methods to the interpolated charcoal
accumulation rate series (\code{charcoal$accI}) and stores the result in
\code{charcoal$accIS}.  Mirrors \code{CharSmooth.m} from the MATLAB v2.0
codebase.
}
\details{
## Smoothing methods
  | Index | Name | Implementation |
  |-------|------|----------------|
  | 1 | Lowess | [char_lowess()] with \code{iter = 0} |
  | 2 | Robust Lowess | [char_lowess()] with \code{iter = 4} |
  | 3 | Moving average | \code{zoo::rollapply(..., mean, partial=TRUE)} |
  | 4 | Running median + Lowess | Shifted-window median loop, then [char_lowess()] |
  | 5 | Running mode + Lowess | Shifted-window 100-bin mode loop, then [char_lowess()] |

  ## Span convention
  The smoothing window width in data-point units is
  \code{s = smoothing$yr / pretreatment$yrInterp}.  This is passed to
  [char_lowess()] as \code{span = s} (number of points), which
  converts it to the fraction required by \code{stats::lowess()} via
  \code{f = round(s) / N}.

  ## NaN bridging
  NaN values in \code{accI} (from record gaps) cannot be passed to
  [char_lowess()] directly.  They are bridged by linear interpolation
  before smoothing and restored afterward, matching the MATLAB fallback
  path in \code{CharSmooth.m} (used when the Curve Fitting Toolbox is
  absent).  Methods 4 and 5 always use the bridged series.

  ## Window selection for methods 4 and 5
  The boundary window is SHIFTED (not shrunk) to maintain exactly
  \code{round(s)} samples, matching MATLAB's loop logic.  Note that
  MATLAB's \code{round()} rounds 0.5 away from zero while R uses banker's
  rounding (round half to even); this can cause single-sample differences
  in window boundaries for odd half-integers.
}
\seealso{
[char_lowess()], [CharAnalysis()]
}
