Skip to main content
v2026.11,858 entries · CC-BY 4.0
LAC HealthMedical & Research SuppliesReagents, PPE & instruments — chain-of-custody documented.Fast, traceable sourcing built for regulated research environments, from bench consumables to instrumentation.Shop lac.us CodeCASRAIlac.us

numpy.convolve: Syntax, Modes and Examples

How numpy.convolve works: the full, same and valid modes, output lengths, a hand-checked example, and when to use SciPy instead.

Written and maintained by CASRAI Editorial Board

Last updated

numpy.convolve(a, v, mode='full') returns the discrete, linear convolution of two one-dimensional sequences. The part that trips people up is the mode argument: it decides how many output values you get and how many of them are distorted by the edges. This guide gives the length rule for each mode, a worked hand calculation that matches the NumPy documentation, and the points at which SciPy’s convolve or fftconvolve is the better tool.

What numpy.convolve is used for in research

Convolution combines two sequences by sliding one across the other, multiplying overlapping values and summing them. In research code it is most often used to smooth a measured signal with a short weighting window (a moving average is the simplest case), or to apply a small filter to a 1-D time series or spectrum. For the wider signal-processing background see what signal processing is; for the SciPy functions that build on the same idea see SciPy signal-processing functions.

The definition in the NumPy documentation is (a * v)n = Σm am vn-m. In practice that means the second array is flipped before it slides across the first.

Install or access

NumPy’s own installation page gives pip install numpy. To keep it in an isolated environment, create one with python -m venv my-env and activate it with source my-env/bin/activate on macOS or Linux. With conda, use conda install numpy from the defaults or conda-forge channel. Then import numpy as np.

Step-by-step: convolve two arrays

  1. Put each sequence in a one-dimensional array: a = np.array([1, 2, 3]) and v = np.array([0, 1, 0.5]).
  2. Call np.convolve(a, v). With no mode given, the default 'full' is used and the result is array([0. , 1. , 2.5, 4. , 1.5]).
  3. Choose a mode if you need a different output length: np.convolve(a, v, mode='same') returns array([1. , 2.5, 4. ]), and np.convolve(a, v, mode='valid') returns array([2.5]).
  4. Check the length against the table below before you use the result as an aligned series.

The same result by hand

Flip v and slide it across a, summing the products where the two overlap. Each output value is one position of the slide:

  • n = 0: 1 × 0 = 0
  • n = 1: 1 × 1 + 2 × 0 = 1
  • n = 2: 1 × 0.5 + 2 × 1 + 3 × 0 = 2.5
  • n = 3: 2 × 0.5 + 3 × 1 = 4
  • n = 4: 3 × 0.5 = 1.5

That gives 0, 1, 2.5, 4, 1.5, the same five values as the documented 'full' example. The middle value, 2.5, is the only position where all three elements of v overlap all three of a, which is why it is the single 'valid' output.

Output and how to read it

For input lengths N (for a) and M (for v), the NumPy documentation gives these output shapes:

mode Output length What the documentation says about the edges
'full' (default) N + M − 1 At the end-points the signals do not overlap completely, and boundary effects may be seen.
'same' max(M, N) Boundary effects are still visible.
'valid' max(M, N) − min(M, N) + 1 Given only for points where the signals overlap completely.

Worked length check: with N = 5 and M = 3, 'full' gives 7 values, 'same' gives 5 and 'valid' gives 3. Running a three-point moving average, np.convolve([1, 2, 3, 4, 5], np.ones(3) / 3, mode=...), with NumPy 2.5.1 gives this:

  • 'valid': 2, 3, 4 (every value is a full three-point average)
  • 'same': 1, 2, 3, 4, 3 (same length as the input, but the first and last values average fewer real points and are pulled toward zero)
  • 'full': 0.33, 1, 2, 3, 4, 3, 1.67 (seven values, three of them distorted at each end by the partial overlap)

If you need values that are not affected by the boundary, use 'valid' and accept a shorter series. If you need the output aligned with the input, use 'same' and treat the end values with care.

numpy.convolve and scipy.signal.convolve differ for mode ‘same’

In NumPy, 'same' has length max(M, N). In scipy.signal.convolve, 'same' is the size of the first input (in1), centred with respect to the full output. The two agree when the first array is the longer one and differ when the second is longer, so check the shape if you move code between them.

Troubleshooting

The arguments seem to be in the wrong order

If v is longer than a, NumPy swaps the arrays before computing. Linear convolution gives the same values either way, for example np.convolve([0, 1, 0.5], [1, 2, 3]) returns the same five numbers as the example above. Only a mode that depends on which array is first (SciPy’s 'same') is sensitive to the order.

The output length is not what I expected

Compare the shape with the table above. The default is 'full', which is longer than either input.

The edges of the result look wrong

This is the boundary effect described in the documentation, not an error. Use 'valid' or discard the edge values.

The call is slow on large arrays

The NumPy documentation notes that convolution in the time domain equals multiplication in the Fourier domain after appropriate padding, and that the padding is needed to prevent circular convolution. For large data sets it points to scipy.signal.fftconvolve.

Every value became NaN or infinity

This happens with SciPy’s FFT method: the SciPy documentation warns that if the input contains NaN or INF the whole output can become NaN or INF. Use method='direct' in scipy.signal.convolve to avoid it.

I passed a 2-D array

np.convolve takes one-dimensional sequences. scipy.signal.convolve is N-dimensional.

Free alternatives

Function What the official documentation says
scipy.signal.convolve(in1, in2, mode='full', method='auto') N-dimensional. The method argument can be direct (sums from the definition), fft (calls fftconvolve) or auto, the default, which picks the faster of the two through choose_conv_method.
scipy.signal.fftconvolve The function the NumPy documentation recommends for large data sets.
numpy.polymul Listed in the “See also” section of the numpy.convolve page.
scipy.linalg.toeplitz Listed in the “See also” section of the numpy.convolve page.

The SciPy documentation example smooths a signal with a Hann window: sig = np.repeat([0., 1., 0.], 100), win = signal.windows.hann(50), then filtered = signal.convolve(sig, win, mode='same') / sum(win). Dividing by the sum of the window keeps the filtered signal on the original scale. The related Richardson-Lucy deconvolution guide covers the reverse problem of undoing a blur.

FAQ

What is the default mode of numpy.convolve?

The default is mode='full', which returns N + M − 1 values, including the end points where the two sequences only partly overlap.

Which mode returns only fully overlapping points?

'valid'. Its length is max(M, N) − min(M, N) + 1.

Does numpy.convolve work on 2-D arrays?

No. It is defined for two one-dimensional sequences. Use scipy.signal.convolve for N-dimensional input.

Sources

  • NumPy documentation, numpy.convolve, checked 2026-10-29.
  • SciPy documentation, scipy.signal.convolve, checked 2026-10-29.
  • NumPy, Installing NumPy, checked 2026-10-29.
  • The moving-average output values were computed by running the code with NumPy 2.5.1 (checked 2026-10-29).

Follow CASRAI

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

Ask CASRAI · free to try

Ask about numpy.convolve: Syntax, Modes and Examples

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

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 free 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 →