A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
snr-to-block-error-rate-manager.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007,2008, 2009 INRIA, UDcast
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mohamed Amine Ismail <amine.ismail@sophia.inria.fr>
7 * <amine.ismail@udcast.com>
8 */
9
11
12#include "default-traces.h"
14
15#include "ns3/assert.h"
16#include "ns3/log.h"
17
18#include <cstring>
19#include <fstream>
20#include <sstream>
21
22namespace ns3
23{
24
25NS_LOG_COMPONENT_DEFINE("SNRToBlockErrorRateManager");
26
28{
29 for (int i = 0; i < 7; i++)
30 {
31 m_recordModulation[i] = new std::vector<SNRToBlockErrorRateRecord*>();
32 }
33 m_activateLoss = false;
34 m_traceFilePath = "DefaultTraces";
35}
36
38{
40 for (int i = 0; i < 7; i++)
41 {
42 delete m_recordModulation[i];
43 }
44}
45
46void
48{
49 for (int i = 0; i < 7; i++)
50 {
51 for (auto iter = m_recordModulation[i]->begin(); iter != m_recordModulation[i]->end();
52 ++iter)
53 {
54 if (*iter)
55 {
56 delete (*iter);
57 (*iter) = 0;
58 }
59 }
60 m_recordModulation[i]->clear();
61 }
62}
63
64void
69
70void
72{
73 std::ifstream traceFile;
75 double snrValue;
76 double bitErrorRate;
77 double burstErrorRate;
78 double sigma2;
79 double I1;
80 double I2;
81
82 for (int i = 0; i < 7; i++)
83 {
84 std::stringstream traceFilePath;
85 traceFilePath << m_traceFilePath << "/modulation" << i << ".txt";
86
87 traceFile.open(traceFilePath.str(), std::ifstream::in);
88 if (!traceFile.good())
89 {
90 NS_LOG_INFO("Unable to load " << traceFilePath.str() << "!! Loading default traces...");
92 return;
93 }
94 while (traceFile.good())
95 {
96 traceFile >> snrValue >> bitErrorRate >> burstErrorRate >> sigma2 >> I1 >> I2;
97 auto record = new SNRToBlockErrorRateRecord(snrValue,
98 bitErrorRate,
99 burstErrorRate,
100 sigma2,
101 I1,
102 I2);
103 m_recordModulation[i]->push_back(record);
104 }
105 traceFile.close();
106 }
107 m_activateLoss = true;
108}
109
110void
112{
113 double snrValue;
114 double bitErrorRate;
115 double burstErrorRate;
116 double sigma2;
117 double I1;
118 double I2;
119 ClearRecords();
120 for (unsigned int j = 0; j < sizeof(modulation0[0]) / sizeof(double); j++)
121 {
122 snrValue = modulation0[0][j];
123 bitErrorRate = modulation0[1][j];
124 burstErrorRate = modulation0[2][j];
125 sigma2 = modulation0[3][j];
126 I1 = modulation0[4][j];
127 I2 = modulation0[5][j];
128 auto record =
129 new SNRToBlockErrorRateRecord(snrValue, bitErrorRate, burstErrorRate, sigma2, I1, I2);
130 m_recordModulation[0]->push_back(record);
131 }
132 for (unsigned int j = 0; j < sizeof(modulation1[0]) / sizeof(double); j++)
133 {
134 snrValue = modulation1[0][j];
135 bitErrorRate = modulation1[1][j];
136 burstErrorRate = modulation1[2][j];
137 sigma2 = modulation1[3][j];
138 I1 = modulation1[4][j];
139 I2 = modulation1[5][j];
140 auto record =
141 new SNRToBlockErrorRateRecord(snrValue, bitErrorRate, burstErrorRate, sigma2, I1, I2);
142 m_recordModulation[1]->push_back(record);
143 }
144 for (unsigned int j = 0; j < sizeof(modulation2[0]) / sizeof(double); j++)
145 {
146 snrValue = modulation2[0][j];
147 bitErrorRate = modulation2[1][j];
148 burstErrorRate = modulation2[2][j];
149 sigma2 = modulation2[3][j];
150 I1 = modulation2[4][j];
151 I2 = modulation2[5][j];
152 auto record =
153 new SNRToBlockErrorRateRecord(snrValue, bitErrorRate, burstErrorRate, sigma2, I1, I2);
154 m_recordModulation[2]->push_back(record);
155 }
156 for (unsigned int j = 0; j < sizeof(modulation3[0]) / sizeof(double); j++)
157 {
158 snrValue = modulation3[0][j];
159 bitErrorRate = modulation3[1][j];
160 burstErrorRate = modulation3[2][j];
161 sigma2 = modulation3[3][j];
162 I1 = modulation3[4][j];
163 I2 = modulation3[5][j];
164 auto record =
165 new SNRToBlockErrorRateRecord(snrValue, bitErrorRate, burstErrorRate, sigma2, I1, I2);
166 m_recordModulation[3]->push_back(record);
167 }
168 for (unsigned int j = 0; j < sizeof(modulation4[0]) / sizeof(double); j++)
169 {
170 snrValue = modulation4[0][j];
171 bitErrorRate = modulation4[1][j];
172 burstErrorRate = modulation4[2][j];
173 sigma2 = modulation4[3][j];
174 I1 = modulation4[4][j];
175 I2 = modulation4[5][j];
176 auto record =
177 new SNRToBlockErrorRateRecord(snrValue, bitErrorRate, burstErrorRate, sigma2, I1, I2);
178 m_recordModulation[4]->push_back(record);
179 }
180 for (unsigned int j = 0; j < sizeof(modulation5[0]) / sizeof(double); j++)
181 {
182 snrValue = modulation5[0][j];
183 bitErrorRate = modulation5[1][j];
184 burstErrorRate = modulation5[2][j];
185 sigma2 = modulation5[3][j];
186 I1 = modulation5[4][j];
187 I2 = modulation5[5][j];
188 auto record =
189 new SNRToBlockErrorRateRecord(snrValue, bitErrorRate, burstErrorRate, sigma2, I1, I2);
190 m_recordModulation[5]->push_back(record);
191 }
192 for (unsigned int j = 0; j < sizeof(modulation6[0]) / sizeof(double); j++)
193 {
194 snrValue = modulation6[0][j];
195 bitErrorRate = modulation6[1][j];
196 burstErrorRate = modulation6[2][j];
197 sigma2 = modulation6[3][j];
198 I1 = modulation6[4][j];
199 I2 = modulation6[5][j];
200 auto record =
201 new SNRToBlockErrorRateRecord(snrValue, bitErrorRate, burstErrorRate, sigma2, I1, I2);
202 m_recordModulation[6]->push_back(record);
203 }
204 m_activateLoss = true;
205}
206
207void
209{
210 double snrValue;
211 double bitErrorRate;
212 double burstErrorRate;
213 double sigma2;
214 double I1;
215 double I2;
216
217 ClearRecords();
218
219 std::ifstream traceFile;
220
221 for (int i = 0; i < 7; i++)
222 {
223 std::stringstream traceFilePath;
224 traceFilePath << m_traceFilePath << "/Modulation" << i << ".txt";
225
226 traceFile.open(traceFilePath.str(), std::ifstream::in);
227 if (!traceFile.good())
228 {
229 NS_LOG_INFO("Unable to load " << traceFilePath.str() << "!!Loading default traces...");
231 return;
232 }
233 while (traceFile.good())
234 {
235 traceFile >> snrValue >> bitErrorRate >> burstErrorRate >> sigma2 >> I1 >> I2;
236 auto record = new SNRToBlockErrorRateRecord(snrValue,
237 bitErrorRate,
238 burstErrorRate,
239 sigma2,
240 I1,
241 I2);
242
243 m_recordModulation[i]->push_back(record);
244 }
245 traceFile.close();
246 }
247 m_activateLoss = true;
248}
249
250void
252{
253 m_traceFilePath = traceFilePath;
254}
255
256std::string
261
262double
264{
265 if (!m_activateLoss)
266 {
267 return 0;
268 }
269
270 std::vector<SNRToBlockErrorRateRecord*>* record = nullptr;
271
272 record = m_recordModulation[modulation];
273
274 if (SNR <= (record->at(0)->GetSNRValue()))
275 {
276 return 1;
277 }
278 if (SNR >= (record->at(record->size() - 1)->GetSNRValue()))
279 {
280 return 0;
281 }
282
283 unsigned int i;
284 for (i = 0; i < record->size(); i++)
285 {
286 if (SNR < record->at(i)->GetSNRValue())
287 {
288 break;
289 }
290 }
291 double intervalSize = (record->at(i)->GetSNRValue() - record->at(i - 1)->GetSNRValue());
292 double coeff1 = (SNR - record->at(i - 1)->GetSNRValue()) / intervalSize;
293 double coeff2 = -1 * (SNR - record->at(i)->GetSNRValue()) / intervalSize;
294 double BlockErrorRate = coeff2 * (record->at(i - 1)->GetBlockErrorRate()) +
295 coeff1 * (record->at(i)->GetBlockErrorRate());
296 return BlockErrorRate;
297}
298
301{
302 if (!m_activateLoss)
303 {
304 return new SNRToBlockErrorRateRecord(SNR, 0, 0, 0, 0, 0);
305 }
306
307 std::vector<SNRToBlockErrorRateRecord*>* record = nullptr;
308 record = m_recordModulation[modulation];
309
310 if (SNR <= (record->at(0)->GetSNRValue()))
311 {
312 return record->at(0)->Copy();
313 }
314 if (SNR >= (record->at(record->size() - 1)->GetSNRValue()))
315 {
316 return record->at(record->size() - 1)->Copy();
317 }
318
319 unsigned int i;
320 for (i = 0; i < record->size(); i++)
321 {
322 if (SNR < record->at(i)->GetSNRValue())
323 {
324 break;
325 }
326 }
327 double intervalSize = (record->at(i)->GetSNRValue() - record->at(i - 1)->GetSNRValue());
328 double coeff1 = (SNR - record->at(i - 1)->GetSNRValue()) / intervalSize;
329 double coeff2 = -1 * (SNR - record->at(i)->GetSNRValue()) / intervalSize;
330 double BER = coeff2 * (record->at(i - 1)->GetBitErrorRate()) +
331 coeff1 * (record->at(i)->GetBitErrorRate());
332 double BlcER = coeff2 * (record->at(i - 1)->GetBlockErrorRate()) +
333 coeff1 * (record->at(i)->GetBlockErrorRate());
334 double sigma2 =
335 coeff2 * (record->at(i - 1)->GetSigma2()) + coeff1 * (record->at(i)->GetSigma2());
336 double I1 = coeff2 * (record->at(i - 1)->GetI1()) + coeff1 * (record->at(i)->GetI1());
337 double I2 = coeff2 * (record->at(i - 1)->GetI2()) + coeff1 * (record->at(i)->GetI2());
338
339 auto SNRToBlockErrorRate = new SNRToBlockErrorRateRecord(SNR, BER, BlcER, sigma2, I1, I2);
340 return SNRToBlockErrorRate;
341}
342
343} // namespace ns3
void ActivateLoss(bool loss)
If activate loss is called with false, all the returned BlcER will be 0 (no losses)
static const double modulation5[6][547]
These represent default traces, providing a number of parameters for each SNR value.
static const double modulation1[6][42]
These represent default traces, providing a number of parameters for each SNR value.
void LoadTraces()
Loads the traces form the repository specified in the constructor or set by SetTraceFilePath function...
SNRToBlockErrorRateRecord * GetSNRToBlockErrorRateRecord(double SNR, uint8_t modulation)
returns a record of type SNRToBlockErrorRateRecord corresponding to a given modulation and SNR value
static const double modulation0[6][29]
These represent default traces, providing a number of parameters for each SNR value.
static const double modulation6[6][626]
These represent default traces, providing a number of parameters for each SNR value.
static const double modulation3[6][117]
These represent default traces, providing a number of parameters for each SNR value.
double GetBlockErrorRate(double SNR, uint8_t modulation)
returns the Block Error Rate for a given modulation and SNR value
void SetTraceFilePath(char *traceFilePath)
Set the path of the repository containing the traces.
static const double modulation2[6][96]
These represent default traces, providing a number of parameters for each SNR value.
std::vector< SNRToBlockErrorRateRecord * > * m_recordModulation[7]
record modulation
void LoadDefaultTraces()
Loads the default traces from default-traces.h file.
static const double modulation4[6][331]
These represent default traces, providing a number of parameters for each SNR value.
This class represents a record (handled by SnrToBlockErrorRate manager) that keeps a mapping between ...
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Every class exported by the ns3 library is enclosed in the ns3 namespace.