Q:

Dynamic Time Wrap (DTW) normalizing, python implementation

I am trying to obtain the correlation between two time series with DTW. But I am finding that the amplitude of the time series influence the result. This is my code:
import numpy as np
import rpy2.robjects.numpy2ri
rpy2.robjects.numpy2ri.activate()
from rpy2.robjects.packages import importr

# Set up our R namespaces
R = rpy2.robjects.r
DTW = importr('dtw')

L1 = [np.sin(i*.01) for i in range(350)];
L2 = [np.sin(i*.01) + 1 for i in range(350)]
L3 = 100*[np.sin(i*.01) + 1 for i in range(350)]

alignment = R.dtw(L1, L2, keep=True)
correlation1 = alignment.rx('normalizedDistance')[0][0]

alignment = R.dtw(L1, L3, keep=True)
correlation2 = alignment.rx('normalizedDistance')[0][0]

The value of correlation1 is  0.4427365468841718 the value of correlation2 is 0.5861839240861364.
I was wondering if there is a simple way to normalize the result.
Thanks in advance

A:

One option would be to normalise the time series themselves. This makes the process shift and scaling invariant.
import numpy as np
import matplotlib.pyplot as plt

def normalise(series):
    max_value = max(series)
    min_value = min(series)
    return (series - min_value) / (max_value - min_value)

if __name__ == "__main__":
    L1 = [np.sin(i*.01) for i in range(350)]
    L2 = [np.sin(i*.01) + 1 for i in range(350)]
    L3 = 100*[np.sin(i*.01) + 1 for i in range(350)]

    norm_L1 = normalise(L1)
    norm_L2 = normalise(L2)
    norm_L3 = normalise(L3)

    # Correlate the normalised signals
    alignment = R.dtw(norm_L1, norm_L2, keep=True)
    correlation1 = alignment.rx('normalizedDistance')[0][0]

    alignment = R.dtw(norm_L1, norm_L3, keep=True)
    correlation2 = alignment.rx('normalizedDistance')[0][0]

Now norm_L1/L2/L3 all vary between 0 and 1. norm_L2 and norm_L3 are actually identical so their correlation with norm_L1 is guaranteed to be the same.

