这段代码定义了一个名为decompose的函数,用于对数据进行分解。以下是对每行代码的解释:

  1. start_index = 384 # 3s pre-trial signals: 设置起始索引为384,表示前3秒的预试信号。
  2. data = read_file(file): 调用read_file函数读取数据文件,并将结果保存在data变量中。
  3. shape = data.shape: 获取数据的形状,并将结果保存在shape变量中。
  4. frequency = 128: 设置采样频率为128。
  5. decomposed_de = np.empty([0, 4, 120]): 创建一个空的三维数组,用于保存分解后的特征。
  6. base_DE = np.empty([0, 128]): 创建一个空的二维数组,用于保存基础特征。
  7. for trial in range(40):: 循环40次,表示40个试验。
  8. temp_base_DE = np.empty([0]): 创建一个空的一维数组,用于保存临时的基础特征。
  9. temp_base_theta_DE = np.empty([0]): 创建一个空的一维数组,用于保存临时的Theta频带基础特征。
  10. temp_base_alpha_DE = np.empty([0]): 创建一个空的一维数组,用于保存临时的Alpha频带基础特征。
  11. temp_base_beta_DE = np.empty([0]): 创建一个空的一维数组,用于保存临时的Beta频带基础特征。
  12. temp_base_gamma_DE = np.empty([0]): 创建一个空的一维数组,用于保存临时的Gamma频带基础特征。
  13. temp_de = np.empty([0, 120]): 创建一个空的二维数组,用于保存临时的试验特征。
  14. for channel in range(32):: 循环32次,表示32个通道。
  15. trial_signal = data[trial, channel, 384:]: 获取当前试验和通道的信号数据,从384开始截取。
  16. base_signal = data[trial, channel, :384]: 获取当前试验和通道的基础信号数据,截取前384个。
  17. base_theta = butter_bandpass_filter(base_signal, 4, 8, frequency, order=3): 调用butter_bandpass_filter函数对基础信号应用Theta频带的带通滤波器,并将结果保存在base_theta变量中。
  18. base_alpha = butter_bandpass_filter(base_signal, 8, 14, frequency, order=3): 调用butter_bandpass_filter函数对基础信号应用Alpha频带的带通滤波器,并将结果保存在base_alpha变量中。
  19. base_beta = butter_bandpass_filter(base_signal, 14, 31, frequency, order=3): 调用butter_bandpass_filter函数对基础信号应用Beta频带的带通滤波器,并将结果保存在base_beta变量中。
  20. base_gamma = butter_bandpass_filter(base_signal, 31, 45, frequency, order=3): 调用butter_bandpass_filter函数对基础信号应用Gamma频带的带通滤波器,并将结果保存在base_gamma变量中。
  21. base_theta_DE = (compute_DE(base_theta[:64]) + compute_DE(base_theta[64:128]) + compute_DE(base_theta[128:192]) + compute_DE(base_theta[192:256]) + compute_DE(base_theta[256:320]) + compute_DE(base_theta[320:])) / 6: 计算基础Theta频带特征的差分熵,将结果保存在base_theta_DE变量中。
  22. base_alpha_DE = (compute_DE(base_alpha[:64]) + compute_DE(base_alpha[64:128]) + compute_DE(base_alpha[128:192]) + compute_DE(base_theta[192:256]) + compute_DE(base_theta[256:320]) + compute_DE(base_theta[320:])) / 6: 计算基础Alpha频带特征的差分熵,将结果保存在base_alpha_DE变量中。
  23. base_beta_DE = (compute_DE(base_beta[:64]) + compute_DE(base_beta[64:128]) + compute_DE(base_beta[128:192]) + compute_DE(base_theta[192:256]) + compute_DE(base_theta[256:320]) + compute_DE(base_theta[320:])) / 6: 计算基础Beta频带特征的差分熵,将结果保存在base_beta_DE变量中。
  24. base_gamma_DE = (compute_DE(base_gamma[:64]) + compute_DE(base_gamma[64:128]) + compute_DE(base_gamma[128:192]) + compute_DE(base_theta[192:256]) + compute_DE(base_theta[256:320]) + compute_DE(base_theta[320:])) / 6: 计算基础Gamma频带特征的差分熵,将结果保存在base_gamma_DE变量中。
  25. temp_base_theta_DE = np.append(temp_base_theta_DE, base_theta_DE): 将Theta频带的基础特征添加到临时Theta频带基础特征数组中。
  26. temp_base_gamma_DE = np.append(temp_base_gamma_DE, base_gamma_DE): 将Gamma频带的基础特征添加到临时Gamma频带基础特征数组中。
  27. temp_base_beta_DE = np.append(temp_base_beta_DE, base_beta_DE): 将Beta频带的基础特征添加到临时Beta频带基础特征数组中。
  28. temp_base_alpha_DE = np.append(temp_base_alpha_DE, base_alpha_DE): 将Alpha频带的基础特征添加到临时Alpha频带基础特征数组中。
  29. theta = butter_bandpass_filter(trial_signal, 4, 8, frequency, order=3): 调用butter_bandpass_filter函数对试验信号应用Theta频带的带通滤波器,并将结果保存在theta变量中。
  30. alpha = butter_bandpass_filter(trial_signal, 8, 14, frequency, order=3): 调用butter_bandpass_filter函数对试验信号应用Alpha频带的带通滤波器,并将结果保存在alpha变量中。
  31. beta = butter_bandpass_filter(trial_signal, 14, 31, frequency, order=3): 调用butter_bandpass_filter函数对试验信号应用Beta频带的带通滤波器,并将结果保存在beta变量中。
  32. gamma = butter_bandpass_filter(trial_signal, 31, 45, frequency, order=3): 调用butter_bandpass_filter函数对试验信号应用Gamma频带的带通滤波器,并将结果保存在gamma变量中。
  33. DE_theta = np.zeros(shape=[0], dtype=float): 创建一个长度为0的一维浮点型数组,用于保存Theta频带的差分熵。
  34. DE_alpha = np.zeros(shape=[0], dtype=float): 创建一个长度为0的一维浮点型数组,用于保存Alpha频带的差分熵。
  35. DE_beta = np.zeros(shape=[0], dtype=float): 创建一个长度为0的一维浮点型数组,用于保存Beta频带的差分熵。
  36. DE_gamma = np.zeros(shape=[0], dtype=float): 创建一个长度为0的一维浮点型数组,用于保存Gamma频带的差分熵。
  37. for index in range(120):: 循环120次,表示120个样本。
  38. DE_theta = np.append(DE_theta, compute_DE(theta[index * 64:(index + 1) * 64])): 计算当前样本Theta频带的差分熵,并将结果添加到Theta频带差分熵数组中。
  39. DE_alpha = np.append(DE_alpha, compute_DE(alpha[index * 64:(index + 1) * 64])): 计算当前样本Alpha频带的差分熵,并将结果添加到Alpha频带差分熵数组中。
  40. DE_beta = np.append(DE_beta, compute_DE(beta[index * 64:(index + 1) * 64])): 计算当前样本Beta频带的差分熵,并将结果添加到Beta频带差分熵数组中。
  41. DE_gamma = np.append(DE_gamma, compute_DE(gamma[index * 64:(index + 1) * 64])): 计算当前样本Gamma频带的差分熵,并将结果添加到Gamma频带差分熵数组中。
  42. temp_de = np.vstack([temp_de, DE_theta]): 将Theta频带的差分熵数组作为一行添加到临时试验特征数组中。
  43. temp_de = np.vstack([temp_de, DE_alpha]): 将Alpha频带的差分熵数组作为一行添加到临时试验特征数组中。
  44. temp_de = np.vstack([temp_de, DE_beta]): 将Beta频带的差分熵数组作为一行添加到临时试验特征数组中。
  45. temp_de = np.vstack([temp_de, DE_gamma]): 将Gamma频带的差分熵数组作为一行添加到临时试验特征数组中。
  46. temp_trial_de = temp_de.reshape(-1, 4, 120): 将临时试验特征数组重塑为三维数组,形状为(样本数量,频带数量,样本长度)。
  47. decomposed_de = np.vstack([decomposed_de, temp_trial_de]): 将重塑后的试验特征数组添加到分解后特征的数组中。
  48. temp_base_DE = np.append(temp_base_theta_DE, temp_base_alpha_DE): 将临时Theta频带和Alpha频带的基础特征数组合并。
  49. temp_base_DE = np.append(temp_base_DE, temp_base_beta_DE): 将临时Beta频带的基础特征数组添加到基础特征数组中。
  50. temp_base_DE = np.append(temp_base_DE, temp_base_gamma_DE): 将临时Gamma频带的基础特征数组添加到基础特征数组中。
  51. base_DE = np.vstack([base_DE, temp_base_DE]): 将基础特征数组添加到基础特征的数组中。
  52. decomposed_de = decomposed_de.reshape(-1, 32, 4, 120).transpose([0, 3, 2, 1]).reshape(-1, 4, 32).reshape(-1, 128): 对分解后的特征进行形状变换,将其变为二维数组。
  53. print("base_DE shape:", base_DE.shape): 打印基础特征的形状。
  54. print("trial_DE shape:", decomposed_de.shape): 打印试验特征的形状。
  55. return base_DE, decomposed_de: 返回基础特征和分解后的特征。
Python 代码逐行解析: 数据分解函数 `decompose`

原文地址: https://www.cveoy.top/t/topic/NiB 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录