% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/cgmguru-functions-docs.R
\name{interpolate_cgm}
\alias{interpolate_cgm}
\title{Interpolate CGM Data}
\usage{
interpolate_cgm(df, reading_minutes = NULL, sort_time = FALSE,
 inter_gap = 45)
}
\arguments{
\item{df}{A dataframe containing CGM data with columns:
\itemize{
  \item \code{id}: Subject identifier
  \item \code{time}: POSIXct measurement timestamp
  \item \code{gl}: Glucose value in mg/dL
}}

\item{reading_minutes}{Time interval for the interpolation grid in minutes.
If omitted or \code{NULL}, it is calculated automatically per id as the
median positive time difference in the data.}

\item{sort_time}{Logical. If \code{TRUE}, sort rows within each id by
\code{time} in C++ before interpolation. Defaults to \code{FALSE}.}

\item{inter_gap}{Maximum gap in minutes to interpolate across. Defaults to
45; larger gaps split the event-detection grid.}
}
\value{
A tibble with columns \code{id}, interpolated \code{time}, and
  interpolated \code{gl}. Rows inside gaps larger than \code{inter_gap} are
  omitted.
}
\description{
Interpolates continuous glucose monitoring (CGM) data onto the same
iglu-compatible, midnight-aligned full-day grid used internally by
cgmguru's event-detection functions. The interpolation is implemented in C++
and is intended for users who want to inspect or reuse the preprocessed grid
behind \code{\link{detect_all_events}},
\code{\link{detect_hyperglycemic_events}}, and
\code{\link{detect_hypoglycemic_events}}.

For each subject, \code{interpolate_cgm()} builds an equally spaced grid at
\code{reading_minutes} intervals. If \code{reading_minutes} is omitted, it is
inferred per subject from the median positive timestamp difference. Glucose
values are linearly interpolated only across gaps up to \code{inter_gap};
larger gaps are treated as missing and removed from the returned data,
preserving segment boundaries used by event calculation.

The GRID-family functions \code{\link{grid}}, \code{\link{maxima_grid}}, and
\code{\link{excursion}} do not call this helper automatically; they operate
on the rows supplied by the user unless the caller explicitly passes an
interpolated dataset.
}
\examples{
df <- data.frame(
  id = "A",
  time = as.POSIXct(c("2026-01-01 00:15:00", "2026-01-01 00:25:00"),
                    tz = "UTC"),
  gl = c(100, 120)
)
interpolate_cgm(df)
}
\seealso{
\link{detect_all_events}, \link{detect_hyperglycemic_events},
  \link{detect_hypoglycemic_events}
}
