Skip to main content
v2026.11,986 entries · CC-BY 4.0

Circular Convolution in MATLAB: cconv and fft/ifft Code

MATLAB code for circular convolution using cconv and fft/ifft, how output length n controls wrapping, and a check against conv.

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):

  • cconv is documented under Signal Processing Toolbox. Obtain MATLAB and the toolbox through MathWorks or your institution’s licence.
  • fft, ifft and conv are 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

  1. Create the inputs. This example is the one used in the MathWorks documentation:
    x = [2 1 2 1];
    y = [1 2 3];
  2. Compute the ordinary (linear) convolution for reference:
    clin = conv(x,y);

    conv returns a vector of length length(x)+length(y)-1, here 6, and computes the direct convolution rather than an FFT-based one.

2. Circular convolution with cconv

  1. Call cconv with an explicit output length of 6:
    ccirc2 = cconv(x,y,6);

    With n at least length(x)+length(y)-1, the circular result equals the linear result. If you omit n, cconv(x,y) uses that same default length.

3. Circular convolution with fft and ifft (no toolbox)

  1. 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; cconv uses the same DFT-based procedure internally.

  2. Equivalently, let fft do the padding: fft(X,n) pads a shorter X with trailing zeros to length n and truncates a longer one.
    n = 6;
    c = ifft(fft(x,n).*fft(y,n));

4. Confirm the methods agree

  1. 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 (or length(a)+length(b)-1 by default).
  • For real inputs, fft returns a conjugate-symmetric spectrum, so the product inverts to a real sequence in exact arithmetic. In practice ifft output 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. cconv is 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 than length(a)+length(b)-1, which wraps the result. Increase n or 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-1 elements, 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.

Sources

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about Circular Convolution in MATLAB: cconv and fft/ifft Code

2 free questions a day, no account.

An AI assistant for research: funder rules, compliance, methods and publishing. It links the sources behind its answers and labels answers that come from a web search.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays open to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →