# Remove regular transient spikes from - otherwise - pretty level data

and let me just say thank you for all the help you good people have been providing to stalkers like me through the years.

So, in the next few days or months or years, I'll be acquiring data of the following general form:

My useful signal is the flat, greenish-tint areas, and my problem is the in-between, quite regular transient spikes, marked in red (-ish, of sorts).

Here's some sample data, too.

Now, due to the width and significant weight of the spikes, and the two-step structure of my data, usual filtering techniques don't seem to produce satisfactory results, and I couldn't conjure any other brilliant, generic ways to throw away the spikes, though it does seem rather trivial.

(Bear in mind, however, that everything but the general form of these data is subject to change: more than two useful levels might be present, the periodicity of the signal might vary etc., so I'm looking for a rather generic approach)

Any ideas would be greatly appreciated.

Thank you all.

EDIT: Well, just after I posted the question, I did come up with something that works for my current datasets. Interpolation of data, and then check the derivative for all x against some appropriate value:

fint = Interpolation[data]; (* data is in the form of {x, y} pairs *)
stepData = Select[data, Abs[fint'[#[[1]]]] < 0.05 &];
(* TODO: don't use a hardcoded test value here *)


I don't know where this will fail, but regardless, all robust and computationally efficient solutions are warmly welcome! Thanks.

sd = Table[{i, data[[i]], StandardDeviation[data[[i - 1 ;; i + 1]]]}, {i, 2, Length[data] - 1}];