def CW_10way(self, way, order, examples=200, split=30, data_len=1024, shuffle=False, normalize=True, label=False, SNR=None): """ 1. examples each file <= 119 * 1024 2. if examples>=119, the value of overlap should be True """ file_dir = [self.case10[order]] print('CW_{}way load [{}] loading ……'.format(way, order)) n_way = len(file_dir[0]) # 10 way assert n_way == way n_file = len(file_dir) # how many files for each way num_each_file = examples num_each_way = num_each_file * n_file data_size = num_each_file * data_len data_set = None for i in range(n_way): data_ = np.zeros([n_file, num_each_file, data_len]) for j in range(n_file): data = get_data_csv(file_dir=file_dir[j][i], num=data_size, header=-1, shift_step=200) data = data.reshape([-1, data_len]) data_[j] = data data_ = data_.reshape([-1, data_len]) # [num_each_way, 2048] if SNR is not None: for k, signal in enumerate(data_): data_[k] = add_noise(signal, SNR) if normalize: data_ = normalization(data_) if i == 0: data_set = data_ else: data_set = np.concatenate((data_set, data_), axis=0) data_set = data_set.reshape([n_way, num_each_way, data_len, 1])[:, :examples] if shuffle: data_set = sample_shuffle(data_set) # 数据少 不建议打乱 不利于训练和测试集有序分开 train_data, test_data = data_set[:, : split], data_set[:, split:] # 先shuffle if label: label = np.arange(n_way)[:, np.newaxis] label = np.tile(label, (1, examples)) # [Nc, examples] train_lab, test_lab = label[:, :split], label[:, split:] return train_data, train_lab, test_data, test_lab # [Nc,num_each_way,2048,1], [Nc, 50] else: return train_data, test_data # [Nc, num_each_way, 2048, 1]
def SA_37way(self, way, examples=200, split=30, data_len=1024, shuffle=False, normalize=True, label=False): file_dir = None if way == 3: file_dir = self.SA3 elif way == 7: file_dir = self.SA7 print('SA_{}way loading ……'.format(way)) n_way = len(file_dir[0]) # 3/7 way assert n_way == way n_file = len(file_dir) # how many files for each way, 1 num_each_file = examples num_each_way = num_each_file * n_file data_size = num_each_file * data_len data_set = None for i in range(n_way): data_ = np.zeros([n_file, num_each_file, data_len]) for j in range(n_file): data = get_data_csv(file_dir=file_dir[j][i], num=data_size, header=-1, shift_step=200) data = data.reshape([-1, data_len]) data_[j] = data data_ = data_.reshape([-1, data_len]) # [num_each_way, 2048] if normalize: data_ = normalization(data_) if i == 0: data_set = data_ else: data_set = np.concatenate((data_set, data_), axis=0) data_set = data_set.reshape([n_way, num_each_way, data_len, 1])[:, :examples] if shuffle: data_set = sample_shuffle(data_set) # 数据少 不建议打乱 不利于训练和测试集有序分开 train_data, test_data = data_set[:, : split], data_set[:, split:] # 先shuffle if label: label = np.arange(n_way)[:, np.newaxis] label = np.tile(label, (1, examples)) # [Nc, examples] train_lab, test_lab = label[:, :split], label[:, split:] return train_data, train_lab, test_data, test_lab # [Nc,num_each_way,1024,1], [Nc, 100] else: return train_data, test_data # [Nc, num_each_way, 1024, 1]
def SA_spd(self, way, examples=100, split=30, data_len=1024, normalize=True, label=False, n_spd=3): file_dir = [self.SA_speed] print('SA_{}speed loading ……'.format(n_spd)) n_way = len(file_dir[0][0]) # 3 way assert n_way == way n_file = len(file_dir) # how many files for each way, 1 # num_each_file = 5 * 45 if overlap else 45 # 2, 3, 4, 5... """ 1. examples each file <= 48, suggest: 45 2. if examples>=48, the value of overlap should be True """ num_each_spd = examples data_size = num_each_spd * data_len train_data = np.zeros([n_way, n_spd, n_file, split, data_len]) test_data = np.zeros( [n_way, n_spd, n_file, num_each_spd - split, data_len]) for i in range(n_way): for j in range(n_spd): for k in range(n_file): data = get_data_csv(file_dir=file_dir[k][j][i], num=data_size, header=-1, shift_step=200) data = data.reshape([-1, data_len])[:examples] # data_[j] = data if normalize: data = normalization(data) train_data[i, j, k] = data[:split] test_data[i, j, k] = data[split:] train_data = train_data.reshape([n_way, n_spd, -1, data_len]) test_data = test_data.reshape([n_way, n_spd, -1, data_len]) if label: label = np.arange(n_way)[:, np.newaxis, np.newaxis] label = np.tile(label, (1, n_spd, num_each_spd)) # [Nc, examples] train_lab, test_lab = label[:, :, split], label[:, :, split:] return train_data, train_lab, test_data, test_lab # [Nc, n_spd, num_each_way, 2048, 1], [Nc, n_spd, num_each_way] else: return train_data, test_data # [Nc, n_spd, num_each_way, 2048, 1]