From 41ceefb0e45728a74ae568a1e05e276e03ca4d82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deronne?= Date: Sat, 18 Jul 2020 13:54:22 +0200 Subject: [PATCH] examples: Add example to compare wifi error rate models --- examples/wireless/examples-to-run.py | 1 + .../wireless/wifi-error-models-comparison.cc | 168 ++++++++++++++++++ examples/wireless/wscript | 3 + 3 files changed, 172 insertions(+) create mode 100644 examples/wireless/wifi-error-models-comparison.cc diff --git a/examples/wireless/examples-to-run.py b/examples/wireless/examples-to-run.py index 9da9f3f98..8d5952e18 100755 --- a/examples/wireless/examples-to-run.py +++ b/examples/wireless/examples-to-run.py @@ -36,6 +36,7 @@ cpp_examples = [ ("wifi-ofdm-ht-validation", "True", "True"), ("wifi-ofdm-vht-validation", "True", "True"), ("wifi-ofdm-he-validation", "True", "True"), + ("wifi-error-models-comparison", "True", "True"), ("wifi-80211n-mimo --simulationTime=0.1 --step=10", "True", "True"), ("wifi-ht-network --simulationTime=0.2 --frequency=5 --useRts=0 --minExpectedThroughput=5 --maxExpectedThroughput=135", "True", "True"), ("wifi-ht-network --simulationTime=0.2 --frequency=5 --useRts=1 --minExpectedThroughput=5 --maxExpectedThroughput=131", "True", "True"), diff --git a/examples/wireless/wifi-error-models-comparison.cc b/examples/wireless/wifi-error-models-comparison.cc new file mode 100644 index 000000000..11b3cfc10 --- /dev/null +++ b/examples/wireless/wifi-error-models-comparison.cc @@ -0,0 +1,168 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2020 University of Washington + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Authors: Sébastien Deronne + * Rohan Patidar + */ + +// This example is to show difference among Nist, Yans and Table-based error rate models. +// +// It outputs plots of the Frame Error Rate versus the Signal-to-noise ratio for +// Nist, Yans and Table-based error rate models and for MCS 0, 4 and 7 value. + +#include +#include +#include "ns3/gnuplot.h" +#include "ns3/command-line.h" +#include "ns3/yans-error-rate-model.h" +#include "ns3/nist-error-rate-model.h" +#include "ns3/table-based-error-rate-model.h" +#include "ns3/wifi-tx-vector.h" + +using namespace ns3; + +int main (int argc, char *argv[]) +{ + uint32_t size = 1500 * 8; //bits + bool tableErrorModelEnabled = true; + bool yansErrorModelEnabled = true; + bool nistErrorModelEnabled = true; + uint8_t beginMcs = 0; + uint8_t endMcs = 7; + uint8_t stepMcs = 4; + std::string format ("Ht"); + + CommandLine cmd (__FILE__); + cmd.AddValue ("size", "The size in bits", size); + cmd.AddValue ("frameFormat", "The frame format to use: Ht, Vht or He", format); + cmd.AddValue ("beginMcs", "The first MCS to test", beginMcs); + cmd.AddValue ("endMcs", "The last MCS to test", endMcs); + cmd.AddValue ("stepMcs", "The step between two MCSs to test", stepMcs); + cmd.AddValue ("includeTableErrorModel", "Flag to include/exclude Table-based error model", tableErrorModelEnabled); + cmd.AddValue ("includeYansErrorModel", "Flag to include/exclude Yans error model", yansErrorModelEnabled); + cmd.AddValue ("includeNistErrorModel", "Flag to include/exclude Nist error model", nistErrorModelEnabled); + cmd.Parse (argc, argv); + + std::ofstream errormodelfile ("wifi-error-rate-models.plt"); + Gnuplot plot = Gnuplot ("wifi-error-rate-models.eps"); + + Ptr yans = CreateObject (); + Ptr nist = CreateObject (); + Ptr table = CreateObject (); + WifiTxVector txVector; + std::vector modes; + + std::stringstream mode; + mode << format << "Mcs" << +beginMcs; + modes.push_back (mode.str ()); + for (uint8_t mcs = (beginMcs + stepMcs); mcs < endMcs; mcs += stepMcs) + { + mode.str (""); + mode << format << "Mcs" << +mcs; + modes.push_back (mode.str ()); + } + mode.str (""); + mode << format << "Mcs" << +endMcs; + modes.push_back (mode.str ()); + + for (uint32_t i = 0; i < modes.size (); i++) + { + std::cout << modes[i] << std::endl; + Gnuplot2dDataset yansdataset (modes[i]); + Gnuplot2dDataset nistdataset (modes[i]); + Gnuplot2dDataset tabledataset (modes[i]); + txVector.SetMode (modes[i]); + + for (double snr = -5.0; snr <= (endMcs * 5); snr += 0.1) + { + double ps = yans->GetChunkSuccessRate (WifiMode (modes[i]), txVector, std::pow (10.0, snr / 10.0), size); + if (ps < 0 || ps > 1) + { + //error + exit (1); + } + yansdataset.Add (snr, 1 - ps); + ps = nist->GetChunkSuccessRate (WifiMode (modes[i]), txVector, std::pow (10.0, snr / 10.0), size); + if (ps < 0 || ps > 1) + { + //error + exit (1); + } + nistdataset.Add (snr, 1 - ps); + ps = table->GetChunkSuccessRate (WifiMode (modes[i]), txVector, std::pow (10.0, snr / 10.0), size); + if (ps < 0 || ps > 1) + { + //error + exit (1); + } + tabledataset.Add (snr, 1 - ps); + } + + if (tableErrorModelEnabled) + { + std::stringstream ss; + ss << "Table-" << modes[i]; + tabledataset.SetTitle (ss.str()); + plot.AddDataset (tabledataset); + } + if (yansErrorModelEnabled) + { + std::stringstream ss; + ss << "Yans-" << modes[i]; + yansdataset.SetTitle (ss.str ()); + plot.AddDataset (yansdataset); + } + if (nistErrorModelEnabled) + { + std::stringstream ss; + ss << "Nist-" << modes[i]; + nistdataset.SetTitle (ss.str ()); + plot.AddDataset (nistdataset); + } + } + + plot.SetTerminal ("postscript eps color enh \"Times-BoldItalic\""); + plot.SetLegend ("SNR(dB)", "Frame Error Rate"); + + std::stringstream plotExtra; + plotExtra << "set xrange [-5:" << endMcs * 5 << "]\n\ +set log y\n\ +set yrange [0.0001:1]\n"; + + uint8_t lineNumber = 1; + for (uint32_t i = 0; i < modes.size (); i++) + { + if (tableErrorModelEnabled) + { + plotExtra << "set style line " << +lineNumber++ << " linewidth 5 linecolor rgb \"red\" \n"; + } + if (yansErrorModelEnabled) + { + plotExtra << "set style line " << +lineNumber++ << " linewidth 5 linecolor rgb \"green\" \n"; + } + if (nistErrorModelEnabled) + { + plotExtra << "set style line " << +lineNumber++ << " linewidth 5 linecolor rgb \"blue\" \n"; + } + } + + plotExtra << "set style increment user"; + plot.SetExtra (plotExtra.str ()); + + plot.GenerateOutput (errormodelfile); + errormodelfile.close (); +} diff --git a/examples/wireless/wscript b/examples/wireless/wscript index d7c4a0bb2..6822ca674 100644 --- a/examples/wireless/wscript +++ b/examples/wireless/wscript @@ -121,3 +121,6 @@ def build(bld): obj = bld.create_ns3_program('wifi-spatial-reuse', ['wifi', 'applications']) obj.source = 'wifi-spatial-reuse.cc' + + obj = bld.create_ns3_program('wifi-error-models-comparison', ['wifi']) + obj.source = 'wifi-error-models-comparison.cc'