from numpy import average,std,polyfit,poly1d from scipy import interpolate ############################################################################## # Define utility functions ############################################################################## def normalise_mean(data): return [x/average(data) for x in data] def interpolate_integrate(x,y,x0,x1): assert len(x) == len(y) #check strictly increasing x assert all(x[idx] < x[idx+1] for idx in xrange(len(x)-1)) assert x1 >= x0 assert x1 <= max(x) and x1 >= min(x) assert x0 <= max(x) and x0 >= min(x) if x1 == x0: return 0.0 interp_fcn = interpolate.interp1d(x,y) integral = 0.0 for idx in xrange(len(x)-1): if x[idx] >= x0 and x[idx+1] <= x1: #completely within range integral += (x[idx+1]-x[idx])*(y[idx]+y[idx+1])*0.5 elif x[idx] >= x0 and x[idx] <= x1 and x[idx+1] >= x1: #end of integral integral += (x1 - x[idx])*(interp_fcn(x1) + y[idx])*0.5 elif x[idx] <= x0 and x[idx+1] >= x1: #integral is all within one step integral += (x1 - x0)*(interp_fcn(x0) + interp_fcn(x1))*0.5 elif x[idx] <= x0 and x[idx+1] > x0 and x[idx+1] <= x1: #start of integral integral += (x[idx+1] - x0)*(interp_fcn(x0) + interp_fcn(x[idx+1]))*0.5 return integral