convmtx
的卷积矩阵。
库::`工程师`
例子:
卷积计算的效率
Details
使用函数计算卷积 *conv的* 当信号是向量时,它通常比使用函数更有效 *convmtx*. 对于多通道信号 *convmtx* 它可能更有效率。
我们来计算两个随机向量的卷积 a 和 b 使用as *conv的*,所以和 *convmtx*. 每个信号包含 1000 数数。 让我们比较两个函数所花费的时间。 通过重复计算消除随机波动 30 一次并平均结果。
import EngeeDSP.Functions: randn, convmtx, conv
Nt = 30
Na = 1000
Nb = 1000
tcnv = 0.0
tmtx = 0.0
for kj in 1:Nt
a = randn(Na,1)
b = randn(Nb,1)
tcnv += @elapsed conv(a, b)
tmtx += @elapsed begin
c = convmtx(b, Na)
d = c * a
end
end
t1col = [tcnv tmtx] / Nt
1×2 Matrix{Float64}:
0.00564892 0.00652422
t1rat = tcnv\tmtx
1.1549499858775314
功能 *conv的* 效率略高 *convmtx*.
让我们在以下情况下重复这个例子: a -多通道信号与 1000 渠道。 通过预分配内存来优化卷积性能。
Nchan = 1000
tcnv = 0.0
tmtx = 0.0
n = zeros(Na + Nb - 1, Nchan)
for kj in 1:Nt
a = randn(Na, Nchan)
b = randn(Nb, 1)
tcnv += @elapsed begin
for k in 1:Nchan
n[:, k] = conv(a[:, k], b)
end
end
tmtx += @elapsed begin
c = convmtx(b, Na)
d = c * a
end
end
t1col = [tcnv tmtx] / Nt
1×2 Matrix{Float64}:
5.07392 0.186065
t1rat = tcnv/tmtx
27.269651929720958