NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
LTENR_Interference.c
1#pragma region HEADER_FILES
2#include "stdafx.h"
3#include "LTENR_PHY.h"
4#pragma endregion
5
6#pragma region INTERFERENCE_PARAMETER
7
8extern char* wynerFile;
10 NETSIM_ID CellId;
11 NETSIM_ID PairId;//PairId
12 double DDRth;
13 int BinCount;//BinCount
14 double* Relative_Intereference_dB;
16}GRADED_DISTANCE_BASED_WYNER_INTERFERENCE, * ptrGRADED_DISTANCE_BASED_WYNER_INTERFERENCE;
17ptrGRADED_DISTANCE_BASED_WYNER_INTERFERENCE GradedDistanceBasedWynerInterferenceheadDownlink;
18ptrGRADED_DISTANCE_BASED_WYNER_INTERFERENCE GradedDistanceBasedWynerInterferenceheadUplink;
19#pragma endregion
20
21//Interference Calc
22static double fn_GetEdgeInterference(NETSIM_ID gNBiID, NETSIM_ID gNBjID, double dUEgnbi, double dUEgnbj,bool isDownlink) {
23 ptrGRADED_DISTANCE_BASED_WYNER_INTERFERENCE temp = NULL;
24 double interference_db = -NEGATIVE_DBM;
25 if(isDownlink)
26 temp = GradedDistanceBasedWynerInterferenceheadDownlink;
27 if(!isDownlink)
28 temp = GradedDistanceBasedWynerInterferenceheadUplink;
29
30 if (!temp) return interference_db;
31 else {
32 while (temp) {
33 if ((temp->CellId == gNBiID && temp->PairId == gNBjID) ||
34 (temp->CellId == gNBjID && temp->PairId == gNBiID)) {
35 double ratio = (dUEgnbj - dUEgnbi) / dUEgnbj;
36 if (ratio < 0 && ratio>(-temp->DDRth)) return temp->Relative_Intereference_dB[0];
37 if (ratio >= 0 && ratio <= temp->DDRth) {
38 double Band = temp->DDRth / temp->BinCount;
39 int i = 1;
40 while (i <= temp->BinCount+1) {
41 double bi = Band * ((double)i);
42 if (ratio < bi) {
43 return(temp->Relative_Intereference_dB[i]);
44 }
45 i++;
46 }
47 }
48 }
49 temp = temp->next;
50 }
51 }
52 return interference_db;
53}
54
55//Graded Distance Based Wyner Model
56static void LTENR_fn_Graded_Distance_Based_Wyner_Model_Interference(ptrLTENR_PROPAGATIONINFO info, double rxPower_dbm,
57 double* Interference,int CA_ID, bool isDownlink) {
58
59 double Interference_linear = 0;
60 double In_db = INFINITY;//Relative Interference
61 ptrLTENR_PROTODATA data = LTENR_PROTODATA_GET(info->ueId, info->ueIf);
62 ptrLTENR_UEPHY uePhy = LTENR_UEPHY_GET(info->ueId, info->ueIf);
63 for (NETSIM_ID r = 0; r < NETWORK->nDeviceCount; r++)
64 {
65 if (info->ueId == r + 1)
66 continue;
67
68 for (NETSIM_ID rin = 0; rin < DEVICE(r + 1)->nNumOfInterface; rin++)
69 {
70 if (!isLTE_NRInterface(r + 1, rin + 1))
71 continue;
72
73 ptrLTENR_PROTODATA pd = LTENR_PROTODATA_GET(r + 1, rin + 1);
74 ptrLTENR_GNBPHY phy = NULL;
75 if (pd->deviceType == LTENR_DEVICETYPE_GNB) {
76 phy = LTENR_GNBPHY_GET(r + 1, rin + 1);
77 ptrLTENR_ASSOCIATEDUEPHYINFO assocInfo = LTENR_ASSOCIATEDUEPHYINFO_GET(phy->gnbId, phy->gnbIf, info->ueId, info->ueIf);
78 ptrLTENR_PROPAGATIONINFO pinfo = assocInfo->propagationInfo[CA_ID];
79
80 if (pinfo && info->gnbId != r + 1 && pinfo->centralFrequency_MHz == info->centralFrequency_MHz) {
81 In_db = fn_GetEdgeInterference(info->gnbId, r + 1, info->dist2D, pinfo->dist2D, isDownlink);
82 Interference_linear += DBM_TO_MW((rxPower_dbm - In_db));
83 //We convert relative interference In into absolute interference(dB)
84 //We than convert dB scale to Linear scale.
85 }
86 }
87 }
88 }
89 *Interference = MW_TO_DBM(Interference_linear);
90 return;
91}
92
93//ConfigInit
94void LTENR_fn_InterferenceInit() {
95 FILE* fp;
96 char* temp;
97 char data[BUFSIZ], input[BUFSIZ];
98 GradedDistanceBasedWynerInterferenceheadDownlink = (ptrGRADED_DISTANCE_BASED_WYNER_INTERFERENCE)malloc(sizeof(GRADED_DISTANCE_BASED_WYNER_INTERFERENCE));
99 ptrGRADED_DISTANCE_BASED_WYNER_INTERFERENCE t = GradedDistanceBasedWynerInterferenceheadDownlink;
100 int line = 0;
101 sprintf(input, "%s%s%s", pszIOPath,pathSeperator, wynerFile);
102
103 fp = fopen(input, "r");
104 if (fp == NULL)
105 {
106 return;
107 }
108 else
109 {
110
111 while (fgets(data, BUFSIZ, fp))
112 {
113 line++;
114 temp = data;
115 if (*temp == '\n')
116 continue;
117 if (*temp == '#' || *temp == 0)
118 continue;
119 bool dev1 = false, dev2 = false;
120 double in;
121 char* newline = strchr(temp, '\n'); if (newline) *newline = 0;
122 char* token = strtok(temp, ",");
123
124
125 if (!t) break;
126 if (token != NULL)
127 {
128 t->CellId = fn_NetSim_Stack_GetDeviceId_asName(token);
129 if (!t->CellId)
130 {
131 fprintf(stderr, "Invalid Input detected in line %d.\n\"%s\" is not a valid Device Name. Please edit the file and re-run simulation. \n"
132 "Press any key to skip this line and continue simulation.\n", line, token);
133 _getch();
134 }
135 else
136 {
137 for (NETSIM_ID In = 1; In <= DEVICE(t->CellId)->nNumOfInterface; In++)
138 {
139 if (!isLTENR_RANInterface(t->CellId, In)) continue;
140 if (isGNB(t->CellId, In))
141 {
142 dev1 = true;
143 break;
144 }
145 }
146 if (!dev1)
147 {
148 fprintf(stderr, "Invalid Input detected in line %d.\n\"%s\" is not a valid gNB Device. Please edit the file and re-run simulation. \n"
149 "Press any key to skip this line and continue simulation.\n", line, token);
150 _getch();
151 }
152 else
153 {
154 if (token == NULL)
155 continue;
156 else
157 {
158 token = strtok(NULL, ",");
159 t->PairId = fn_NetSim_Stack_GetDeviceId_asName(token);
160
161 if (!t->PairId)
162 {
163 fprintf(stderr, "Invalid Input detected in line %d.\n\"%s\" is not a valid Device Name. Please edit the file and re-run simulation. \n"
164 "Press any key to skip this line and continue simulation.\n", line, token);
165 _getch();
166 }
167 else
168 {
169 for (NETSIM_ID In = 1; In <= DEVICE(t->PairId)->nNumOfInterface; In++)
170 {
171 if (!isLTENR_RANInterface(t->PairId, In)) continue;
172 if (isGNB(t->PairId, In))
173 {
174 dev2 = true;
175 break;
176 }
177 }
178 if (!dev2)
179 {
180 fprintf(stderr, "Invalid Input detected in line %d.\n\"%s\" is not a valid LTE NR Device. Please edit the file and re-run simulation. \n"
181 "Press any key to skip this line and continue simulation.\n", line, token);
182 _getch();
183 }
184 }
185 }
186 }
187 }
188 }
189
190 token = strtok(NULL, ",");
191 if (token != NULL) {
192 t->DDRth = strtod(token, NULL);
193 }
194
195 token = strtok(NULL, ",");
196 int bincount = 4, k = 1;
197 if (token != NULL) {
198 t->BinCount = atoi(token);
199 bincount = t->BinCount;
200 }
201
202 t->Relative_Intereference_dB = calloc((bincount + 2), sizeof(t->Relative_Intereference_dB));//Relative Interference
203
204 token = strtok(NULL, ",");
205 if (token != NULL) {
206 in = strtod(token, NULL);
207 if (in >= -3 && in <= 0) t->Relative_Intereference_dB[0] = in;
208 else
209 printf("\nIncorrect value of Relative Interference"
210 "has been found in line: %d, for Relative Interference -1 Enter value in range of 0 to 10", line);
211 }
212 while (k < bincount + 2) {
213
214 token = strtok(NULL, ",");
215 if (token != NULL) {
216 in = strtod(token, NULL);
217 if (t->Relative_Intereference_dB[k - 1] <= in && 0 <= in)
218 t->Relative_Intereference_dB[k] = in;
219 else
220 printf("\nIncorrect value of Relative Interference has been found in line: %d, for In %d", line, k - 1);
221 }
222 k++;
223 }
224 t->next = (ptrGRADED_DISTANCE_BASED_WYNER_INTERFERENCE)malloc(1*sizeof(GRADED_DISTANCE_BASED_WYNER_INTERFERENCE));
225 t = t->next;
226
227 }
228
229 }
230 if (fp) fclose(fp);
231}
232
233//Geometric Edge Interference
234void LTENR_fn_ExactGeometricModelInterference(ptrLTENR_PROPAGATIONINFO info, double rxPower_dbm,
235 double* Interference,int layerId, int CA_ID, bool isDownlink) {
236
237 double Interference_linear = 0;
238
239 ptrLTENR_PROTODATA data = LTENR_PROTODATA_GET(info->ueId, info->ueIf);
240 if(isDownlink)
241 for (NETSIM_ID r = 0; r < NETWORK->nDeviceCount; r++)
242 {
243 if (info->ueId == r + 1)
244 continue;
245
246 for (NETSIM_ID rin = 0; rin < DEVICE(r + 1)->nNumOfInterface; rin++)
247 {
248 if (!isLTE_NRInterface(r + 1, rin + 1))
249 continue;
250
251 if (data->isDCEnable) {
252 if (data->SecCellType == MMWAVE_CELL_TYPE) {
253 if (!fn_NetSim_isDeviceLTENR(r + 1, rin + 1))
254 continue;
255 }
256 else {
257 if (fn_NetSim_isDeviceLTENR(r + 1, rin + 1))
258 continue;
259 }
260 }
261 ptrLTENR_PROTODATA pd = LTENR_PROTODATA_GET(r + 1, rin + 1);
262 ptrLTENR_GNBPHY phy = NULL;
263 if (pd->deviceType == LTENR_DEVICETYPE_GNB) {
264 phy = LTENR_GNBPHY_GET(r + 1, rin + 1);
265 ptrLTENR_ASSOCIATEDUEPHYINFO assocInfo = LTENR_ASSOCIATEDUEPHYINFO_GET(phy->gnbId, phy->gnbIf, info->ueId, info->ueIf);
266 ptrLTENR_PROPAGATIONINFO pinfo = assocInfo->propagationInfo[CA_ID];
267 if (pinfo && info->gnbId != r + 1 && pinfo->centralFrequency_MHz == info->centralFrequency_MHz) {
268 if (isDownlink) Interference_linear += DBM_TO_MW(pinfo->downlink.rxPower_dbm[layerId]);
269 else Interference_linear += DBM_TO_MW(pinfo->uplink.rxPower_dbm[layerId]);
270 }
271 }
272 }
273 }
274
275 *Interference = MW_TO_DBM(Interference_linear);
276 return;
277}
278
279
280void LTENR_Calculate_Interference(ptrLTENR_PROPAGATIONINFO info, double rxPower_dbm,
281 double* Interference,int layerId, int CA_ID, bool isDownlink) {
282 ptrLTENR_GNBPHY phy = LTENR_GNBPHY_GET(info->gnbId, info->gnbIf);
283
284 if (phy->propagationConfig->DownlinkInterferenceModel == LTENR_EXACT_GEOMETRIC_MODEL_INTERFERENCE)
285 LTENR_fn_ExactGeometricModelInterference(info, rxPower_dbm, Interference, layerId, CA_ID, isDownlink);
286
287 else if (phy->propagationConfig->DownlinkInterferenceModel == LTENR_GRADED_DISTANCE_BASED_WYNER_MODEL_INTERFERENCE)
288 LTENR_fn_Graded_Distance_Based_Wyner_Model_Interference(info, rxPower_dbm, Interference, CA_ID, isDownlink);//Cell Edge Interference
289 else
290 *Interference = NEGATIVE_DBM;
291
292}
293
294void free_interference() {
295 if (GradedDistanceBasedWynerInterferenceheadDownlink) {
296 while (GradedDistanceBasedWynerInterferenceheadDownlink!=NULL) {
297 ptrGRADED_DISTANCE_BASED_WYNER_INTERFERENCE temp = GradedDistanceBasedWynerInterferenceheadDownlink;
298 GradedDistanceBasedWynerInterferenceheadDownlink = GradedDistanceBasedWynerInterferenceheadDownlink->next;
299 free(temp->Relative_Intereference_dB);
300 temp->Relative_Intereference_dB = NULL;
301 free(temp);
302 temp = NULL;
303 }
304 free(wynerFile);
305 }
306 return;
307}