Learn R Programming

IncDTW (version 1.0.2)

idtw: Incremental Dynamic Time Warping

Description

Wrapper function for the incremental designed C++ calculations of the global cost matrix and the BACKTRACK_cpp that finds the cheapest warping path.

Usage

idtw(Q, C, newO, gcm, dm, diffM = NULL, ws = NULL, 
     return_cm = FALSE,
     return_diffM = FALSE,
     return_wp = FALSE,
     return_diffp = FALSE,
     return_QC = FALSE)

Arguments

Q

one dimensional numeric vector

C

one dimensional numeric vector

newO

one dimensional numeric vector of new observations to be appended to C

gcm

global cost matrix, output from dtw(Q=Q, C=C, ws=ws)

dm

direction matrix, output from dtw(Q=Q, C=C, ws=ws)

diffM

differences matrix, output from dtw(Q=Q, C=C, ws=ws)

ws

integer, describes the window size for the sakoe chiba window. If NULL, then no window is applied. (default = NULL)

return_cm

boolean, if TRUE then the Matrix of costs (the absolute value) is returned. (default = FALSE)

return_diffM

boolean, if TRUE then the Matrix of differences (not the absolute value) is returned. (default = FALSE)

return_wp

boolean, if TRUE then the warping path is returned. (default = FALSE) If return_diffp == TRUE, then return_wp is set to TRUE as well.

return_diffp

boolean, if TRUE then the path of differences (not the absolute value) is returned. (default = FALSE)

return_QC

boolean, if TRUE then the input vectors Q and C are appended to the returned list. This is useful for the plot.idtw function. (default = FALSE)

Value

distance

the DTW distance, that is the element of the last row and last column of gcm

gcm

global cost matrix

dm

direction matrix (3=up, 1=diagonal, 2=left)

wp

warping path

ii

indices of C of the optimal path

jj

indices of Q of the optimal path

cm

Matrix of costs

diffM

Matrix of differences

diffp

path of differences

Q

input Q

C

input C

Details

The dynamic time warping distance is the element in the last row and last column of the global cost matrix.

References

Sakoe, H.; Chiba, S., Dynamic programming algorithm optimization for spoken word recognition, Acoustics, Speech, and Signal Processing [see also IEEE Transactions on Signal Processing], IEEE Transactions on , vol.26, no.1, pp. 43-49, Feb 1978. http://ieeexplore.ieee.org/xpls/abs_all.jsp?arnumber=1163055

Examples

Run this code
# NOT RUN {
Q <- cumsum(rnorm(100))
C <- Q[11:100] + rnorm(90, 0, 0.5)
newObs <-  c(2,3)# new observation
base <- dtw(Q=Q, C=C, ws = 15, return_diffM = TRUE) # the ordinary calculation


#--- recalculation from scratch with new observations
result0 <- dtw(Q=Q, C=c(C, newObs), ws = 15,  return_diffM = TRUE) # the ordinary calculation

#--- the incremental step with new observations
result1 <- idtw(Q, C, ws = 15, newO = newObs, gcm=base$gcm, dm=base$dm, diffM = base$diffM, 
               return_diffp = TRUE,  return_diffM = TRUE) 

#--- the incremental step with new observations, 
#     but already calculated additive costMatrix cm_add
mQ <- matrix(Q, ncol = length(newObs), nrow = length(Q), byrow = FALSE)
mC <- matrix(newObs, ncol = length(newObs), nrow = length(Q), byrow = TRUE)
cm_add <- matrix(abs(mQ - mC), ncol = length(newObs))
result2 <- idtw(Q=cm_add, C="cm_add", ws = 15, newO = newObs, gcm=base$gcm, dm=base$dm) 

c(result0$distance, result1$distance, result2$distance)


#--- now with integers
Q <- 1:10
C <- 2:20
base <- dtw(Q=Q, C=C, ws = 15, return_diffM = TRUE) # the ordinary calculation
# new observation
newObs <-  c(2L,3L)
# the incremental step with new observations
tmp1 <- idtw(Q, C, newO = newObs, gcm=base$gcm, dm=base$dm, diffM = base$diffM, 
      return_diffp = TRUE, ws = 15, return_diffM = TRUE) 
str(tmp1)



# }

Run the code above in your browser using DataLab