Qu

conv

Builtin index · Signal processing

result = conv(x, h, [mode="full"])

Input: x, h, mode · Output: result

What it is

x is a length-N vector, h a length-M vector (the kernel/impulse response). mode (optional string) follows MATLAB's conv/NumPy's convolve naming: "full" (default, length N+M-1, every nonzero-overlap sample), "same" (length N, the middle samples aligned with the input), "valid" (length max(N,M)-min(N,M)+1, only where h fully overlaps x).

Overloads

3 forms. Each carries its own inputs, outputs and worked example — pick the tab for the one you are calling.

CodeInputOutputDescription
result = conv(x, h) / conv(x, h, mode="full")
x vectornot documented yet
h vectornot documented yet
result vector
N+M-1

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

  y = conv([1, 2, 3], [0, 1, 0.5])                  # [0, 1, 2.5, 4, 1.5]

  filtered = conv(signal, kernel, mode="same")       # same length as signal

from numerics-dsp

x = [1, 2, 3, 4]
h = [1, 1, 1]
f = conv(x, h)
s = conv(x, h, mode="same")
v = conv(x, h, mode="valid")
print("full ({length(f)}): {f}")     # full (6): [1, 3, 6, 9, 7, 4]

print("same ({length(s)}): {s}")     # same (4): [3, 6, 9, 7]

print("valid ({length(v)}): {v}")    # valid (2): [6, 9]

output
full (6): [1, 3, 6, 9, 7, 4]
same (4): [3, 6, 9, 7]
valid (2): [6, 9]

from signal-processing

CodeInputOutputDescription
result = conv(x, h, mode="same")
x vectorx is a length-N vector, h a length-M vector (the kernel/impulse response).
h vectorx is a length-N vector, h a length-M vector (the kernel/impulse response).
mode string (default "same")mode (optional string) follows MATLAB's conv/NumPy's convolve naming: "full" (default, length N+M-1, every nonzero-overlap sample), "same" (length N, the middle samples aligned with the input), "valid" (length max(N,M)-min(N,M)+1, only where h fully overlaps x).
result vector
N

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

  y = conv([1, 2, 3], [0, 1, 0.5])                  # [0, 1, 2.5, 4, 1.5]

  filtered = conv(signal, kernel, mode="same")       # same length as signal

from numerics-dsp

x = [1, 2, 3, 4]
h = [1, 1, 1]
f = conv(x, h)
s = conv(x, h, mode="same")
v = conv(x, h, mode="valid")
print("full ({length(f)}): {f}")     # full (6): [1, 3, 6, 9, 7, 4]

print("same ({length(s)}): {s}")     # same (4): [3, 6, 9, 7]

print("valid ({length(v)}): {v}")    # valid (2): [6, 9]

output
full (6): [1, 3, 6, 9, 7, 4]
same (4): [3, 6, 9, 7]
valid (2): [6, 9]

from signal-processing

CodeInputOutputDescription
result = conv(x, h, mode="valid")
x vectorx is a length-N vector, h a length-M vector (the kernel/impulse response).
h vectorx is a length-N vector, h a length-M vector (the kernel/impulse response).
mode string (default "valid")mode (optional string) follows MATLAB's conv/NumPy's convolve naming: "full" (default, length N+M-1, every nonzero-overlap sample), "same" (length N, the middle samples aligned with the input), "valid" (length max(N,M)-min(N,M)+1, only where h fully overlaps x).
result vector
max(N,M)-min(N,M)+1

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

  y = conv([1, 2, 3], [0, 1, 0.5])                  # [0, 1, 2.5, 4, 1.5]

  filtered = conv(signal, kernel, mode="same")       # same length as signal

from numerics-dsp

x = [1, 2, 3, 4]
h = [1, 1, 1]
f = conv(x, h)
s = conv(x, h, mode="same")
v = conv(x, h, mode="valid")
print("full ({length(f)}): {f}")     # full (6): [1, 3, 6, 9, 7, 4]

print("same ({length(s)}): {s}")     # same (4): [3, 6, 9, 7]

print("valid ({length(v)}): {v}")    # valid (2): [6, 9]

output
full (6): [1, 3, 6, 9, 7, 4]
same (4): [3, 6, 9, 7]
valid (2): [6, 9]

from signal-processing

How it works

conv(x, h, [mode="full"]) — discrete convolution. mode follows MATLAB's conv/NumPy's convolve naming: "full" (default, every nonzero-overlap sample), "same" (the middle length(x) samples, aligned with the input — the usual choice for filtering in place), "valid" (only where h fully overlaps x).

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.