I am working with a signal which is a periodic square signal with some kind of noise and some outliers. I would like to know which is the best solution in order to get the period and clean the outliers that can be seen in the image:

The final goal is to binarize the signal.
Thanks in advance,
Here's what I would do: