1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#Smoothing the noise in real-world data #This window rolls over the data and is used to compute the average over that window. import matplotlib.pyplot as plt import numpy as np import os os.chdir(r'D:\projects\wordpress\ex65') def moving_average(interval, window_size): #Compute convoluted window for given size window = np.ones(int(window_size)) / float(window_size) return np.convolve(interval, window, 'same') t = np.linspace(-4, 4, 100) y = np.sin(t) + np.random.randn(len(t))*0.1 plt.plot(t, y, "k.") # compute moving average y_av = moving_average(y, 10) plt.plot(t, y_av,"r") #xlim(0,1000) plt.xlabel("Time") plt.ylabel("Value") plt.grid(True) plt.savefig('ex65.png', dpi=300) plt.show() |
Recent Comments