title: How to filter/smooth with SciPy/Numpy?
body: <p>I am trying to filter/smooth signal obtained from a pressure transducer of sampling frequency 50 kHz. A sample signal is shown below:</p>
  
  <p><img src="https://i.stack.imgur.com/ZtxYX.png" alt="enter image description here"></p>
  
  <p>I would like to obtain a smooth signal obtained by loess in MATLAB (I am not plotting the same data, values are different).</p>
  
  <p><img src="https://i.stack.imgur.com/gnf5c.png" alt="enter image description here"></p>
  
  <p>I calculated the power spectral density using matplotlib's psd() function and the power spectral density is also provided below:</p>
  
  <p><img src="https://i.stack.imgur.com/JE7UD.png" alt="enter image description here"></p>
  
  <p>I have tried using the following code and obtained a filtered signal:</p>
  
  <pre><code>import csv
  import numpy as np
  import matplotlib.pyplot as plt
  import scipy as sp
  from scipy.signal import butter, lfilter, freqz
  
  def butter_lowpass(cutoff, fs, order=5):
      nyq = 0.5 * fs
      normal_cutoff = cutoff / nyq
      b, a = butter(order, normal_cutoff, btype='low', analog=False)
      return b, a
  
  def butter_lowpass_filter(data, cutoff, fs, order=5):
      b, a = butter_lowpass(cutoff, fs, order=order)
      y = lfilter(b, a, data)
      return y
  
  data = np.loadtxt('data.dat', skiprows=2, delimiter=',', unpack=True).transpose()
  time = data[:,0]
  pressure = data[:,1]
  cutoff = 2000
  fs = 50000
  pressure_smooth = butter_lowpass_filter(pressure, cutoff, fs)
  
  figure_pressure_trace = plt.figure(figsize=(5.15, 5.15))
  figure_pressure_trace.clf()
  plot_P_vs_t = plt.subplot(111)
  plot_P_vs_t.plot(time, pressure, linewidth=1.0)
  plot_P_vs_t.plot(time, pressure_smooth, linewidth=1.0)
  plot_P_vs_t.set_ylabel('Pressure (bar)', labelpad=6)
  plot_P_vs_t.set_xlabel('Time (ms)', labelpad=6)
  plt.show()
  plt.close()
  </code></pre>
  
  <p>The output I get is:</p>
  
  <p><img src="https://i.stack.imgur.com/m1sAf.png" alt="enter image description here"></p>
  
  <p>I need more smoothing, I tried changing the cutoff frequency but still satisfactory results can not be obtained. I can't get the same smoothness by MATLAB. I am sure it can be done in Python, but how?</p>
  
  <p>You can find the data <a href="https://drive.google.com/file/d/0B4oAWIdgwj-eNkl5NzIxYnJEenM/view?usp=sharing">here</a>.</p>
  
  <p><strong>Update</strong></p>
  
  <p>I applied lowess smoothing from statsmodels, this also does not provide satisfactory results.</p>
  
  <p><img src="https://i.stack.imgur.com/tFPea.png" alt="enter image description here"></p>