TrajVelocity: Velocity of a trajectory

View source: R/speed.R

TrajVelocityR Documentation

Velocity of a trajectory

Description

The velocity is approximated at each point of the trajectory using first-order finite differences. Central, forward or backward differences can be used. Central differences yield a more accurate approximation if the velocity is smooth. As a practical guide, if velocity doesn't change much between steps, use central differences. If it changes substantially (and not just as an artifact of recording noise), then use either forward or backward differences.

Usage

TrajVelocity(trj, diff = c("central", "forward", "backward"))

Arguments

trj

Trajectory whose velocity is to be calculated.

diff

Type of difference to be calculated, one of "central" (the default), "forward" or "backward".

Details

Intuitively, think of the central difference velocity at a point as the mean of the velocities of the two adjacent steps. Forward difference velocity is the velocity of the step starting at the point. Backward difference is the velocity of the step ending at the point.

Value

A vector of complex numbers representing the velocity at each point along the trajectory. The modulus (Mod(v)) is the magnitude of the velocity, i.e. the speed; the argument (Arg(v)) is the direction of the velocity; the real part (Re(v)) is velocity in the X direction; and the imaginary part (Im(v)) is velocity in the Y direction. The vector has an attribute, trj, with the trajectory as its value. If diff is "central", the first and last velocity values are NA since velocity cannot be calculated for them. If diff is "forward", the last value will be NA, and if diff is "backward", the first value will be NA.

See Also

TrajAcceleration for calculating acceleration; TrajResampleTime and TrajRediscretize to resample a trajectory to fixed time or length steps; TrajSpeedIntervals for calculating when speed crosses some threshold; Finite differences on Wikipedia.

Examples

set.seed(11)
trj <- TrajGenerate(100)
# calculate velocity
vel <- TrajVelocity(trj)

# Obtain speed over time, with NAs removed
speed <- na.omit(data.frame(speed = Mod(vel), time = trj$time))

plot(speed ~ time, speed, type = 'l')


trajr documentation built on May 29, 2024, 11:52 a.m.