无法对我的数据应用 scipy.signal lfilter
Can't apply scipy.signal lfilter on my data
使用此 post 中的最佳答案:
我无法重新使用代码对我的数据 -> 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 相关。我不想缩放它,我想平滑它。在原来的 post 中,他们也使用 n=15 但去噪数据没有缩放。我试过改变 n,只改变比例,没有平滑。
过滤前:
过滤后:
编辑:应用答案中提出的修复后,一切顺利,没有缩放!:
请注意,当您使用 pandas.read_csv
读取该文件时,您应该使用 header=None
,否则第一行数据将被视为 header:
In [27]: data = pd.read_csv("Gain_Loss_test.csv", header=None)
用 lfilter
过滤 data
的奇怪结果的原因是 Pandas DataFrame
看起来像 two-dimensional 数组,形状为 (300, 1)
:
In [28]: data.shape
Out[28]: (300, 1)
scipy.lfilter
与 n-dimensional 数组一起使用,但必须告诉它哪个
axis 包含要过滤的信号。默认值为 axis=-1
,即
最后一个轴。对于您的数据,这意味着它要过滤 300 个信号,每个信号都有
length 1,那绝对不是你想要的。
有几种简单的方法可以解决这个问题:
在 lfilter
调用中使用 axis=0
:
yy = lfilter(b, a, data, axis=0)
不是将 DataFrame
传递给 lfilter
,而是仅传递第一列:
yy = lfilter(b, a, data[0])
data[0]
是一个 Pandas Series
object,看起来 one-dimensional.
跳过 Pandas,然后使用 numpy.loadtxt
:
读取数据
In [46]: data = np.loadtxt('Gain_Loss_test.csv')
In [47]: data.shape
Out[47]: (300,)
使用此 post 中的最佳答案:
我无法重新使用代码对我的数据 -> 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 相关。我不想缩放它,我想平滑它。在原来的 post 中,他们也使用 n=15 但去噪数据没有缩放。我试过改变 n,只改变比例,没有平滑。
过滤前:
过滤后:
编辑:应用答案中提出的修复后,一切顺利,没有缩放!:
请注意,当您使用 pandas.read_csv
读取该文件时,您应该使用 header=None
,否则第一行数据将被视为 header:
In [27]: data = pd.read_csv("Gain_Loss_test.csv", header=None)
用 lfilter
过滤 data
的奇怪结果的原因是 Pandas DataFrame
看起来像 two-dimensional 数组,形状为 (300, 1)
:
In [28]: data.shape
Out[28]: (300, 1)
scipy.lfilter
与 n-dimensional 数组一起使用,但必须告诉它哪个
axis 包含要过滤的信号。默认值为 axis=-1
,即
最后一个轴。对于您的数据,这意味着它要过滤 300 个信号,每个信号都有
length 1,那绝对不是你想要的。
有几种简单的方法可以解决这个问题:
在
lfilter
调用中使用axis=0
:yy = lfilter(b, a, data, axis=0)
不是将
DataFrame
传递给lfilter
,而是仅传递第一列:yy = lfilter(b, a, data[0])
data[0]
是一个 PandasSeries
object,看起来 one-dimensional.跳过 Pandas,然后使用
读取数据numpy.loadtxt
:In [46]: data = np.loadtxt('Gain_Loss_test.csv') In [47]: data.shape Out[47]: (300,)