RunMat
GitHub

conv — Compute one-dimensional linear convolution in MATLAB and RunMat.

conv(a, b) computes the one-dimensional linear convolution of vectors a and b. The default returns full convolution length, while 'same' and 'valid' select alternate output shapes following MATLAB semantics.

Syntax

C = conv(A, B)
C = conv(A, B, shape)

Inputs

NameTypeRequiredDefaultDescription
AAnyYesFirst input vector/scalar.
BAnyYesSecond input vector/scalar.
shapeStringScalarNo"full"Output shape: "full", "same", or "valid".

Returns

NameTypeDescription
CNumericArrayConvolution result.

Errors

IdentifierWhenMessage
RunMat:conv:ShapeInvalidThird argument is missing or not one of full/same/valid.conv: third argument must be the string 'full', 'same', or 'valid'
RunMat:conv:ArgCountMore than three input arguments are provided.conv: expected at most three input arguments
RunMat:conv:InvalidInputAn input value is not numeric/logical scalar/vector/tensor compatible.conv: unsupported input type

How conv works

  • Accepts real or complex scalars, vectors, or tensors that can be flattened column-major into a vector.
  • Keeps the orientation of the first input when returning row or column vectors ('same' and 'valid' honour this rule).
  • Supports the three MATLAB shape modes: 'full' (default), 'same', and 'valid'.
  • Returns empty outputs when either input is empty or when 'valid' is requested with insufficient overlap.
  • Logical inputs are promoted to double precision before the convolution.
  • GPU inputs are gathered automatically when the active provider lacks a native 1-D convolution kernel.

Examples

Computing the full convolution of two row vectors

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

Expected output:

c = [1 3 6 5 3]

Keeping the same length as the first input

kernel = [1 0 -1];
signal = [3 4 5 6 7];
edge = conv(signal, kernel, 'same')

Expected output:

edge = [4 2 2 2 -6]

Valid convolution without zero padding

weights = [1 2 3 4];
window = [1 1 1];
valid = conv(weights, window, 'valid')

Expected output:

valid = [6 9]

Convolving column vectors

a = (1:3)';
b = [2; 0; -2];
c = conv(a, b)

Expected output:

c =
     2
     4
     4
     -4
     -6

Multiplying polynomials using convolution

p = [1 3 3 1];    % (x + 1)^3 coefficients
q = [1 -1];       % (x - 1)
coeff = conv(p, q)

Expected output:

coeff = [1 2 0 -2 -1]

Complex-valued convolution

t = 0:3;
sig = exp(1i * pi/4 * t);
filt = [1 2i];
resp = conv(sig, filt)

Expected output:

resp =
   1.0000   0.7071 + 2.7071i  -1.4142 + 2.4142i  -2.7071 + 0.7071i  -1.4142 - 1.4142i

Scaling a signal by a scalar

s = [4 5 6];
y = conv(2, s)

Expected output:

y = [8 10 12]

Using gpuArray inputs with automatic host fallback

g = gpuArray([1 2 3 4]);
h = gpuArray([1 0 -1]);
edge = conv(g, h, 'same');
result = gather(edge)

Using conv with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how conv changes the result.

Run a small conv example, explain the result, then change one input and compare the output.

FAQ

Does conv require row vectors?

No. conv accepts row vectors, column vectors, scalars, and tensors that can be flattened into a vector. The result preserves the orientation of the first input when that orientation is unambiguous.

What happens when one of the inputs is empty?

The result is an empty vector with an orientation derived from the non-empty input (or a 0×1 column vector when both inputs are empty), matching MATLAB's behaviour.

How is 'same' computed?

'same' returns the central portion of the full convolution whose length matches the first input. Internally RunMat performs a full convolution and slices the appropriate window.

When should I use 'valid'?

Use 'valid' when you only want results that do not rely on zero padding. This is common when sliding windows should fit completely inside the input without extending past the boundaries.

Does conv support single precision?

The host path computes in double precision. Provider implementations choose their native precision: the in-process provider mirrors double results, while the WGPU backend emits either f32 or f64 kernels depending on device support. Providers without a conv1d hook gather inputs back to the CPU to maintain MATLAB-compatible answers.

Will the result stay on the GPU?

Yes—provided the active provider exposes the conv1d hook (the in-process provider and WGPU backend do). Without that hook, RunMat gathers inputs, computes on the host, and returns a CPU tensor with the correct orientation.

Can I convolve matrices or higher-dimensional arrays?

conv treats inputs as vectors using MATLAB column-major order. For multi-dimensional convolution use the dedicated conv2 or convn builtins (planned).

How do I convolve with an impulse (delta) kernel?

Include a 1 followed by zeros in your kernel. The input will be preserved (with appropriate padding) because convolution with a delta function is an identity operation.

What about circular convolution?

Use cconv for circular convolution, or compute the FFT manually and multiply in the frequency domain before performing an inverse FFT.

Are there GPU-specific tuning knobs?

Not yet. Current providers choose kernel launch parameters automatically; user-facing tuning switches will arrive alongside future backend updates.

Elementwise

abs · angle · complex · conj · double · exp · expm1 · factorial · gamma · heaviside · hypot · imag · ldivide · log · log10 · log1p · log2 · minus · nextpow2 · plus · pow2 · power · rdivide · real · sign · single · sqrt · times

Trigonometry

acos · acosh · asin · asinh · atan · atan2 · atanh · cos · cosd · cosh · deg2rad · rad2deg · sin · sind · sinh · tan · tand · tanh

Reduction

all · any · cummax · cummin · cumprod · cumsum · cumtrapz · diff · gradient · max · mean · median · min · nnz · prod · std · sum · trapz · var

Rounding

ceil · fix · floor · mod · rem · round

Factor

chol · eig · lu · qr · svd

Solve

cond · det · inv · linsolve · norm · null · pinv · rank · rcond · rref

Symbolic

digits · int · limit · sym · syms · vpa

Fft

fft · fft2 · fftshift · ifft · ifft2 · ifftshift

Interpolation

interp1 · interp2 · pchip · ppval · spline

Ode

ode15s · ode23 · ode45

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how conv is executed, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.

  • RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
  • Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.