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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Mohamed Amine Ismail <amine.ismail@sophia.inria.fr>
18 * <amine.ismail@udcast.com>
19 */
20
21#include "ns3/snr-to-block-error-rate-manager.h"
22
23#include "default-traces.h"
24
25#include "ns3/assert.h"
26#include "ns3/log.h"
27#include "ns3/snr-to-block-error-rate-record.h"
28
29#include <cstring>
30#include <fstream>
31#include <sstream>
32
33namespace ns3
34{
35
36NS_LOG_COMPONENT_DEFINE("SNRToBlockErrorRateManager");
37
39{
40 for (int i = 0; i < 7; i++)
41 {
42 m_recordModulation[i] = new std::vector<SNRToBlockErrorRateRecord*>();
43 }
44 m_activateLoss = false;
45 m_traceFilePath = "DefaultTraces";
46}
47
49{
51 for (int i = 0; i < 7; i++)
52 {
53 delete m_recordModulation[i];
54 }
55}
56
57void
59{
60 for (int i = 0; i < 7; i++)
61 {
62 for (std::vector<SNRToBlockErrorRateRecord*>::iterator iter =
63 m_recordModulation[i]->begin();
64 iter != m_recordModulation[i]->end();
65 ++iter)
66 {
67 if (*iter)
68 {
69 delete (*iter);
70 (*iter) = 0;
71 }
72 }
73 m_recordModulation[i]->clear();
74 }
75}
76
77void
79{
80 m_activateLoss = loss;
81}
82
83void
85{
86 std::ifstream traceFile;
88 double snrValue;
89 double bitErrorRate;
90 double burstErrorRate;
91 double sigma2;
92 double I1;
93 double I2;
94
95 for (int i = 0; i < 7; i++)
96 {
97 std::stringstream traceFilePath;
98 traceFilePath << m_traceFilePath << "/modulation" << i << ".txt";
99
100 traceFile.open(traceFilePath.str(), std::ifstream::in);
101 if (!traceFile.good())
102 {
103 NS_LOG_INFO("Unable to load " << traceFilePath.str() << "!! Loading default traces...");
105 return;
106 }
107 while (traceFile.good())
108 {
109 traceFile >> snrValue >> bitErrorRate >> burstErrorRate >> sigma2 >> I1 >> I2;
111 bitErrorRate,
112 burstErrorRate,
113 sigma2,
114 I1,
115 I2);
116 m_recordModulation[i]->push_back(record);
117 }
118 traceFile.close();
119 }
120 m_activateLoss = true;
121}
122
123void
125{
126 double snrValue;
127 double bitErrorRate;
128 double burstErrorRate;
129 double sigma2;
130 double I1;
131 double I2;
132 ClearRecords();
133 for (unsigned int j = 0; j < sizeof(modulation0[0]) / sizeof(double); j++)
134 {
135 snrValue = modulation0[0][j];
136 bitErrorRate = modulation0[1][j];
137 burstErrorRate = modulation0[2][j];
138 sigma2 = modulation0[3][j];
139 I1 = modulation0[4][j];
140 I2 = modulation0[5][j];
142 new SNRToBlockErrorRateRecord(snrValue, bitErrorRate, burstErrorRate, sigma2, I1, I2);
143 m_recordModulation[0]->push_back(record);
144 }
145 for (unsigned int j = 0; j < sizeof(modulation1[0]) / sizeof(double); j++)
146 {
147 snrValue = modulation1[0][j];
148 bitErrorRate = modulation1[1][j];
149 burstErrorRate = modulation1[2][j];
150 sigma2 = modulation1[3][j];
151 I1 = modulation1[4][j];
152 I2 = modulation1[5][j];
154 new SNRToBlockErrorRateRecord(snrValue, bitErrorRate, burstErrorRate, sigma2, I1, I2);
155 m_recordModulation[1]->push_back(record);
156 }
157 for (unsigned int j = 0; j < sizeof(modulation2[0]) / sizeof(double); j++)
158 {
159 snrValue = modulation2[0][j];
160 bitErrorRate = modulation2[1][j];
161 burstErrorRate = modulation2[2][j];
162 sigma2 = modulation2[3][j];
163 I1 = modulation2[4][j];
164 I2 = modulation2[5][j];
166 new SNRToBlockErrorRateRecord(snrValue, bitErrorRate, burstErrorRate, sigma2, I1, I2);
167 m_recordModulation[2]->push_back(record);
168 }
169 for (unsigned int j = 0; j < sizeof(modulation3[0]) / sizeof(double); j++)
170 {
171 snrValue = modulation3[0][j];
172 bitErrorRate = modulation3[1][j];
173 burstErrorRate = modulation3[2][j];
174 sigma2 = modulation3[3][j];
175 I1 = modulation3[4][j];
176 I2 = modulation3[5][j];
178 new SNRToBlockErrorRateRecord(snrValue, bitErrorRate, burstErrorRate, sigma2, I1, I2);
179 m_recordModulation[3]->push_back(record);
180 }
181 for (unsigned int j = 0; j < sizeof(modulation4[0]) / sizeof(double); j++)
182 {
183 snrValue = modulation4[0][j];
184 bitErrorRate = modulation4[1][j];
185 burstErrorRate = modulation4[2][j];
186 sigma2 = modulation4[3][j];
187 I1 = modulation4[4][j];
188 I2 = modulation4[5][j];
190 new SNRToBlockErrorRateRecord(snrValue, bitErrorRate, burstErrorRate, sigma2, I1, I2);
191 m_recordModulation[4]->push_back(record);
192 }
193 for (unsigned int j = 0; j < sizeof(modulation5[0]) / sizeof(double); j++)
194 {
195 snrValue = modulation5[0][j];
196 bitErrorRate = modulation5[1][j];
197 burstErrorRate = modulation5[2][j];
198 sigma2 = modulation5[3][j];
199 I1 = modulation5[4][j];
200 I2 = modulation5[5][j];
202 new SNRToBlockErrorRateRecord(snrValue, bitErrorRate, burstErrorRate, sigma2, I1, I2);
203 m_recordModulation[5]->push_back(record);
204 }
205 for (unsigned int j = 0; j < sizeof(modulation6[0]) / sizeof(double); j++)
206 {
207 snrValue = modulation6[0][j];
208 bitErrorRate = modulation6[1][j];
209 burstErrorRate = modulation6[2][j];
210 sigma2 = modulation6[3][j];
211 I1 = modulation6[4][j];
212 I2 = modulation6[5][j];
214 new SNRToBlockErrorRateRecord(snrValue, bitErrorRate, burstErrorRate, sigma2, I1, I2);
215 m_recordModulation[6]->push_back(record);
216 }
217 m_activateLoss = true;
218}
219
220void
222{
223 double snrValue;
224 double bitErrorRate;
225 double burstErrorRate;
226 double sigma2;
227 double I1;
228 double I2;
229
230 ClearRecords();
231
232 std::ifstream traceFile;
233
234 for (int i = 0; i < 7; i++)
235 {
236 std::stringstream traceFilePath;
237 traceFilePath << m_traceFilePath << "/Modulation" << i << ".txt";
238
239 traceFile.open(traceFilePath.str(), std::ifstream::in);
240 if (!traceFile.good())
241 {
242 NS_LOG_INFO("Unable to load " << traceFilePath.str() << "!!Loading default traces...");
244 return;
245 }
246 while (traceFile.good())
247 {
248 traceFile >> snrValue >> bitErrorRate >> burstErrorRate >> sigma2 >> I1 >> I2;
250 bitErrorRate,
251 burstErrorRate,
252 sigma2,
253 I1,
254 I2);
255
256 m_recordModulation[i]->push_back(record);
257 }
258 traceFile.close();
259 }
260 m_activateLoss = true;
261}
262
263void
265{
266 m_traceFilePath = traceFilePath;
267}
268
269std::string
271{
272 return m_traceFilePath;
273}
274
275double
277{
278 if (!m_activateLoss)
279 {
280 return 0;
281 }
282
283 std::vector<SNRToBlockErrorRateRecord*>* record = nullptr;
284
285 record = m_recordModulation[modulation];
286
287 if (SNR <= (record->at(0)->GetSNRValue()))
288 {
289 return 1;
290 }
291 if (SNR >= (record->at(record->size() - 1)->GetSNRValue()))
292 {
293 return 0;
294 }
295
296 unsigned int i;
297 for (i = 0; i < record->size(); i++)
298 {
299 if (SNR < record->at(i)->GetSNRValue())
300 {
301 break;
302 }
303 }
304 double intervalSize = (record->at(i)->GetSNRValue() - record->at(i - 1)->GetSNRValue());
305 double coeff1 = (SNR - record->at(i - 1)->GetSNRValue()) / intervalSize;
306 double coeff2 = -1 * (SNR - record->at(i)->GetSNRValue()) / intervalSize;
307 double BlockErrorRate = coeff2 * (record->at(i - 1)->GetBlockErrorRate()) +
308 coeff1 * (record->at(i)->GetBlockErrorRate());
309 return BlockErrorRate;
310}
311
314{
315 if (!m_activateLoss)
316 {
317 return new SNRToBlockErrorRateRecord(SNR, 0, 0, 0, 0, 0);
318 }
319
320 std::vector<SNRToBlockErrorRateRecord*>* record = nullptr;
321 record = m_recordModulation[modulation];
322
323 if (SNR <= (record->at(0)->GetSNRValue()))
324 {
325 return record->at(0)->Copy();
326 }
327 if (SNR >= (record->at(record->size() - 1)->GetSNRValue()))
328 {
329 return record->at(record->size() - 1)->Copy();
330 }
331
332 unsigned int i;
333 for (i = 0; i < record->size(); i++)
334 {
335 if (SNR < record->at(i)->GetSNRValue())
336 {
337 break;
338 }
339 }
340 double intervalSize = (record->at(i)->GetSNRValue() - record->at(i - 1)->GetSNRValue());
341 double coeff1 = (SNR - record->at(i - 1)->GetSNRValue()) / intervalSize;
342 double coeff2 = -1 * (SNR - record->at(i)->GetSNRValue()) / intervalSize;
343 double BER = coeff2 * (record->at(i - 1)->GetBitErrorRate()) +
344 coeff1 * (record->at(i)->GetBitErrorRate());
345 double BlcER = coeff2 * (record->at(i - 1)->GetBlockErrorRate()) +
346 coeff1 * (record->at(i)->GetBlockErrorRate());
347 double sigma2 =
348 coeff2 * (record->at(i - 1)->GetSigma2()) + coeff1 * (record->at(i)->GetSigma2());
349 double I1 = coeff2 * (record->at(i - 1)->GetI1()) + coeff1 * (record->at(i)->GetI1());
350 double I2 = coeff2 * (record->at(i - 1)->GetI2()) + coeff1 * (record->at(i)->GetI2());
351
352 SNRToBlockErrorRateRecord* SNRToBlockErrorRate =
353 new SNRToBlockErrorRateRecord(SNR, BER, BlcER, sigma2, I1, I2);
354 return SNRToBlockErrorRate;
355}
356
357} // 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:202
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
Every class exported by the ns3 library is enclosed in the ns3 namespace.