R/compute_derivative.R

Defines functions compute_derivative

Documented in compute_derivative

#' Compute Derivative of Data
#'
#' This function computes the derivative of data (e.g., angular position) using either
#' a smooth spline fit or numerical gradient.
#'
#' @param x A numeric vector representing the independent variable (e.g., time).
#' @param y A numeric vector representing the dependent variable (e.g., angular position).
#' @param method A character string specifying the method to compute the derivative.
#'   Options are "spline" for smooth spline fitting or "gradient" for numerical gradient using \code{pracma::gradient}.
#'   Default is "spline".
#' @param deriv An integer specifying the order of the derivative. Default is 1 for the first derivative.
#'   Higher values (e.g., 2) will return higher derivatives (second derivative, etc.).
#'
#' @return A numeric vector representing the derivative (e.g., angular velocity or acceleration).
#'
#' @export
compute_derivative <- function(x, y, method = c("spline", "gradient"), deriv = 1) {

  # Match method argument to ensure it's valid
  method <- match.arg(method)

  if (method == "spline") {

    # Fit smooth spline
    y_0_spline <- smooth.spline(x, y)

    # Take the first derivative
    y_deriv_result <- predict(y_0_spline, deriv = deriv)$y

  } else if (method == "gradient") {

    # Compute numerical gradient using pracma::gradient
    y_deriv <- y

    # Apply the gradient function 'deriv' times to compute higher derivatives
    for (i in 1:deriv) {

      #update the derivative to respect derive
      y_deriv <- pracma::gradient(y_deriv, x)

    }

    #save the final derivative
    y_deriv_result <- y_deriv
  }

  return(y_deriv_result)
}
Kneerav/biomechanics documentation built on July 16, 2025, 4:51 p.m.