the sliding average algorithm in Python (moving Average) scenario:
#!/usr/bin/env python #-*-coding:utf-8-*-import NumPy as NP # is equivalent to the smooth function in MATLAB, but the smooth window must be odd.
# yy = Smooth (y) smooths the data in the column vector y. # The few elements of yy are given by # yy (1) = y (1) # yy (2) = (Y (1) + y (2) + y (3))/3 # yy (3) = (Y (1) + y (2) + y (3) + Y (4) + y (5))/5 # yy (4) = (Y (2) + y (3) + y (4) + y (5) + y (6))/5 # ... def smooth (A,WSZ): # A: Raw data, numpy 1-d array con Taining the data to be smoothed # must be 1-d, if not, please use Np.ravel () or Np.squeeze () Convert # wsz:smoothing window size needs, Which must is odd number, # as in the original MATLAB implementation out0 = Np.convolve (A,np.ones (Wsz,dtype=int), ' Valid ')/wsz R = Np.arange (1,wsz-1,2) start = Np.cumsum (A[:wsz-1]) [:: 2]/r stop = (np.cumsum (a[:-wsz:-1)) [:: 2]/ R) [:: -1] Return Np.concatenate ((Start, out0, stop)) # Another one, edge handling not good "" "Def movingaverage (data, Window_si Ze): window = np.ones (int (window_size))/float (window_size) return np.convolve (Data, window, ' same' "" "# Another one, faster # The output is not equal to the original data, assuming the original data is M, and the smoothing step is T, then the output data is M-t+1" "" Def MovingAverage "(Data, window_size): CUMSU M_vec = Np.cumsum (np.insert (data, 0, 0)) Ma_vec = (cumsum_vec[window_size:]-cumsum_vec[:-window_size])/Window_siz E return Ma_vec "" "