One Model, Two Algorithms
When an SSM's parameters do not change over time (linear time-invariant, as in S4), the recurrence can be unrolled into a convolution with a fixed kernel Kˉ. Training uses the convolution, inference uses the recurrence, and the two must give the same output. Implement both for one channel with a diagonal state matrix.
lti_ssm(x, a, b, c, delta)
x has shape (L,). a, b, c have shape (N,) and describe a diagonal SSM: A=diag(a) with every an<0, input vector b, output vector c. delta is a positive scalar step size.
- Discretise with the zero-order hold for both parameters (as in the paper and S4D): aˉn=eΔan and bˉn=(eΔan−1)bn/an.
- The kernel has length
L, starting at power zero:
Kˉk=n=1∑Ncnaˉnkbˉn,k=0,…,L−1
y_conv is the causal convolution yt=∑k=0tKˉkxt−k computed with numpy.fft. Zero-pad both signals to an FFT length of at least 2L−1 so that no output wraps around, and keep the first L real outputs.
y_rec runs the recurrence ht=aˉ⊙ht−1+bˉxt with h−1=0 and reads out yt=c⋅ht.
- Return
(K, y_conv, y_rec), three float arrays of shape (L,).
Hints
- The powers aˉnk for all k and n form an
(L, N) Vandermonde-style matrix; the kernel is that matrix times one length-N vector.
- The product of two DFTs is a circular convolution of length equal to the FFT size. Padding is what makes it equal to the linear one.
- An impulse x=(1,0,0,…) must return the kernel itself from both paths, which is a quick self-check.