Regex to remove bit signal noise spikes
<p>I am dealing with RF signals that sometimes have noise spikes.<br>
The input is something like this:<br>
<code>00000001111100011110001111100001110000001000001111000000111001111000
</code>  </p>

<p>Before parsing the data in the signal, I need to remove the spike bits, that are 0's and 1's sequence with a lenght lower than (in this example) 3.</p>

<p>So basically I need to match <code>0000000111110001111000111110000111000000(1)000001111000000111(00)1111000
</code><br>
After match, I replace it by the bit before it, so a clean signal look like this:
<code>00000001111100011110001111100001110000000000001111000000111111111000
</code>  </p>

<p>So far I achieved this with two different Regex:</p>

<pre><code>self.re_one_spikes = re.compile("(?:[^1])(?P&lt;spike&gt;1{1,%d})(?=[^1])" % (self._SHORTEST_BIT_LEN - 1))
self.re_zero_spikes = re.compile("(?:[^0])(?P&lt;spike&gt;0{1,%d})(?=[^0])" % (self._SHORTEST_BIT_LEN - 1))
</code></pre>

<p>Then I iterate on the matches and replace.  </p>

<p>How can I do this with a single regex? And can I use regex to replace different sizes matches?<br>
I tried something like this with no success:</p>

<pre><code>re.compile("(?![\1])([01]{1,2})(?![\1])")
</code></pre>

<p>Alternative approach without using <code>regex</code>, and by using <code>replace()</code> instead (in case someone might find it useful in future):</p>

<pre><code>&gt;&gt;&gt; my_signal = '00000001111100011110001111100001110000001000001111000000111001111000'
&gt;&gt;&gt; my_threshold = 3
&gt;&gt;&gt; for i in range(my_threshold):
...     my_signal = my_signal.replace('0{}0'.format('1'*(i+1)), '0{}0'.format('0'*(i+1)))
... 
&gt;&gt;&gt; my_signal
'00000001111100011110001111100000000000000000001111000000000001111000'
</code></pre>

python regex