How to reconstruct the signal if there were some temporary offsets
I just had a go at something stupidly simple: applying a median filter.

It's not ideal, but it might be a pointer to how to proceed.

The top plot is my instance of your plot.  The bottom plot shows the "noiseless" line from the top plot, with the median filtered version in black.



N2= 10;
for k=1:1000,
    idx1 = max(1,k-N2);
    idx2 = min(1000,k+N2)
    M(k) = median(Z(idx1:idx2));
end