How to reconstruct the signal if there were some temporary offsets I am not a signal processing expert. I have data like this:



where the red is what I have and the black is what I want to have. Any idea how to reconstruct this? The code for generating the data is here:

close all
Ndata = 1000;
X = nan(Ndata,1);
sigma = 0.5;
Y(1) = 50;
offset = 0;
for i =2:Ndata
    % consumption
    X(i,1) = -0.8*rand;
    if rand&lt;0.01 % supply
        X(i,2) = 50;
    end
    if rand&lt;0.5 % offset change
        if abs(offset)==0
           offset = exprnd(50)*(randi(2)*2-3);
        else
           offset = 0;
        end
    end
    X(i,3) = offset ;
    % noise
    X(i,4) = sigma*randn;
    Y(i) = Y(i-1)+sum(X(i,1:2));
    Z(i) = Y(i)+sum(X(i,3:4));
end
plot(Z,'r')
hold on
plot(Y)


The tricky thing is the fact that there is not only a small white noise, but there are also temporary excesses that are relatively large.

Thank you for any advice.
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