A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
lr-wpan-per-plot.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 Tokushima University, Japan
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Authors: Alberto Gallegos Ramonet <alramonet@is.tokushima-u.ac.jp>
7 * Tommaso Pecorella <tommaso.pecorella@unifi.it>
8 */
9
10/*
11 This program produces a gnuplot file that plots the theoretical and experimental packet error
12 rate (PER) as a function of receive signal for the 802.15.4 model. As described by the standard,
13 the PER is calculated with the transmission of frames with a PSDU of 20 bytes. This is equivalent
14 to an MPDU = MAC header (11 bytes) + FCS (2 bytes) + payload (MSDU 7 bytes). In the experimental
15 test, 1000 frames are transmitted for each Rx signal ranging from -130 dBm to -100 dBm with
16 increments of 0.01 dBm. The point before PER is < 1 % is the device receive sensitivity.
17 Theoretical and experimental Rx sensitivity is printed at the end of the end and a plot is
18 generated.
19
20 Example usage:
21
22 ./ns3 run "lr-wpan-per-plot --rxSensitivity=-92"
23
24*/
25
26#include <ns3/core-module.h>
27#include <ns3/gnuplot.h>
28#include <ns3/lr-wpan-module.h>
29#include <ns3/mobility-module.h>
30#include <ns3/network-module.h>
31#include <ns3/propagation-module.h>
32#include <ns3/spectrum-module.h>
33
34using namespace ns3;
35using namespace ns3::lrwpan;
36
37uint32_t g_packetsReceived = 0; //!< number of packets received
38
39NS_LOG_COMPONENT_DEFINE("LrWpanErrorDistancePlot");
40
41/**
42 * Function called when a Data indication is invoked
43 * \param params MCPS data indication parameters
44 * \param p packet
45 */
46void
51
52int
53main(int argc, char* argv[])
54{
56
57 std::ostringstream os;
58 std::ofstream perfile("802.15.4-per-vs-rxSignal.plt");
59
60 double minRxSignal = -111; // dBm
61 double maxRxSignal = -82; // dBm
62 double increment = 0.01;
63 int maxPackets = 1000;
64 int packetSize = 7; // bytes (MPDU payload)
65 double txPower = 0; // dBm
66 uint32_t channelNumber = 11;
67 double rxSensitivity = -106.58; // dBm
68
69 CommandLine cmd(__FILE__);
70
71 cmd.AddValue("txPower", "transmit power (dBm)", txPower);
72 cmd.AddValue("packetSize", "packet (MSDU) size (bytes)", packetSize);
73 cmd.AddValue("channelNumber", "channel number", channelNumber);
74 cmd.AddValue("rxSensitivity", "the rx sensitivity (dBm)", rxSensitivity);
75 cmd.Parse(argc, argv);
76
77 Gnuplot perplot = Gnuplot("802.15.4-per-vs-rxSignal.eps");
78 Gnuplot2dDataset perdatasetExperimental("Experimental");
79 Gnuplot2dDataset perdatasetTheoretical("Theoretical");
80
85
86 dev0->SetAddress(Mac16Address("00:01"));
87 dev1->SetAddress(Mac16Address("00:02"));
90
91 channel->AddPropagationLossModel(propModel);
92 dev0->SetChannel(channel);
93 dev1->SetChannel(channel);
94 n0->AddDevice(dev0);
95 n1->AddDevice(dev1);
97 dev0->GetPhy()->SetMobility(mob0);
99 dev1->GetPhy()->SetMobility(mob1);
100 mob0->SetPosition(Vector(0, 0, 0));
101 mob1->SetPosition(Vector(0, 0, 0));
102
104 Ptr<SpectrumValue> psd = svh.CreateTxPowerSpectralDensity(txPower, channelNumber);
105 dev0->GetPhy()->SetTxPowerSpectralDensity(psd);
106
107 // Set Rx sensitivity of the receiving device
108 dev1->GetPhy()->SetRxSensitivity(rxSensitivity);
109
112 dev1->GetMac()->SetMcpsDataIndicationCallback(cb0);
113
114 //////////////////////////////////
115 // Experimental PER v.s Signal //
116 //////////////////////////////////
117
118 double per = 1;
119 double sensitivityExp = 0;
120 bool sensThreshold = true;
121
122 for (double j = minRxSignal; j < maxRxSignal; j += increment)
123 {
124 propModel->SetRss(j);
125 if (sensThreshold)
126 {
127 sensitivityExp = j;
128 }
129
130 for (int i = 0; i < maxPackets; i++)
131 {
133 params.m_srcAddrMode = SHORT_ADDR;
134 params.m_dstAddrMode = SHORT_ADDR;
135 params.m_dstPanId = 0;
136 params.m_dstAddr = Mac16Address("00:02");
137 params.m_msduHandle = 0;
138 params.m_txOptions = 0;
139 Ptr<Packet> p;
141 Simulator::Schedule(Seconds(i), &LrWpanMac::McpsDataRequest, dev0->GetMac(), params, p);
142 }
143
145
146 per = (static_cast<double>(maxPackets - g_packetsReceived) / maxPackets) * 100;
147
148 std::cout << "Experimental Test || Signal: " << j << " dBm | Received " << g_packetsReceived
149 << " pkts"
150 << "/" << maxPackets << " | PER " << per << " %\n";
151
152 if (per <= 1 && sensThreshold)
153 {
154 sensThreshold = false;
155 }
156
157 perdatasetExperimental.Add(j, per);
159 }
160
161 /////////////////////////////////
162 // Theoretical PER v.s. Signal //
163 /////////////////////////////////
164
167
168 // Calculate the noise that accounts for both thermal noise (floor noise) and
169 // imperfections on the chip or lost before reaching the demodulator.
170 // In O-QPSK 250kbps, the point where PER is <= 1% without
171 // additional noise is -106.58 dBm (Noise Factor = 1)
172 double maxRxSensitivityW = (pow(10.0, -106.58 / 10.0) / 1000.0);
173 long double noiseFactor = (pow(10.0, rxSensitivity / 10.0) / 1000.0) / maxRxSensitivityW;
174
175 psdHelper.SetNoiseFactor(noiseFactor);
177 double noise = LrWpanSpectrumValueHelper::TotalAvgPower(noisePsd, 11);
178
179 double signal = 0;
180 double sensitivityTheo = 0;
181 double perTheoretical = 0;
182 double snr = 0;
183 sensThreshold = true;
184
185 for (double j = minRxSignal; j < maxRxSignal; j += increment)
186 {
187 if (sensThreshold)
188 {
189 sensitivityTheo = j;
190 }
191
192 signal = pow(10.0, j / 10.0) / 1000.0; // signal in Watts
193 snr = signal / noise;
194
195 // According to the standard, Packet error rate should be obtained
196 // using a PSDU of 20 bytes using
197 // the equation PER = 1 - (1 - BER)^nbits
198 perTheoretical = (1.0 - lrWpanError->GetChunkSuccessRate(snr, (packetSize + 13) * 8)) * 100;
199 std::cout << "Theoretical Test || Signal: " << j << " dBm | SNR: " << snr << "| PER "
200 << perTheoretical << " % \n";
201
202 if (perTheoretical <= 1 && sensThreshold)
203 {
204 sensThreshold = false;
205 }
206
207 perdatasetTheoretical.Add(j, perTheoretical);
208 }
209
210 std::cout << "_____________________________________________________________________________\n";
211 std::cout << "Experimental Test || Receiving with a current sensitivity of " << sensitivityExp
212 << " dBm\n";
213 std::cout << "Theoretical Test || Receiving with a current sensitivity of " << sensitivityTheo
214 << " dBm\n";
215 std::cout << "Gnu plot generated.";
216
217 os << "Pkt Payload (MSDU) size = " << packetSize << " bytes | "
218 << "Tx power = " << txPower << " dBm | "
219 << "Rx Sensitivity (Theo) = " << sensitivityTheo << " dBm";
220
221 perplot.AddDataset(perdatasetExperimental);
222 perplot.AddDataset(perdatasetTheoretical);
223
224 perplot.SetTitle(os.str());
225 perplot.SetTerminal("postscript eps color enh \"Times-BoldItalic\"");
226 perplot.SetLegend("Rx signal (dBm)", "Packet Error Rate (%)");
227 perplot.SetExtra("set xrange [-110:-82]\n\
228 set logscale y\n\
229 set yrange [0.000000000001:120]\n\
230 set xtics 2\n\
231 set grid\n\
232 set style line 1 linewidth 5\n\
233 set style line 2 linewidth 3\n\
234 set style increment user\n\
235 set arrow from -110,1 to -82,1 nohead lc 'web-blue' front");
236 perplot.GenerateOutput(perfile);
237 perfile.close();
238
240 return 0;
241}
Parse command-line arguments.
Class to represent a 2D points plot.
Definition gnuplot.h:105
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
Definition gnuplot.h:359
void AddDataset(const GnuplotDataset &dataset)
Definition gnuplot.cc:785
void SetLegend(const std::string &xLegend, const std::string &yLegend)
Definition gnuplot.cc:765
void SetTerminal(const std::string &terminal)
Definition gnuplot.cc:753
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition gnuplot.cc:791
void SetExtra(const std::string &extra)
Definition gnuplot.cc:772
void SetTitle(const std::string &title)
Definition gnuplot.cc:759
This class can contain 16 bit addresses.
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
void McpsDataRequest(McpsDataRequestParams params, Ptr< Packet > p) override
IEEE 802.15.4-2006, section 7.1.1.1 MCPS-DATA.request Request to transfer a MSDU.
This class defines all functions to create spectrum model for LrWpan.
Ptr< SpectrumValue > CreateNoisePowerSpectralDensity(uint32_t channel)
create spectrum value for noise
static double TotalAvgPower(Ptr< const SpectrumValue > psd, uint32_t channel)
total average power of the signal is the integral of the PSD using the limits of the given channel
Ptr< SpectrumValue > CreateTxPowerSpectralDensity(double txPower, uint32_t channel)
create spectrum value
void SetNoiseFactor(double f)
Set the noise factor added to the thermal noise.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
uint32_t g_packetsReceived
number of packets received
void PacketReceivedCallback(McpsDataIndicationParams params, Ptr< Packet > p)
Function called when a Data indication is invoked.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition callback.h:684
LogLevel
Logging severity classes and levels.
Definition log.h:83
@ LOG_PREFIX_FUNC
Prefix all trace prints with function.
Definition log.h:107
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition log.h:108
@ LOG_PREFIX_NODE
Prefix all trace prints with simulation node.
Definition log.h:109
void LogComponentEnableAll(LogLevel level)
Enable the logging output for all registered log components.
Definition log.cc:309
channel
Definition third.py:77
params
Fit Fluctuating Two Ray model to the 3GPP TR 38.901 using the Anderson-Darling goodness-of-fit ##.
static const uint32_t packetSize
Packet size generated at the AP.