Written and maintained by CASRAI Editorial Board
Published · Last updated
On this page
Short answer: in MATLAB, c = cconv(a,b,n) returns the circular convolution of vectors a and b with output length n. Without the toolbox, the same result comes from ifft(fft(a,n).*fft(b,n)). This page gives working MATLAB code for circular convolution both ways, shows what the length n does to the result, and explains how to check that the two agree.
It is written for researchers and analysts who need circular (periodic) convolution for signal processing, filtering or DFT-based methods and want to know why the output length matters. For the ordinary, non-circular operation see numpy.convolve (the NumPy equivalent of conv).
What circular convolution is used for in research
Circular convolution is the convolution that the discrete Fourier transform (DFT) computes natively: MathWorks documents that circular convolution equals the inverse DFT of the product of the vectors’ DFTs. Researchers meet it when implementing frequency-domain filtering, when verifying DFT properties, and when comparing a direct computation against an FFT-based one. For filter design context, see FIR MATLAB: filter design with fir1 and freqz; for an iterative image-restoration method built on convolution, see Richardson-Lucy deconvolution.
How to install or access it
Two functions are involved, and they sit in different products according to the MathWorks documentation (read 2026-11-08):
cconvis documented under Signal Processing Toolbox. Obtain MATLAB and the toolbox through MathWorks or your institution’s licence.fft,ifftandconvare core MATLAB functions, documented in the MATLAB reference, so the manual method needs no toolbox.
If you are new to the environment, What is MATLAB used for? covers first steps.
Step-by-step: matlab code for circular convolution
1. Define the two vectors
- Create the inputs. This example is the one used in the MathWorks documentation:
x = [2 1 2 1]; y = [1 2 3]; - Compute the ordinary (linear) convolution for reference:
clin = conv(x,y);convreturns a vector of lengthlength(x)+length(y)-1, here 6, and computes the direct convolution rather than an FFT-based one.
2. Circular convolution with cconv
- Call
cconvwith an explicit output length of 6:ccirc2 = cconv(x,y,6);With
nat leastlength(x)+length(y)-1, the circular result equals the linear result. If you omitn,cconv(x,y)uses that same default length.
3. Circular convolution with fft and ifft (no toolbox)
- Zero-pad both vectors to the output length, multiply their DFTs element by element, and invert:
xpad = [x zeros(1,6-length(x))]; ypad = [y zeros(1,6-length(y))]; ccirc = ifft(fft(xpad).*fft(ypad));This is the pattern in the MathWorks “Linear and Circular Convolution” documentation;
cconvuses the same DFT-based procedure internally. - Equivalently, let
fftdo the padding:fft(X,n)pads a shorterXwith trailing zeros to lengthnand truncates a longer one.n = 6; c = ifft(fft(x,n).*fft(y,n));
4. Confirm the methods agree
- Compare the results numerically instead of by eye:
max(abs(clin - ccirc2)) max(abs(clin - ccirc))Both should be zero or negligibly small, because 6 equals
length(x)+length(y)-1.
How the length n changes the result
This is the part most code snippets skip. The cconv documentation states that the default output length is length(a)+length(b)-1, which equals the linear convolution, and that a smaller n wraps (aliases) the linear result: the modulo-n output is the sum of the linear convolution taken in blocks of length n. To match linear convolution you must pad both vectors to at least N+L-1 points.
| Choice of n | What the output is | Source basis |
|---|---|---|
n omitted |
Length length(a)+length(b)-1; equals linear convolution |
cconv documentation |
n at least length(a)+length(b)-1 |
Same values as linear convolution | Linear and Circular Convolution documentation |
n smaller than that |
Linear result wrapped: blocks of length n summed | cconv documentation |
Worked example of wrapping
For x = [2 1 2 1] and y = [1 2 3], the linear convolution conv(x,y) is [2 5 10 8 8 3] (computed by hand from the definition). Applying the block-sum rule for n = 4 adds the last two values onto the first two, so cconv(x,y,4) should return [10 8 10 8]: the first element is 2 + 8 and the second is 5 + 3. Run it in your own session to confirm.
Output and how to read it
- The result is a vector of length
n(orlength(a)+length(b)-1by default). - For real inputs,
fftreturns a conjugate-symmetric spectrum, so the product inverts to a real sequence in exact arithmetic. In practiceifftoutput can carry tiny imaginary parts from floating-point rounding; compare with a tolerance rather than exact equality. - Use
max(abs(...))differences, as in step 6, to compare implementations.
Troubleshooting
- Undefined function cconv.
cconvis documented under Signal Processing Toolbox; use the fft/ifft method if you do not have that toolbox. - Result differs from
conv. The output length is probably smaller thanlength(a)+length(b)-1, which wraps the result. Increasenor zero-pad. - Dimension mismatch in the elementwise product. Both padded vectors must have the same length and orientation (both rows or both columns) before
.*. - Matching linear convolution after padding to a longer length. When you pad beyond the minimum, keep the first
N+L-1elements, as the MathWorks documentation does.
Free alternatives
| Tool | Approach | Source basis |
|---|---|---|
| NumPy | numpy.fft.fft(a, n) crops input longer than n and zero-pads shorter input; ifft is its inverse, so the same multiply-and-invert pattern applies |
NumPy fft documentation, read 2026-11-08 |
FAQ
What is the MATLAB code for circular convolution?
With Signal Processing Toolbox, c = cconv(a,b,n). Without it, c = ifft(fft(a,n).*fft(b,n)). Both compute circular convolution of length n.
Is circular convolution the same as linear convolution?
Only when the output length is at least length(a)+length(b)-1. For a shorter length the linear result wraps around.
Does cconv use the FFT?
MathWorks states that cconv uses the same DFT-based procedure as the manual fft/ifft method.







