for i in range(group_num): for j in range(m_num): print 'Running... Current group: ', i, j pos_A, pos_B, pos_E = agreement(P, {'m': m[j]}) bits_A, diagram_A, x = sender(pos_A) h_ab, H_ab, y_b = transmission(x) h_cs, H_cs, bits_cs, diagram_cs = receiver(y_b, pos_B) h_ae, H_ae, y_e = transmission(x) h_eva, H_eva, bits_eva, diagram = receiver(y_e, pos_E) bob_MSE[i, j] = MSE(H_ab, H_cs) eva_MSE[i, j] = MSE(H_ae, H_eva) bob_BER[i, j] = BMR(bits_A, bits_cs) eva_BER[i, j] = BMR(bits_A, bits_eva) SC[i, j] = SecCap(bob_BER[i, j], eva_BER[i, j]) bob_MSE = mean(bob_MSE, 0) eva_MSE = mean(eva_MSE, 0) bob_BER = mean(bob_BER, 0) eva_BER = mean(eva_BER, 0) SC = mean(SC, 0) ''' 画图 ''' plt.figure(figsize=(8, 5)) plt.plot(m, bob_MSE, 'ko-') plt.xlabel('m') plt.ylabel('MSE(dB)') plt.title('MSE') plt.figure(figsize=(8, 5)) plt.semilogy(m, bob_BER, 'ko-')
h_cs, H_cs, bits_cs, diagram_cs = receiver(y_b, pos_B, 'CS', 'from_pos') ''' 窃听信道 ''' h_ae, H_ae, y_e = transmission(x) ''' 非法用户 ''' # 非法用户随机猜测导频位置。与发送端的导频图样pos_A相比,非法用户猜对了j个,j取值[0,P) h_eva, H_eva, bits_eva, diagram_eva = receiver(y_e, pos_A, 'CS', '%d' % (j)) ''' 评价性能 ''' lx_MSE[i, j] = MSE(H_ab, H_lx) CS_MSE[i, j] = MSE(H_ab, H_cs) eva_MSE[i, j] = MSE(H_ae, H_eva) lx_BER[i, j] = BMR(bits_A, bits_lx) CS_BER[i, j] = BMR(bits_A, bits_cs) eva_BER[i, j] = BMR(bits_A, bits_eva) lx_SC[i, j] = SecCap(lx_BER[i, j], eva_BER[i, j]) CS_SC[i, j] = SecCap(CS_BER[i, j], eva_BER[i, j]) lx_MSE = mean(lx_MSE, 0) CS_MSE = mean(CS_MSE, 0) eva_MSE = mean(eva_MSE, 0) lx_BER = mean(lx_BER, 0) CS_BER = mean(CS_BER, 0) eva_BER = mean(eva_BER, 0) lx_SC = mean(lx_SC, 0) CS_SC = mean(CS_SC, 0) ''' 画图 ''' plt.figure(figsize=(8, 5)) plt.plot(right, lx_MSE, 'ko-', label='Bob(Method 1)') plt.plot(right, CS_MSE, 'k^:', label='Bob(Method 2)') plt.plot(right, eva_MSE, 'ks--', label='Eve')
for j in range(SNR_num): print 'Running... Current group: ', i, j ''' Random pilot ''' pos_A, pos_B, pos_E = agreement(P) bits_A, diagram_A, x = sender(N, Ncp, pos_A, modulate_type) h_ab, H_ab, y_b = transmission(x, L, K, N, Ncp, SNR[j]) h_cs, H_cs, bits_cs, diagram_cs = receiver(y_b, L, K, N, Ncp, pos_A, modulate_type) h_ae, H_ae, y_e = transmission(x, L, K, N, Ncp, SNR[j]) h_eva, H_eva, bits_eva, diagram = receiver(y_e, L, K, N, Ncp, pos_E, modulate_type) random_MSE[i, j] = MSE(H_ab, H_cs) random_eva_MSE[i, j] = MSE(H_ab, H_eva) random_BER[i, j] = BMR(bits_A, bits_cs) random_eva_BER[i, j] = BMR(bits_A, bits_eva) random_SC[i, j] = SecCap(random_BER[i, j], random_eva_BER[i, j]) ''' Even pilot ''' pos_A = pos_B = pos_E = pos_even bits_A, diagram_A, x = sender(N, Ncp, pos_A, modulate_type) h_ab, H_ab, y_b = transmission(x, L, K, N, Ncp, SNR[j]) h_cs, H_cs, bits_cs, diagram_cs = receiver(y_b, L, K, N, Ncp, pos_A, modulate_type) h_ae, H_ae, y_e = transmission(x, L, K, N, Ncp, SNR[j]) h_eva, H_eva, bits_eva, diagram = receiver(y_e, L, K, N, Ncp, pos_E, modulate_type) even_MSE[i, j] = MSE(H_ab, H_cs) even_eva_MSE[i, j] = MSE(H_ab, H_eva) even_BER[i, j] = BMR(bits_A, bits_cs) even_eva_BER[i, j] = BMR(bits_A, bits_eva) even_SC[i, j] = SecCap(even_BER[i, j], even_eva_BER[i, j])
for j in range(SNR_num): print 'Running... Current group: ', i, j ''' CS ''' pos_A, pos_B, pos_E = agreement(P) bits_A, diagram_A, x = sender(N, Ncp, pos_A, modulate_type) h_ab, H_ab, y_b = transmission(x, L, K, N, Ncp, SNR[j]) h_cs, H_cs, bits_cs, diagram_cs = receiver(y_b, L, K, N, Ncp, pos_A, modulate_type, 'CS') h_ae, H_ae, y_e = transmission(x, L, K, N, Ncp, SNR[j]) h_eva, H_eva, bits_eva, diagram = receiver(y_e, L, K, N, Ncp, pos_E, modulate_type, 'CS') CS_MSE[i, j] = MSE(H_ab, H_cs) CS_eva_MSE[i, j] = MSE(H_ae, H_eva) CS_BER[i, j] = BMR(bits_A, bits_cs) CS_eva_BER[i, j] = BMR(bits_A, bits_eva) CS_SC[i, j] = SecCap(CS_BER[i, j], CS_eva_BER[i, j]) ''' LS ''' pos_A = pos_B = pos_E = pos_ls bits_A, diagram_A, x = sender(N, Ncp, pos_A, modulate_type) h_ab, H_ab, y_b = transmission(x, L, K, N, Ncp, SNR[j]) h_ls, H_ls, bits_ls, diagram_ls = receiver(y_b, L, K, N, Ncp, pos_B, modulate_type, 'LS') h_ae, H_ae, y_e = transmission(x, L, K, N, Ncp, SNR[j]) h_eva, H_eva, bits_eva, diagram = receiver(y_e, L, K, N, Ncp, pos_E, modulate_type, 'LS') LS_MSE[i, j] = MSE(H_ab, H_ls) LS_eva_MSE[i, j] = MSE(H_ae, H_eva) LS_BER[i, j] = BMR(bits_A, bits_ls) LS_eva_BER[i, j] = BMR(bits_A, bits_eva) LS_SC[i, j] = SecCap(LS_BER[i, j], LS_eva_BER[i, j])
if etype[k] == 'CS': pos_A = pos_B = get_random_pilot(N, P) pos_E = get_random_pilot(N, P) elif etype[k] == 'LS': pos_A = pos_B = pos_E = pos_ls bits_A, diagram_A, x = sender(pos_A) h_ab, H_ab, y_b = transmission(x, SNR[j]) h_cs, H_cs, bits_cs, diagram_cs = receiver(y_b, pos_B, etype[k]) h_ae, H_ae, y_e = transmission(x, SNR[j]) h_eva, H_eva, bits_eva, diagram = receiver(y_e, pos_E, etype[k]) bob_MSE[i, j, k] = MSE(H_ab, H_cs) eva_MSE[i, j, k] = MSE(H_ab, H_eva) bob_BER[i, j, k] = BMR(bits_A, bits_cs) eva_BER[i, j, k] = BMR(bits_A, bits_eva) SC[i, j, k] = SecCap(bob_BER[i, j, k], eva_BER[i, j, k]) bob_MSE = mean(bob_MSE, 0) eva_MSE = mean(eva_MSE, 0) bob_BER = mean(bob_BER, 0) eva_BER = mean(eva_BER, 0) SC = mean(SC, 0) ''' 画图 ''' plt.figure(figsize=(8, 5)) plt.plot(SNR, bob_MSE[:, 0], 'ko-', label='CS(Random, P=%s)' % (P)) plt.plot(SNR, bob_MSE[:, 1], 'k^:', label='LS(Even, P=%s)' % (len(pos_ls))) plt.xlabel('SNR(dB)') plt.ylabel('MSE(dB)') plt.title('MSE') plt.legend() plt.show()
modulate_type) ''' 窃听信道 ''' h_ae, H_ae, y_e = transmission(x, L, K, N, M, Ncp, Nt, Nr, SNR) ''' 非法用户 ''' h_eva, H_eva, bits_eva, diagram_eva = receiver(y_e, L, K, N, M, Ncp, Nt, Nr, pos_E, modulate_type) ''' 评价性能 ''' lx_MSE = MSE(H_ab[0, 0, :], H_lx[0, 0, :, 0]) CS_MSE = MSE(H_ab[0, 0, :], H_cs[0, 0, :, 0]) eva_MSE = MSE(H_ae[0, 0, :], H_eva[0, 0, :, 0]) lx_BER = BMR(bits_A, bits_lx) CS_BER = BMR(bits_A, bits_cs) eva_BER = BMR(bits_A, bits_eva) lx_SC = SecCap(lx_BER, eva_BER) CS_SC = SecCap(CS_BER, eva_BER) ''' 打印信息 ''' print lx_MSE, CS_MSE, eva_MSE print lx_BER, CS_BER, eva_BER print lx_SC, CS_SC ''' 画图 ''' # 第一个发送天线 plot(h_ab[0, 0, :], H_ab[0, 0, :], diagram_A, h_cs[0, 0, :, 0], H_cs[0, 0, :, 0], diagram_cs) # 第二个发送天线 plot(h_ab[0, 1, :], H_ab[0, 1, :], diagram_A, h_cs[0, 1, :, 0], H_cs[0, 1, :, 0], diagram_cs) # 窃听用户 plot(h_ae[0, 0, :], H_ae[0, 0, :], diagram_A, h_eva[0, 0, :, 0], H_eva[0, 0, :, 0], diagram_eva)
for j in range(P_num): print 'Running... Current group: ', i, j pos_A, pos_B, pos_E = agreement(P[j]) bits_A, diagram_A, x = sender(N, Ncp, pos_A, modulate_type) h_ab, H_ab, y_b = transmission(x, L, K, N, Ncp, SNR) h_lx, H_lx, bits_lx, diagram_lx = receiver(y_b, L, K, N, Ncp, pos_A, modulate_type) h_ae, H_ae, y_e = transmission(x, L, K, N, Ncp, SNR) h_eva, H_eva, bits_eva, diagram = receiver(y_e, L, K, N, Ncp, pos_E, modulate_type) val_MSE[i, j] = MSE(H_ab, H_lx) eva_MSE[i, j] = MSE(H_ae, H_eva) val_BER[i, j] = BMR(bits_A, bits_lx) eva_BER[i, j] = BMR(bits_A, bits_eva) SC[i, j] = SecCap(val_BER[i, j], eva_BER[i, j]) val_MSE = mean(val_MSE, 0) eva_MSE = mean(eva_MSE, 0) val_BER = mean(val_BER, 0) eva_BER = mean(eva_BER, 0) SC = mean(SC, 0) ''' 画图 ''' plt.figure(figsize=(8, 5)) plt.plot(P, val_MSE, 'ko-', label='Bob') plt.plot(P, eva_MSE, 'ks--', label='Eve') plt.xlabel('Pilot Amount') plt.ylabel('MSE(dB)') plt.title('MSE') plt.legend() plt.show()