VPdtw: Variable Penalty Dynamic Time Warping function

View source: R/VPdtw.R

VPdtwR Documentation

Variable Penalty Dynamic Time Warping function

Description

Use variable penalty dynamic time warping to align one (or many) query signal(s) to a master signal. Penalties are incurred whenever a non-diagonal move is taken.

Usage

VPdtw(reference, query, penalty = 0, maxshift = 50, 
      Reference.type = c("random","median","mean","trimmed"))

Arguments

reference

Reference signal, NULL or a vector, see details.

query

Query signal, a vector or matrix, see details

penalty

Penalty term, a vector of same length as reference (not checked) or a matrix. See details. Default is 0 repeated to the length of reference

maxshift

Maximum allowable shift, an integer

Reference.type

Choices for reference if NULL is input

Details

Performs variable penalty dynamic time warping of query to reference. Sakoe Chiba dtw used with width maxshift.

The basic operation aligns a query vector to a reference vector.

If reference is not specified and query is a matrix then the reference is created based on the value of Reference.type. The four choices are random, median, mean and trimmed. These choose a column of query at random as a reference, or the piecewise median, mean or trimmed mean (with trim=0.1) with missing values removed.

If query is a matrix and penalty is a vector then the same penalty is used to align each column of query to the reference. Different alignment paths are chosen for each column of the query matrix.

If query is a vector and penalty is a matrix then the query is aligned to the reference several times, using each column of the penalty matrix in turn as the penalty for the alignment.

If query and penalty are matrices then nothing happens. If you wish to align many query vectors and test many penalty vectors at the same time then do the appropriate looping (over queries, or penalties) outside of VPdtw.

Value

xVals

For plotting everything to correct index

reference

reference vector used by VPdtw expanded by NAs for plotting

query

query passed to VPdtw

penalty

penalty passed to VPdtw

warpedQuery

result of alignment, same class as query

shift

shifts required to achieve alignment

summary

Summary information about the alignment. Used for print.VPdtw

information

Information about the alignment. Used for print.VPdtw

Author(s)

David Clifford, Glenn Stone

References

Alignment Using Variable Penalty Dynamic Time Warping by CLIFFORD, D; STONE, G; MONTOLIU, I; et al. ANALYTICAL CHEMISTRY Volume: 81 Issue: 3 Pages: 1000-1007 Published: 2009

See Also

Also check out the dtw package by Toni Giorgino which covers many variations of dynamic time warping.

Examples

  ## Citation
  citation("VPdtw")

  ## Basic Examples of zero-penalty DTW

  ## Example of exact fit in the middle
  query <- c(1,5,4,3,9,8,5,2,6,5,4)
  reference <- c(rnorm(5),query,rnorm(5))
  lambda <- rep(0,length(reference))
  maxshift <- 11
  res <- VPdtw(reference,query,lambda,maxshift)
  plot(res)
  res

  ## Example of exact fit on one side
  reference <- c(1,5,4,3,9,8,5,2,6,5,4)
  query <- c(rnorm(5),reference)
  reference <- c(reference,rnorm(5))
  lambda <- rep(0,length(reference))
  maxshift <- 6
  res <- VPdtw(reference,query,lambda,maxshift)
  plot(res)
  res

  ## Example of exact fit on the other side
  reference <- c(1,5,4,3,9,8,5,2,6,5,4)
  query <- c(reference,rnorm(5))
  reference <- c(rnorm(5),reference)
  lambda <- rep(0,length(reference))
  maxshift <- 6
  res <- VPdtw(reference,query,lambda,maxshift)
  plot(res)
  res

  ## Example of exact fit except where one query gets dropped and its all on one side
  reference <- c(1,5,4,3,9,8,5,2,6,5,4)
  query <- c(reference[1:5],20,reference[6:11])
  reference <- c(rnorm(5),reference)
  query <- c(query,rnorm(5))
  lambda <- rep(0,length(reference))
  maxshift <- 6
  res <- VPdtw(reference,query,lambda,maxshift)
  plot(res)
  res
  
## Examples that use penalty term. Examples with long signals

data(reference)
data(query)
## Do alignment on log scale
reference <- log(reference)
query <- log(query)

## VPdtw
result <- VPdtw(reference=reference[1:2500],query=query[1:2500],
                penalty=dilation(reference[1:2500],150)/4,maxshift=150)
plot(result)
result

## Zero penalty DTW
result2 <- VPdtw(reference=reference[1:2500],query=query[1:2500],
                 penalty=rep(0,length(reference)),maxshift=150)
plot(result2)

## Try both penalties at the same time
penalty <- dilation(reference,350)/5
penalty <- cbind(penalty,rep(0,length(penalty)))

result <- VPdtw(reference,query,penalty=penalty,maxshift=350)
plot(result,"After")
plot(result,"Shift")
result

## All three plots at once
plot(result)


VPdtw documentation built on July 14, 2022, 5:07 p.m.