def smooth_curve(curve_data,N_smooth,exp_max=-1,shift_0=0,fix_first_nonzero=False,plotit=False,t='x for plot'):
"""
smoothens the curve data for plotting as good as possible while maintaining last and first value
curve data => np.array, 1D that should be smoothened out
N_smooth => number of points to smooth over (float)
exp_max => adjust exponential behavior for average (0='normal' moving average)
shift_0 => manually fix from where on smoothing is active, e.g. up till where no smootthing is applied
fix_first_nonzero => if set to true, then automatically determines shift_0 to be where the first nonzero entry is
plotit => plot results
t => x-cooordinate for plot
"""
a=curve_data
N=N_smooth
v=np.exp(np.linspace(exp_max, 0., N))
v=v/v.sum()
a_v=np.convolve(a,v,'same')
if fix_first_nonzero==True:
shift_0=np.nonzero(a != 0)[0][0]
for n in range(0,len(v)):
if n!=0:
v=np.exp(np.linspace(exp_max, 0., n))
v=v/v.sum()
a_v[n+shift_0]=np.convolve(a,v,'same')[n+shift_0]
a_v[len(a)-n-1]=np.convolve(a,v,'same')[len(a)-n-1]
else:
a_v[n+shift_0]=a[n+shift_0]
for i in range(0,n+shift_0):
a_v[i]=a[i]
a_v[len(a)-n-1]=a[len(a)-n-1]
if plotit:
try:
np.sin(t)
except:
t=np.linspace(0,len(curve_data),len(curve_data))
import pylab as plt
plt.plot(t,a,label='original data')
plt.plot(t,a_v,label='smoothened')
plt.legend(loc='best',fancybox=True)
plt.title('curve smoothing')
plt.show()
return a_v
评论列表
文章目录