如何对某些NaN表示缺少条目的数据应用Butterwoth过滤?

问题描述

我想将Butterworth过滤应用于某些有辍学的时间序列。我使用python / numpy / scipy。我很清楚Python中实现的Butterworth过滤器。

在我的输入数组中,辍学用一些np.nan值表示。看来,即使我只有1个nan值,Butterworth过滤器的整个输出也就只有nan(示例是从sosfiltfilt的scipy文档改编而成):

from scipy.signal import sosfiltfilt,butter
import matplotlib.pyplot as plt
import numpy as np

n = 201
t = np.linspace(0,1,n)
np.random.seed(123)
x = 1 + (t < 0.5) - 0.25*t**2 + 0.05*np.random.randn(n)

x[24] = np.nan

sos = butter(4,0.125,output='sos')
y = sosfiltfilt(sos,x)

any_real_output = np.logical_not(np.isnan(y)).any()
print("it is {} that the filtered signal has any real output".format(any_real_output))

产生:it is False that the filtered signal has any real output

我认为这是有道理的,因为我对这些过滤器的浅浅理解是,它们使用“广泛地”围绕每个点的数据(即使实际中取决于过滤器也来自整个时间序列)来过滤每个点。 / p>

因此,我的问题是:是否可以/如何对缺少条目(通常由某些nan表示)的数据应用Butterworth过滤(或类似方法)?有没有一种“干净”的方法来做到这一点,而只是忽略了难点?如果没有,您将如何建议过滤这样的时间序列?

注意:

  • 我知道在当前情况下我可以执行线性插值,但是我的情况更加复杂,因此我无法使用这种简单的方法来“修补” nan值

  • 我可以尝试应用一些“虚拟替换”,例如0,但是在这种情况下,这会导致滤波后的输出出现一些非自然的振荡。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)