Created
June 23, 2019 13:38
-
-
Save tttapa/b9560d10db977c586d10017d77842ccb to your computer and use it in GitHub Desktop.
This file contains 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
class PeakDetector: | |
def __init__(self, | |
threshold: int, hysteresis: int): | |
self.threshold = threshold | |
self.hysteresis = hysteresis | |
self.previous_peak_maximum = 0 | |
self.current_maximum = 0 | |
def __call__(self, value: int): | |
if self.current_maximum > 0: | |
if value >= self.current_maximum: | |
self.current_maximum = value | |
elif self.threshold - value > self.hysteresis: | |
self.previous_peak_maximum = self.current_maximum | |
self.current_maximum = 0 | |
elif value >= self.threshold: | |
self.current_maximum = value | |
return self.previous_peak_maximum | |
if __name__ == '__main__': | |
import numpy as np | |
from matplotlib import pyplot as plt | |
# Generate a test signal | |
n = np.arange(0, 4096) | |
signal = (511-128) * np.sin(2 * np.pi * n / 512) + 511 | |
signal += 63 * np.sin(2 * np.pi * n / 4096) + 63 | |
signal = np.round(signal).astype(np.int) | |
# Apply the peak detector | |
pkdetector = PeakDetector(800, 32) | |
peak_signal = list(map(lambda x: pkdetector(x), signal)) | |
# Plot the results | |
plt.figure(figsize=(10, 6)) | |
plt.plot(signal) | |
plt.plot(peak_signal) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment