File: wifi-error-models-comparison.cc

package info (click to toggle)
ns3 3.47-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 106,848 kB
  • sloc: cpp: 633,577; python: 15,491; ansic: 6,773; makefile: 1,959; sh: 1,021; pascal: 632; javascript: 167; perl: 102
file content (173 lines) | stat: -rw-r--r-- 5,488 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * Copyright (c) 2020 University of Washington
 *
 * SPDX-License-Identifier: GPL-2.0-only
 *
 * Authors: Sébastien Deronne <sebastien.deronne@gmail.com>
 *          Rohan Patidar <rpatidar@uw.edu>
 */

// 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 "ns3/command-line.h"
#include "ns3/gnuplot.h"
#include "ns3/nist-error-rate-model.h"
#include "ns3/table-based-error-rate-model.h"
#include "ns3/wifi-tx-vector.h"
#include "ns3/yans-error-rate-model.h"

#include <cmath>
#include <fstream>

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<YansErrorRateModel> yans = CreateObject<YansErrorRateModel>();
    Ptr<NistErrorRateModel> nist = CreateObject<NistErrorRateModel>();
    Ptr<TableBasedErrorRateModel> table = CreateObject<TableBasedErrorRateModel>();
    WifiTxVector txVector;
    std::vector<std::string> 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 (const auto& mode : modes)
    {
        std::cout << mode << std::endl;
        Gnuplot2dDataset yansdataset(mode);
        Gnuplot2dDataset nistdataset(mode);
        Gnuplot2dDataset tabledataset(mode);
        txVector.SetMode(mode);

        WifiMode wifiMode(mode);

        for (double snrDb = -5.0; snrDb <= (endMcs * 5); snrDb += 0.1)
        {
            double snr = std::pow(10.0, snrDb / 10.0);

            double ps = yans->GetChunkSuccessRate(wifiMode, txVector, snr, size);
            if (ps < 0 || ps > 1)
            {
                // error
                exit(1);
            }
            yansdataset.Add(snrDb, 1 - ps);
            ps = nist->GetChunkSuccessRate(wifiMode, txVector, snr, size);
            if (ps < 0 || ps > 1)
            {
                // error
                exit(1);
            }
            nistdataset.Add(snrDb, 1 - ps);
            ps = table->GetChunkSuccessRate(wifiMode, txVector, snr, size);
            if (ps < 0 || ps > 1)
            {
                // error
                exit(1);
            }
            tabledataset.Add(snrDb, 1 - ps);
        }

        if (tableErrorModelEnabled)
        {
            std::stringstream ss;
            ss << "Table-" << mode;
            tabledataset.SetTitle(ss.str());
            plot.AddDataset(tabledataset);
        }
        if (yansErrorModelEnabled)
        {
            std::stringstream ss;
            ss << "Yans-" << mode;
            yansdataset.SetTitle(ss.str());
            plot.AddDataset(yansdataset);
        }
        if (nistErrorModelEnabled)
        {
            std::stringstream ss;
            ss << "Nist-" << mode;
            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();

    return 0;
}