【DTW的实现】

'''原文链接 感谢博主
https://blog.csdn.net/weixin_39653948/article/details/124416873?ops_request_misc=&request_id=&biz_id=102&utm_term=DTW&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-0-124416873.142^v87^insert_down28v1,239^v2^insert_chatgpt&spm=1018.2226.3001.4187

'''

def dtw(s, t, window): #可以使用
    n, m = len(s), len(t)
    w = np.max([window, abs(n - m)])
    dtw_matrix = np.zeros((n + 1, m + 1))

    for i in range(n + 1):
        for j in range(m + 1):
            dtw_matrix[i, j] = np.inf
    dtw_matrix[0, 0] = 0

    for i in range(1, n + 1):
        for j in range(np.max([1, i - w]), np.min([m, i + w]) + 1):
            dtw_matrix[i, j] = 0

    for i in range(1, n + 1):
        for j in range(np.max([1, i - w]), np.min([m, i + w]) + 1):
            cost = abs(s[i - 1] - t[j - 1])

            # take last min from a square box
            last_min = np.min([dtw_matrix[i - 1, j], dtw_matrix[i, j - 1], dtw_matrix[i - 1, j - 1]])
            dtw_matrix[i, j] = cost + last_min

    return dtw_matrix


x = [1, 2, 3, 3, 5, 2, 6, 3]
y = [1, 2, 2, 2, 2, 2, 2, 4]
# x=torch.randn(1,8)
# print(x)
# y=torch.randn(1,8)

outputs = dtw(x, y, window=3)
print(outputs)


from fastdtw import fastdtw
from scipy.spatial.distance import euclidean

# x = np.array([1, 2, 3, 3, 7, 2, 2, 4])
# y = np.array([1, 2, 2, 2, 2, 2, 2, 4])

# x0 = torch.tensor([[1, 2, 3, 3, 7, 2, 2, 4]])
# print(x0)
# y = torch.tensor[1, 2, 2, 2, 2, 2, 2, 4]

# x=torch.randn(1,8)
# print(x)
# y=torch.randn(1,8)

x1=torch.tensor(x)
x1=x1.reshape(1,8)
y1=torch.tensor(y)
y1=y1.reshape(1,8)
distance1, path1 = fastdtw(x1, y1, dist=euclidean)
#
print(distance1)
print(path1)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值