Created
January 3, 2017 20:37
-
-
Save matham/c6e8794736ed9c99e6eed16853061f64 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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