1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| import matplotlib.pyplot as plt import numpy as np import copy
def average_smooth(arr, weights=[1, 2, 3, 5, 3, 2, 1]): result = copy.copy(arr)
for i in range(len(weights) // 2, len(arr) - len(weights)): result[i] = np.average(result[i:i+len(weights)], axis=0, weights=weights)
return result
if __name__ == '__main__':
original_data = np.random.random(100)
smoothed_data = average_smooth(original_data)
plt.figure() plt.grid() plt.plot(original_data) plt.plot(smoothed_data, 'g') plt.show()
|