You are here : matlabSignal Processingconvmtx

convmtx() - Signal Processing

A = convmtx(h,n) returns the
convolution matrix, A, such that the product of A and
a vector, x, is the convolution of h and x.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.convmtx handles edge conditions
by zero padding.


Syntax

A = convmtx(h,n)


Example

Efficient Computation of ConvolutionOpen This Example
It is generally more efficient to compute a convolution using conv when the signals are vectors. 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 time 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
t1rat = tcnv\tmtx

t1col =

    0.0006    0.0599


t1rat =

   94.6975

conv is about two orders of magnitude more efficient.Repeat the exercise for the case where a is a mutichannel 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
tmrat = tcnv/tmtx

tmcol =

    0.2961    0.0884


tmrat =

    3.3487

convmtx is about 3 times as efficient as conv.


Output / Return Value


Limitations


Alternatives / See Also


Reference