如何对不完整的数据应用scipy.signal.filtfilt()

sq1bmfud  于 2022-11-10  发布在  其他
关注(0)|答案(2)|浏览(154)

我想要绘制不完整的数据(有些值是None)。此外,我想要在数据集上套用butter函数,并同时显示不完整和平滑的图形。筛选函数似乎无法行程不完整的数据。
数据文件:data.csv

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from scipy import signal

data = np.genfromtxt('data.csv', delimiter = ',')
df = pd.DataFrame(data)
df.set_index(0, inplace = True)

b, a = signal.butter(5, 0.1)
y = signal.filtfilt(b,a, df[1].values)
df2 = pd.DataFrame(y, index=df.index)

df.plot()
df2.plot()

plt.show()

第一次

sh7euo9m

sh7euo9m1#

我知道这是旧的,但我没有找到任何有用的其他地方,所以我这样做了(可能不是最快的,但仍然有效)。我张贴在这里的情况下,它可以帮助别人:)

from scipy import signal
import numpy as np

b, a = signal.butter(4, 0.15)
data_filtered = np.zeros((len(data)))
data_filtered[:] = np.nan
while_bool = True
current_index = 0
if np.where(np.isnan(data))[0][0] == 0:
    next_number = np.where(~np.isnan(data))[0][0]
    current_index += next_number
else:
    next_number = 0
while while_bool:
    if np.shape(np.where(np.isnan(data[next_number:]))[0]) == (0,):
        next_nan = len(data)
        while_bool = False
    else:
        next_nan = np.where(np.isnan(data[next_number:]))[0]
        next_nan = next_nan[0] + current_index
        current_index = next_nan
    data_filtered[next_number:next_nan] = signal.filtfilt(b, a, data[next_number:next_nan])
    next_number = np.where(~np.isnan(data[next_nan:]))[0]
    if np.shape(next_number) == (0,):
        while_bool = False
    else:
        next_number = next_number[0] + current_index
        current_index = next_number

Gave this filtering

qgelzfjb

qgelzfjb2#

documentation page没有提到任何与NaN相关的内容。您可能必须先从值列表中删除NaN。下面是使用Numpy isnan函数执行此操作的方法:

y = signal.filtfilt(b, a, df[1].values[~np.isnan(df[1].values)])
df2 = pd.DataFrame(y, index=df.index[~np.isnan(df[1].values)])

相关问题