How to filter a regular ticking out of a digital (on/off) signal? I have a digital on/off signal sampled at a regular interval.  There was a problem with the sensor that produced the signal which has sometimes caused a regular on/off ticking when the sensor should be in the off state.  I.e. sometimes when the readings should be zero, there is a square wave instead (but with an unequal mark:space).

How would I go about designing a filter to remove this square wave (i.e. output a zero if there is this regular ticking occurring, output the input value otherwise)?

The ticking has a frequency of around 16 minutes, the events that the sensor was observing might last anything from one minute to a few hours.  I expect that this means that perfect recovery of the correct data is impossible, but I’d like to get as close as possible.

I would think some kind of notch filter is what I need, but everything I’ve found so far seems to be about filtering analogue signals. 

(My memory of signal processing courses is a bit patchy, hopefully I’ve used the right terms here).

EDIT:

Thanks for your comments so far.  I've attached an image illustrating what I am trying to achieve.  Also I wrote the wrong frequency for the regular ticking - it's actually 16 minutes, not 8, but I doubt that affects the overall approach.

@Jan - yes, ideally I would just have a better sensor, but this is the data I have for now, I don't currently have the option of switching to a different sensor.

EDIT 2:

The data processing is happening offline, so non causal solutions are fine.
Are you processing this data offline or in real time? If you're doing offline processing, or can live with a significant delay, then you could apply a simple filter as follows:


From each input sample, check to see if the sensor reported "on".
If it did, search for a "few" 16-minute periods in each direction and see whether the sensor reported "on" there as well. This is a noncausal operation; if you're processing data in real time, you can search in both directions by first applying a large enough delay to the signal.
If there are "enough" consecutive "on" samples at the spurious period in either direction, suppress the "on" indication from the sensor.


This should suppress the unwanted square wave of "on" indications, but as indicated in the comments, it also could possibly suppress valid "on" reports. The "few" and "enough" parameters you'll have to select yourself based upon what is reasonable given the overall behavior of your system. Basically, you want to set "few" so that you're looking at enough spurious periods to make a good decision. You'll want to set "enough" small enough to suppress short bursts of the spurious period, but large enough to avoid suppressing too many valid "on" indications that happen to be separated by 16 minutes.