fir1
Builtin index · Signal processing
result = fir1(n, cutoff, [kind="low"], [window="hamming"], [fs=2], [beta=])
Input: n, cutoff, kind, window, fs, beta · Output: result
What it is
n: number (order, taps = n+1); cutoff: number or length-2 vector; kind (optional string); window (optional string: "hamming", "hann"/"hanning", "blackman", or "kaiser" — the last needs beta=, a required number when chosen); fs (optional number, default 2.0, MATLAB's normalized convention where 1 = Nyquist) — pass an explicit fs= for butter-style Hz cutoffs.
Overloads
cutoff can be number or vector.
2 forms. Each carries its own inputs, outputs and worked example — pick the tab for the one you are calling.
| Code | Input | Output | Description |
result = fir1(n, cutoff, kind="low"/"high", ...) | n numbern: number (order, taps = n+1).cutoff number or vectorcutoff: number or length-2 vector.kind string (default "low"/"high")kind (optional string).... repeatablenot documented yet | result number | A single normalized/Hz number. |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
fs = 1000
fir = fir1(6, 100, kind="low", fs=fs)
print("FIR poles: {poles(fir)}")
# FIR poles: [0, 0, 0, 0, 0, 0]
FIR poles: [0, 0, 0, 0, 0, 0]
from signal-processing
fs = 1000
lp = fir1(20, 100, kind="low", fs=fs)
bp = fir1(20, [80, 120], kind="band", fs=fs)
print("lowpass taps: {length(lp.b)}") # lowpass taps: 21
print("bandpass taps: {length(bp.b)}") # bandpass taps: 21
lowpass taps: 21 bandpass taps: 21
from signal-processing
fs = 1000
fir = fir1(6, 100, kind="low", fs=fs)
sf = filter_init(fir)
print("FIR state length: {length(sf.z)}") # FIR state length: 6
FIR state length: 6
from signal-processing
fs = 100
t = (0 to 19) / fs
x = sin(2*pi*10*t)
fir = fir1(6, 20, kind="low", fs=fs)
y_fir = sosfilt(fir, x)
print("FIR-filtered length: {length(y_fir)}") # FIR-filtered length: 20
FIR-filtered length: 20
from signal-processing
| Code | Input | Output | Description |
result = fir1(n, cutoff, kind="band"/"stop", ...) | n numbern: number (order, taps = n+1).cutoff number or vectorcutoff: number or length-2 vector.kind string (default "band"/"stop")kind (optional string).... repeatablenot documented yet | result number or vector | A length-2 [low, high] number/Hz vector. |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
fs = 1000
fir = fir1(6, 100, kind="low", fs=fs)
print("FIR poles: {poles(fir)}")
# FIR poles: [0, 0, 0, 0, 0, 0]
FIR poles: [0, 0, 0, 0, 0, 0]
from signal-processing
fs = 1000
lp = fir1(20, 100, kind="low", fs=fs)
bp = fir1(20, [80, 120], kind="band", fs=fs)
print("lowpass taps: {length(lp.b)}") # lowpass taps: 21
print("bandpass taps: {length(bp.b)}") # bandpass taps: 21
lowpass taps: 21 bandpass taps: 21
from signal-processing
fs = 1000
fir = fir1(6, 100, kind="low", fs=fs)
sf = filter_init(fir)
print("FIR state length: {length(sf.z)}") # FIR state length: 6
FIR state length: 6
from signal-processing
fs = 100
t = (0 to 19) / fs
x = sin(2*pi*10*t)
fir = fir1(6, 20, kind="low", fs=fs)
y_fir = sosfilt(fir, x)
print("FIR-filtered length: {length(y_fir)}") # FIR-filtered length: 20
FIR-filtered length: 20
from signal-processing
How it works
fir1(n, cutoff, [kind="low"], [window="hamming"], [fs=2]) — windowed-sinc FIR design (MATLAB's/SciPy's fir1): the ideal lowpass/highpass/bandpass/bandstop impulse response truncated to n+1 taps, multiplied by the requested window (reusing hamming/hann/blackman/kaiser's own Rust functions directly — raised_cosine_window/kaiser_window below — not reimplemented), normalized to unity gain at the passband center. kind accepts the same aliases as butter. fs defaults to 2.0, MATLAB's own *normalized* fir1 convention (cutoff directly in [0, 1], 1 = Nyquist — also fvtool's own existing fallback below); pass an explicit fs= for butter-style Hz cutoffs instead. Model kind "filter", field b (a plain tap vector) — no sos field at all, since an FIR filter has no feedback/denominator and can't be forced into a biquad cascade without first factoring its transfer-function polynomial into quadratic roots (a real numerical step this design function doesn't do). Every generic filter function below (sosfilt/ filtfilt/freqz/group_delay/fvtool/filter_init/ filter_next/poles/zeros/is_stable/pzplot) branches on whether a filter model has sos or b.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.