forked from Sonohi/monster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulate.m
249 lines (209 loc) · 8.35 KB
/
simulate.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
function simulate(Param, DataIn, utilLo, utilHi)
% SIMULATE is used to run a single simulation
%
% Function fingerprint
% Param -> general simulation parameters
% DataIn -> input data as struct
% utilLo -> value of low utilisation for this simulation
% utilHi -> value for high utilisation for this simulation
trSource = DataIn.trSource;
Stations = DataIn.Stations;
Users = DataIn.Users;
Channel = DataIn.Channel;
ChannelEstimator = DataIn.ChannelEstimator;
SimulationMetrics = MetricRecorder(Param, utilLo, utilHi);
% Create structures to hold transmission data
if (Param.storeTxData)
[tbMatrix, tbMatrixInfo] = initTbMatrix(Param);
[cwdMatrix, cwdMatrixInfo] = initCwdMatrix(Param);
end
[symMatrix, symMatrixInfo] = initSymMatrix(Param);
% create a string to mark the output of this simulation
outPrexif = strcat('utilLo_', num2str(utilLo), '-utilHi_', num2str(utilHi));
if Param.generateHeatMap
switch Param.heatMapType
case 'perClass'
HeatMap = generateHeatMapClass(Stations, Channel, Param);
case 'perStation'
HeatMap = generateHeatmap(Stations, Channel, Param);
otherwise
sonohilog('Unknown heatMapType selected in simulation parameters', 'ERR')
end
else
load('utils/heatmap/HeatMap_eHATA_fBS_pos_5m_res');
end
% if Param.draw
% drawHeatMap(HeatMap, Stations);
% end
% Rounds are 0-based for the subframe indexing, so add 1 when needed
for iRound = 0:(Param.schRounds-1)
% In each scheduling round, check UEs associated with each station and
% allocate PRBs through the scheduling function per each station
sonohilog(sprintf('Round %i/%i',iRound+1,Param.schRounds),'NFO');
% refresh UE-eNodeB association
simTime = iRound*10^-3;
if mod(simTime, Param.refreshAssociationTimer) == 0
sonohilog('Refreshing user association', 'NFO');
[Users, Stations] = refreshUsersAssociation(Users, Stations, Channel, Param, simTime);
end
% Update RLC transmission queues for the users and reset the scheduled flag
for iUser = 1:length(Users)
queue = updateTrQueue(trSource, simTime, Users(iUser));
Users(iUser) = setQueue(Users(iUser), queue);
end
% ---------------------
% ENODEB SCHEDULE START
% ---------------------
for iStation = 1:length(Stations)
% schedule only if at least 1 user is associated
if ~isempty(find([Stations(iStation).Users.UeId] ~= -1))
[Stations(iStation), Users] = schedule(Stations(iStation), Users, Param);
end
% Check utilisation
sch = find([Stations(iStation).ScheduleDL.UeId] ~= -1);
utilPercent = 100*find(sch, 1, 'last' )/length(sch);
% check utilPercent and change to 0 if null
if isempty(utilPercent)
utilPercent = 0;
end
% calculate the power that will be used in this round by this eNodeB
pIn = getPowerIn(Stations(iStation), utilPercent/100);
% store eNodeB-space results
resultsStore(iStation).util = utilPercent;
resultsStore(iStation).power = pIn;
resultsStore(iStation).schedule = Stations(iStation).ScheduleDL;
% Check utilisation metrics and change status if needed
Stations(iStation) = checkUtilisation(Stations(iStation), utilPercent,...
Param, utilLo, utilHi, Stations);
end
% -------------------
% ENODEB SCHEDULE END
% -------------------
% ----------------------------------------------
% ENODEB CREATE DL-SCH TB TO PDSCH SYMBOLS START
% ----------------------------------------------
for iUser = 1:length(Users)
% get the eNodeB this UE is connected to
iServingStation = find([Stations.NCellID] == Users(iUser).ENodeBID);
% Check if this UE is scheduled otherwise skip
if checkUserSchedule(Users(iUser), Stations(iServingStation))
% generate transport block for the user
[Stations(iServingStation), Users(iUser)] = ...
createTransportBlock(Stations(iServingStation), Users(iUser), Param, simTime);
% generate codeword (RV defaulted to 0)
Users(iUser) = createCodeword(Users(iUser), Param);
% finally, generate the arrays of complex symbols by setting the
% correspondent values per each eNodeB-UE pair
% setup current subframe for serving eNodeB
if Users(iUser).CodewordInfo.cwdSize ~= 0
[sym, SymInfo] = createSymbols(Stations(iServingStation), Users(iUser),...
Users(iUser).Codeword, Users(iUser).CodewordInfo, Param);
end
if SymInfo.symSize > 0
symMatrix(iServingStation, iUser, :) = sym;
symMatrixInfo(iServingStation, iUser) = SymInfo;
% Store the pre-OFDM modulated symbols in the UE structure to calculate SER
Users(iUser).Symbols = sym;
Users(iUser).SymbolsInfo = SymInfo;
end
% Save to data structures
if Param.storeTxData
tbMatrix(iServingStation, iUser, :) = Users(iUser).TransportBlock;
tbMatrixInfo(iServingStation, iUser) = Users(iUser).TransportBlockInfo;
cwdMatrix(iServingStation, iUser, :) = Users(iUser).Codeword;
cwdMatrixInfo(iServingStation, iUser) = Users(iUser).CodewordInfo;
end
end
end
% --------------------------------------------
% ENODEB CREATE DL-SCH TB TO PDSCH SYMBOLS END
% --------------------------------------------
% ----------------------------------
% ENODEB GRID MAPPING AND MODULATION
% ----------------------------------
sonohilog('eNodeB grid mapping and modulation', 'NFO');
Stations = enbTxBulk(Stations, symMatrix, Param);
% ----------------------------------
% DL CHANNEL SYNCHRONIZATION
% ------------------------------------
% Setup the channel based on scheduled users
Channel = Channel.setupChannelDL(Stations,Users);
sonohilog('Running sync routine', 'NFO');
[Users, Channel] = syncRoutine(Stations, Users, Channel, Param);
% ------------------
% CHANNEL TRAVERSE
% ------------------
% Once all eNodeBs have created and stored their txWaveforms, we can go
% through the UEs and compute the rxWaveforms
sonohilog(sprintf('Traversing channel in DL (mode: %s)...',Param.channel.modeDL), 'NFO');
[Stations, Users] = Channel.traverse(Stations, Users, 'downlink');
% ------------
% UE RECEPTION
% ------------
sonohilog('UE reception block', 'NFO');
Users = ueRxBulk(Stations, Users, ChannelEstimator.Downlink);
% ----------------
% UE DATA DECODING
% ----------------
sonohilog('UE data decoding block', 'NFO');
[Stations, Users] = ueDataDecoding(Stations, Users, Param, simTime);
% --------------------------
% UE UPLINK
% ---------------------------
sonohilog('Uplink transmission', 'NFO');
[Stations, compoundWaveforms, Users] = ueTxBulk(Stations, Users, iRound, mod(iRound,10));
% ------------------
% CHANNEL TRAVERSE
% ------------------
sonohilog(sprintf('Traversing channel in UL (mode: %s)...',Param.channel.modeUL), 'NFO');
Channel = Channel.setupChannelUL(Stations,Users,'compoundWaveform',compoundWaveforms);
[Stations, Users] = Channel.traverse(Stations, Users,'uplink');
% --------------------------
% ENODEB RECEPTION
% ---------------------------
sonohilog('eNodeB reception block', 'NFO');
Stations = enbRxBulk(Stations, Users, simTime, ChannelEstimator.Uplink);
% ----------------
% ENODEB DATA DECODING
% ----------------
sonohilog('eNodeB data decoding block', 'NFO');
[Stations, Users] = enbDataDecoding(Stations, Users, Param, simTime);
% --------------------------
% ENODEB SPACE METRICS RECORDING
% ---------------------------
sonohilog('eNodeB-space metrics recording', 'NFO');
SimulationMetrics = SimulationMetrics.recordEnbMetrics(Stations, iRound);
% --------------------------
% UE SPACE METRICS RECORDING
% ---------------------------
sonohilog('UE-space metrics recording', 'NFO');
SimulationMetrics = SimulationMetrics.recordUeMetrics(Users, iRound);
% -----------
% UE MOVEMENT
% -----------
sonohilog('UE movement', 'NFO');
for iUser = 1:length(Users)
Users(iUser) = move(Users(iUser), simTime, Param);
end
% Plot resource grids for all users
if Param.draw
delete_figs; % Redraws the plots
[hScatter(1), hScatter(2)] = plotConstDiagram_rx(Stations,Users);
[hGrids(1), hGrids(2)] = plotReGrids(Users);
[hSpectrums(1)] = plotSpectrums(Users,Stations);
end
% --------------------
% RESET FOR NEXT ROUND
% --------------------
sonohilog('Resetting objects for next simulation round', 'NFO');
for iUser = 1:length(Users)
Users(iUser) = Users(iUser).reset();
end
for iStation = 1:length(Stations)
Stations(iStation) = Stations(iStation).reset(iRound + 1);
end
Channel = Channel.resetChannelModels();
end % end round
% Once this simulation set is done, save the output
save(strcat('results/', outPrexif, '.mat'), 'SimulationMetrics');
end