-
Notifications
You must be signed in to change notification settings - Fork 1
/
interp2.R
31 lines (29 loc) · 1.08 KB
/
interp2.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#' @title Interpolation aka Time-Normalization
#'
#' @description Convenient wrapper to [`signal::interp1()`] for linear
#' interpolation. Assumes that you want interpolated values of
#' `xy_old` at `n_xy_new` equidistant data points.
#'
#' @param time_old Timestamps of the `xy_old` coordinates.
#' @param xy_old To-be normalized x or y coordinates.
#' @param n_xy_new Number of equidistant timepoints that should be generated.
#' Defaults to 101.
#'
#' @return Vector of length `n_xy_new` with interpolated x or y values.
#'
#' @references Wirth, R., Foerster, A., Kunde, W., & Pfister, R. (2020).
#' Design choices: Empirical recommendations for designing two-dimensional
#' finger tracking experiments. Behavior Research Methods, 52, 2394 - 2416.
#' \doi{10.3758/s13428-020-01409-0}
#'
#'
#' @examples
#' plot(interp2(0:10, (0:10)^2))
#'
#' @export
interp2 <- function(time_old, xy_old, n_xy_new = 101) {
time_old <- time_old - min(time_old)
time_old <- time_old / max(time_old)
time_old <- time_old * (n_xy_new - 1)
return(interp1(time_old, xy_old, seq(0, (n_xy_new - 1)), method = "linear"))
}