![](/img/trans.png)
[英]in python how to force lfilter from scipy.signal to work over integers
[英]Can't apply scipy.signal lfilter on my data
使用這篇文章中的最佳答案: 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,只改變比例,沒有平滑。
過濾前:
過濾后:
編輯:應用答案中提出的修復后,一切順利,沒有縮放!:
請注意,當您使用pandas.read_csv
讀取該文件時,您應該使用header=None
,否則第一行數據將被視為標題:
In [27]: data = pd.read_csv("Gain_Loss_test.csv", header=None)
使用lfilter
過濾data
出現奇怪結果的原因是 Pandas DataFrame
看起來像一個形狀為(300, 1)
的二維數組:
In [28]: data.shape
Out[28]: (300, 1)
scipy.lfilter
適用於 n 維數組,但必須告知哪個軸包含要過濾的信號。 默認值為axis=-1
,即最后一個軸。 對於您的數據,這意味着它正在過濾 300 個信號,每個信號的長度為 1。這絕對不是您想要的。
有幾種簡單的方法可以解決這個問題:
在lfilter
調用中使用axis=0
:
yy = lfilter(b, a, data, axis=0)
不是將DataFrame
傳遞給lfilter
,而是僅傳遞第一列:
yy = lfilter(b, a, data[0])
data[0]
是一個 Pandas Series
對象,看起來是一維的。
跳過 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.