NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
LTENR_BeamForming.c
1/************************************************************************************
2* Copyright (C) 2021 *
3* TETCOS, Bangalore. India *
4* *
5* Tetcos owns the intellectual property rights in the Product and its content. *
6* The copying, redistribution, reselling or publication of any or all of the *
7* Product or its content without express prior written consent of Tetcos is *
8* prohibited. Ownership and / or any other right relating to the software and all *
9* intellectual property rights therein shall remain at all times with Tetcos. *
10* *
11* This source code is licensed per the NetSim license agreement. *
12* *
13* No portion of this source code may be used as the basis for a derivative work, *
14* or used, for any purpose other than its intended use per the NetSim license *
15* agreement. *
16* *
17* This source code and the algorithms contained within it are confidential trade *
18* secrets of TETCOS and may not be used as the basis for any other software, *
19* hardware, product or service. *
20* *
21* Author: Shashi Kant Suman *
22* *
23* ----------------------------------------------------------------------------------*/
24#pragma region HEADER_FILES
25#include "stdafx.h"
26#include "LTENR_PHY.h"
27#include "LTENR_AntennaModel.h"
28#pragma endregion
29
30#define MAX_FASTFADING_ENTRY 50000
32{
33 char fileName[BUFSIZ];
34 FILE* file;
35
36 UINT txAntennaCount;
37 UINT rxAntennaCount;
38
39 UINT entryCount;
40 double** entries;
41}LTENR_FASTFADINGHANDLE, * ptrLTENR_FASTFADINGHANDLE;
42static ptrLTENR_FASTFADINGHANDLE* fastFadingHandles = NULL;
43static UINT fastFadingHandleCount = 0;
44
45static ptrLTENR_FASTFADINGHANDLE fastfading_find_handle(UINT txAntennaCount, UINT rxAntennaCount)
46{
47 if (fastFadingHandleCount == 0) return NULL;
48 for (UINT i = 0; i < fastFadingHandleCount; i++)
49 {
50 if ((fastFadingHandles[i]->txAntennaCount == 0 || fastFadingHandles[i]->txAntennaCount == txAntennaCount) &&
51 (fastFadingHandles[i]->rxAntennaCount == 0 || fastFadingHandles[i]->rxAntennaCount == rxAntennaCount))
52 return fastFadingHandles[i];
53 }
54 return NULL;
55}
56
57static bool read_fastfading_file(ptrLTENR_FASTFADINGHANDLE handle, UINT layerCount)
58{
59 handle->file = fopen(handle->fileName, "r");
60 if (!handle->file)
61 {
62 fnSystemError("Unable to open %s file.", handle->fileName);
63 perror(handle->fileName);
64 return false;
65 }
66
67 char s[5000];
68 while (fgets(s, 5000, handle->file))
69 {
70 char* t;
71 for (UINT i = 0; i < layerCount; i++)
72 {
73 if (handle->entries[i])
74 handle->entries[i] = realloc(handle->entries[i], ((size_t)handle->entryCount + 1) * (sizeof * handle->entries[i]));
75 else
76 handle->entries[i] = calloc(1, sizeof * handle->entries[i]);
77
78 if (i == 0) t = strtok(s, ",");
79 else t = strtok(NULL,",");
80 handle->entries[i][handle->entryCount] = atof(t);
81 }
82 handle->entryCount++;
83 }
84 fclose(handle->file);
85 return true;
86}
87
88static bool fastfading_open_handle(ptrLTENR_FASTFADINGHANDLE handle)
89{
90 sprintf(handle->fileName, "%s/5GBeamforming/%d_%d.beamforming",
91 pszAppPath,
92 handle->txAntennaCount,
93 handle->rxAntennaCount);
94
95 UINT layerCount = min(handle->txAntennaCount, handle->rxAntennaCount);
96 handle->entries = calloc(layerCount, sizeof * handle->entries);
97
98 if (read_fastfading_file(handle, layerCount))
99 {
100 return true;
101 }
102 else
103 {
104 free(handle->entries);
105 return false;
106 }
107}
108
109static ptrLTENR_FASTFADINGHANDLE fastfading_init_handle(UINT txAntennaCount, UINT rxAntennaCount)
110{
111 ptrLTENR_FASTFADINGHANDLE handle = fastfading_find_handle(txAntennaCount, rxAntennaCount);
112 if (handle) return handle;
113
114 handle = calloc(1, sizeof * handle);
115 handle->rxAntennaCount = rxAntennaCount;
116 handle->txAntennaCount = txAntennaCount;
117
118 if (fastfading_open_handle(handle))
119 {
120 if (fastFadingHandleCount == 0)
121 fastFadingHandles = calloc(1, sizeof * fastFadingHandles);
122 else
123 fastFadingHandles = realloc(fastFadingHandles, ((size_t)fastFadingHandleCount + 1) * (sizeof * fastFadingHandles));
124 fastFadingHandles[fastFadingHandleCount] = handle;
125 fastFadingHandleCount++;
126 return handle;
127 }
128 else
129 {
130 free(handle);
131 return NULL;
132 }
133}
134
135double LTENR_BeamForming_GetValue(ptrLTENR_FASTFADINGHANDLE handle, UINT layerId, UINT* index)
136{
137 double val = handle->entries[layerId][*index];
138 (*index)++;
139 if (*index == handle->entryCount) *index = 0;
140 assert(val > 0.0);
141 return 10*log10(val);
142}
143
144static void LTENR_BeamFormingGain_Update(ptrLTENR_ASSOCIATEDUEPHYINFO info)
145{
146 for (NETSIM_ID c = 0; c < MAX_CA_COUNT; c++)
147 {
148 ptrLTENR_PROPAGATIONINFO propagation = info->propagationInfo[c];
149 if (propagation && (propagation->propagationConfig->fastFadingModel == LTENR_FASTFADING_MODEL_AWGN_WITH_RAYLEIGH_FADING))
150 {
151 for (UINT l = 0; l < propagation->downlink.layerCount; l++)
152 {
153 propagation->downlink.beamFormingGain[l] = LTENR_BeamForming_GetValue(propagation->downlink.beamformingHandle,
154 l,
155 &propagation->downlink.beamFormingIndex[l]);
156 }
157
158 for (UINT l = 0; l < propagation->uplink.layerCount; l++)
159 {
160 propagation->uplink.beamFormingGain[l] = LTENR_BeamForming_GetValue(propagation->uplink.beamformingHandle,
161 l,
162 &propagation->uplink.beamFormingIndex[l]);
163 }
164 }
165 else break;
166 }
167}
168
169void LTENR_BeamForming_HandleCoherenceTimer()
170{
171 NETSIM_ID d = pstruEventDetails->nDeviceId;
172 NETSIM_ID in = pstruEventDetails->nInterfaceId;
173 ptrLTENR_GNBPHY phy = LTENR_GNBPHY_GET(d, in);
174
175 pstruEventDetails->dEventTime += phy->propagationConfig->coherenceTime;
176 fnpAddEvent(pstruEventDetails);
177 pstruEventDetails->dEventTime -= phy->propagationConfig->coherenceTime;
178
179 ptrLTENR_ASSOCIATEDUEPHYINFO info = phy->associatedUEPhyInfo;
180 while (info)
181 {
182 LTENR_BeamFormingGain_Update(info);
183 LTENR_ASSOCIATEDUEPHYINFO_NEXT(info);
184 }
185}
186
187void LTENR_BeamForming_InitCoherenceTimer(ptrLTENR_GNBPHY phy)
188{
189 static bool isCalled = false;
190 if (!isCalled)
191 {
192 LTENR_SUBEVENT_REGISTER(LTENR_SUBEVENT_FASTFADING_COHERENCE_TIMER, "FastFading_Coherence_Timer", LTENR_BeamForming_HandleCoherenceTimer);
193 isCalled = true;
194 }
195
196 ptrLTENR_PROPAGATIONCONFIG propagation = phy->propagationConfig;
197 if (propagation->fastFadingModel != LTENR_FASTFADING_MODEL_AWGN_WITH_RAYLEIGH_FADING && propagation->fastFadingModel != LTENR_FASTFADING_MODEL_NO_FADING_MIMO_ARRAY_GAIN) return;
198
199 NetSim_EVENTDETAILS pevent;
200 memset(&pevent, 0, sizeof pevent);
201 pevent.nDeviceId = phy->gnbId;
202 pevent.nDeviceType = DEVICE_TYPE(phy->gnbId);
203 pevent.nEventType = TIMER_EVENT;
204 pevent.nInterfaceId = phy->gnbIf;
205 ptrLTENR_PROTODATA pd = LTENR_PROTODATA_GET(phy->gnbId,phy->gnbIf);
206 LTENR_SET_SUBEVENT(pd, &pevent, LTENR_SUBEVENT_FASTFADING_COHERENCE_TIMER);
207 fnpAddEvent(&pevent);
208}
209
210static ptrLTENR_FASTFADINGHANDLE beamforming_init(UINT tx,UINT rx,UINT** ppindex, double** ppgain)
211{
212 ptrLTENR_FASTFADINGHANDLE handle = fastfading_init_handle(tx, rx);
213 UINT layer = min(tx, rx);
214 UINT* index = calloc(layer, sizeof * index);
215 double* gain = calloc(layer, sizeof * gain);
216 for (UINT i = 0; i < layer; i++)
217 {
218 index[i] = NETSIM_RAND_RN(handle->entryCount, 0);
219 gain[i] = LTENR_BeamForming_GetValue(handle, i, &index[i]);
220 }
221
222 *ppindex = index;
223 *ppgain = gain;
224 return handle;
225}
226
227void LTENR_BeamForming_Init(ptrLTENR_PROPAGATIONINFO propagation)
228{
229 propagation->downlink.beamformingHandle = beamforming_init(propagation->downlink.txAntennaCount,
230 propagation->downlink.rxAntennaCount,
231 &propagation->downlink.beamFormingIndex,
232 &propagation->downlink.beamFormingGain);
233
234 propagation->uplink.beamformingHandle = beamforming_init(propagation->uplink.txAntennaCount,
235 propagation->uplink.rxAntennaCount,
236 &propagation->uplink.beamFormingIndex,
237 &propagation->uplink.beamFormingGain);
238}
double coherenceTime
For AWGN-RAYLEIGH/Rician Fading.