convmtx
Convolution matrix
Syntax
Description
Examples
Efficient Computation of Convolution
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.0003 0.0060
t1rat = tcnv\tmtx
t1rat = 22.6591
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.0615 0.0383
tmrat = tcnv/tmtx
tmrat = 1.6066
convmtx
is about three times as efficient as conv
.
Input Arguments
h
— Input vector
vector
Input vector, specified as a row or column.
Data Types: single
| double
n
— Length of vector to convolve
positive integer
Length of vector to convolve, specified as a positive integer.
If
h
is a column vector of lengthm
,A
is(m+n-1)
-by-n
, and the product ofA
and a column vector,x
, of lengthn
is the convolution ofh
andx
.If
h
is a row vector of lengthm
,A
isn
-by-(m+n-1)
, and the product of a row vector,x
, of lengthn
withA
is the convolution ofh
andx
.
Output Arguments
A
— Convolution matrix
matrix
Convolution matrix of input h
and the vector
x
, returned as a matrix.
Algorithms
convmtx
uses the functiontoeplitz
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™.
Version History
Introduced before R2006a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)