【问题标题】:Dynamic time warping in C [closed]C中的动态时间扭曲
【发布时间】:2021-03-23 18:16:32
【问题描述】:

所以我可以找到很多关于 Python 的 DTW 的指南,它们可以正常工作。但是我需要把代码翻译成C,但是我写C代码已经一年多了。

所以在 C 代码中我有这两个数组

static int codeLock[6][2] = {
{1, 0},
{2, 671},
{3, 1400},
{4, 2000},
{5, 2800},
        };;

static int code[6][2] = {
{1, 0},
{2, 600},
{3, 1360},
{4, 1990},
{5, 2800},
        };;

而且我要使用DTW来比较数组右边的codeLock(n)(1) / code(m)(1),所以1..5不应该看。

但是是的..

在 python 中,我有两个函数,一个是 euclidean distance,即:

def compute_euclidean_distance_matrix(x, y) -> np.array:
    """Calculate distance matrix
    This method calcualtes the pairwise Euclidean distance between two sequences.
    The sequences can have different lengths.
    """
    dist = np.zeros((len(y), len(x)))
    for i in range(len(y)):
        for j in range(len(x)):
            dist[i,j] = (x[j]-y[i])**2
    return dist

另一个是accumulated cost:

def compute_accumulated_cost_matrix(x, y) -> np.array:
    """Compute accumulated cost matrix for warp path using Euclidean distance
    """
    distances = compute_euclidean_distance_matrix(x, y)
    # Initialization
    cost = np.zeros((len(y), len(x)))
    cost[0,0] = distances[0,0]

    for i in range(1, len(y)):
        cost[i, 0] = distances[i, 0] + cost[i-1, 0]  
        
    for j in range(1, len(x)):
        cost[0, j] = distances[0, j] + cost[0, j-1]  

    # Accumulated warp path cost
    for i in range(1, len(y)):
        for j in range(1, len(x)):
            cost[i, j] = min(
                cost[i-1, j],    # insertion
                cost[i, j-1],    # deletion
                cost[i-1, j-1]   # match
            ) + distances[i, j] 
            
    return cost

这段代码来自我遵循的指南,以了解 DTW 的工作原理,但它在 python 中,我需要在 C 中。

这可以像这样在 python 中很容易地进行测试:

x = [0, 671, 1400, 2000, 2800]
y = [0, 600, 1360, 1990, 2800]

compute_euclidean       = compute_euclidean_distance_matrix(x, y)
compute_accumulated     = compute_accumulated_cost_matrix(x, y)


print("\ncompute_euclidean_distance_matrix")
print(compute_euclidean)
print("\ncompute_accumulated_cost_matrix")
print(compute_accumulated)
print("\nflipud")
print(np.flipud(compute_accumulated))

这是我的输出

我也查看了fastdtw,然后我的测试看起来像这样

x = [0, 671, 1400, 2000, 2800]
y = [0, 600, 1360, 1990, 2800]

dtw_distance, warp_path = fastdtw(x, y, dist=euclidean)

print("\ndtw_distance")
print(dtw_distance)

这是我的输出

你们中有人知道哪里有关于如何在 C 中完成所有这些操作的 GitHub/指南吗?因为这对我有很大帮助。如果您愿意帮我翻译这段代码,我当然会感激不尽。

【问题讨论】:

  • 你们中的任何人都知道哪里有关于如何在 C 中执行所有这些操作的 GitHub/指南 - 要在 C 中执行此操作,您需要了解 C。这确实不过看起来不是很复杂,所以不需要专家级别。
  • 这是我使用过的指南的链接 --> Link to guide
  • @EugeneSh。我的问题是我找不到一个可用的,我只是不希望你们中的一些人花很多时间翻译这个,如果那里有一个我不知道的 GitHub
  • 我的一个朋友发现了一个链接,我或许可以从中获得一些灵感:GitHub

标签: python c dtw


【解决方案1】:

动态时间扭曲的C实现在https://github.com/wannesm/dtaidistance/tree/master/dtaidistance/lib/DTAIDistanceC/DTAIDistanceC

您始终可以使用 Cython https://people.duke.edu/~ccc14/sta-663/FromPythonToC.html 将 python 转换为 C,但是生成的代码有时不起作用,最好完全重写

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-11
    • 1970-01-01
    • 2019-11-22
    • 1970-01-01
    • 2016-04-02
    • 2012-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多