Does anyone know a scipy/numpy module which will allow to fit exponential decay to data?
Google search returned a few blog posts, for example - http://exnumerus.blo
I never got curve_fit to work properly, as you say I don't want to guess anything. I was trying to simplify Joe Kington's example and this is what I got working. The idea is to translate the 'noisy' data into log and then transalte it back and use polyfit and polyval to figure out the parameters:
model = np.polyfit(xVals, np.log(yVals) , 1);
splineYs = np.exp(np.polyval(model,xVals[0]));
pyplot.plot(xVals,yVals,','); #show scatter plot of original data
pyplot.plot(xVals,splineYs('b-'); #show fitted line
pyplot.show()
where xVals and yVals are just lists.
If your decay starts not from 0 use:
popt, pcov = curve_fit(self.func, x-x0, y)
where x0 the start of decay (where you want to start the fit). And then again use x0 for plotting:
plt.plot(x, self.func(x-x0, *popt),'--r', label='Fit')
where the function is:
def func(self, x, a, tau, c):
return a * np.exp(-x/tau) + c