Skip to content

Instantly share code, notes, and snippets.

@matham
Created January 3, 2017 20:37
Show Gist options
  • Save matham/c6e8794736ed9c99e6eed16853061f64 to your computer and use it in GitHub Desktop.
Save matham/c6e8794736ed9c99e6eed16853061f64 to your computer and use it in GitHub Desktop.
def schmitt_trigger(up, high, down, low, data):
'''up is the value that when we're in low state and we hit the up value we'll go into up (going up) state.
high is the value that when we're in up state and we hit the high value we'll go into high state.
down is the value that when we're in high state and we hit the down value we'll go into down (going down) state.
low is the value that when we're in down state and we hit the low value we'll go into low state.
data is list of data values.
'''
result = []
for s, val in enumerate(data):
if val < low:
state = 'low'
result.append((s, 0))
break
elif val > high:
state = 'high'
result.append((s, 1))
break
for i in range(s, len(data)):
val = data[i]
if state == 'low':
if val >= up:
state = 'up'
result.append((i, 1))
elif state == 'up':
if val >= down:
state = 'up2'
elif state == 'up2':
if val >= high:
state = 'high'
elif state == 'high':
if val < down:
state = 'down'
result.append((i, 0))
elif state == 'down':
if val < up:
state = 'down2'
elif state == 'down2':
if val < low:
state = 'low'
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment