This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

convmtx

Convolution matrix

Syntax

A = convmtx(h,n)

Description

example

A = convmtx(h,n) returns the convolution matrix, A, such that the product of A and an n-element vector, x, is the convolution of h and x. A and h should satisfy .

Examples

collapse all

Computing a convolution using conv when the signals are vectors is generally more efficient than using convmtx. For multichannel signals, convmtx might be more efficient.

Compute the convolution of two random vectors, a and b, using both conv and convmtx. The signals have 1000 samples each. Compare the times spent by the two functions. Eliminate random fluctuations by repeating the calculation 30 times and averaging.

Nt = 30;
Na = 1000;
Nb = 1000;

tcnv = 0;
tmtx = 0;

for kj = 1:Nt
    a = randn(Na,1);
    b = randn(Nb,1);

    tic
    n = conv(a,b);
    tcnv = tcnv+toc;

    tic
    c = convmtx(b,Na);
    d = c*a;
    tmtx = tmtx+toc;
end

t1col = [tcnv tmtx]/Nt
t1col = 1×2

    0.0010    0.0283

t1rat = tcnv\tmtx
t1rat = 29.5718

conv is about two orders of magnitude more efficient.

Repeat the exercise for the case where a is a multichannel signal with 1000 channels. Optimize conv's performance by preallocating.

Nchan = 1000;

tcnv = 0;
tmtx = 0;

n = zeros(Na+Nb-1,Nchan);

for kj = 1:Nt
    a = randn(Na,Nchan);
    b = randn(Nb,1);
    
    tic
    for k = 1:Nchan
        n(:,k) = conv(a(:,k),b);
    end
    tcnv = tcnv+toc;

    tic
    c = convmtx(b,Na);
    d = c*a;
    tmtx = tmtx+toc;
end

tmcol = [tcnv tmtx]/Nt
tmcol = 1×2

    0.5143    0.1909

tmrat = tcnv/tmtx
tmrat = 2.6941

convmtx is about three times as efficient as conv.

Input Arguments

collapse all

Input vector, specified as a row or column.

Data Types: single | double

Length of vector to convolve, specified as a positive integer.

  • If h is a column vector of length m, A is (m+n-1)-by-n, and the product of A and a column vector, x, of length n is the convolution of h and x.

  • If h is a row vector of length m, A is n-by-(m+n-1), and the product of a row vector, x, of length n with A is the convolution of h and x.

Output Arguments

collapse all

Convolution matrix of input h and the vector x, returned as a matrix.

Algorithms

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

  • convmtx handles edge conditions by zero padding.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

See Also

| | | |

Introduced before R2006a