lms_init
Builtin index · Signal processing
result = lms_init(n_taps, mu)
Input: n_taps, mu · Output: result
What it is
The least-mean-squares (LMS) adaptive FIR filter's initial state. n_taps (number, at least 1): the adaptive FIR's length; mu (number, positive): the step size.
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = lms_init(n_taps, mu) | n_taps numbern_taps (number, at least 1): the adaptive FIR's length.mu numbermu (number, positive): the step size. | Model —kind "lms"w vectora length-n_taps vector of weights, all zerohist —the length-n_taps sliding input window, all zeroy —the last output, 0e —the last error, 0 | Returns a Model (kind "lms") with fields w (a length-n_taps vector of weights, all zero), hist (the length-n_taps sliding input window, all zero), mu, y (the last output, 0) and e (the last error, 0). State is immutable, the same convention kalman_init uses: thread the value returned by .update(x, d) through successive calls rather than mutating in place, so the whole weight trajectory stays available. Stability: the weights converge for 0 < mu < 2 / (n_taps * E[x^2]); only mu > 0 is enforced, because E[x^2] is a property of the input signal that lms_init has not seen yet — the upper bound is the caller's to respect. Added in v0.3.0. |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
s = lms_init(1, 0.1)
for i = 1 to 50
s = s.update(1, 0.5)
end for
print("w = {s.w[0]}") # w = 0.4974231...
w = 0.497423
from signal-processing
h = [0.5, 0.3, -0.2]
s = lms_init(3, 0.05) # mu well inside 2/(3*1) = 0.667
x = randn(1500, seed = 7)
xm1 = 0
xm2 = 0
for i = 0 to 1499
xi = x[i]
d = h[0]*xi + h[1]*xm1 + h[2]*xm2 # the unknown system's output
s = s.update(xi, d)
xm2 = xm1
xm1 = xi
end for
print("w = {s.w}") # w = [0.5, 0.3, -0.2] to ~2 decimals
print("residual = {s.e}") # ~0 once identified
w = [0.5, 0.3, -0.2] residual = 0
from signal-processing
How it works
The adaptive-filter family: a least-mean-squares (LMS) adaptive FIR, deliberately built on the SAME immutable Value::Model state handle the Kalman filter above uses (kind "lms", fields w/hist/mu/y/e) rather than a mutable filter object — a script threads the returned state through successive state.update(x, d) calls, and the receiver is never modified in place. That matters more here than for kalman: the whole point of an adaptive filter is that you keep the weight history, and an in-place update would quietly destroy the very trajectory you are studying. lms_init(n_taps, mu) — an n_taps-tap filter with all weights AND its sliding input window zeroed, step size mu. Stability: the weights converge for 0 < mu < 2 / (n_taps * E[x^2]). mu is NOT checked against that bound here, and cannot be: E[x^2] is a property of the input signal, which lms_init has not seen a single sample of yet. Only the two things knowable at init time -- n_taps >= 1 and mu > 0 -- are enforced; the upper bound is documented in book/src/stdlib/signal-processing.md for the caller to apply, not silently assumed away.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.