Skip to content

Instantly share code, notes, and snippets.

@dbuscombe-usgs
Last active August 29, 2015 14:02
Show Gist options
  • Save dbuscombe-usgs/687a101942c0787456a6 to your computer and use it in GitHub Desktop.
Save dbuscombe-usgs/687a101942c0787456a6 to your computer and use it in GitHub Desktop.
example application of using RunningStats class for running mean calcs
import numpy as np
import matplotlib.pylab as plt
# generate some data
x = np.random.rand(1000,1)
# get the class
import RunningStats
# get an instance of the class
rs = RunningStats.RunningStats()
# define a window size for the running average
window_size = 10
from itertools import product
# use sliding window_nd from here: http://www.johnvinyard.com/blog/?p=268
xs = sliding_window_nd(np.squeeze(x),window_size,1)
runav = []
for j in xrange(len(xs)):
for k in xs[j]:
rs.Push(k)
runav.append(rs.Mean())
rs.Clear()
i = np.linspace(1,len(x),len(runav))
plt.plot(x); plt.plot(i,runav,'r');
# compute using a larger window size
window_size = 50
xs = sliding_window_nd(np.squeeze(x),window_size,1)
runav = []
for j in xrange(len(xs)):
for k in xs[j]:
rs.Push(k)
runav.append(rs.Mean())
rs.Clear()
i = np.linspace(1,len(x),len(runav))
plt.plot(i,runav,'k');
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment