Optimally Smoothing Noisy Curves
Consider a dataset approximated by:
import numpy as np x = np.linspace(0, 2*np.pi, 100) y = np.sin(x) np.random.random(100) * 0.2
This includes 20% variation. Approaches like UnivariateSpline and moving averages present limitations.
Savitzky-Golay Filter
An effective solution is the Savitzky-Golay filter, available in scipy. It uses least squares regression to estimate the value at the center of a small window using a polynomial. The window then shifts to repeat the process, resulting in optimized adjustment of each point.
import numpy as np import matplotlib.pyplot as plt from scipy.signal import savgol_filter x = np.linspace(0, 2*np.pi, 100) y = np.sin(x) np.random.random(100) * 0.2 yhat = savgol_filter(y, 51, 3) # window size 51, polynomial order 3 plt.plot(x,y) plt.plot(x,yhat, color='red') plt.show()
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3