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