Skip to content

Instantly share code, notes, and snippets.

@andrewgiessel
Created August 1, 2014 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewgiessel/7fc86d805f694e33b779 to your computer and use it in GitHub Desktop.
Save andrewgiessel/7fc86d805f694e33b779 to your computer and use it in GitHub Desktop.
linear de-trend using curve_fit and sigma
from scipy.optimize import curve_fit
def linear(x, m, b):
return m*x + b
x = np.arange(0,400,1)
y = linear(x, -0.2, 3)
yn = y + np.random.normal(size=len(x))*2
yn[50:350] *= np.random.normal(size=300)
weights = np.zeros_like(yn) + 0.001
weights[50:350] = 1.0
popt, pconv = curve_fit(linear, x, yn, sigma=weights)
y_fit = linear(x,*popt)
plt.figure(figsize=(12,6))
plt.subplot(1,2,1)
plt.plot(x,yn)
plt.plot(y_fit)
plt.subplot(1,2,2)
plt.plot(yn-y_fit)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment