I have a list of signals (representing consecutive measurements):
signals = [0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0]
I consider a signal that is valid only if it is equal to the previous n measures.
Eg. if we consider only 2 dimensions for validation (n=2) , then the first time signals turn from 0 to 1, we consider it to be 0, but the next measurement, if it is 1 again, we believe that it is still valid and does its 1. Then we need 2 measurements 0 to turn it back to 0, etc. Here the signals are 0 and 1 for simplicity, but in my application they can be other integers.
Required Conclusion:
# For n = 2: valid_s = [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0]
I was looking for a Python single-line way to do this, but couldn't find the result I needed. I tried something line by line:
S = signals # For n = 2 [S[i] if S[i] == S[i-1] else S[i-2] for i, _ in enumerate(S)] # gives [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0] # For n = 3 [S[i] if S[i] == S[i-1] == S[i-2] else S[i-3] for i, _ in enumerate(S)] # gives [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0]
Edit: I am open to numpy if it makes importing it easier.