python实现lstm_利⽤Python搭建⽆线通信仿真系统(⼆)利⽤Python搭建通信系统时,有些内置函数我们很难⽤Python实现,因此对于这些函数,我们可以通过MATLAB中的Python API通过Python调⽤这些函数,前⾯我们已经介绍了怎么在Python上安装MATLAB引擎。我们所需要搭建的⽆线通信系统框图如图1所⽰。
前⾯我们介绍了通信系统中的调制解调部分。接着,将调制后传输信号按⼀定的帧结构组成物理帧,在LTE协议中,物理帧中的最⼩单元称为“资源元素”,即为每⼀个调制后的符号,所有资源元素构成资源⽹格。资源元素映射是按⼀定的映射规则将数据符号和导频符号映射到资源⽹格中相应的位,其中数据符号为⽐特流调制后的信号,导频符号为接收双⽅知晓的参考信号,这⾥我们随机⽣成⼀定数量的⽐特信号,然后通过QPSK调制成发送的频域导频符号,其函数如下所⽰
数据符号和导频符号按照导频插⼊的规则分别映射到资源⽹格上,其函数为
其中idx_data表⽰数据符号在资源⽹格处的索引,idx_csr表⽰导频符号在资源⽹格处的索引。将数据符号和导频符号都映射到资源⽹格上后,接下来对完成映射的资源⽹格进⾏OFDM调制,由于OFDM调制的数学原理与傅⾥叶逆(IFFT)变换等效,因此可以直接对资源⽹格进⾏IFFT完成OFDM调制操作。信号经OFDM调制后,添加循环前缀(CP),然后通过并串转换得到发射的时域信号。加⼊CP的⽬的是消除符号间⼲扰(ISI),由于多径的影响,将破坏⼦载波间的正交性,引⼊ISI。其函数为
此时我们已经得到时域的发射信号,接下来是通过衰落信道和AWGN信道,衰落信道我们这⾥使⽤的是MATLAB⾃带的库函数
comm.MIMOChannel,⽽这个函数很难⽤Python实现,因此我们直接在Python中通过MATLAB API调⽤该函数,其调⽤形式为
modulate其中engine为Python中MATLAB的引擎接⼝。AWGN信道⽐较容易实现,其函数为
将发射的时域信号通过这两种信道就得到接收的时域信号,当接收机接收到衰落的信号后,⾸先去除
信号中的CP,然后对去除CP后的信号进⾏OFDM解调,即对信号进⾏(FFT)操作将时域信号转换为频域信号,其函数为
接着对OFDM解调后的信号进⾏资源元素反映射,得到接收的符号流,其函数为
最后通过信道估计、信道均衡和解调将信号还原为⽐特流。这部分后⾯详细介绍。我们将所有函数编写成主函数为
1. import sys
2. sys.path.append('../')
3.
4. import numpy as np
5. import matlab
6. import time
7. import scipy.io as sio
8. ine
9. import pickle as pkl
10.import matplotlib.pyplot as plt
11.from functional import REmapper_Tx, OFDM_Tx, CSRGenerator, get_Model, ChanEstimate_mTx
12.from functional import MIMOFadingChan, AWGNChannel, IdChEst, OFDM_Rx, REdemapper_Tx
13.from functional import ExtChResponse, NMSECaculator, MIMOReceiver, Model, Model_params
14.from MIMOParams import getParams
15.from QAM import Modulator
16.
17.
18.# start matlab engine
19.if 'engine' not in locals() and 'engine' not in globals():
20. try:
21. engine = t_matlab()
22. except:
23. engine = ine.start_matlab()
25.
26.# simulation parameters
27.Dopplers = [130, 260, 389, 519, 648, 778]
28.Delays = [3, 6, 9, 12, 15, 18]
29.Velocity = ['50km/h', '100km/h', '150km/h', '200km/h', '250km/h', '300km/h']
30.dp_indices = 5
31.dl_indices = 5
32.paras = dict(Nsubframe=100,
33. BWCase=1,
34. Doppler=Dopplers[dp_indices],
35. Delay=Delays[dl_indices],
36. Separation=3)
37.p = getParams(**paras)
38.p.EqMode = 1
39.# load model
40.Model_params['bias'] = False
41.strftime = '_%d'%p.Separation
44.
45.SNRs = np.arange(0,26,5).tolist()
46.#SNRs = [30]
47.
48.ber_ls = []
49.ber_lmmse = []
50.ber_cdp = []
51.ber_sta = []
52.ber_mygru = []
53.ber_lstm = []
54.ber_perfect = []
61.for i, SNR in enumerate(SNRs):
62. numErrs_ls = 0
63. numErrs_lmmse = 0
64. numErrs_cdp = 0
65. numErrs_sta = 0
66. numErrs_mygru = 0
67. numErrs_lstm = 0
68. numErrs_p = 0
69. nmse_ls_tmp = 0
70. nmse_lmmse_tmp = 0
71. nmse_cdp_tmp = 0
72. nmse_sta_tmp = 0
73. nmse_mygru_tmp = 0
74. nmse_lstm_tmp = 0
75. st=time.perf_counter()
76. for n in range(p.Nsubframe):
77.# dataIn = engine.randi(matlab.double([0, 1]),p.FRM,1)
78. dataIn = np.random.randint(0,2,(p.FRM,1))
79. # Modulate
80. modOut = Modulator(dType)
81. # CSR generate
82.# csrTx = pkl.load(open('./csrTx','rb'))[0]
83. csrTx = CSRGenerator(p)
84. txGrid = REmapper_Tx(modOut,csrTx,p)
85. txSig = OFDM_Tx(txGrid,p)
86. # MIMO fading channel
87. txSig = matlab.list(),is_complex=True)
88. rxFade, chPathG = MIMOFadingChan(txSig, p, engine)
89. # AWGN channel
90. nVar = 10**(0.1*(-SNR))
91. rxSig = AWGNChannel(np.array(rxFade), nVar)
92. chPathG = np.array(chPathG)
93. H = IdChEst(chPathG, p)
94. rxGrid = OFDM_Rx(rxSig, p)
95. dataRx, csrRx = REdemapper_Tx(rxGrid, p)
96. # channel extimation
97. h_piolt, h_ls, h_lmmse, h_cdp, h_sta = ChanEstimate_mTx(rxGrid, csrRx, csrTx, SNR, p)
98. h_mygru = Model(model_mygru, h_piolt, dl_indices, dp_indices)
99. h_lstm = Model(model_lstm, h_piolt, dl_indices, dp_indices)
100.
101. hD_ls = ExtChResponse(h_ls, p)
102. hD_lmmse = ExtChResponse(h_lmmse, p)
103. hD_cdp = ExtChResponse(h_cdp, p)
104. hD_sta = ExtChResponse(h_sta, p)
105. hD_mygru = ExtChResponse(h_mygru, p)
106. hD_lstm = ExtChResponse(h_lstm, p)
107. hD_p = ExtChResponse(H, p)
108. # NMSE Caculate
109. nmse_ls_tmp += NMSECaculator(hD_ls, hD_p)
110. nmse_cdp_tmp += NMSECaculator(hD_cdp, hD_p)
111. nmse_sta_tmp += NMSECaculator(hD_sta, hD_p)
112. nmse_lmmse_tmp += NMSECaculator(hD_lmmse, hD_p)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论