I have a square signal using the following code. How can I count the rising edges using python?
JavaScript
x
7
1
from scipy import signal
2
import numpy as np
3
import matplotlib.pyplot as plot
4
5
t = np.linspace(0, 1, 1000, endpoint=True)
6
plt.plot(t, signal.square(2 * np.pi * 5 * t))
7
Following image represents my output:
Advertisement
Answer
If your data is super clean (like the signal provided), then just find the number of occurrences when the value is greater than the previous:
JavaScript
1
2
1
np.sum(np.diff(s) > 0) # 5
2