在 python 中实现一维卷积的最佳方法是什么?

发布于 2025-01-10 21:31:17 字数 732 浏览 0 评论 0原文

我正在尝试为信号实现一维卷积。

它应该具有相同的输出:

ary1 = np.array([1, 1, 2, 2, 1])
ary2 = np.array([1, 1, 1, 3])
conv_ary = np.convolve(ary2, ary1, 'full')

>>>> [1 2 4 8 8 9 7 3]

我想出了这种方法:

def convolve_1d(signal, kernel):
    n_sig = signal.size
    n_ker = kernel.size
    n_conv = n_sig - n_ker + 1

    # by a factor of 3.
    rev_kernel = kernel[::-1].copy()
    result = np.zeros(n_conv, dtype=np.double)
    for i in range(n_conv):
        result[i] = np.dot(signal[i: i + n_ker], rev_kernel)
    return result

但我的结果是 [8,8] 我可能必须对数组进行零填充并更改其索引。

有没有更顺利的方法来达到预期的结果?

I am trying to implement 1D-convolution for signals.

It should have the same output as:

ary1 = np.array([1, 1, 2, 2, 1])
ary2 = np.array([1, 1, 1, 3])
conv_ary = np.convolve(ary2, ary1, 'full')

>>>> [1 2 4 8 8 9 7 3]

I came up with this approach:

def convolve_1d(signal, kernel):
    n_sig = signal.size
    n_ker = kernel.size
    n_conv = n_sig - n_ker + 1

    # by a factor of 3.
    rev_kernel = kernel[::-1].copy()
    result = np.zeros(n_conv, dtype=np.double)
    for i in range(n_conv):
        result[i] = np.dot(signal[i: i + n_ker], rev_kernel)
    return result

But my result is [8,8] I might have to zero pad my array instead and change its indexing.

Is there a smoother way to achieve the desired outcome?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

極樂鬼 2025-01-17 21:31:17

这是一个可能的解决方案:

def convolve_1d(signal, kernel):
    kernel = kernel[::-1]
    return [
        np.dot(
            signal[max(0,i):min(i+len(kernel),len(signal))],
            kernel[max(-i,0):len(signal)-i*(len(signal)-len(kernel)<i)],
        )
        for i in range(1-len(kernel),len(signal))
    ]

这是一个示例:

>>> convolve_1d([1, 1, 2, 2, 1], [1, 1, 1, 3])
[1, 2, 4, 8, 8, 9, 7, 3]

Here is a possible solution:

def convolve_1d(signal, kernel):
    kernel = kernel[::-1]
    return [
        np.dot(
            signal[max(0,i):min(i+len(kernel),len(signal))],
            kernel[max(-i,0):len(signal)-i*(len(signal)-len(kernel)<i)],
        )
        for i in range(1-len(kernel),len(signal))
    ]

Here is an example:

>>> convolve_1d([1, 1, 2, 2, 1], [1, 1, 1, 3])
[1, 2, 4, 8, 8, 9, 7, 3]
独闯女儿国 2025-01-17 21:31:17

另一种解决方案,但效率不高:

import numpy as np
def convolve_1d(signal, kernel):
    kernel = kernel[::-1]
    k = len(kernel)
    s = len(signal)
    signal = [0]*(k-1)+signal+[0]*(k-1)
    n = s+(k-1)
    res = []
    for i in range(s+k-1):
        res.append(np.dot(signal[i:(i+k)], kernel))
    return res

A = [1,1,2,2,1]
B = [1,1,1,3]
# result = [1,2,4,8,8,9,7,3]
print(convolve_1d(A, B))

Another solution, but it is not efficient:

import numpy as np
def convolve_1d(signal, kernel):
    kernel = kernel[::-1]
    k = len(kernel)
    s = len(signal)
    signal = [0]*(k-1)+signal+[0]*(k-1)
    n = s+(k-1)
    res = []
    for i in range(s+k-1):
        res.append(np.dot(signal[i:(i+k)], kernel))
    return res

A = [1,1,2,2,1]
B = [1,1,1,3]
# result = [1,2,4,8,8,9,7,3]
print(convolve_1d(A, B))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文