Skip to contents

Split the bursts in an IMU vector into bursts of a given time duration. The result is a list of vectors of the same length as the input, with the same class as x.

Usage

split_imu(x, interval)

Arguments

x

An IMU vector (e.g. acc, mag, gyro).

interval

Numeric or units object defining the time intervals at which x will be split. If no units are provided, the interval is assumed to be in period units of x (i.e., 1 divided by the frequency units).

Value

A list of vectors (same class as x), the same length as x. Each element contains the split pieces of the corresponding input burst.

Examples

a <- acc(
  list(cbind(X = 1:60, Y = 1:60), cbind(X = 101:140)),
  frequency = c(units::set_units(20, "Hz"), units::set_units(40, "Hz")),
  start = as.POSIXct(c(0, 10), tz = "UTC")
)

x <- split_imu(a, units::set_units(1, "s"))
x
#> [[1]]
#> <acceleration[3]>
#> [1] (10.5 10.5) (30.5 30.5) (50.5 50.5)
#> # frequency: 20 [Hz]
#> [[2]]
#> <acceleration[1]>
#> [1] (120.5)
#> # frequency: 40 [Hz]

# Flatten to a single vector
flat <- purrr::reduce(x, c)
flat
#> <acceleration[4]>
#> [1] (10.5 10.5) (30.5 30.5) (50.5 50.5) (120.5)    
#> # frequency: 20 [Hz] - 40 [Hz]

# Start times are updated to match the start of each split component
starts(flat)
#> [1] "1970-01-01 00:00:00 UTC" "1970-01-01 00:00:01 UTC"
#> [3] "1970-01-01 00:00:02 UTC" "1970-01-01 00:00:10 UTC"

# Use merge_imu() on flat
identical(merge_imu(flat, drop = TRUE), a)
#> [1] TRUE

if (FALSE) { # \dontrun{
# In a dataframe, split and unnest to retain index matching
library(dplyr)
library(tidyr)

tbl <- tibble::tibble(id = c("a", "b"), burst = a)

tbl <- tbl |>
  mutate(burst = split_imu(burst, units::set_units(1, "s"))) |>
  unnest(burst) |>
  mutate(timestamp = starts(burst))

tbl

# Use merge_imu() to recover original bursts
tbl |>
  mutate(burst = merge_imu(burst, ids = id, drop = FALSE))
} # }