简体   繁体   English

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

[英]Can't apply scipy.signal lfilter on my data

Using the best answer from this post: Reducing noise on Data使用这篇文章中的最佳答案: Reducing noise on Data

I cannot manage to re-use the code to denoise my data-> csv file that can be found here: https://drive.google.com/open?id=1qVOKjDTAIEdB4thiTgv7BmSmfIoDyZ0J我无法重新使用代码来对我的数据进行降噪 -> 可以在此处找到的 csv 文件: https ://drive.google.com/open?id=1qVOKjDTAIEdB4thiTgv7BmSmfIoDyZ0J

My code:我的代码:

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")

Both charts look the same, only the scale is changing, in relation to n.两个图表看起来相同,只是相对于 n 的比例发生了变化。 I don't want to scale it, i want to smooth it.我不想缩放它,我想平滑它。 In the original post, they also use n=15 but the denoised data is not scaled.在原帖中,他们也使用了 n=15,但是去噪后的数据没有被缩放。 I tried changing n, only changes scale, no smoothing.我尝试改变 n,只改变比例,没有平滑。

Before filter:过滤前:

在此处输入图片说明

After filter:过滤后:

在此处输入图片说明

Edit: After applying the fix proposed in the answer, all smooth, no scaling !:编辑:应用答案中提出的修复后,一切顺利,没有缩放!:

在此处输入图片说明

Note that you should use header=None when you read that file using pandas.read_csv , otherwise the first line of data is treated as a header:请注意,当您使用pandas.read_csv读取该文件时,您应该使用header=None ,否则第一行数据将被视为标题:

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

The reason for the strange result of filtering data with lfilter is that the Pandas DataFrame looks like a two-dimensional array with shape (300, 1) :使用lfilter过滤data出现奇怪结果的原因是 Pandas DataFrame看起来像一个形状为(300, 1)的二维数组:

In [28]: data.shape
Out[28]: (300, 1)

scipy.lfilter works with n-dimensional arrays, but it must be told which axis contains the signal(s) to be filter. scipy.lfilter适用于 n 维数组,但必须告知哪个轴包含要过滤的信号。 The default is axis=-1 , which is the last axis.默认值为axis=-1 ,即最后一个轴。 For your data, that means it is filtering 300 signals, each with length 1. That is definitely not what you want.对于您的数据,这意味着它正在过滤 300 个信号,每个信号的长度为 1。这绝对不是您想要的。

There are several simple ways to fix this:有几种简单的方法可以解决这个问题:

  • Use axis=0 in the lfilter call:lfilter调用中使用axis=0

     yy = lfilter(b, a, data, axis=0)
  • Instead of passing the DataFrame to lfilter , pass just the first column:不是将DataFrame传递给lfilter ,而是仅传递第一列:

     yy = lfilter(b, a, data[0])

    data[0] is a Pandas Series object, which looks one-dimensional. data[0]是一个 Pandas Series对象,看起来是一维的。

  • Skip Pandas, and read the data using, say, numpy.loadtxt :跳过 Pandas,使用numpy.loadtxt读取数据:

     In [46]: data = np.loadtxt('Gain_Loss_test.csv') In [47]: data.shape Out[47]: (300,)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM