A Discrete-Event Network Simulator
API
snr-to-block-error-rate-manager.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007,2008, 2009 INRIA, UDcast
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mohamed Amine Ismail <amine.ismail@sophia.inria.fr>
19  * <amine.ismail@udcast.com>
20  */
21 
22 #include <cstring>
23 #include "ns3/snr-to-block-error-rate-manager.h"
24 #include "ns3/snr-to-block-error-rate-record.h"
25 #include "default-traces.h"
26 #include "ns3/log.h"
27 #include "ns3/assert.h"
28 #include <fstream>
29 #include <sstream>
30 
31 namespace ns3 {
32 
33 NS_LOG_COMPONENT_DEFINE ("SNRToBlockErrorRateManager");
34 
36 {
37 
38  for (int i = 0; i < 7; i++)
39  {
40  m_recordModulation[i] = new std::vector<SNRToBlockErrorRateRecord*> ();
41  }
42  m_activateLoss = false;
43  m_traceFilePath = "DefaultTraces";
44 }
45 
47 {
48  ClearRecords ();
49  for (int i = 0; i < 7; i++)
50  {
51  delete m_recordModulation[i];
52  }
53 }
54 
55 void
57 {
58  for (int i = 0; i < 7; i++)
59  {
60 
61  for (std::vector<SNRToBlockErrorRateRecord*>::iterator iter = m_recordModulation[i]->begin (); iter
62  != m_recordModulation[i]->end (); ++iter)
63  {
64  if (*iter)
65  {
66  delete (*iter);
67  (*iter) = 0;
68  }
69  }
70  m_recordModulation[i]->clear ();
71  }
72 }
73 
74 void
76 {
77  m_activateLoss = loss;
78 }
79 
80 void
82 {
83  std::ifstream traceFile;
84  ClearRecords ();
85  double snrValue, bitErrorRate, burstErrorRate, sigma2, I1, I2;
86 
87  for (int i = 0; i < 7; i++)
88  {
89  std::stringstream traceFilePath;
90  traceFilePath << m_traceFilePath << "/modulation" << i << ".txt";
91 
92  traceFile.open (traceFilePath.str ().c_str (), std::ifstream::in);
93  if (traceFile.good () == false)
94  {
95  NS_LOG_INFO ("Unable to load " << traceFilePath.str () << "!! Loading default traces...");
97  return;
98  }
99  while (traceFile.good ())
100  {
101  traceFile >> snrValue >> bitErrorRate >> burstErrorRate >> sigma2 >> I1 >> I2;
103  bitErrorRate,
104  burstErrorRate,
105  sigma2,
106  I1,
107  I2);
108  m_recordModulation[i]->push_back (record);
109 
110  }
111  traceFile.close ();
112  }
113  m_activateLoss = true;
114 }
115 
116 void
118 {
119  double snrValue, bitErrorRate, burstErrorRate, sigma2, I1, I2;
120  ClearRecords ();
121  for (unsigned int j = 0; j < sizeof(modulation0[0]) / sizeof(double); j++)
122  {
123  snrValue = modulation0[0][j];
124  bitErrorRate = modulation0[1][j];
125  burstErrorRate = modulation0[2][j];
126  sigma2 = modulation0[3][j];
127  I1 = modulation0[4][j];
128  I2 = modulation0[5][j];
130  bitErrorRate,
131  burstErrorRate,
132  sigma2,
133  I1,
134  I2);
135  m_recordModulation[0]->push_back (record);
136  }
137  for (unsigned int j = 0; j < sizeof(modulation1[0]) / sizeof(double); j++)
138  {
139  snrValue = modulation1[0][j];
140  bitErrorRate = modulation1[1][j];
141  burstErrorRate = modulation1[2][j];
142  sigma2 = modulation1[3][j];
143  I1 = modulation1[4][j];
144  I2 = modulation1[5][j];
146  bitErrorRate,
147  burstErrorRate,
148  sigma2,
149  I1,
150  I2);
151  m_recordModulation[1]->push_back (record);
152  }
153  for (unsigned int j = 0; j < sizeof(modulation2[0]) / sizeof(double); j++)
154  {
155  snrValue = modulation2[0][j];
156  bitErrorRate = modulation2[1][j];
157  burstErrorRate = modulation2[2][j];
158  sigma2 = modulation2[3][j];
159  I1 = modulation2[4][j];
160  I2 = modulation2[5][j];
162  bitErrorRate,
163  burstErrorRate,
164  sigma2,
165  I1,
166  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  bitErrorRate,
179  burstErrorRate,
180  sigma2,
181  I1,
182  I2);
183  m_recordModulation[3]->push_back (record);
184  }
185  for (unsigned int j = 0; j < sizeof(modulation4[0]) / sizeof(double); j++)
186  {
187  snrValue = modulation4[0][j];
188  bitErrorRate = modulation4[1][j];
189  burstErrorRate = modulation4[2][j];
190  sigma2 = modulation4[3][j];
191  I1 = modulation4[4][j];
192  I2 = modulation4[5][j];
194  bitErrorRate,
195  burstErrorRate,
196  sigma2,
197  I1,
198  I2);
199  m_recordModulation[4]->push_back (record);
200  }
201  for (unsigned int j = 0; j < sizeof(modulation5[0]) / sizeof(double); j++)
202  {
203  snrValue = modulation5[0][j];
204  bitErrorRate = modulation5[1][j];
205  burstErrorRate = modulation5[2][j];
206  sigma2 = modulation5[3][j];
207  I1 = modulation5[4][j];
208  I2 = modulation5[5][j];
210  bitErrorRate,
211  burstErrorRate,
212  sigma2,
213  I1,
214  I2);
215  m_recordModulation[5]->push_back (record);
216  }
217  for (unsigned int j = 0; j < sizeof(modulation6[0]) / sizeof(double); j++)
218  {
219  snrValue = modulation6[0][j];
220  bitErrorRate = modulation6[1][j];
221  burstErrorRate = modulation6[2][j];
222  sigma2 = modulation6[3][j];
223  I1 = modulation6[4][j];
224  I2 = modulation6[5][j];
226  bitErrorRate,
227  burstErrorRate,
228  sigma2,
229  I1,
230  I2);
231  m_recordModulation[6]->push_back (record);
232  }
233  m_activateLoss = true;
234 }
235 
236 void
238 {
239  double snrValue, bitErrorRate, burstErrorRate, sigma2, I1, I2;
240 
241  ClearRecords ();
242 
243  std::ifstream traceFile;
244 
245  for (int i = 0; i < 7; i++)
246  {
247  std::stringstream traceFilePath;
248  traceFilePath << m_traceFilePath << "/Modulation" << i << ".txt";
249 
250  traceFile.open (traceFilePath.str ().c_str (), std::ifstream::in);
251  if (traceFile.good () == false)
252  {
253  NS_LOG_INFO ("Unable to load " << traceFilePath.str () << "!!Loading default traces...");
255  return;
256  }
257  while (traceFile.good ())
258  {
259  traceFile >> snrValue >> bitErrorRate >> burstErrorRate >> sigma2 >> I1 >> I2;
261  bitErrorRate,
262  burstErrorRate,
263  sigma2,
264  I1,
265  I2);
266 
267  m_recordModulation[i]->push_back (record);
268 
269  }
270  traceFile.close ();
271  }
272  m_activateLoss = true;
273 }
274 
275 void
277 {
278  m_traceFilePath = traceFilePath;
279 }
280 
281 std::string
283 {
284  return m_traceFilePath;
285 }
286 
287 double
288 SNRToBlockErrorRateManager::GetBlockErrorRate (double SNR, uint8_t modulation)
289 {
290 
291  if (m_activateLoss == false)
292  {
293  return 0;
294  }
295 
296  std::vector<SNRToBlockErrorRateRecord *> *record = 0;
297 
298  record = m_recordModulation[modulation];
299 
300  if (SNR <= (record->at (0)->GetSNRValue ()))
301  {
302  return 1;
303  }
304  if (SNR >= (record->at (record->size () - 1)->GetSNRValue ()))
305  {
306  return 0;
307  }
308 
309  unsigned int i;
310  for (i = 0; i < record->size (); i++)
311  {
312  if (SNR < record->at (i)->GetSNRValue ())
313  {
314  break;
315  }
316  }
317  double intervalSize = (record->at (i)->GetSNRValue () - record->at (i - 1)->GetSNRValue ());
318  double coeff1 = (SNR - record->at (i - 1)->GetSNRValue ()) / intervalSize;
319  double coeff2 = -1 * (SNR - record->at (i)->GetSNRValue ()) / intervalSize;
320  double BlockErrorRate = coeff2 * (record->at (i - 1)->GetBlockErrorRate ()) + coeff1 * (record->at (i)->GetBlockErrorRate ());
321  return BlockErrorRate;
322 }
323 
326  uint8_t modulation)
327 {
328 
329  if (m_activateLoss == false)
330  {
331  return new SNRToBlockErrorRateRecord (SNR, 0, 0, 0, 0, 0);
332  }
333 
334  std::vector<SNRToBlockErrorRateRecord *> *record = 0;
335  record = m_recordModulation[modulation];
336 
337  if (SNR <= (record->at (0)->GetSNRValue ()))
338  {
339  return
340  record->at (0)->Copy ();
341  }
342  if (SNR >= (record->at (record->size () - 1)->GetSNRValue ()))
343  {
344  return
345  record->at (record->size () - 1)->Copy ();
346  }
347 
348  unsigned int i;
349  for (i = 0; i < record->size (); i++)
350  {
351  if (SNR < record->at (i)->GetSNRValue ())
352  {
353  break;
354  }
355  }
356  double intervalSize = (record->at (i)->GetSNRValue ()
357  - record->at (i - 1)->GetSNRValue ());
358  double coeff1 = (SNR - record->at (i - 1)->GetSNRValue ()) / intervalSize;
359  double coeff2 = -1 * (SNR - record->at (i)->GetSNRValue ()) / intervalSize;
360  double BER = coeff2 * (record->at (i - 1)->GetBitErrorRate ()) + coeff1 * (record->at (i)->GetBitErrorRate ());
361  double BlcER = coeff2 * (record->at (i - 1)->GetBlockErrorRate ()) + coeff1 * (record->at (i)->GetBlockErrorRate ());
362  double sigma2 = coeff2 * (record->at (i - 1)->GetSigma2 ()) + coeff1 * (record->at (i)->GetSigma2 ());
363  double I1 = coeff2 * (record->at (i - 1)->GetI1 ()) + coeff1 * (record->at (i)->GetI1 ());
364  double I2 = coeff2 * (record->at (i - 1)->GetI2 ()) + coeff1 * (record->at (i)->GetI2 ());
365 
366  SNRToBlockErrorRateRecord * SNRToBlockErrorRate = new SNRToBlockErrorRateRecord (SNR, BER, BlcER, sigma2, I1,I2);
367  return SNRToBlockErrorRate;
368 }
369 
370 }
371 
static const double modulation5[6][547]
These represent default traces, providing a number of parameters for each SNR value.
void LoadDefaultTraces(void)
Loads the default traces from default-traces.h file.
static const double modulation2[6][96]
These represent default traces, providing a number of parameters for each SNR value.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
This class represents a record (handled by SnrToBlockErrorRate manager) that keeps a mapping between ...
static const double modulation3[6][117]
These represent default traces, providing a number of parameters for each SNR value.
SNRToBlockErrorRateRecord * GetSNRToBlockErrorRateRecord(double SNR, uint8_t modulation)
returns a record of type SNRToBlockErrorRateRecord corresponding to a given modulation and SNR value ...
void ActivateLoss(bool loss)
If activate loss is called with false, all the returned BlcER will be 0 (no losses) ...
void SetTraceFilePath(char *traceFilePath)
Set the path of the repository containing the traces.
static const double modulation6[6][626]
These represent default traces, providing a number of parameters for each SNR value.
void LoadTraces(void)
Loads the traces form the repository specified in the constructor or set by SetTraceFilePath function...
double GetBlockErrorRate(double SNR, uint8_t modulation)
returns the Block Error Rate for a given modulation and SNR value
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static const double modulation1[6][42]
These represent default traces, providing a number of parameters for each SNR value.
std::vector< SNRToBlockErrorRateRecord * > * m_recordModulation[7]
record modulation
static const double modulation0[6][29]
These represent default traces, providing a number of parameters for each SNR value.
void ClearRecords(void)
Clear records function.
Ptr< T > Copy(Ptr< T > object)
Return a deep copy of a Ptr.
Definition: ptr.h:690
static const double modulation4[6][331]
These represent default traces, providing a number of parameters for each SNR value.