def running_average(obs, ws):
'''
calculates a running average
obs -- observations
ws -- window size (number of points to average)
'''
ws=int(ws)
try:
tmp_vals = np.convolve(np.ones(ws, dtype=float)/ws, obs, mode='same')
# fix the edges. using mode='same' assumes zeros outside the range
if ws%2==0:
tmp_vals[:ws//2]*=float(ws)/np.arange(ws//2,ws)
if ws//2>1:
tmp_vals[-ws//2+1:]*=float(ws)/np.arange(ws-1,ws//2,-1.0)
else:
tmp_vals[:ws//2]*=float(ws)/np.arange(ws//2+1,ws)
tmp_vals[-ws//2:]*=float(ws)/np.arange(ws,ws//2,-1.0)
except:
import ipdb; ipdb.set_trace()
tmp_vals = 0.5*np.ones_like(obs, dtype=float)
return tmp_vals
评论列表
文章目录