Qu

firls

Builtin index · Signal processing

result = firls(n, freq_bands, desired, [weights=])

Input: n, freq_bands, desired, weights · Output: result

What it is

n: number, must be even (taps = n+1); freq_bands: a length-2B vector of band-edge pairs (normalized 0..1, 1=Nyquist); desired: a length-2B vector, the target amplitude at each edge; weights (optional length-B vector, default all 1.0), one weight per band.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = firls(n, freq_bands, desired, [weights=])
n numbern: number, must be even (taps = n+1).
freq_bands vectorfreq_bands: a length-2B vector of band-edge pairs (normalized 0..1, 1=Nyquist).
desired vectordesired: a length-2B vector, the target amplitude at each edge.
weights vector, optionalweights (optional length-B vector, default all 1.0), one weight per band.
Model vectorkind "filter", field b, a length-n+1 vector
Returns a Model (kind "filter", field b, a length-n+1 vector). Least-squares FIR design (MATLAB/SciPy firls): given a piecewise-linear desired response, finds the symmetric FIR minimizing weighted squared error.

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
ep = ellip(4, 1, 40, "low", 100, fs)
c1 = cheby1(4, 1, "low", 100, fs)
c2 = cheby2(4, 40, "low", 100, fs)
print("ellip stable: {is_stable(ep)}, cheby1 stable: {is_stable(c1)}, cheby2 stable: {is_stable(c2)}")

fir = fir1(6, 100, kind="low", window="hamming", fs=fs)
b = fir.b
a = [1]
n = 0:1:255
t = n / fs
x = sin(2*pi*40*t) + sin(2*pi*300*t)
y  = filter_ba(b, a, x)
y2 = lfilter(b, a, x)               # lfilter is the same builtin under another name

print("filter_ba/lfilter agree: {max(abs(y - y2)) < 1e-12}")

lsq = firls(20, [0, 0.2, 0.3, 1.0], [1, 1, 0, 0])   # low-pass band edges/response

print("firls taps: {length(lsq.b)}")

gd = group_delay(fir, 256)
print("FIR group delay (should be ~order/2=3): {gd[10]}")
print("ellip filter has {length(poles(ep))} poles")

ep.fvtool()   # 2x2 magnitude/phase/group-delay/impulse-response figure

pzplot(ep)    # pole-zero plot with the unit circle and stability verdict

output
ellip stable: true, cheby1 stable: true, cheby2 stable: true
filter_ba/lfilter agree: true
firls taps: 21
FIR group delay (should be ~order/2=3): 3
ellip filter has 4 poles
-80 -60 -40 -20 0 0 150 300 450 Magnitude Response Frequency (Hz) Magnitude (dB) -150 -100 -50 0 50 100 150 0 150 300 450 Phase Response Frequency (Hz) Phase (deg) -250 -200 -150 -100 -50 0 0 150 300 450 Group Delay Frequency (Hz) Delay (samples) -20 -10 0 10 20 0 20 40 60 x=-0.135, y=0.991 x=-0.135, y=-0.991 x=0.57, y=0.821 x=0.57, y=-0.821 Pole-Zero Plot (stable) Real part Imaginary part

from signal-processing

How it works

firls(n, freq_bands, desired, [weights=]) — least-squares FIR design (MATLAB's/SciPy's firls): given a piecewise- linear desired response as band-edge pairs (freq_bands, normalized 0..1, 1 = Nyquist, matching fir1's own fs=2 convention) and the desired amplitude at each edge, finds the length-n+1 symmetric FIR minimizing the weighted squared error against that response (see numeric::filter::firls's own doc comment for the exact least-squares construction). n must be even. weights= is one weight per band (default all 1.0), a plain style kwarg like fir1's window=/fs=. Same "filter" model shape (field b) as fir1 — works with every generic filter function exactly like a fir1 design does.

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