R/unwrap.R

Defines functions unwrap

#' Unwrap angle.
#'
#' Maps jumps greater than pi to their 2pi complement.
#'
#' @param y Input vector in degrees.
#' @returns Unwrapped vector in degrees.
#' @noRd
unwrap <- function(y) {
  cx <- 0.0 # single number, c in C but reserved namespace
  dy <- 0.0 # single number
  # vector
  cv <- rep(0, length(y))

  for (i in 2:length(y)) {
    dy <- (y[i] - y[i - 1]) / R2D
    if (dy > pi) {
      cx <- cx - 2 * pi
    } else if (dy < -pi) {
      cx <- cx + 2 * pi
    }
    cv[i] <- cx
  }
  yu <- rep(0, length(y))
  for (i in seq_along(y)) {
    yu[i] <- y[i] + cv[i] * R2D
  }
  yu
}

Try the snvecR package in your browser

Any scripts or data that you put into this service are public.

snvecR documentation built on April 4, 2025, 12:12 a.m.