R/Read_IMU_Filter.R

Defines functions imu_filter_gyroscope

Documented in imu_filter_gyroscope

#' Low-Pass filter Gyroscope data
#'
#' @inheritParams check_second
#' @inheritParams read_AG_IMU
#' @inheritParams read_AG_counts
#' @param samp_rate The sampling rate, in Hz
#'
#' @return A data frame with filtered gyroscope data
#'
#' @examples
#' data(imu_to_collapse)
#' imu_filter_gyroscope(imu_to_collapse, 100)
#'
#' @export
imu_filter_gyroscope <- function(
  AG, samp_rate, filter_hz = 35, verbose = FALSE
) {

  if (!isTRUE(requireNamespace("seewave", quietly = TRUE))) {
    stop(
      "You must install the seewave package to filter IMU data",
      call. = FALSE
    )
  }

  if (verbose) message_update(19, filter_hz = filter_hz)

  gyro_names <- grepl("gyroscope", names(AG), ignore.case = TRUE)
  AG[ , gyro_names] <- sapply(
    AG[, gyro_names],
    function(x) {
      seewave::bwfilter(
        wave = x,
        f = samp_rate,
        n = 2,
        to = filter_hz
      )
    }
  )

  if (verbose) message_update(20)

  return(AG)

}

Try the AGread package in your browser

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

AGread documentation built on Sept. 13, 2022, 5:06 p.m.