Home > Backend Development > Python Tutorial > How to Effectively Smoothen Noisy Data Curves?

How to Effectively Smoothen Noisy Data Curves?

Susan Sarandon
Release: 2024-10-20 15:58:29
Original
765 people have browsed it

How to Effectively Smoothen Noisy Data Curves?

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
Copy after login

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()
Copy after login

The above is the detailed content of How to Effectively Smoothen Noisy Data Curves?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template