降低D上的噪音

2024-06-06 10:30:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个列表,里面有数据点。

x = ["bunch of data points"]
y = ["bunch of data points"]

我在python中使用matplotlib生成了一个图形

import matplotlib.pyplot as plt

plt.plot(x, y, linewidth=2, linestyle="-", c="b")
plt.show()
plt.close()

我能减少数据上的噪音吗?卡尔曼滤波器在这里工作吗?

enter image description here


Tags: of数据import图形列表dataplotmatplotlib
1条回答
网友
1楼 · 发布于 2024-06-06 10:30:01

这取决于你如何定义“噪音”以及它是如何引起的。既然你没有提供多少关于你的案子的信息,我就把你的问题当作“如何使曲线平滑”。卡尔曼滤波器可以做到,但太复杂了,我更喜欢简单的IIR滤波器

import matplotlib.pyplot as plt

mu, sigma = 0, 500

x = np.arange(1, 100, 0.1)  # x axis
z = np.random.normal(mu, sigma, len(x))  # noise
y = x ** 2 + z # data
plt.plot(x, y, linewidth=2, linestyle="-", c="b")  # it include some noise

enter image description here

后过滤器

from scipy.signal import lfilter

n = 15  # the larger n is, the smoother curve will be
b = [1.0 / n] * n
a = 1
yy = lfilter(b,a,y)
plt.plot(x, yy, linewidth=2, linestyle="-", c="b")  # smooth by filter

enter image description here

lfilter是来自scipy.signal的函数。

顺便说一下,如果您想使用Kalman滤波器进行平滑,scipy还提供了一个example。卡尔曼滤波也应该在这种情况下工作,只是没有那么必要。

网友
2楼 · 发布于 2024-06-06 10:30:01

根据您有多喜欢消除噪音,您还可以使用来自scipy的Savitzky Golay过滤器。

下面以@lyken syu为例:

import matplotlib.pyplot as plt
import numpy as np
mu, sigma = 0, 500
x = np.arange(1, 100, 0.1)  # x axis
z = np.random.normal(mu, sigma, len(x))  # noise
y = x ** 2 + z # data
plt.plot(x, y, linewidth=2, linestyle="-", c="b")  # it include some noise

enter image description here

并应用Savitzky Golay滤波器

from scipy.signal import savgol_filter
w = savgol_filter(y, 101, 2)
plt.plot(x, w, 'b')  # high frequency noise removed

window_length = 101

window_length增加到501:

window_length = 501

阅读有关筛选器的详细信息here

相关问题 更多 >