def learn(self, train_x: np.ndarray, train_y: np.ndarray, test_x: np.ndarray, test_y: np.ndarray, n_epochs: int, batch_size: int, h_si_len: int = 1): self.h_lin = fd.ls_estimation(train_x, train_y, h_si_len) yCanc = fd.si_cancellation_linear( train_x, self.h_lin.flatten(order='F')).reshape((-1, 1), order='F') y_train = train_y.reshape((-1, 1), order='F') - yCanc self.yVar = np.var(y_train) y_train = y_train / np.sqrt(self.yVar) x_1 = np.vstack((np.zeros( (h_si_len - 1, 1)), train_x.reshape((-1, 1), order='F'))) x_train = np.array([x_1[i:i + h_si_len] for i in range(train_x.size) ]).reshape(train_x.size, h_si_len) # NNの入力に合うように1つのベクトルにする train = np.zeros((x_train.shape[0], (2 * h_si_len))) train[:, 0:(h_si_len)] = x_train.real train[:, (h_si_len):(2 * h_si_len)] = x_train.imag # テストデータの作成 yCanc = fd.si_cancellation_linear(test_x, self.h_lin.flatten('F')).reshape( (-1, 1), order='F') y_test = test_y.reshape((-1, 1)) - yCanc y_test = y_test / np.sqrt(self.yVar) x_1 = np.vstack((np.zeros( (h_si_len - 1, 1)), test_x.reshape((-1, 1), order='F'))) x_test = np.array([x_1[i:i + h_si_len] for i in range(test_x.size) ]).reshape(test_x.size, h_si_len) test = np.zeros((x_test.shape[0], (2 * h_si_len))) test[:, 0:(h_si_len)] = x_test.real test[:, (h_si_len):(2 * h_si_len)] = x_test.imag # 学習 self.history = self.model.fit( train, [y_train.real, y_train.imag], epochs=n_epochs, batch_size=batch_size, verbose=0, validation_data=(test, [y_test.real, y_test.imag]))
def lin_cancel_simulation(params: dict, sigma, h_si, h_s) -> np.ndarray: system_model = PreviousSystemModel( sigma, params['gamma'], params['phi'], params['PA_IBO_dB'], params['PA_rho'], params['LNA_IBO_dB'], params['LNA_rho'], h_si, params['h_si_len'], h_s, params['h_s_len'], params['TX_IQI'], params['PA'], params['LNA'], params['RX_IQI']) system_model.set_lna_a_sat( params['lin_n'], params['LNA_IBO_dB'], ) system_model.transceive_si(params['lin_n']) h_lin = fd.ls_estimation(system_model.x[0:int(params['lin_n'] / 2)], system_model.y, params['h_si_len']) system_model.transceive_si_s(params['lin_n'], ) yCanc = fd.si_cancellation_linear( system_model.x[0:int(params['lin_n'] / 2)], h_lin) cancelled_y = system_model.y - yCanc s_hat = cancelled_y * h_s.conj() / (np.abs(h_s)**2) d_hat = m.demodulate_qpsk(s_hat) d_hat_len = d_hat.shape[0] error = np.sum(system_model.d_s[0:d_hat_len] != d_hat) return error
def cancel(self, x: np.ndarray, h_si_len): self.y_canc_lin = fd.si_cancellation_linear( x, self.h_lin.flatten('F')).reshape((1, -1), order='F') x_1 = np.vstack((np.zeros( (h_si_len - 1, 1)), x.reshape((-1, 1), order='F'))) nn_x = np.array([x_1[i:i + h_si_len] for i in range(x.size)]).reshape(x.size, h_si_len) # NNの入力に合うように1つのベクトルにする x_pred = np.zeros((nn_x.shape[0], (2 * h_si_len))) x_pred[:, 0:(h_si_len)] = nn_x.real x_pred[:, (h_si_len):(2 * h_si_len)] = nn_x.imag # 学習したモデルを評価 self.pred = self.model.predict(x_pred) self.y_canc_non_lin = np.squeeze(self.pred[0] + 1j * self.pred[1], axis=1) self.y_hat = self.y_canc_lin + (np.sqrt(self.yVar) * self.y_canc_non_lin)
def cancel(self, x: np.ndarray, y: np.ndarray, chanLen: int): # Prepare test data for NN x_real = np.reshape( np.array([x[i:i + chanLen].real for i in range(x.size - chanLen)]), (x.size - chanLen, chanLen)) x_imag = np.reshape( np.array([x[i:i + chanLen].imag for i in range(x.size - chanLen)]), (x.size - chanLen, chanLen)) x_pred = np.zeros((x.size - chanLen, 2 * chanLen)) x_pred[:, 0:chanLen] = x_real x_pred[:, chanLen:2 * chanLen] = x_imag # Normalize data for NN yCanc = fd.si_cancellation_linear(x, self.h_lin) y_lin_canc = y - yCanc y_lin_canc = y_lin_canc / np.sqrt(self.yVar) self.pred = self.model.predict(x_pred) self.y_canc_non_lin = np.squeeze(self.pred[0] + 1j * self.pred[1], axis=1) self.cancelled_y = y_lin_canc[0:self.y_canc_non_lin. shape[0]] - self.y_canc_non_lin
def learn(self, x: np.ndarray, y: np.ndarray, training_ratio: float, chanLen: int, epochs: int, batch_size: int): training_samples = int(np.floor(x.size * training_ratio)) x_train = x[0:training_samples] y_train = y[0:training_samples] x_test = x[training_samples:] y_test = y[training_samples:] # Step 1: Estimate linear cancellation arameters and perform linear cancellation self.h_lin = fd.ls_estimation(x_train, y_train, chanLen) yCanc = fd.si_cancellation_linear(x_train, self.h_lin) # Normalize data for NN y_train = y_train - yCanc self.yVar = np.var(y_train) y_train = y_train / np.sqrt(self.yVar) # Prepare training data for NN x_train_real = np.reshape( np.array([ x_train[i:i + chanLen].real for i in range(x_train.size - chanLen) ]), (x_train.size - chanLen, chanLen)) x_train_imag = np.reshape( np.array([ x_train[i:i + chanLen].imag for i in range(x_train.size - chanLen) ]), (x_train.size - chanLen, chanLen)) x_train = np.zeros((x_train.size - chanLen, 2 * chanLen)) x_train[:, 0:chanLen] = x_train_real x_train[:, chanLen:2 * chanLen] = x_train_imag y_train = np.reshape(y_train[chanLen:], (y_train.size - chanLen, 1)) # Prepare test data for NN yCanc = fd.si_cancellation_linear(x_test, self.h_lin) y_test = y_test - yCanc y_test = y_test / np.sqrt(self.yVar) x_test_real = np.reshape( np.array([ x_test[i:i + chanLen].real for i in range(x_test.size - chanLen) ]), (x_test.size - chanLen, chanLen)) x_test_imag = np.reshape( np.array([ x_test[i:i + chanLen].imag for i in range(x_test.size - chanLen) ]), (x_test.size - chanLen, chanLen)) x_test = np.zeros((x_test.size - chanLen, 2 * chanLen)) x_test[:, 0:chanLen] = x_test_real x_test[:, chanLen:2 * chanLen] = x_test_imag y_test = np.reshape(y_test[chanLen:], (y_test.size - chanLen, 1)) ##### Training ##### # Step 2: train NN to do non-linear cancellation self.history = self.model.fit( x_train, [y_train.real, y_train.imag], epochs=epochs, batch_size=batch_size, verbose=0, validation_data=(x_test, [y_test.real, y_test.imag]))
params['h_s_len'], ) system_model.set_lna_a_sat( params['n'], params['LNA_IBO_dB'], ) system_model.transceive_si(params['n']) h_lin = fd.ls_estimation(system_model.x[0:int(params['n'] / 2)], system_model.y, params['h_si_len']) system_model.transceive_si_s(params['n'], ) yCanc = fd.si_cancellation_linear( system_model.x[0:int(params['n'] / 2)], h_lin) cancelled_y = system_model.y - yCanc s_hat = cancelled_y * h_s.conj() / (np.abs(h_s)**2) d_hat = m.demodulate_qpsk(s_hat) d_hat_len = d_hat.shape[0] error = np.sum(system_model.d_s[0:d_hat_len] != d_hat) error_array[sigma_index][trials_index] = error result = Result(params, error_array, None, None) with open(output_dir + '/result.pkl', 'wb') as f: pickle.dump(result, f) ber_fig, ber_ax = graph.new_snr_ber_canvas(params['SNR_MIN'], params['SNR_MAX'])