butter — Design Butterworth IIR filters.
butter(n, Wn) designs an order-n Butterworth filter. Digital designs use MATLAB's normalized convention where Wn = 1 is Nyquist. Scalar Wn produces a lowpass filter by default; a two-element Wn produces a bandpass filter by default. Add an ftype string for low, high, bandpass, or stop, and add 's' for analog transfer functions.
Syntax
b = butter(n, Wn)
b = butter(n, Wn, ftype)
b = butter(n, Wn, 's')
b = butter(n, Wn, ftype, 's')
[b, a] = butter(n, Wn)
[b, a] = butter(n, Wn, ftype)All supported butter forms
b = butter(n, Wn)
b = butter(n, Wn, ftype)
b = butter(n, Wn, 's')
b = butter(n, Wn, ftype, 's')
[b, a] = butter(n, Wn)
[b, a] = butter(n, Wn, ftype)
[b, a] = butter(n, Wn, 's')
[b, a] = butter(n, Wn, ftype, 's')
[z, p, k] = butter(n, Wn)
[z, p, k] = butter(n, Wn, ftype)
[z, p, k] = butter(n, Wn, 's')
[z, p, k] = butter(n, Wn, ftype, 's')
[A, B, C, D] = butter(n, Wn)
[A, B, C, D] = butter(n, Wn, ftype)
[A, B, C, D] = butter(n, Wn, 's')
[A, B, C, D] = butter(n, Wn, ftype, 's')Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
n | IntegerScalar | Yes | — | Filter order. |
Wn | NumericArray | Yes | — | Normalized digital cutoff frequency or analog cutoff frequency. |
ftype | StringScalar | No | "low" for scalar Wn, "bandpass" for two-element Wn | Filter type: low, high, bandpass, or stop. |
Wn | NumericArray | Yes | — | Analog cutoff frequency. |
s | StringScalar | Yes | — | Analog filter-design flag. |
s | StringScalar | No | — | Analog filter-design flag. |
Returns
| Name | Type | Description |
|---|---|---|
b | NumericArray | Numerator coefficient vector. |
a | NumericArray | Denominator coefficient vector. |
z | NumericArray | Filter zeros. |
p | NumericArray | Filter poles. |
k | NumericScalar | Filter gain. |
A | NumericArray | State matrix. |
B | NumericArray | Input matrix. |
C | NumericArray | Output matrix. |
D | NumericScalar | Feedthrough term. |
Returned values from butter depend on how many outputs the caller requests.
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:butter:ArgCount | The argument count is outside butter's supported forms. | butter: expected two to four input arguments |
RunMat:butter:InvalidOrder | Filter order is missing, non-finite, non-integer, less than one, or too large. | butter: order must be a positive integer |
RunMat:butter:InvalidFrequency | Cutoff frequencies are malformed or outside the valid range. | butter: invalid cutoff frequency |
RunMat:butter:InvalidOption | Filter type or analog flag is malformed or unknown. | butter: invalid option |
RunMat:butter:TooManyOutputs | More than four outputs are requested. | butter: too many output arguments |
RunMat:butter:Internal | Filter design or output materialization fails internally. | butter: internal error |
How butter works
b = butter(n, Wn)returns numerator coefficients as a row vector.[b, a] = butter(...)returns numerator and denominator coefficient row vectors suitable forfilter(b, a, x)in digital mode.[z, p, k] = butter(...)returns zero-pole-gain form.[A, B, C, D] = butter(...)returns a controllable-canonical state-space realization.- Digital cutoff frequencies must be finite, greater than 0, and less than 1. Analog cutoff frequencies must be finite and greater than 0.
- Bandpass and bandstop designs require a strictly increasing two-element
Wnvector.
Examples
Low-pass filtering a noisy signal
fs = 1000;
t = (0:999) / fs;
x = sin(2*pi*25*t) + 0.2*sin(2*pi*200*t);
[b, a] = butter(4, 80/(fs/2), 'low');
y = filter(b, a, x);Expected output:
y contains the low-pass filtered signal.High-pass filter coefficients
[b, a] = butter(2, 0.25, 'high')Expected output:
b and a are row-vector IIR coefficients.Band-pass filter coefficients
[b, a] = butter(3, [0.2 0.4], 'bandpass')Expected output:
b and a each contain 7 coefficients.Analog low-pass prototype
[b, a] = butter(1, 2, 's')Expected output:
b = [0 2]
a = [1 2]Using butter with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how butter changes the result.
Run a small butter example, explain the result, then change one input and compare the output.
FAQ
What does Wn mean for digital filters?⌄
RunMat follows MATLAB's normalized digital convention: Wn = 1 is the Nyquist frequency, so a 600 Hz low-pass filter at a 4800 Hz sample rate uses butter(n, 600/(4800/2)).
Can I use the result directly with filter?⌄
Yes. Digital [b, a] outputs are ordinary row vectors and can be passed directly to filter(b, a, x).
Does butter execute on the GPU?⌄
No. It generates small coefficient arrays on the host. GPU acceleration applies when those coefficients are used by filter with a real gpuArray signal and a provider that implements iir_filter.
Related Math functions
Elementwise
abs · angle · complex · conj · double · exp · expm1 · factorial · gamma · 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 butter is executed, line by line, in Rust.
- View the source for butter 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.