def sampling_phase(sampling_period, sampling_time, corr_ab=0.9, corr_ae=0.4): ''' sampling_period: 采样周期/采样间隔。单位ms。每隔一个采样周期,可采集到一个样本 sampling_time: 采样时间。单位s。一共采样了这么长的时间,也即每隔一个采样时间,更新一次密钥/导频位置 corr_ab: Alice和Bob的信道测量值的相关系数 corr_ae: Alice和Eve的信道测量值的相关系数 注:在给定相关系数corr时,得到与samples_A满足corr的samples_B(或samples_E) 返回值: 采样序列。一共有time/period个采样点。如period=1,time=25,共采样25*1000/1=25000个 ''' sampling_num = sampling_time * 1000 / sampling_period corr_SNR_dict = { 0.1: 6, 0.2: 8, 0.3: 10, 0.4: 12, 0.5: 14, 0.6: 17, 0.7: 20, 0.8: 23, 0.9: 29, 1.0: 1000 } samples_A = 2 * pi * random(sampling_num) # [0,2pi)之间均匀分布 samples_B = mod(awgn(samples_A, corr_SNR_dict[corr_ab]), 2 * pi) samples_E = mod(awgn(samples_A, corr_SNR_dict[corr_ae]), 2 * pi) return samples_A, samples_B, samples_E
def transmission(x, SNR=dSNR, L=dL, K=dK, N=dN, Ncp=dNcp): ''' x: 发送端的发送信号 SNR: 信噪比 L: 信道长度 K: 稀疏度 N: 子载波数 Ncp: 循环前缀长度 ''' ''' 时域的信道脉冲响应''' h = channel(L, K) ''' 信道频率响应H ''' W = fftMatrix(N, L) # 傅里叶正变换矩阵,即:使稀疏的h变为不稀疏的H的基 H = dot(W, h) # 频率的冲激响应 H.shape = (N, 1) ''' 不考虑循环前缀在信道中的传输 ''' x = x[:, Ncp:] x = x.reshape(N) # 将发送信号作为观测矩阵的对角元素,X=diag(X(0),X(1),...,X(N-1))是N*N的子载波矩阵 X = fft(x) ''' 测量矩阵 ''' X = diag(X) ''' 理想信道传输 ''' X_H = dot(X, H) ''' 添加高斯白噪声,得接收信号向量Y ''' Y = awgn(X_H, SNR) # 加入复高斯白噪声,得到接收到的信号(频域表示) #No = Y-X_H # Y = X*H + No y = ifft(Y, axis=0) y = y.reshape(1, N) y = np.c_[y[:, -Ncp:], y] return h, H, y
def draw_SNR_corr(stype, sampling_period, sampling_time): SNR = range(0, 51, 1) group = 100 SNR_num = len(SNR) corr = zeros((group, SNR_num)) if stype == 'RSSI': samples_A, temp, temp = sampling_RSSI(sampling_period, sampling_time) elif stype == 'Phase': samples_A, temp, temp = sampling_phase(sampling_period, sampling_time) for i in range(group): print 'running...', i for j in range(SNR_num): samples_B = awgn(samples_A, SNR[j]) if stype == 'Phase': samples_B = mod(samples_B, 2 * pi) corr[i, j] = corrcoef(samples_A, samples_B, rowvar=0)[0, 1] corr = mean(corr, 0) plt.figure(figsize=(8, 5)) plt.plot(SNR, corr, 'bo-') plt.xlabel('SNR(dB)') plt.ylabel('Corrcoef') plt.title('Corrcoef in different SNR for ' + stype)
def transmission(x, L, K, N, M, Ncp, Nt, Nr, SNR): ''' x: 发送端的发送信号 L: 信道长度 K: 稀疏度 N: 子载波数 M: 每帧的OFDM符号数 Ncp: 循环前缀长度 Nt: 发送天线数 Nr: 接收天线数 SNR: 信噪比 ''' ''' 时域/频域的信道响应''' h = zeros((Nr, Nt, L), dtype=np.complex) H = zeros((Nr, Nt, N), dtype=np.complex) for r in range(Nr): for t in range(Nt): h[r, t, :] = channel(L, K) # 第r个接收天线,与第t个发送天线的1个符号周期内的信道脉冲响应 H[r, t, :] = dot(fftMatrix(N, L), h[r, t, :]) # 信道频率响应 ''' 傅里叶正变换矩阵 ''' W = fftMatrix((N + Ncp), L) # 傅里叶正变换矩阵,即:使稀疏的h变为不稀疏的H的基 W_M = W # 对于慢时变信道,在M个符号周期内,H保持不变。相当于对W做M次重复 for i in range(M - 1): W_M = np.r_[W_M, W] for t in range(Nt): ''' 第t个天线的发送数据 ''' x_t = x[:, t] ''' 将时域的发送信号,变换到频域 ''' X_t = fft(x_t) ''' 测量矩阵 ''' X_t = diag( X_t) # 将发送信号作为观测矩阵的对角元素,X=diag(X(0),X(1),...,X(N-1))是N*N的子载波矩阵 if t == 0: X_wave = dot(X_t, W_M) else: X_wave = np.c_[X_wave, dot(X_t, W_M)] ''' X_wave与Nr*Nr单位矩阵的克罗内克积 ''' Kronecker = kron(eye(Nr, Nr), X_wave) ''' Nt*Nr*L的信道脉冲响应向量 ''' for r in range(Nr): for t in range(Nt): h_rt = h[r, t, :].reshape(-1, 1) # 第r个接收天线,与第t个发送天线的1个符号周期内的信道脉冲响应 if r == 0 and t == 0: h_vector = h_rt else: h_vector = np.r_[h_vector, h_rt] ''' 理想信道传输 ''' Y = dot(Kronecker, h_vector) ''' AWGN ''' Y = awgn(Y, SNR) y = Y.reshape(-1, Nr) y = ifft(y, axis=0) return h, H, y
corr_SNR_dict = { 0.1: 6, 0.2: 8, 0.3: 10, 0.4: 12, 0.5: 14, 0.6: 17, 0.7: 20, 0.8: 23, 0.9: 29, 1.0: 1000 } samples_A = 2 * pi * random(sampling_num) # [0,2pi)之间均匀分布 samples_B = awgn(samples_A, corr_SNR_dict[corr_ab]) samples_E = awgn(samples_A, corr_SNR_dict[corr_ae]) return samples_A, samples_B, samples_E def draw_SNR_corr(stype, sampling_period, sampling_time): SNR = range(0, 51, 1) group = 100 SNR_num = len(SNR) corr = zeros((group, SNR_num)) samples_A, temp, temp = sampling(stype, sampling_period, sampling_time) for i in range(group): print 'running...', i for j in range(SNR_num):