Engee documentation

convmtx

The convolution matrix.

Library

EngeeDSP

Syntax

Function call

  • A = convmtx(h,n) — returns the convolution matrix A, the product of which is based on n-an elementary vector x it is a convolution h and x.

Arguments

Input arguments

# h is the input vector

+ vector

Details

The input vector, specified as a row vector or column vector.

Типы данных

Float32, Float64

# n is the length of the convolution vector

+ a positive integer

Details

The length of the convolution vector, set as a positive integer.

  • If h — a column vector of length m Then A — matrix size (m+n−1)×n, and the product A and column vectors x length n "It’s a package h and x.

  • If h — vector-a string of length m Then A — matrix size n×(m+n−1), and the product of a vector is a string x length n on A "It’s a package h and x.

Output arguments

# A is the convolution matrix

+ the matrix

Details

The convolution matrix of the input vector h and vectors x, returned as a matrix.

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

Algorithms

  • Function convmtx uses the function toeplitz to generate the convolution matrix.

  • Function convmtx handles boundary conditions by adding zeros.

Literature

  1. Orfanidis, Sophocles J. Introduction to Signal Processing. Englewood Cliffs, NJ: Prentice-Hall, 1996, pp. 524–529.