pythonscipynumpy-ndarraypower-analysis

Python - sosfiltfilt: vector x must be greater than padlen (in reality it is greater)


I'm trying to do an analysis of the power trace in Python 3.12.2. I keep getting ValueError: The length of the input vector x must be greater than padlen, which is 15. on the following line:

proc_pattern = sosfiltfilt(sos, env_pattern)

I checked the size of env_pattern, which is 70000. env_pattern is of type numpy.ndarray as well as variable power or pattern. I also tried to change the parameters of butter, but it only changed the length of padlen in error. My data has 70,000 records, so there should be no problem with the size of them. All numpy.ndarray are 2D. I also tried to transpose the env_pattern and in that case everything works but corresponding graph has no values in it. Adding all the code below:

import os
import numpy as np
import matplotlib.pyplot as plt
from tools import *
import pandas as pd
from scipy.io import loadmat
from scipy import signal
import dask.dataframe as dd
from scipy.signal import hilbert, sosfiltfilt, butter

path = 'D:/DP data/data/'
tmp_directory = '../tmp'

os.chdir(path)
os.getcwd()

data = scipy.io.loadmat('20240408-0001.mat')
power = data['A'] 

pattern = power[2630000:2700000]
np.save(os.path.join(tmp_directory,'raw_outer_iteration_pattern.npy'),pattern)
hilbert_data = hilbert(pattern)
env_pattern = np.abs(hilbert_data)
sos = signal.butter(4, 0.01, output='sos')  
proc_pattern = sosfiltfilt(sos, env_pattern)
fig, axes = plt.subplots(2,1,figsize=(16,4))
axes[0].plot(env_pattern)
axes[0].set_title('Pattern envelop')
axes[1].plot(proc_pattern)
axes[1].set_title('Filtered pattern envelop')
plt.show()

I need help to resolve the error:

Traceback (most recent call last): File "d:\DP data\spracovanie dat\attack.py", line 69, in <module> proc_pattern = sosfiltfilt(sos, env_pattern) File "C:\Users\jmiku\AppData\Local\Programs\Python\Python312\Lib\site-packages\scipy\signal\_signaltools.py", line 4452, in sosfiltfilt edge, ext = _validate_pad(padtype, padlen, x, axis, File "C:\Users\jmiku\AppData\Local\Programs\Python\Python312\Lib\site-packages\scipy\signal\_signaltools.py", line 4221, in _validate_pad raise ValueError("The length of the input vector x must be greater " ValueError: The length of the input vector x must be greater than padlen, which is 15.

I think the problem has something to do with the shape of the env_pattern, but so far I couldn't identify it.


Solution

  • You say "I think the problem has something to do with the shape of the env_pattern, but so far I couldn't identify it." Earlier you write "I checked the size of env_pattern, which is 70000", and you say that all the arrays are 2D, so I suspect env_pattern.shape is (70000, 1). By default, sosfiltfilt acts on the last dimension of the input. If the shape is (70000, 1), sosfiltfilt is trying to apply the filter to signals with length 1, which results in the error that you reported. Transposing the array worked because that makes the shape (1, 70000), and so sosfiltfilt is applied to the dimension with length 70000.

    The simplest fix is probably to add axis=0 in the call of sosfiltfilt, e.g.

    proc_pattern = sosfiltfilt(sos, env_pattern, axis=0)
    

    Alternatively, you could adjust the creation of pattern so that it is one-dimensional, e.g. something like

    pattern = power[2630000:2700000, 0]
    

    or

    pattern = power[2630000:2700000].ravel()