无法在我的数据上应用scipy.signal lfilter

时间:2018-11-17 15:49:02

标签: python scipy

使用此帖子的最佳答案: Reducing noise on Data

我无法重新使用代码对数据进行降噪处理->可以在此处找到的csv文件: https://drive.google.com/open?id=1qVOKjDTAIEdB4thiTgv7BmSmfIoDyZ0J

我的代码:

import pandas as pd
import matplotlib.pyplot as plt
from scipy.signal import lfilter

data = pd.read_csv("Gain_Loss_test.csv")

#plot the original data
x = np.arange(1, 300, 1)  # x axis
y = data
plt.plot(x, y, linewidth=1, linestyle="-", c="b")

#apply the filter and plot the denoised data
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=1, linestyle="-", c="b")

两个图表看起来都一样,只是比例相对于n在变化。我不想缩放它,我想使其平滑。在原始帖子中,它们也使用n = 15,但去噪后的数据未缩放。我尝试更改n,仅更改比例,不进行平滑处理。

在过滤器之前

enter image description here

过滤后:

enter image description here

编辑:应用答案中提出的解决方案后,所有操作都平滑无缩放!:

enter image description here

1 个答案:

答案 0 :(得分:1)

请注意,使用header=None读取文件时,应使用pandas.read_csv,否则数据的第一行将被视为标头:

In [27]: data = pd.read_csv("Gain_Loss_test.csv", header=None)

使用data过滤lfilter的奇怪结果的原因是,熊猫DataFrame看起来像形状为{{1 }}:

(300, 1)

In [28]: data.shape Out[28]: (300, 1) 适用于n维数组,但必须告诉它哪个 轴包含要过滤的信号。默认值为scipy.lfilter,即 最后一个轴。对于您的数据,这意味着它正在过滤300个信号,每个信号 长度1.绝对不是您想要的。

有几种简单的方法可以解决此问题:

  • axis=-1呼叫中使用axis=0

    lfilter
  • 而不是将yy = lfilter(b, a, data, axis=0) 传递给DataFrame,而仅传递第一列:

    lfilter

    yy = lfilter(b, a, data[0]) 是熊猫data[0]对象,看起来是一维的。

  • 跳过熊猫,并使用Series读取数据:

    numpy.loadtxt