conv - Convolution and polynomial multiplication - MATLAB (original) (raw)
Main Content
Convolution and polynomial multiplication
Syntax
Description
w = conv([u,v](#bucr8kb-1-uv))
returns the convolution of vectors u
and v
. If u
and v
are vectors of polynomial coefficients, convolving them is equivalent to multiplying the two polynomials.
w = conv([u,v](#bucr8kb-1-uv),[shape](#bucr8kb-1-shape))
returns a subsection of the convolution, as specified by shape
. For example, conv(u,v,'same')
returns only the central part of the convolution, the same size as u
, and conv(u,v,'valid')
returns only the part of the convolution computed without the zero-padded edges.
Examples
Polynomial Multiplication via Convolution
Create vectors u
and v
containing the coefficients of the polynomials x2+1 and 2x+7.
Use convolution to multiply the polynomials.
w
contains the polynomial coefficients for 2x3+7x2+2x+7.
Vector Convolution
Create two vectors and convolve them.
u = [1 1 1]; v = [1 1 0 0 0 1 1]; w = conv(u,v)
w = 1×9
1 2 2 1 0 1 2 2 1
The length of w
is length(u)+length(v)-1
, which in this example is 9
.
Central Part of Convolution
Create two vectors. Find the central part of the convolution of u
and v
that is the same size as u
.
u = [-1 2 3 -2 0 1 2]; v = [2 4 -1 1]; w = conv(u,v,'same')
w
has a length of 7
. The full convolution would be of length length(u)+length(v)-1
, which in this example would be 10.
Input Arguments
u,v
— Input vectors
vectors
Input vectors, specified as either row or column vectors. The vectors u
and v
can be different lengths or data types.
When u
or v
are of type single
, then the output is of type single
. Otherwise, conv
converts inputs to type double
and returns type double
.
Data Types: double
| single
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
Complex Number Support: Yes
shape
— Subsection of convolution
'full'
(default) | 'same'
| 'valid'
Subsection of the convolution, specified as 'full'
, 'same'
, or 'valid'
.
'full' | Full convolution (default). |
---|---|
'same' | Central part of the convolution of the same size as u. |
'valid' | Only those parts of the convolution that are computed without the zero-padded edges. Using this option, length(w) is max(length(u)-length(v)+1,0), except when length(v) is zero. If length(v) = 0, then length(w) = length(u). |
More About
Convolution
The convolution of two vectors, u
and v
, represents the area of overlap under the points as v
slides across u
. Algebraically, convolution is the same operation as multiplying polynomials whose coefficients are the elements of u
and v
.
Let m = length(u)
and n = length(v)
. Thenw
is the vector of length m+n-1
whosek
th element is
The sum is over all the values of j
that lead to legal subscripts for u(j)
and v(k-j+1)
, specifically j
=
max(1,k+1-n):1:min(k,m)
. When m
=
n
, this gives
w(1) = u(1)*v(1) w(2) = u(1)*v(2)+u(2)*v(1) w(3) = u(1)*v(3)+u(2)*v(2)+u(3)*v(1) ... w(n) = u(1)*v(n)+u(2)*v(n-1)+ ... +u(n)v(1) ... w(2n-1) = u(n)*v(n)
Using this definition, conv
calculates the direct convolution of two vectors, rather than the FFT-based convolution.
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
Theconv
function supports tall arrays with the following usage notes and limitations:
- The inputs
u
andv
must be column vectors. - If
shape
is'full'
(default), then only one ofu
orv
can be a tall array. - If
shape
is'same'
or'valid'
, thenv
cannot be a tall array.
For more information, see Tall Arrays.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
For information about C/C++ code generation limitations, see Variable-Sizing Restrictions for Code Generation of Toolbox Functions (MATLAB Coder).
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
The conv
function fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray (Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced before R2006a