filtfilt — Apply zero-phase forward-backward digital filtering.
filtfilt(b, a, x) filters a vector forward and backward to remove phase delay. Coefficients follow the same digital-filter convention as filter: b is the numerator, a is the denominator, and a(1) is normalized internally.
Syntax
filtfilt(b, a, x)How filtfilt works
- The v1 implementation supports vector signals and preserves row or column orientation.
- RunMat uses odd reflection at both signal edges with
3 * (max(numel(a), numel(b)) - 1)samples, matching the common MATLAB-compatible zero-phase filtering path. - Input length must be greater than the reflection length unless the filter has zero state.
- Real gpuArray inputs use provider-backed filter passes when available; complex inputs fall back to the host reference path.
Does RunMat run filtfilt on the GPU?
Real gpuArray signal inputs are eligible for provider-backed forward and reverse filter passes through the existing iir_filter hook.
If provider execution is unavailable or inputs are complex, RunMat gathers to the host and evaluates the MATLAB-compatible reference path.
Examples
Compare causal filtering with zero-phase filtering
b = ones(1, 3) / 3;
a = 1;
x = 1:20;
y_causal = filter(b, a, x);
y_zero = filtfilt(b, a, x);
fprintf('%.4f %.4f\n', y_causal(8), y_zero(8));Expected output:
7.0000 8.0000Filter with FIR coefficients from fir1
b = fir1(10, 0.25, 'low');
x = 1:40;
y = filtfilt(b, 1, x);Run zero-phase filtering on a gpuArray
g = gpuArray(1:40);
b = ones(1, 3) / 3;
y_gpu = filtfilt(b, 1, g);
y = gather(y_gpu);Using filtfilt with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how filtfilt changes the result.
Run a small filtfilt example, explain the result, then change one input and compare the output.
FAQ
How is filtfilt different from filter?⌄
filter is causal and introduces phase delay. filtfilt applies the filter forward and backward, cancelling that phase delay while doubling the magnitude response order.
Does filtfilt support matrices?⌄
This initial implementation supports vector inputs. Matrix and explicit-dimension support should be added as a separate compatibility expansion.
Can I use Butterworth or FIR coefficients?⌄
Yes. Coefficients from butter, fir1, or hand-written digital filters can be used as long as a(1) is non-zero.
Related Math functions
Signal
blackman · butter · conv · conv2 · deconv · downsample · filter · fir1 · freqz · gauspuls · hamming · hann · hilbert · pulstran · rectpuls · sawtooth · sinc · square · tripuls · unwrap · upsample
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
Structure
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how filtfilt is executed, line by line, in Rust.
- View the source for filtfilt in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
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.