## Documentation Center |

Modulo-N circular convolution

`c = cconv(a,b,n)c = cconv(gpuArrayA,gpuArrayB,n)`

Circular convolution is used to convolve two discrete Fourier transform (DFT) sequences. For very long sequences, circular convolution may be faster than linear convolution.

`c = cconv(a,b,n)` circularly
convolves vectors `a` and `b`. `n` is
the length of the resulting vector. If you omit `n`,
it defaults to `length(a)+length(b)-1`. When `n
= length(a)+length(b)-1`, the circular convolution is equivalent
to the linear convolution computed with `conv`.
You can also use `cconv` to compute the circular
cross-correlation of two sequences (see the example below).

`c = cconv(gpuArrayA,gpuArrayB,n)` returns
the circular convolution of the input vectors of class `gpuArray`.
See Establish Arrays on a GPU for
details on gpuArray objects. Using `cconv` with `gpuArray` objects
requires Parallel Computing Toolbox™ software and a CUDA-enabled
NVIDIA GPU with compute capability 1.3 or above. See http://www.mathworks.com/products/parallel-computing/requirements.html for
details. The output vector, `c`, is a gpuArray object.
See Circular Convolution using the GPU for
an example of using the GPU to compute the circular convolution.

The following example calculates a modulo-4 circular convolution.

a = [2 1 2 1]; b = [1 2 3 4]; c = cconv(a,b,4)

c = 14 16 14 16

The following example compares a circular correlation, where `n` uses
the default value, and a linear convolution. The resulting norm is
a value that is virtually zero, which shows that the two convolutions
produce virtually the same result.

a = [1 2 -1 1]; b = [1 1 2 1 2 2 1 1]; c = cconv(a,b); % Circular convolution cref = conv(a,b); % Linear convolution dif = norm(c-cref)

dif = 9.7422e-16

The following example uses `cconv` to compute
the circular cross-correlation of two sequences. The result is compared
to the cross-correlation computed using `xcorr`.

a = [1 2 2 1]+1i; b = [1 3 4 1]-2*1i; c = cconv(a,conj(fliplr(b)),7); % Compute using cconv cref = xcorr(a,b); % Compute using xcorr dif = norm(c-cref)

dif = 3.3565e-15

The following example requires Parallel Computing Toolbox software and a CUDA-enabled NVIDIA GPU with compute capability 1.3 or above. See http://www.mathworks.com/products/parallel-computing/requirements.html for details.

Create two signals consisting of a 1 kHz sine wave in additive white Gaussian noise. The sampling rate is 10 kHz

Fs = 1e4; t = 0:1/Fs:10-(1/Fs); x = cos(2*pi*1e3*t)+randn(size(t)); y = sin(2*pi*1e3*t)+randn(size(t));

Put `x` and `y` on the
GPU using `gpuArray`. Obtain the circular convolution
using the GPU.

x = gpuArray(x); y = gpuArray(y); cirC = cconv(x,y,length(x)+length(y)-1);

Compare the result to the linear convolution of `x` and `y`.

linC = conv(x,y); norm(linC-cirC,2)

Return the circular convolution, `cirC`,
to the MATLAB^{®} workspace using `gather`.

cirC = gather(cirC);

[1] Orfanidis, S. J. *Introduction
to Signal Processing*. Englewood Cliffs, NJ: Prentice-Hall,
1996, pp. 524–529.

Was this topic helpful?