A Discrete-Event Network Simulator
API
wifi-error-models-comparison.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: S├ębastien Deronne <sebastien.deronne@gmail.com>
19  * Rohan Patidar <rpatidar@uw.edu>
20  */
21 
22 // This example is to show difference among Nist, Yans and Table-based error rate models.
23 //
24 // It outputs plots of the Frame Error Rate versus the Signal-to-noise ratio for
25 // Nist, Yans and Table-based error rate models and for MCS 0, 4 and 7 value.
26 
27 #include <fstream>
28 #include <cmath>
29 #include "ns3/gnuplot.h"
30 #include "ns3/command-line.h"
31 #include "ns3/yans-error-rate-model.h"
32 #include "ns3/nist-error-rate-model.h"
33 #include "ns3/table-based-error-rate-model.h"
34 #include "ns3/wifi-tx-vector.h"
35 
36 using namespace ns3;
37 
38 int main (int argc, char *argv[])
39 {
40  uint32_t size = 1500 * 8; //bits
41  bool tableErrorModelEnabled = true;
42  bool yansErrorModelEnabled = true;
43  bool nistErrorModelEnabled = true;
44  uint8_t beginMcs = 0;
45  uint8_t endMcs = 7;
46  uint8_t stepMcs = 4;
47  std::string format ("Ht");
48 
49  CommandLine cmd (__FILE__);
50  cmd.AddValue ("size", "The size in bits", size);
51  cmd.AddValue ("frameFormat", "The frame format to use: Ht, Vht or He", format);
52  cmd.AddValue ("beginMcs", "The first MCS to test", beginMcs);
53  cmd.AddValue ("endMcs", "The last MCS to test", endMcs);
54  cmd.AddValue ("stepMcs", "The step between two MCSs to test", stepMcs);
55  cmd.AddValue ("includeTableErrorModel", "Flag to include/exclude Table-based error model", tableErrorModelEnabled);
56  cmd.AddValue ("includeYansErrorModel", "Flag to include/exclude Yans error model", yansErrorModelEnabled);
57  cmd.AddValue ("includeNistErrorModel", "Flag to include/exclude Nist error model", nistErrorModelEnabled);
58  cmd.Parse (argc, argv);
59 
60  std::ofstream errormodelfile ("wifi-error-rate-models.plt");
61  Gnuplot plot = Gnuplot ("wifi-error-rate-models.eps");
62 
63  Ptr <YansErrorRateModel> yans = CreateObject<YansErrorRateModel> ();
64  Ptr <NistErrorRateModel> nist = CreateObject<NistErrorRateModel> ();
65  Ptr <TableBasedErrorRateModel> table = CreateObject<TableBasedErrorRateModel> ();
66  WifiTxVector txVector;
67  std::vector <std::string> modes;
68 
69  std::stringstream mode;
70  mode << format << "Mcs" << +beginMcs;
71  modes.push_back (mode.str ());
72  for (uint8_t mcs = (beginMcs + stepMcs); mcs < endMcs; mcs += stepMcs)
73  {
74  mode.str ("");
75  mode << format << "Mcs" << +mcs;
76  modes.push_back (mode.str ());
77  }
78  mode.str ("");
79  mode << format << "Mcs" << +endMcs;
80  modes.push_back (mode.str ());
81 
82  for (uint32_t i = 0; i < modes.size (); i++)
83  {
84  std::cout << modes[i] << std::endl;
85  Gnuplot2dDataset yansdataset (modes[i]);
86  Gnuplot2dDataset nistdataset (modes[i]);
87  Gnuplot2dDataset tabledataset (modes[i]);
88  txVector.SetMode (modes[i]);
89 
90  for (double snr = -5.0; snr <= (endMcs * 5); snr += 0.1)
91  {
92  double ps = yans->GetChunkSuccessRate (WifiMode (modes[i]), txVector, std::pow (10.0, snr / 10.0), size);
93  if (ps < 0 || ps > 1)
94  {
95  //error
96  exit (1);
97  }
98  yansdataset.Add (snr, 1 - ps);
99  ps = nist->GetChunkSuccessRate (WifiMode (modes[i]), txVector, std::pow (10.0, snr / 10.0), size);
100  if (ps < 0 || ps > 1)
101  {
102  //error
103  exit (1);
104  }
105  nistdataset.Add (snr, 1 - ps);
106  ps = table->GetChunkSuccessRate (WifiMode (modes[i]), txVector, std::pow (10.0, snr / 10.0), size);
107  if (ps < 0 || ps > 1)
108  {
109  //error
110  exit (1);
111  }
112  tabledataset.Add (snr, 1 - ps);
113  }
114 
115  if (tableErrorModelEnabled)
116  {
117  std::stringstream ss;
118  ss << "Table-" << modes[i];
119  tabledataset.SetTitle (ss.str());
120  plot.AddDataset (tabledataset);
121  }
122  if (yansErrorModelEnabled)
123  {
124  std::stringstream ss;
125  ss << "Yans-" << modes[i];
126  yansdataset.SetTitle (ss.str ());
127  plot.AddDataset (yansdataset);
128  }
129  if (nistErrorModelEnabled)
130  {
131  std::stringstream ss;
132  ss << "Nist-" << modes[i];
133  nistdataset.SetTitle (ss.str ());
134  plot.AddDataset (nistdataset);
135  }
136  }
137 
138  plot.SetTerminal ("postscript eps color enh \"Times-BoldItalic\"");
139  plot.SetLegend ("SNR(dB)", "Frame Error Rate");
140 
141  std::stringstream plotExtra;
142  plotExtra << "set xrange [-5:" << endMcs * 5 << "]\n\
143 set log y\n\
144 set yrange [0.0001:1]\n";
145 
146  uint8_t lineNumber = 1;
147  for (uint32_t i = 0; i < modes.size (); i++)
148  {
149  if (tableErrorModelEnabled)
150  {
151  plotExtra << "set style line " << +lineNumber++ << " linewidth 5 linecolor rgb \"red\" \n";
152  }
153  if (yansErrorModelEnabled)
154  {
155  plotExtra << "set style line " << +lineNumber++ << " linewidth 5 linecolor rgb \"green\" \n";
156  }
157  if (nistErrorModelEnabled)
158  {
159  plotExtra << "set style line " << +lineNumber++ << " linewidth 5 linecolor rgb \"blue\" \n";
160  }
161  }
162 
163  plotExtra << "set style increment user";
164  plot.SetExtra (plotExtra.str ());
165 
166  plot.GenerateOutput (errormodelfile);
167  errormodelfile.close ();
168 }
ns3::CommandLine
Parse command-line arguments.
Definition: command-line.h:228
ns3::Gnuplot::SetLegend
void SetLegend(const std::string &xLegend, const std::string &yLegend)
Definition: gnuplot.cc:736
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::Gnuplot
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
Definition: gnuplot.h:372
ns3::Gnuplot::SetTerminal
void SetTerminal(const std::string &terminal)
Definition: gnuplot.cc:724
ns3::WifiTxVector::SetMode
void SetMode(WifiMode mode)
Sets the selected payload transmission mode.
Definition: wifi-tx-vector.cc:225
ns3::WifiTxVector
This class mimics the TXVECTOR which is to be passed to the PHY in order to define the parameters whi...
Definition: wifi-tx-vector.h:71
ns3::Gnuplot::GenerateOutput
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition: gnuplot.cc:762
ns3::Gnuplot::SetExtra
void SetExtra(const std::string &extra)
Definition: gnuplot.cc:743
ns3::Gnuplot2dDataset
Class to represent a 2D points plot.
Definition: gnuplot.h:118
ns3::ErrorRateModel::GetChunkSuccessRate
double GetChunkSuccessRate(WifiMode mode, const WifiTxVector &txVector, double snr, uint64_t nbits, uint8_t numRxAntennas=1, WifiPpduField field=WIFI_PPDU_FIELD_DATA, uint16_t staId=SU_STA_ID) const
This method returns the probability that the given 'chunk' of the packet will be successfully receive...
Definition: error-rate-model.cc:63
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
ns3::WifiMode
represent a single transmission mode
Definition: wifi-mode.h:48
second.cmd
cmd
Definition: second.py:35
ns3::Gnuplot::AddDataset
void AddDataset(const GnuplotDataset &dataset)
Definition: gnuplot.cc:756