for a school project I need to implement the below function:
Make a function find_intervals(s, threshold)
that receives in input a Series s and a threshold value.
Find the contiguous periods when the signal is above the given threshold.
The function should return a Series that has as index the start date for each contiguous period and has as associated value the period length expressed in a number of days. The result should be sorted in descending order of period length.
When applied to a signal such as this (orange line with thershold=0):
it should return the following Series:
70 35
140 35
1 34
Name: interval, dtype: int64
that is, the largest interval is 35 units and it starts at label 70, then there is another interval of length 35 that starts at 140, etc. In the exercise, the index will be a date and the length of the interval is expressed in days.
I have written the following function ( with the help of this Stackoverflow answer.)
def intervals(samples,threshold):
samples = np.array(samples)
start = -1
intervals = []
for idx,x in enumerate(samples):
if start < 0 and abs(x) < threshold:
start = idx
elif start >= 0 and abs(x) >= threshold:
dur = idx-start
if dur >= 0:
intervals.append((start))
start = -1
return intervals
However, when I call this function on a similar Sin wave, the function doesn't work for the threshold value 0 or any negative values. I couldn't really figure out why.
Edit: Here's what I tried and the result I got;
With the following piece, I plotted a simple Sin wave.
x = np.arange(0,64*np.pi,1)
y = np.sin(x/11)
df = pd.Series(data=y,index=x)
plt.plot(x,y)
df = np.array(df)
when I run the code with intervals(df,0.5)
I get
[0, 34, 69, 103, 138, 172]
which is expected.
However;
If I do; intervals(df,0)
I get an empty list, the same thing can be said for any negative threshold value.