convmtx
The convolution matrix.
| Library |
|
Arguments
Examples
Efficiency of convolution calculation
Details
Calculating the convolution using the function conv when the signals are vectors, it is usually more efficient than using a function convmtx. For multi-channel signals convmtx it may be more efficient.
Let’s calculate the convolution of two random vectors a and b using as conv, so and convmtx. Each signal contains 1000 counts. Let’s compare the time spent by the two functions. Eliminate random fluctuations by repeating the calculation 30 once and averaging the results.
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
Function conv marginally more efficient convmtx.
Let’s repeat the example for the case when a — multi-channel signal with 1000 channels. Optimize the convolution performance by preallocating memory.
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