A force log records a steady 12 newtons while a robot holds a part. The logging system writes the samples to two files. If a causal low-pass filter starts from rest on each file, the second filtered file can begin with an apparent force drop even though the original sensor readings are unchanged. For consecutive samples from one stream, carry the filter's final delay state into the next chunk.

Separate the startup condition from the file boundary

SciPy's sosfilt runs a filter represented as second-order sections. With no initial state, it starts with zero-valued delays. When an initial state is supplied, the function also returns the final state. In the example below the record starts at a steady nonzero level, so the initial state is the unit-step steady state from sosfilt_zi scaled by the first sample. The same initial state is used for the whole-record baseline and for the first chunk.

import numpy as np

from scipy import signal

sos = signal.butter(2, 0.15, output="sos")

force = np.full(24, 12.0)

force[15:] = 15.0

split = 12

initial = signal.sosfilt_zi(sos) * force[0]

whole, _ = signal.sosfilt(sos, force, zi=initial)

left, next_state = signal.sosfilt(sos, force[:split], zi=initial)

right, _ = signal.sosfilt(sos, force[split:], zi=next_state)

reset = signal.sosfilt(sos, force[split:])

assert np.allclose(np.r_[left, right], whole)

assert np.isclose(right[0], 12.0)

assert reset[0] < 1.0

In SciPy 1.18.1, the carried-state result starts the second chunk at 12.000000 newtons. Starting that chunk from rest instead gives 0.495042 newtons at the same sample. The real input step from 12 to 15 newtons occurs three samples later. The filter still smooths that step, and the two chunks with state carried forward agree with the whole-record result.

Check that the samples really are consecutive

Carry state across a file boundary when the chunks belong to the same channel, arrive in order, and share the same filter coefficients and sampling schedule. A dropped segment, device restart, or change in sample rate needs an explicit policy for the gap. Mark the boundary and record how the filter was initialized. Using a steady-state initialization assumes the signal was steady at its starting level before the available samples.

For a regression test, split a record first in the middle of a steady interval and then near a known input step. Compare each reassembled output with the whole-record baseline. Preserve the input trace alongside the filtered trace so a reviewer can distinguish a real sensor event from a startup transient or a filter's delayed response.

Sources

SciPy sosfilt reference for initial and final filter delays

https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.sosfilt.html

SciPy sosfilt_zi reference for steady-state initialization

https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.sosfilt_zi.html