我需要使用信号重采样对 np 数组进行重采样
I need to resample a np array with signal resample
我有一些包含不同长度数据的 .csv 文件。我需要加载一些已定义的颜色,对它们进行排序并重新采样到相同的长度 (100%)。
我写了一些定义的嵌套循环来对数据进行排序,但重采样功能最终不起作用。
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' 对象没有属性 'resample'
if i 运行 from scipy import signal 然后对加载的主题、条件和试验使用重新采样。
我认为你的问题是你正在用你调用信号的变量覆盖 scipy 中的信号函数:
signal = np.array(data2[:,4])
如果您重命名您的变量,它应该是固定的。
我有一些包含不同长度数据的 .csv 文件。我需要加载一些已定义的颜色,对它们进行排序并重新采样到相同的长度 (100%)。
我写了一些定义的嵌套循环来对数据进行排序,但重采样功能最终不起作用。
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' 对象没有属性 'resample'
if i 运行 from scipy import signal 然后对加载的主题、条件和试验使用重新采样。
我认为你的问题是你正在用你调用信号的变量覆盖 scipy 中的信号函数:
signal = np.array(data2[:,4])
如果您重命名您的变量,它应该是固定的。