Phy*_*hys 7 python filter scipy
我在Python中使用函数filtfilt,如下所示
import numpy as np
from scipy.signal import filtfilt
a = np.array([1, -lambd]).T
b = np.array([-lambd,1]).T
delayed = filtfilt(b,a,sig)
Run Code Online (Sandbox Code Playgroud)
其中 sig 的形状为 (6,)。结果,我收到以下错误:
ValueError:输入向量x的长度必须大于padlen,即6。
由 scipy.signal 生成。
如果 sig 的形状为 (7,) 或更长,则相同的代码可以正常工作,而对于任何小于 (6,) 的形状,它会返回相同的错误。任何想法?
如果错误是:
ValueError:输入向量 x 的长度必须大于 padlen,即 6 [或其他]。
为什么不过滤长度>6的信号子元素?
df = df[[len(x) > 6 for x in df['listSignal']]]
Run Code Online (Sandbox Code Playgroud)
对我来说,过滤 a 已经足够len(x) > 1
避免错误了。我在嵌套的“测试”列中有相当多的 0 长度和 1 长度信号列表。我认为这> 1
是必需的,因为只有 2 的长度才提供左侧一项和右侧一项。
我从https://github.com/leilamr/LowPassFilter借用的函数保持不变:
import pandas as pd
from scipy.signal import butter, freqz, filtfilt
def butter_lowpass(cutoff, fs, order=5):
nyq = 0.5 * fs
normal_cutoff = cutoff / nyq
b, a = butter(order, normal_cutoff, btype='low', analog=False, fs=None)
return b, a
def butter_lowpass_filter(x, cutoff, fs, order=5):
b, a = butter_lowpass(cutoff, fs, order=order)
return filtfilt(b, a, x)
Run Code Online (Sandbox Code Playgroud)
过滤后的 df 没有错误len(x) > 1
(自己尝试一下,你的可能会更高):
df = df[[len(x) > 1 for x in df['test']]]
df['testFilt'] = [butter_lowpass_filter(x, cutoff, fs, order) for x in df['test']]
Run Code Online (Sandbox Code Playgroud)
感谢@DavideNardone提出的过滤函数返回值的想法,而我现在提前过滤主df,想法是一样的。