title: Find where a function starts and ends to decrease
body: <p>I have data which describe a series like that:</p>
  
  <p><a href="https://i.stack.imgur.com/jIhWh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jIhWh.png" alt="enter image description here"></a></p>
  
  <p>I am interested in calculating the points where the strong decreasing starts and ends. Like that:</p>
  
  <p><a href="https://i.stack.imgur.com/odn5z.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/odn5z.png" alt="enter image description here"></a></p>
  
  <p>So I calcuted the second derivative of my function and get the points when there a negative minima (start decreasing) and a positive maximum.</p>
  
  <p>But what I obtain in the most of cases is similar to that:</p>
  
  <p><a href="https://i.stack.imgur.com/lZNjQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lZNjQ.png" alt="enter image description here"></a></p>
  
  <p>Where am I wrong?</p>
  
  <p>Here there is my python code:</p>
  
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  from scipy.interpolate import InterpolatedUnivariateSpline
  
  # set x and y
  y = data_series
  x = range(len(data_series)) 
  
  # interpolation
  f =  InterpolatedUnivariateSpline(x, y) 
  
  # gen function points
  new_x = np.linspace(min(x), max(x), num=1000, endpoint=True)
  new_y = f(new_x)
  
  # calculate second derivative
  y_df1 = np.insert(np.diff(y), 0, 0)
  y_df2 = np.insert(np.diff(y_df1), 0, 0) 
  
  # points where the decrease starts and ends
  # (where the second derivative is minimum and maximum)
  x_dec = np.where(y_df2 == min(y_df2))[0][0]
  x_inc = np.where(y_df2 == max(y_df2))[0][0]
  
  # plot
  plt.plot(new_x, new_y, 'b', lw=3, alpha=0.7)
  plt.plot(x_dec, f(x_dec), 'ro', ms= 8)
  plt.plot(x_inc, f(x_inc),'ro', ms=8)
  plt.show()         
  </code></pre>