A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
30 NS_LOG_COMPONENT_DEFINE ("SNRToBlockErrorRateManager");
31 
32 namespace ns3 {
33 
35 {
36 
37  for (int i = 0; i < 7; i++)
38  {
39  m_recordModulation[i] = new std::vector<SNRToBlockErrorRateRecord*> ();
40  }
41  m_activateLoss = false;
42  std::strcpy (m_traceFilePath,"DefaultTraces");
43 }
44 
46 {
47  ClearRecords ();
48  for (int i = 0; i < 7; i++)
49  {
50  delete m_recordModulation[i];
51  }
52 }
53 
54 void
56 {
57  for (int i = 0; i < 7; i++)
58  {
59 
60  for (std::vector<SNRToBlockErrorRateRecord*>::iterator iter = m_recordModulation[i]->begin (); iter
61  != m_recordModulation[i]->end (); ++iter)
62  {
63  if (*iter)
64  {
65  delete (*iter);
66  (*iter) = 0;
67  }
68  }
69  m_recordModulation[i]->clear ();
70  }
71 }
72 
73 void
75 {
76  m_activateLoss = loss;
77 }
78 
79 void
81 {
82  std::ifstream m_ifTraceFile;
83  ClearRecords ();
84  double snrValue, bitErrorRate, burstErrorRate, sigma2, I1, I2;
85 
86  for (int i = 0; i < 7; i++)
87  {
88  char traceFile[1024];
89  sprintf (traceFile, "%s/modulation%d.txt", m_traceFilePath, i);
90 
91  m_ifTraceFile.open (traceFile, std::ifstream::in);
92  if (m_ifTraceFile.good () == false)
93  {
94  NS_LOG_INFO ("Unable to load " << traceFile << "!! Loading default traces...");
96  return;
97  }
98  while (m_ifTraceFile.good ())
99  {
100  m_ifTraceFile >> snrValue >> bitErrorRate >> burstErrorRate >> sigma2 >> I1 >> I2;
102  bitErrorRate,
103  burstErrorRate,
104  sigma2,
105  I1,
106  I2);
107  m_recordModulation[i]->push_back (record);
108 
109  }
110  m_ifTraceFile.close ();
111  }
112  m_activateLoss = true;
113 }
114 
115 void
117 {
118  double snrValue, bitErrorRate, burstErrorRate, sigma2, I1, 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];
129  bitErrorRate,
130  burstErrorRate,
131  sigma2,
132  I1,
133  I2);
134  m_recordModulation[0]->push_back (record);
135  }
136  for (unsigned int j = 0; j < sizeof(modulation1[0]) / sizeof(double); j++)
137  {
138  snrValue = modulation1[0][j];
139  bitErrorRate = modulation1[1][j];
140  burstErrorRate = modulation1[2][j];
141  sigma2 = modulation1[3][j];
142  I1 = modulation1[4][j];
143  I2 = modulation1[5][j];
145  bitErrorRate,
146  burstErrorRate,
147  sigma2,
148  I1,
149  I2);
150  m_recordModulation[1]->push_back (record);
151  }
152  for (unsigned int j = 0; j < sizeof(modulation2[0]) / sizeof(double); j++)
153  {
154  snrValue = modulation2[0][j];
155  bitErrorRate = modulation2[1][j];
156  burstErrorRate = modulation2[2][j];
157  sigma2 = modulation2[3][j];
158  I1 = modulation2[4][j];
159  I2 = modulation2[5][j];
161  bitErrorRate,
162  burstErrorRate,
163  sigma2,
164  I1,
165  I2);
166  m_recordModulation[2]->push_back (record);
167  }
168  for (unsigned int j = 0; j < sizeof(modulation3[0]) / sizeof(double); j++)
169  {
170  snrValue = modulation3[0][j];
171  bitErrorRate = modulation3[1][j];
172  burstErrorRate = modulation3[2][j];
173  sigma2 = modulation3[3][j];
174  I1 = modulation3[4][j];
175  I2 = modulation3[5][j];
177  bitErrorRate,
178  burstErrorRate,
179  sigma2,
180  I1,
181  I2);
182  m_recordModulation[3]->push_back (record);
183  }
184  for (unsigned int j = 0; j < sizeof(modulation4[0]) / sizeof(double); j++)
185  {
186  snrValue = modulation4[0][j];
187  bitErrorRate = modulation4[1][j];
188  burstErrorRate = modulation4[2][j];
189  sigma2 = modulation4[3][j];
190  I1 = modulation4[4][j];
191  I2 = modulation4[5][j];
193  bitErrorRate,
194  burstErrorRate,
195  sigma2,
196  I1,
197  I2);
198  m_recordModulation[4]->push_back (record);
199  }
200  for (unsigned int j = 0; j < sizeof(modulation5[0]) / sizeof(double); j++)
201  {
202  snrValue = modulation5[0][j];
203  bitErrorRate = modulation5[1][j];
204  burstErrorRate = modulation5[2][j];
205  sigma2 = modulation5[3][j];
206  I1 = modulation5[4][j];
207  I2 = modulation5[5][j];
209  bitErrorRate,
210  burstErrorRate,
211  sigma2,
212  I1,
213  I2);
214  m_recordModulation[5]->push_back (record);
215  }
216  for (unsigned int j = 0; j < sizeof(modulation6[0]) / sizeof(double); j++)
217  {
218  snrValue = modulation6[0][j];
219  bitErrorRate = modulation6[1][j];
220  burstErrorRate = modulation6[2][j];
221  sigma2 = modulation6[3][j];
222  I1 = modulation6[4][j];
223  I2 = modulation6[5][j];
225  bitErrorRate,
226  burstErrorRate,
227  sigma2,
228  I1,
229  I2);
230  m_recordModulation[6]->push_back (record);
231  }
232  m_activateLoss = true;
233 }
234 
235 void
237 {
238  double snrValue, bitErrorRate, burstErrorRate, sigma2, I1, I2;
239 
240  ClearRecords ();
241 
242  std::ifstream m_ifTraceFile;
243 
244  for (int i = 0; i < 7; i++)
245  {
246  char traceFile[1024];
247  sprintf (traceFile, "%s/Modulation%d.txt", m_traceFilePath, i);
248 
249  m_ifTraceFile.open (traceFile, std::ifstream::in);
250  if (m_ifTraceFile.good () == false)
251  {
252  NS_LOG_INFO ("Unable to load " << traceFile << "!!Loading default traces...");
254  return;
255  }
256  while (m_ifTraceFile.good ())
257  {
258  m_ifTraceFile >> snrValue >> bitErrorRate >> burstErrorRate >> sigma2 >> I1 >> I2;
260  bitErrorRate,
261  burstErrorRate,
262  sigma2,
263  I1,
264  I2);
265 
266  m_recordModulation[i]->push_back (record);
267 
268  }
269  m_ifTraceFile.close ();
270  }
271  m_activateLoss = true;
272 }
273 
274 void
276 {
277  NS_ASSERT_MSG (std::strlen (traceFilePath) < TRACE_FILE_PATH_SIZE,
278  "char * traceFilePath too long");
279  std::strcpy (m_traceFilePath, traceFilePath);
280 }
281 
282 std::string
284 {
285  return (std::string (m_traceFilePath));
286 }
287 
288 double
289 SNRToBlockErrorRateManager::GetBlockErrorRate (double SNR, uint8_t modulation)
290 {
291 
292  if (m_activateLoss == false)
293  {
294  return 0;
295  }
296 
297  std::vector<SNRToBlockErrorRateRecord *> *record = 0;
298 
299  record = m_recordModulation[modulation];
300 
301  if (SNR <= (record->at (0)->GetSNRValue ()))
302  {
303  return 1;
304  }
305  if (SNR >= (record->at (record->size () - 1)->GetSNRValue ()))
306  {
307  return 0;
308  }
309 
310  std::vector<SNRToBlockErrorRateRecord*>::const_iterator iter;
311  unsigned int i;
312  for (i = 0; i < record->size (); i++)
313  {
314  if (SNR < record->at (i)->GetSNRValue ())
315  {
316  break;
317  }
318  }
319  double intervalSize = (record->at (i)->GetSNRValue () - record->at (i - 1)->GetSNRValue ());
320  double coeff1 = (SNR - record->at (i - 1)->GetSNRValue ()) / intervalSize;
321  double coeff2 = -1 * (SNR - record->at (i)->GetSNRValue ()) / intervalSize;
322  double BlockErrorRate = coeff2 * (record->at (i - 1)->GetBlockErrorRate ()) + coeff1 * (record->at (i)->GetBlockErrorRate ());
323  return BlockErrorRate;
324 }
325 
328  uint8_t modulation)
329 {
330 
331  if (m_activateLoss == false)
332  {
333  return new SNRToBlockErrorRateRecord (SNR, 0, 0, 0, 0, 0);
334  }
335 
336  std::vector<SNRToBlockErrorRateRecord *> *record = 0;
337  record = m_recordModulation[modulation];
338 
339  if (SNR <= (record->at (0)->GetSNRValue ()))
340  {
341  return
342  record->at (0)->Copy ();
343  }
344  if (SNR >= (record->at (record->size () - 1)->GetSNRValue ()))
345  {
346  return
347  record->at (record->size () - 1)->Copy ();
348  }
349 
350  std::vector<SNRToBlockErrorRateRecord*>::const_iterator iter;
351  unsigned int i;
352  for (i = 0; i < record->size (); i++)
353  {
354  if (SNR < record->at (i)->GetSNRValue ())
355  {
356  break;
357  }
358  }
359  double intervalSize = (record->at (i)->GetSNRValue ()
360  - record->at (i - 1)->GetSNRValue ());
361  double coeff1 = (SNR - record->at (i - 1)->GetSNRValue ()) / intervalSize;
362  double coeff2 = -1 * (SNR - record->at (i)->GetSNRValue ()) / intervalSize;
363  double BER = coeff2 * (record->at (i - 1)->GetBitErrorRate ()) + coeff1 * (record->at (i)->GetBitErrorRate ());
364  double BlcER = coeff2 * (record->at (i - 1)->GetBlockErrorRate ()) + coeff1 * (record->at (i)->GetBlockErrorRate ());
365  double sigma2 = coeff2 * (record->at (i - 1)->GetSigma2 ()) + coeff1 * (record->at (i)->GetSigma2 ());
366  double I1 = coeff2 * (record->at (i - 1)->GetI1 ()) + coeff1 * (record->at (i)->GetI1 ());
367  double I2 = coeff2 * (record->at (i - 1)->GetI2 ()) + coeff1 * (record->at (i)->GetI2 ());
368 
369  SNRToBlockErrorRateRecord * SNRToBlockErrorRate = new SNRToBlockErrorRateRecord (SNR, BER, BlcER, sigma2, I1,I2);
370  return SNRToBlockErrorRate;
371 }
372 
373 }
374 
static const double modulation2[6][96]
void LoadDefaultTraces(void)
Loads the default traces from default-traces.h file.
static const double modulation6[6][626]
static const double modulation1[6][42]
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:223
static const double modulation4[6][115]
This class represents a record (handled by SnrToBlockErrorRate manager) that keeps a mapping between ...
SNRToBlockErrorRateRecord * GetSNRToBlockErrorRateRecord(double SNR, uint8_t modulation)
returns a record of type SNRToBlockErrorRateRecord corresponding to a given modulation and SNR value ...
static const double modulation3[6][117]
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.
void LoadTraces(void)
Loads the traces form the repository specified in the constructor or setted by SetTraceFilePath funct...
double GetBlockErrorRate(double SNR, uint8_t modulation)
returns the Block Error Rate for a given modulation and SNR value
std::vector< SNRToBlockErrorRateRecord * > * m_recordModulation[7]
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:84
static const double modulation5[6][547]
static const double modulation0[6][29]
Ptr< T > Copy(Ptr< T > object)
Definition: ptr.h:388