我正在尝试使用 scipy 过滤器在 python 中过滤包含 nan 值的数组:
import numpy as np
import scipy.signal as sp
def apply_filter(x,fs,fc):
l_filt = 2001
b = sp.firwin(l_filt, fc, window='blackmanharris', pass_zero='lowpass', fs=fs)
# zero-phase filter:
xmean = np.nanmean(x)
y = sp.filtfilt(b, 1, x - xmean, padlen=9)
y += xmean
return y
my_array = [13.049393453879606, 11.710994125276567, 15.39159227893492, 14.053192950331884, np.nan, np.nan, np.nan, np.nan, np.nan, 18.57029068436713, np.nan, np.nan, np.nan, np.nan, 15.893492027161058, 16.228091859311817, 15.558892195010298, np.nan, 8.866895551995118, 14.053192950331882]
tt = apply_filter(my_array,64,30)
在上面的代码中,“tt”的值是一个只包含nan值的数组,而不是过滤后的my_array。我究竟做错了什么? (ps.数组只是使代码可重现的示例)。
您的输入数组
my_array
包含NaN值。
当你制作没有 nans 的输入数组时,输出数组也没有 nans:
my_array = np.random.random(1000) # input without NaNs
tt = apply_filter(my_array,64,30) # output has no NaNs
这让我想起了古老的GIGO规则