Qu

filter_next

Builtin index · Signal processing

result = filter_next(state, x)

Input: state, x · Output: result

What it is

state: a Model (kind "filter_state", from filter_init or a prior filter_next); x: a single number (one new sample).

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = filter_next(state, x)
state Modelstate: a Model (kind "filter_state", from filter_init or a prior filter_next).
x numberx: a single number (one new sample).
Model numberfield y, a number, holds the filtered output
Returns a new "filter_state" Model (field y, a number, holds the filtered output). Use as state = filter_next(state, next_sample) in a loop, reading state.y each time — the real-time/serial-fed-data counterpart to sosfilt/filtfilt.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

  state = filter_init(lp)
  for i = 0 to length(samples) - 1
      state = filter_next(state, samples[i])
      print(state.y)         # this sample's filtered output, right away

  end for

from numerics-dsp

lp = butter(4, "low", 80, 1000)

function lp_block(b, st)
    s   = st
    out = ()
    for v in b
        s   = filter_next(s, v)
        out = append(out, s.y)
    end for
    return (out, s)
end function

p = processor("lp_block", block = 256, state = filter_init(lp), rate = 1000)
y = process(p, x)          # or: p.process(x)


print("{p.latency} samples, {p.latency_ms} ms")   # 256 samples, 256 ms

output
256 samples, 256 ms

from signal-processing

How it works

filter_next(state, x) — one sample through the filter. Branches on the underlying filter's own representation (read from state.filt, not a separate tag): sos steps the biquad cascade (z is the (n_sections, 2) state matrix); b shifts one sample through the FIR delay line (z is a plain vector of the last len(b)-1 inputs).

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