A Discrete-Event Network Simulator
API
table-based-error-rate-model.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2020 University of Washington
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  * Authors: Rohan Patidar <rpatidar@uw.edu>
19  * Sébastien Deronne <sebastien.deronne@gmail.com>
20  */
21 
22 #include <cmath>
23 #include <algorithm>
24 #include "ns3/log.h"
25 #include "ns3/string.h"
26 #include "ns3/pointer.h"
27 #include "ns3/uinteger.h"
29 #include "wifi-utils.h"
30 #include "wifi-tx-vector.h"
31 #include "dsss-error-rate-model.h"
32 #include "yans-error-rate-model.h"
33 
34 static const double SNR_PRECISION = 2;
35 
36 namespace ns3 {
37 
38 NS_OBJECT_ENSURE_REGISTERED (TableBasedErrorRateModel);
39 
40 NS_LOG_COMPONENT_DEFINE ("TableBasedErrorRateModel");
41 
42 TypeId
44 {
45  static TypeId tid = TypeId ("ns3::TableBasedErrorRateModel")
47  .SetGroupName ("Wifi")
48  .AddConstructor<TableBasedErrorRateModel> ()
49  .AddAttribute ("FallbackErrorRateModel",
50  "Ptr to the fallback error rate model to be used when no matching value is found in a table",
51  PointerValue (CreateObject<YansErrorRateModel> ()),
53  MakePointerChecker <ErrorRateModel> ())
54  .AddAttribute ("SizeThreshold",
55  "Threshold in bytes over which the table for large size frames is used",
56  UintegerValue (400),
58  MakeUintegerChecker<uint64_t> ())
59  ;
60  return tid;
61 }
62 
64 {
65  NS_LOG_FUNCTION (this);
66 }
67 
69 {
70  NS_LOG_FUNCTION (this);
72 }
73 
74 
75 double
76 TableBasedErrorRateModel::RoundSnr (double snr, double precision) const
77 {
78  NS_LOG_FUNCTION (this << snr);
79  double multiplier = std::round (std::pow (10.0, precision));
80  return std::floor (snr * multiplier + 0.5) / multiplier;
81 }
82 
83 uint8_t
85 {
86  uint8_t mcs = 0xff; // Initialize to invalid mcs
88  {
89  if (mode.GetConstellationSize () == 2)
90  {
91  if (mode.GetCodeRate () == WIFI_CODE_RATE_1_2)
92  {
93  mcs = 0;
94  }
95  if (mode.GetCodeRate () == WIFI_CODE_RATE_3_4)
96  {
97  mcs = 1;
98  }
99  }
100  else if (mode.GetConstellationSize () == 4)
101  {
102  if (mode.GetCodeRate () == WIFI_CODE_RATE_1_2)
103  {
104  mcs = 2;
105  }
106  else if (mode.GetCodeRate () == WIFI_CODE_RATE_3_4)
107  {
108  mcs = 3;
109  }
110  }
111  else if (mode.GetConstellationSize () == 16)
112  {
113  if (mode.GetCodeRate () == WIFI_CODE_RATE_1_2)
114  {
115  mcs = 4;
116  }
117  else if (mode.GetCodeRate () == WIFI_CODE_RATE_3_4)
118  {
119  mcs = 5;
120  }
121  }
122  else if (mode.GetConstellationSize () == 64)
123  {
124  if (mode.GetCodeRate () == WIFI_CODE_RATE_2_3)
125  {
126  mcs = 6;
127  }
128  else if (mode.GetCodeRate () == WIFI_CODE_RATE_3_4)
129  {
130  mcs = 7;
131  }
132  }
133  }
135  {
136  mcs = mode.GetMcsValue ();
137  }
138  NS_ABORT_MSG_IF (mcs == 0xff, "Error, MCS value for mode not found");
139  return mcs;
140 }
141 
142 double
143 TableBasedErrorRateModel::DoGetChunkSuccessRate (WifiMode mode, WifiTxVector txVector, double snr, uint64_t nbits) const
144 {
145  NS_LOG_FUNCTION (this << mode << txVector << snr << nbits);
146  uint64_t size = std::max<uint64_t> (1, (nbits / 8));
147  double roundedSnr = RoundSnr (RatioToDb (snr), SNR_PRECISION);
148  uint8_t mcs = GetMcsForMode (mode);
149  bool ldpc = txVector.IsLdpc ();
150  NS_LOG_FUNCTION (this << +mcs << roundedSnr << size << ldpc);
151 
152  // HT: for mcs greater than 7, use 0 - 7 curves for data rate
153  if (mode.GetModulationClass () == WIFI_MOD_CLASS_HT)
154  {
155  mcs = mcs % 8;
156  }
157 
159  {
160  NS_LOG_WARN ("Table missing for MCS: " << +mcs << " in TableBasedErrorRateModel: use fallback error rate model");
161  return m_fallbackErrorModel->GetChunkSuccessRate (mode, txVector, snr, nbits);
162  }
163 
164  auto errorTable = (ldpc ? AwgnErrorTableLdpc1458 : (size < m_threshold ? AwgnErrorTableBcc32 : AwgnErrorTableBcc1458));
165  auto itVector = errorTable[mcs];
166  auto itTable = std::find_if (itVector.begin(), itVector.end(),
167  [&roundedSnr](const std::pair<double, double>& element) {
168  return element.first == roundedSnr;
169  });
170  double minSnr = itVector.begin ()->first;
171  double maxSnr = (--itVector.end ())->first;
172  double per;
173  if (itTable == itVector.end ())
174  {
175  if (roundedSnr < minSnr)
176  {
177  per = 1.0;
178  }
179  else if (roundedSnr > maxSnr)
180  {
181  per = 0.0;
182  }
183  else
184  {
185  double a = 0.0, b = 0.0, previousSnr = 0.0, nextSnr = 0.0;
186  for (auto i = itVector.begin (); i != itVector.end (); ++i)
187  {
188  if (i->first < roundedSnr)
189  {
190  previousSnr = i->first;
191  a = i->second;
192  }
193  else
194  {
195  nextSnr = i->first;
196  b = i->second;
197  break;
198  }
199  }
200  per = a + (roundedSnr - previousSnr) * (b - a) / (nextSnr - previousSnr);
201  }
202  }
203  else
204  {
205  per = itTable->second;
206  }
207 
209  if (size != tableSize)
210  {
211  // From IEEE document 11-14/0803r1 (Packet Length for Box 0 Calibration)
212  per = (1.0 - std::pow ((1 - per), (static_cast<double> (size) / tableSize)));
213  }
214 
215  return 1.0 - per;
216 }
217 
218 } //namespace ns3
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
ERP-OFDM PHY (19.5)
Definition: wifi-mode.h:56
This class mimics the TXVECTOR which is to be passed to the PHY in order to define the parameters whi...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
const uint8_t ERROR_TABLE_LDPC_MAX_NUM_MCS
const uint8_t ERROR_TABLE_BCC_MAX_NUM_MCS
Ptr< ErrorRateModel > m_fallbackErrorModel
Error rate model to fallback to if no value is found in the table.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
const uint16_t ERROR_TABLE_LDPC_FRAME_SIZE
VHT PHY (Clause 22)
Definition: wifi-mode.h:62
represent a single transmission modeA WifiMode is implemented by a single integer which is used to lo...
Definition: wifi-mode.h:99
bool IsLdpc(void) const
Check if LDPC FEC coding is used or not.
the interface for Wifi&#39;s error models
uint64_t m_threshold
Threshold in bytes over which the table for large size frames is used.
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: pointer.h:227
static const std::vector< std::pair< double, double > > AwgnErrorTableBcc32[ERROR_TABLE_BCC_MAX_NUM_MCS+1]
double DoGetChunkSuccessRate(WifiMode mode, WifiTxVector txVector, double snr, uint64_t nbits) const
A pure virtual method that must be implemented in the subclass.
Introspection did not find any typical Config paths.
Hold an unsigned integer type.
Definition: uinteger.h:44
double RoundSnr(double snr, double precision) const
Round SNR (in dB) to the specified precision.
static const std::vector< std::pair< double, double > > AwgnErrorTableBcc1458[ERROR_TABLE_BCC_MAX_NUM_MCS+1]
static const std::vector< std::pair< double, double > > AwgnErrorTableLdpc1458[ERROR_TABLE_LDPC_MAX_NUM_MCS+1]
HT PHY (Clause 20)
Definition: wifi-mode.h:60
static TypeId GetTypeId(void)
Get the type ID.
WifiModulationClass GetModulationClass() const
Definition: wifi-mode.cc:480
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Hold objects of type Ptr<T>.
Definition: pointer.h:36
const uint16_t ERROR_TABLE_BCC_SMALL_FRAME_SIZE
static const double SNR_PRECISION
uint16_t GetConstellationSize(void) const
Definition: wifi-mode.cc:386
double RatioToDb(double ratio)
Convert from ratio to dB.
Definition: wifi-utils.cc:53
static uint8_t GetMcsForMode(WifiMode mode)
Utility function to convert WifiMode to an MCS value.
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:265
OFDM PHY (Clause 17)
Definition: wifi-mode.h:58
const uint16_t ERROR_TABLE_BCC_LARGE_FRAME_SIZE
WifiCodeRate GetCodeRate(void) const
Definition: wifi-mode.cc:310
Definition: first.py:1
uint8_t GetMcsValue(void) const
Definition: wifi-mode.cc:458
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: uinteger.h:45
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
HE PHY (Clause 26)
Definition: wifi-mode.h:64