def derivative_fit(xdata, ydata, neighbors=<NUM_LIT:1>):
x    = []<EOL>dydx = []<EOL>nmax = len(xdata)-<NUM_LIT:1><EOL>for n in range(nmax+<NUM_LIT:1>):<EOL><INDENT>i1 = max(<NUM_LIT:0>, n-neighbors)<EOL>i2 = min(nmax, n+neighbors)<EOL>xmini = _n.array(xdata[i1:i2+<NUM_LIT:1>])<EOL>ymini = _n.array(ydata[i1:i2+<NUM_LIT:1>])<EOL>slope, intercept = fit_linear(xmini, ymini)<EOL>x.append(float(sum(xmini))/len(xmini))<EOL>dydx.append(slope)<EOL><DEDENT>return _n.array(x), _n.array(dydx)<EOL>
loops over the data points, performing a least-squares linear fit of the
nearest neighbors at each point. Returns an array of x-values and slopes.

xdata should probably be well-ordered.

neighbors   How many data point on the left and right to include.