A Discrete-Event Network Simulator
API
yans-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) 2005,2006 INRIA
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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  * Sébastien Deronne <sebastien.deronne@gmail.com>
20  */
21 
22 #include <cmath>
23 #include "yans-error-rate-model.h"
24 #include "wifi-phy.h"
25 #include "ns3/log.h"
26 
27 namespace ns3 {
28 
29 NS_LOG_COMPONENT_DEFINE ("YansErrorRateModel");
30 
31 NS_OBJECT_ENSURE_REGISTERED (YansErrorRateModel);
32 
33 TypeId
35 {
36  static TypeId tid = TypeId ("ns3::YansErrorRateModel")
38  .SetGroupName ("Wifi")
39  .AddConstructor<YansErrorRateModel> ()
40  ;
41  return tid;
42 }
43 
45 {
46 }
47 
48 double
49 YansErrorRateModel::Log2 (double val) const
50 {
51  return std::log (val) / std::log (2.0);
52 }
53 
54 double
55 YansErrorRateModel::GetBpskBer (double snr, uint32_t signalSpread, uint32_t phyRate) const
56 {
57  NS_LOG_FUNCTION (this << snr << signalSpread << phyRate);
58  double EbNo = snr * signalSpread / phyRate;
59  double z = std::sqrt (EbNo);
60  double ber = 0.5 * erfc (z);
61  NS_LOG_INFO ("bpsk snr=" << snr << " ber=" << ber);
62  return ber;
63 }
64 
65 double
66 YansErrorRateModel::GetQamBer (double snr, unsigned int m, uint32_t signalSpread, uint32_t phyRate) const
67 {
68  NS_LOG_FUNCTION (this << snr << m << signalSpread << phyRate);
69  double EbNo = snr * signalSpread / phyRate;
70  double z = std::sqrt ((1.5 * Log2 (m) * EbNo) / (m - 1.0));
71  double z1 = ((1.0 - 1.0 / std::sqrt (m)) * erfc (z));
72  double z2 = 1 - std::pow ((1 - z1), 2);
73  double ber = z2 / Log2 (m);
74  NS_LOG_INFO ("Qam m=" << m << " rate=" << phyRate << " snr=" << snr << " ber=" << ber);
75  return ber;
76 }
77 
78 uint32_t
80 {
81  uint32_t fact = 1;
82  while (k > 0)
83  {
84  fact *= k;
85  k--;
86  }
87  return fact;
88 }
89 
90 double
91 YansErrorRateModel::Binomial (uint32_t k, double p, uint32_t n) const
92 {
93  double retval = Factorial (n) / (Factorial (k) * Factorial (n - k)) * std::pow (p, static_cast<double> (k)) * std::pow (1 - p, static_cast<double> (n - k));
94  return retval;
95 }
96 
97 double
98 YansErrorRateModel::CalculatePdOdd (double ber, unsigned int d) const
99 {
100  NS_ASSERT ((d % 2) == 1);
101  unsigned int dstart = (d + 1) / 2;
102  unsigned int dend = d;
103  double pd = 0;
104 
105  for (unsigned int i = dstart; i < dend; i++)
106  {
107  pd += Binomial (i, ber, d);
108  }
109  return pd;
110 }
111 
112 double
113 YansErrorRateModel::CalculatePdEven (double ber, unsigned int d) const
114 {
115  NS_ASSERT ((d % 2) == 0);
116  unsigned int dstart = d / 2 + 1;
117  unsigned int dend = d;
118  double pd = 0;
119 
120  for (unsigned int i = dstart; i < dend; i++)
121  {
122  pd += Binomial (i, ber, d);
123  }
124  pd += 0.5 * Binomial (d / 2, ber, d);
125 
126  return pd;
127 }
128 
129 double
130 YansErrorRateModel::CalculatePd (double ber, unsigned int d) const
131 {
132  NS_LOG_FUNCTION (this << ber << d);
133  double pd;
134  if ((d % 2) == 0)
135  {
136  pd = CalculatePdEven (ber, d);
137  }
138  else
139  {
140  pd = CalculatePdOdd (ber, d);
141  }
142  return pd;
143 }
144 
145 double
146 YansErrorRateModel::GetFecBpskBer (double snr, double nbits,
147  uint32_t signalSpread, uint32_t phyRate,
148  uint32_t dFree, uint32_t adFree) const
149 {
150  NS_LOG_FUNCTION (this << snr << nbits << signalSpread << phyRate << dFree << adFree);
151  double ber = GetBpskBer (snr, signalSpread, phyRate);
152  if (ber == 0.0)
153  {
154  return 1.0;
155  }
156  double pd = CalculatePd (ber, dFree);
157  double pmu = adFree * pd;
158  pmu = std::min (pmu, 1.0);
159  double pms = std::pow (1 - pmu, nbits);
160  return pms;
161 }
162 
163 double
164 YansErrorRateModel::GetFecQamBer (double snr, uint32_t nbits,
165  uint32_t signalSpread,
166  uint32_t phyRate,
167  uint32_t m, uint32_t dFree,
168  uint32_t adFree, uint32_t adFreePlusOne) const
169 {
170  NS_LOG_FUNCTION (this << snr << nbits << signalSpread << phyRate << m << dFree << adFree << adFreePlusOne);
171  double ber = GetQamBer (snr, m, signalSpread, phyRate);
172  if (ber == 0.0)
173  {
174  return 1.0;
175  }
176  /* first term */
177  double pd = CalculatePd (ber, dFree);
178  double pmu = adFree * pd;
179  /* second term */
180  pd = CalculatePd (ber, dFree + 1);
181  pmu += adFreePlusOne * pd;
182  pmu = std::min (pmu, 1.0);
183  double pms = std::pow (1 - pmu, static_cast<double> (nbits));
184  return pms;
185 }
186 
187 double
188 YansErrorRateModel::GetChunkSuccessRate (WifiMode mode, WifiTxVector txVector, double snr, uint32_t nbits) const
189 {
190  NS_LOG_FUNCTION (this << mode << txVector.GetMode () << snr << nbits);
195  {
196  if (mode.GetConstellationSize () == 2)
197  {
198  if (mode.GetCodeRate () == WIFI_CODE_RATE_1_2)
199  {
200  return GetFecBpskBer (snr,
201  nbits,
202  txVector.GetChannelWidth () * 1000000, //signal spread
203  mode.GetPhyRate (txVector), //phy rate
204  10, //dFree
205  11); //adFree
206  }
207  else
208  {
209  return GetFecBpskBer (snr,
210  nbits,
211  txVector.GetChannelWidth () * 1000000, //signal spread
212  mode.GetPhyRate (txVector), //phy rate
213  5, //dFree
214  8); //adFree
215  }
216  }
217  else if (mode.GetConstellationSize () == 4)
218  {
219  if (mode.GetCodeRate () == WIFI_CODE_RATE_1_2)
220  {
221  return GetFecQamBer (snr,
222  nbits,
223  txVector.GetChannelWidth () * 1000000, //signal spread
224  mode.GetPhyRate (txVector), //phy rate
225  4, //m
226  10, //dFree
227  11, //adFree
228  0); //adFreePlusOne
229  }
230  else
231  {
232  return GetFecQamBer (snr,
233  nbits,
234  txVector.GetChannelWidth () * 1000000, //signal spread
235  mode.GetPhyRate (txVector), //phy rate
236  4, //m
237  5, //dFree
238  8, //adFree
239  31); //adFreePlusOne
240  }
241  }
242  else if (mode.GetConstellationSize () == 16)
243  {
244  if (mode.GetCodeRate () == WIFI_CODE_RATE_1_2)
245  {
246  return GetFecQamBer (snr,
247  nbits,
248  txVector.GetChannelWidth () * 1000000, //signal spread
249  mode.GetPhyRate (txVector), //phy rate
250  16, //m
251  10, //dFree
252  11, //adFree
253  0); //adFreePlusOne
254  }
255  else
256  {
257  return GetFecQamBer (snr,
258  nbits,
259  txVector.GetChannelWidth () * 1000000, //signal spread
260  mode.GetPhyRate (txVector), //phy rate
261  16, //m
262  5, //dFree
263  8, //adFree
264  31); //adFreePlusOne
265  }
266  }
267  else if (mode.GetConstellationSize () == 64)
268  {
269  if (mode.GetCodeRate () == WIFI_CODE_RATE_2_3)
270  {
271  return GetFecQamBer (snr,
272  nbits,
273  txVector.GetChannelWidth () * 1000000, //signal spread
274  mode.GetPhyRate (txVector), //phy rate
275  64, //m
276  6, //dFree
277  1, //adFree
278  16); //adFreePlusOne
279  }
280  if (mode.GetCodeRate () == WIFI_CODE_RATE_5_6)
281  {
282  //Table B.32 in Pâl Frenger et al., "Multi-rate Convolutional Codes".
283  return GetFecQamBer (snr,
284  nbits,
285  txVector.GetChannelWidth () * 1000000, //signal spread
286  mode.GetPhyRate (txVector), //phy rate
287  64, //m
288  4, //dFree
289  14, //adFree
290  69); //adFreePlusOne
291  }
292  else
293  {
294  return GetFecQamBer (snr,
295  nbits,
296  txVector.GetChannelWidth () * 1000000, //signal spread
297  mode.GetPhyRate (txVector), //phy rate
298  64, //m
299  5, //dFree
300  8, //adFree
301  31); //adFreePlusOne
302  }
303  }
304  else if (mode.GetConstellationSize () == 256)
305  {
306  if (mode.GetCodeRate () == WIFI_CODE_RATE_5_6)
307  {
308  return GetFecQamBer (snr,
309  nbits,
310  txVector.GetChannelWidth () * 1000000, // signal spread
311  mode.GetPhyRate (txVector), //phy rate
312  256, // m
313  4, // dFree
314  14, // adFree
315  69 // adFreePlusOne
316  );
317  }
318  else
319  {
320  return GetFecQamBer (snr,
321  nbits,
322  txVector.GetChannelWidth () * 1000000, // signal spread
323  mode.GetPhyRate (txVector), //phy rate
324  256, // m
325  5, // dFree
326  8, // adFree
327  31 // adFreePlusOne
328  );
329  }
330  }
331  }
333  {
334  switch (mode.GetDataRate (20, 0, 1))
335  {
336  case 1000000:
338  case 2000000:
340  case 5500000:
342  case 11000000:
344  default:
345  NS_ASSERT ("undefined DSSS/HR-DSSS datarate");
346  }
347  }
348  return 0;
349 }
350 
351 } //namespace ns3
static TypeId GetTypeId(void)
double CalculatePdEven(double ber, unsigned int d) const
static double GetDsssDqpskCck11SuccessRate(double sinr, uint32_t nbits)
Return the chunk success rate of the differential encoded QPSK for 11Mbps data rate.
double CalculatePd(double ber, unsigned int d) const
#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:58
This class mimics the TXVECTOR which is to be passed to the PHY in order to define the parameters whi...
double GetFecBpskBer(double snr, double nbits, uint32_t signalSpread, uint32_t phyRate, uint32_t dFree, uint32_t adFree) const
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
#define min(a, b)
Definition: 80211b.c:44
enum WifiModulationClass GetModulationClass() const
Definition: wifi-mode.cc:379
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
double CalculatePdOdd(double ber, unsigned int d) const
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
double GetQamBer(double snr, unsigned int m, uint32_t signalSpread, uint32_t phyRate) const
Return BER of QAM-m with the given parameters.
static double GetDsssDqpskSuccessRate(double sinr, uint32_t nbits)
Return the chunk success rate of the differential encoded QPSK.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:244
VHT PHY (Clause 22)
Definition: wifi-mode.h:64
HR/DSSS PHY (Clause 18)
Definition: wifi-mode.h:52
represent a single transmission modeA WifiMode is implemented by a single integer which is used to lo...
Definition: wifi-mode.h:99
the interface for Wifi's error models
double GetFecQamBer(double snr, uint32_t nbits, uint32_t signalSpread, uint32_t phyRate, uint32_t m, uint32_t dfree, uint32_t adFree, uint32_t adFreePlusOne) const
uint64_t GetPhyRate(uint32_t channelWidth, bool isShortGuardInterval, uint8_t nss) const
Definition: wifi-mode.cc:74
uint16_t GetConstellationSize(void) const
Definition: wifi-mode.cc:289
Model the error rate for different modulations.
enum WifiCodeRate GetCodeRate(void) const
Definition: wifi-mode.cc:237
uint32_t GetChannelWidth(void) const
HT PHY (Clause 20)
Definition: wifi-mode.h:62
virtual double GetChunkSuccessRate(WifiMode mode, WifiTxVector txVector, double snr, uint32_t nbits) const
A pure virtual method that must be implemented in the subclass.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
double Log2(double val) const
Return the logarithm of the given value to base 2.
uint64_t GetDataRate(uint32_t channelWidth, bool isShortGuardInterval, uint8_t nss) const
Definition: wifi-mode.cc:109
uint32_t Factorial(uint32_t k) const
Return k!
static double GetDsssDqpskCck5_5SuccessRate(double sinr, uint32_t nbits)
Return the chunk success rate of the differential encoded QPSK for 5.5Mbps data rate.
double GetBpskBer(double snr, uint32_t signalSpread, uint32_t phyRate) const
Return BER of BPSK with the given parameters.
OFDM PHY (Clause 17)
Definition: wifi-mode.h:60
double Binomial(uint32_t k, double p, uint32_t n) const
Return Binomial distribution for a given k, p, and n.
static double GetDsssDbpskSuccessRate(double sinr, uint32_t nbits)
Return the chunk success rate of the differential BPSK.
WifiMode GetMode(void) const
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:904
DSSS PHY (Clause 15)
Definition: wifi-mode.h:50