首页 > 解决方案 > 我需要使用信号重采样对 np 数组进行重采样

问题描述

我有一些 .csv 文件,其中包含不同长度的数据。我需要加载一些已定义的列,对它们进行排序并重新采样到相同的长度(100%)。

我已经编写了 som 定义的嵌套循环来对数据进行排序,但是重采样功能最终不起作用。

subjects   = ['S01_']
conditions = ['Shoe1_', 'Shoe2_']
trials     = ['Run_1', 'Run_2','Run_3']

results_GRF_AP = np.empty(shape=(100,6))*np.NaN
results_GRF_ML = np.empty(shape=(100,6))*np.NaN
results_GRF_VERT = np.empty(shape=(100,6))*np.NaN

results_AnkPower = np.empty(shape=(100,6))*np.NaN


ind = 0
for s, subject in enumerate(subjects):
    for c, condition in enumerate(conditions):
        for t, trial in enumerate(trials):
            ind += 1
            filename = path + subject + condition + trial + extension
            try:
                data2 = pd.read_csv(filename,delimiter = ';')
                data2=np.array(data2)



            except Exception as err:
                print(filename, err)          
                continue
            else:
                print(filename, 'loaded')
                pass


            threshold = 30 ## defined threshold value in Newton from vertical GRF
            signal = np.array(data2[:,4])
            indices_bigger_than_threshold = np.where(signal > threshold)[0] # get item
            non_consecutive = np.where(np.diff(indices_bigger_than_threshold) != 1)[0]+1 # +1 for selecting the next
            first_bigger_than_threshold1 = np.zeros_like(signal, dtype=np.bool)
            first_bigger_than_threshold1[indices_bigger_than_threshold[0]] = True # retain the first
            first_bigger_than_threshold1[indices_bigger_than_threshold[non_consecutive]] = True
            indices_bigger_than_threshold = np.array(indices_bigger_than_threshold)

            GRF_AP = (data2[indices_bigger_than_threshold,2])
            GRF_ML = (data2[indices_bigger_than_threshold,3])
            GRF_VERT= (data2[indices_bigger_than_threshold,4])
            Ank_Power = (data2[indices_bigger_than_threshold,28])



            GRF_AP_Norm = signal.resample(GRF_AP,100)
            GRF_ML_Norm = signal.resample(GRF_ML,100)
            GRF_VERT_Norm= signal.resample(GRF_VERT,100)
            Ank_Power_Norm = signal.resample(Ank_Power,100)


            # talking one coloum from the loaded .csv file to store in the results parameter.  
            results_GRF_AP[:,ind-1] = GRF_AP_Norm
            results_GRF_ML[:,ind-1] = GRF_ML_Norm
            results_GRF_VERT[:,ind-1] = GRF_VERT_Norm

            results_AnkPower[:,ind-1] = Ank_Power_Norm

所有部分单独工作,但在循环中我得到一个错误,因为 AttributeError: 'numpy.ndarry' object has no attribute 'resample'

如果我从 scipy 导入信号运行,然后在加载的主题、条件和试验上使用重新采样,它就可以工作。

标签: pythonsignalsdata-analysisnumpy-ndarray

解决方案


我认为您的问题是您正在使用您称为信号的变量覆盖 scipy 中的信号函数:

signal = np.array(data2[:,4])

如果重命名变量,它应该是固定的。


推荐阅读