If the error is saying:
ValueError: The length of the input vector x must be greater than padlen, which is 6 [or whatever].
Why not filtering the length of the signal sub-elements > 6?
df = df[[len(x) > 6 for x in df['listSignal']]]
For me, it was already enough to filter for a len(x) > 1
to avoid the error.
I had quite a few 0-length and 1-length signal lists in the nested 'test' column. I assume that > 1
is needed since only a length of 2 provides one item on the left and one on the right.
The functions that I borrowed from https://github.com/leilamr/LowPassFilter stay untouched:
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)
No error with the filtered df len(x) > 1
(try yourself, yours might be higher):
df = df[[len(x) > 1 for x in df['test']]]
df['testFilt'] = [butter_lowpass_filter(x, cutoff, fs, order) for x in df['test']]
Thanks go to @DavideNardone with his idea of filtering the return value of the function, wheras I am now filtering the main df in advance, the idea is the same.
padlen - int or None, optional - The number of elements by which to extend x at both ends of axis before applying the filter. This value must be less than x.shape[axis] - 1. padlen=0 implies no padding. The default value is 3 * max(len(a), len(b)).
(x is third element, that is your sig). In your case default value of padlen is 3*2=6 - but at the same time ` This value must be less than x.shape[axis] - 1`. Source - docs.scipy.org/doc/scipy/reference/generated/… – Mackenzielen(x) < len(a)*len(b)
in a function and in addition constrain the input with[butter_lowpass_filter(x, cutoff, fs, order) for x in df['test'] if len(x) > 0]
so that the input is constrained withif len(x) > 0
before reaching your condition, it works. Thus the remaining error was probably caused by some signal lists of length =0 (or strangly also =1, see answer). – Hugibert