title: Progressively filter/smooth a signal in python (to straight line on the left to no filtering on the right)
body: <p>A picture is worth a thousand words (sorry for the shoddy work):</p>
  <p><a href="https://i.stack.imgur.com/7CDs7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7CDs7.png" alt="Example of filtering" /></a></p>
  <p>If the solution is preserving the value and the slope at both ends it is better.
  If, in addition, the position and sharpness of the transition can be adjusted it is perfect.</p>
  <p>But I have not found any solution yet...</p>
  <p>Thank you very much for your help</p>
  <p>Here is a piece of code to get started:</p>
  <pre class="lang-py prettyprint-override"><code>import matplotlib.pyplot as plt
  from scipy.signal import savgol_filter
  import numpy as np 
  
  def round_up_to_odd(f):
      return np.int(np.ceil(f / 2.) * 2 + 1)
  
  def generateRandomSignal(n=1000, seed=None):
      &quot;&quot;&quot;
      Parameters
      ----------
      n : integer, optional
          Number of points in the signal. The default is 1000.
  
      Returns
      -------
      sig : numpy array
  
      &quot;&quot;&quot;
      np.random.seed(seed)
      print(&quot;Seed was:&quot;, seed)
      steps = np.random.choice(a=[-1, 0, 1], size=(n-1))
      roughSig = np.concatenate([np.array([0]), steps]).cumsum(0)
      sig = savgol_filter(roughSig, round_up_to_odd(n/20), 6)
      return sig
  
  n = 1000
  t = np.linspace(0,10,n)
  seed = np.random.randint(0,high=100000)
  #seed = 45136
  sig = generateRandomSignal(seed=seed)
  
  ###############################
  # ????
  # sigFilt = adaptiveFilter(sig)
  ###############################
  
  # Plot
  plt.figure()
  plt.plot(t, sig, label=&quot;Signal&quot;)
  # plt.plot(t, sigFilt, label=&quot;Signal filtered&quot;)
  plt.legend()
  </code></pre>