A Discrete-Event Network Simulator
API
wifi-error-models-comparison.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 University of Washington
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Authors: Sébastien Deronne <sebastien.deronne@gmail.com>
18  * Rohan Patidar <rpatidar@uw.edu>
19  */
20 
21 // This example is to show difference among Nist, Yans and Table-based error rate models.
22 //
23 // It outputs plots of the Frame Error Rate versus the Signal-to-noise ratio for
24 // Nist, Yans and Table-based error rate models and for MCS 0, 4 and 7 value.
25 
26 #include "ns3/command-line.h"
27 #include "ns3/gnuplot.h"
28 #include "ns3/nist-error-rate-model.h"
29 #include "ns3/table-based-error-rate-model.h"
30 #include "ns3/wifi-tx-vector.h"
31 #include "ns3/yans-error-rate-model.h"
32 
33 #include <cmath>
34 #include <fstream>
35 
36 using namespace ns3;
37 
38 int
39 main(int argc, char* argv[])
40 {
41  uint32_t size = 1500 * 8; // bits
42  bool tableErrorModelEnabled = true;
43  bool yansErrorModelEnabled = true;
44  bool nistErrorModelEnabled = true;
45  uint8_t beginMcs = 0;
46  uint8_t endMcs = 7;
47  uint8_t stepMcs = 4;
48  std::string format("Ht");
49 
50  CommandLine cmd(__FILE__);
51  cmd.AddValue("size", "The size in bits", size);
52  cmd.AddValue("frameFormat", "The frame format to use: Ht, Vht or He", format);
53  cmd.AddValue("beginMcs", "The first MCS to test", beginMcs);
54  cmd.AddValue("endMcs", "The last MCS to test", endMcs);
55  cmd.AddValue("stepMcs", "The step between two MCSs to test", stepMcs);
56  cmd.AddValue("includeTableErrorModel",
57  "Flag to include/exclude Table-based error model",
58  tableErrorModelEnabled);
59  cmd.AddValue("includeYansErrorModel",
60  "Flag to include/exclude Yans error model",
61  yansErrorModelEnabled);
62  cmd.AddValue("includeNistErrorModel",
63  "Flag to include/exclude Nist error model",
64  nistErrorModelEnabled);
65  cmd.Parse(argc, argv);
66 
67  std::ofstream errormodelfile("wifi-error-rate-models.plt");
68  Gnuplot plot = Gnuplot("wifi-error-rate-models.eps");
69 
70  Ptr<YansErrorRateModel> yans = CreateObject<YansErrorRateModel>();
71  Ptr<NistErrorRateModel> nist = CreateObject<NistErrorRateModel>();
72  Ptr<TableBasedErrorRateModel> table = CreateObject<TableBasedErrorRateModel>();
73  WifiTxVector txVector;
74  std::vector<std::string> modes;
75 
76  std::stringstream mode;
77  mode << format << "Mcs" << +beginMcs;
78  modes.push_back(mode.str());
79  for (uint8_t mcs = (beginMcs + stepMcs); mcs < endMcs; mcs += stepMcs)
80  {
81  mode.str("");
82  mode << format << "Mcs" << +mcs;
83  modes.push_back(mode.str());
84  }
85  mode.str("");
86  mode << format << "Mcs" << +endMcs;
87  modes.push_back(mode.str());
88 
89  for (uint32_t i = 0; i < modes.size(); i++)
90  {
91  std::cout << modes[i] << std::endl;
92  Gnuplot2dDataset yansdataset(modes[i]);
93  Gnuplot2dDataset nistdataset(modes[i]);
94  Gnuplot2dDataset tabledataset(modes[i]);
95  txVector.SetMode(modes[i]);
96 
97  for (double snr = -5.0; snr <= (endMcs * 5); snr += 0.1)
98  {
99  double ps = yans->GetChunkSuccessRate(WifiMode(modes[i]),
100  txVector,
101  std::pow(10.0, snr / 10.0),
102  size);
103  if (ps < 0 || ps > 1)
104  {
105  // error
106  exit(1);
107  }
108  yansdataset.Add(snr, 1 - ps);
109  ps = nist->GetChunkSuccessRate(WifiMode(modes[i]),
110  txVector,
111  std::pow(10.0, snr / 10.0),
112  size);
113  if (ps < 0 || ps > 1)
114  {
115  // error
116  exit(1);
117  }
118  nistdataset.Add(snr, 1 - ps);
119  ps = table->GetChunkSuccessRate(WifiMode(modes[i]),
120  txVector,
121  std::pow(10.0, snr / 10.0),
122  size);
123  if (ps < 0 || ps > 1)
124  {
125  // error
126  exit(1);
127  }
128  tabledataset.Add(snr, 1 - ps);
129  }
130 
131  if (tableErrorModelEnabled)
132  {
133  std::stringstream ss;
134  ss << "Table-" << modes[i];
135  tabledataset.SetTitle(ss.str());
136  plot.AddDataset(tabledataset);
137  }
138  if (yansErrorModelEnabled)
139  {
140  std::stringstream ss;
141  ss << "Yans-" << modes[i];
142  yansdataset.SetTitle(ss.str());
143  plot.AddDataset(yansdataset);
144  }
145  if (nistErrorModelEnabled)
146  {
147  std::stringstream ss;
148  ss << "Nist-" << modes[i];
149  nistdataset.SetTitle(ss.str());
150  plot.AddDataset(nistdataset);
151  }
152  }
153 
154  plot.SetTerminal("postscript eps color enh \"Times-BoldItalic\"");
155  plot.SetLegend("SNR(dB)", "Frame Error Rate");
156 
157  std::stringstream plotExtra;
158  plotExtra << "set xrange [-5:" << endMcs * 5 << "]\n\
159 set log y\n\
160 set yrange [0.0001:1]\n";
161 
162  uint8_t lineNumber = 1;
163  for (uint32_t i = 0; i < modes.size(); i++)
164  {
165  if (tableErrorModelEnabled)
166  {
167  plotExtra << "set style line " << +lineNumber++
168  << " linewidth 5 linecolor rgb \"red\" \n";
169  }
170  if (yansErrorModelEnabled)
171  {
172  plotExtra << "set style line " << +lineNumber++
173  << " linewidth 5 linecolor rgb \"green\" \n";
174  }
175  if (nistErrorModelEnabled)
176  {
177  plotExtra << "set style line " << +lineNumber++
178  << " linewidth 5 linecolor rgb \"blue\" \n";
179  }
180  }
181 
182  plotExtra << "set style increment user";
183  plot.SetExtra(plotExtra.str());
184 
185  plot.GenerateOutput(errormodelfile);
186  errormodelfile.close();
187 
188  return 0;
189 }
Parse command-line arguments.
Definition: command-line.h:232
Class to represent a 2D points plot.
Definition: gnuplot.h:116
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
Definition: gnuplot.h:370
void AddDataset(const GnuplotDataset &dataset)
Definition: gnuplot.cc:796
void SetLegend(const std::string &xLegend, const std::string &yLegend)
Definition: gnuplot.cc:776
void SetTerminal(const std::string &terminal)
Definition: gnuplot.cc:764
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition: gnuplot.cc:802
void SetExtra(const std::string &extra)
Definition: gnuplot.cc:783
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
represent a single transmission mode
Definition: wifi-mode.h:50
This class mimics the TXVECTOR which is to be passed to the PHY in order to define the parameters whi...
void SetMode(WifiMode mode)
Sets the selected payload transmission mode.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:33