Main Content

convmtx

卷积matrix

Description

example

A= convmtx(h,n)returns the convolution matrix,A, such that the product ofAand ann-element vector,x, is the convolution ofhandx.

Examples

collapse all

Computing a convolution usingconvwhen the signals are vectors is generally more efficient than usingconvmtx. For multichannel signals,convmtxmight be more efficient.

Compute the convolution of two random vectors,aandb, using bothconvandconvmtx. 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;forkj = 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;endt1col = [tcnv tmtx]/Nt
t1col =1×20.0008 0.0242
t1rat = tcnv\tmtx
t1rat = 31.3551

convis about two orders of magnitude more efficient.

Repeat the exercise for the case whereais a multichannel signal with 1000 channels. Optimizeconv's performance by preallocating.

Nchan = 1000; tcnv = 0; tmtx = 0; n = zeros(Na+Nb-1,Nchan);forkj = 1:Nt a = randn(Na,Nchan); b = randn(Nb,1); ticfork = 1:Nchan n(:,k) = conv(a(:,k),b);endtcnv = tcnv+toc; tic c = convmtx(b,Na); d = c*a; tmtx = tmtx+toc;endtmcol = [tcnv tmtx]/Nt
tmcol =1×20.1461 0.0786
tmrat = tcnv/tmtx
tmrat = 1.8589

convmtxis about three times as efficient asconv.

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.

  • Ifhis a column vector of lengthm,Ais(m+n-1)-by-n, and the product ofAand a column vector,x, of lengthnis the convolution ofhandx.

  • Ifhis a row vector of lengthm,Aisn-by-(m+n-1), and the product of a row vector,x, of lengthnwithAis the convolution ofhandx.

Output Arguments

collapse all

卷积matrix of inputhand the vectorx, returned as a matrix.

Algorithms

  • convmtxuses the functiontoeplitzto generate the convolution matrix.

  • convmtxhandles edge conditions by zero padding.

Extended Capabilities

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

Introduced before R2006a