DTWDT Functions
These are fundamental functions to calculate DTW distance distance array and DTW error between two time series.
Main function
dtwdt
returns time shift, distance function and DTW error between two signals.
Example:
stbarTime, stbar, dist, dtwerror = dtwdt(u0, u1, dt, dtwnorm="L2", maxLag=80, b=1, direction=1)
DTWDT.dtwdt
— Function.dtwdt(u0::Union{Array{Float32,1},Array{Float64,1}}, u1::Union{Array{Float32,1},Array{Float64,1}}, dt::Float64; dtwnorm::String="L2"
maxlag::Int64=80, b::Int64=1, direction::Int64=1)
returns minimum distance time lag and index in dist array, and dtw error between traces.
Arguments
u0, u1::Union{Array{Float32,1},Array{Float64,1}}
: Time series.dt::Float64
: time step (dt of u0 and u1 should be same)dtwnorm::String
: norm to calculate distance; effect on the unit of dtw error. (L2 or L1)- Note: L2 is not squard, thus distance is calculated as (u1[i]-u0[j])^2
maxlag::Int64
: number of maxLag id to search the distance.b::Int64t
: b value to controll in distance calculation algorithm (see Mikesell et al. 2015).direction::Int64
: length of noise data window, in seconds, to cross-correlate.
Outputs
stbarTime::Array{Float64,1}
: series of time shift at t.stbar::Array{Int64,1}
: series of minimum distance index in distance array.dist::Array{Int64,2}
: distance array.dtwerror::Float64
: dtw error (distance) between two time series.
Sub functions
DTWDT.DTWDTfunctions.computeErrorFunction
— Function.USAGE: err = computeErrorFunction( u1, u0, nSample, lag )
INPUT:
u1 = trace that we want to warp; size = (nsamp,1)
u0 = reference trace to compare with: size = (nsamp,1)
nSample = numer of points to compare in the traces
lag = maximum lag in sample number to search
norm = 'L2' or 'L1' (default is 'L2')
OUTPUT:
err = the 2D error function; size = (nsamp,2*lag+1)
The error function is equation 1 in Hale, 2013.
You could umcomment the L1 norm and comment the L2 norm if you want on Line 29
Original by Di Yang Last modified by Dylan Mikesell (25 Feb. 2015)
DTWDT.DTWDTfunctions.accumulateErrorFunction
— Function.USAGE: d = accumulationdiwmod( dir, err, nSample, lag, b )
INPUT:
dir = accumulation direction ( dir > 0 = forward in time, dir <= 0 = backward in time)
err = the 2D error function; size = (nsamp,2*lag+1)
nSample = numer of points to compare in the traces
lag = maximum lag in sample number to search
b = strain limit (integer value >= 1)
OUTPUT:
d = the 2D distance function; size = (nsamp,2*lag+1)
The function is equation 6 in Hale, 2013.
Original by Di Yang Last modified by Dylan Mikesell (25 Feb. 2015)
DTWDT.DTWDTfunctions.backtrackDistanceFunction
— Function.USAGE: stbar = backtrackDistanceFunction( dir, d, err, lmin, b )
INPUT:
dir = side to start minimization ( dir > 0 = front, dir <= 0 = back)
d = the 2D distance function; size = (nsamp,2*lag+1)
err = the 2D error function; size = (nsamp,2*lag+1)
lmin = minimum lag to search over
b = strain limit (integer value >= 1)
OUTPUT:
stbar = vector of integer shifts subject to |u(i)-u(i-1)| <= 1/b
The function is equation 2 in Hale, 2013.
Original by Di Yang Last modified by Dylan Mikesell (19 Dec. 2014)
DTWDT.DTWDTfunctions.computeDTWerror
— Function.Compute the accumulated error along the warping path for Dynamic Time Warping.
USAGE: function error = computeDTWerror( Aerr, u, lag0 )
INPUT:
Aerr = error MATRIX (equation 13 in Hale, 2013)
u = warping function (samples) VECTOR
lag0 = value of maximum lag (samples) SCALAR
Written by Dylan Mikesell Last modified: 25 February 2015