A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
spectrum-analyzer-helper.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 CTTC
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Nicola Baldo <nbaldo@cttc.es>
7 */
9
10#include "ns3/antenna-model.h"
11#include "ns3/config.h"
12#include "ns3/log.h"
13#include "ns3/mobility-model.h"
14#include "ns3/names.h"
15#include "ns3/non-communicating-net-device.h"
16#include "ns3/output-stream-wrapper.h"
17#include "ns3/propagation-delay-model.h"
18#include "ns3/simulator.h"
19#include "ns3/spectrum-analyzer.h"
20#include "ns3/spectrum-channel.h"
21#include "ns3/spectrum-propagation-loss-model.h"
22#include "ns3/trace-helper.h"
23
24namespace ns3
25{
26
27NS_LOG_COMPONENT_DEFINE("SpectrumAnalyzerHelper");
28
29/**
30 * \ingroup spectrum
31 * \brief Writes a report of the Average Power Spectral Density
32 * \param streamWrapper the wrapper to the output stream
33 * \param avgPowerSpectralDensity Average Power Spectral Density
34 */
35static void
37 Ptr<const SpectrumValue> avgPowerSpectralDensity)
38{
39 NS_LOG_FUNCTION(streamWrapper << avgPowerSpectralDensity);
40 std::ostream* ostream = streamWrapper->GetStream();
41 if (ostream->good())
42 {
43 auto fi = avgPowerSpectralDensity->ConstBandsBegin();
44 auto vi = avgPowerSpectralDensity->ConstValuesBegin();
45 while (fi != avgPowerSpectralDensity->ConstBandsEnd())
46 {
47 NS_ASSERT(vi != avgPowerSpectralDensity->ConstValuesEnd());
48 *ostream << Now().GetSeconds() << " " << fi->fc << " " << *vi << std::endl;
49 ++fi;
50 ++vi;
51 }
52 // An additional line separates different spectrums sweeps
53 *ostream << std::endl;
54 }
55}
56
58{
59 NS_LOG_FUNCTION(this);
60 m_phy.SetTypeId("ns3::SpectrumAnalyzer");
61 m_device.SetTypeId("ns3::NonCommunicatingNetDevice");
62 m_antenna.SetTypeId("ns3::IsotropicAntennaModel");
63}
64
69
70void
76
77void
78SpectrumAnalyzerHelper::SetChannel(std::string channelName)
79{
80 NS_LOG_FUNCTION(this);
82 m_channel = channel;
83}
84
85void
87{
88 NS_LOG_FUNCTION(this);
89 m_phy.Set(name, v);
90}
91
92void
94{
95 NS_LOG_FUNCTION(this);
96 m_device.Set(name, v);
97}
98
99void
105
106void
108{
109 NS_LOG_FUNCTION(this);
110 m_prefix = prefix;
111}
112
115{
116 NS_LOG_FUNCTION(this);
117 NetDeviceContainer devices;
118 for (auto i = c.Begin(); i != c.End(); ++i)
119 {
120 Ptr<Node> node = *i;
121
124
126 NS_ASSERT(phy);
127
128 dev->SetPhy(phy);
129
130 NS_ASSERT(node);
131 phy->SetMobility(node->GetObject<MobilityModel>());
132
133 NS_ASSERT(dev);
134 phy->SetDevice(dev);
135
137 "you forgot to call SpectrumAnalyzerHelper::SetRxSpectrumModel ()");
138 phy->SetRxSpectrumModel(m_rxSpectrumModel);
139
140 NS_ASSERT_MSG(m_channel, "you forgot to call SpectrumAnalyzerHelper::SetChannel ()");
141 m_channel->AddRx(phy);
142
143 dev->SetChannel(m_channel);
144
145 Ptr<AntennaModel> antenna = (m_antenna.Create())->GetObject<AntennaModel>();
146 NS_ASSERT_MSG(antenna, "error in creating the AntennaModel object");
147 phy->SetAntenna(antenna);
148
149 uint32_t devId = node->AddDevice(dev);
150 devices.Add(dev);
151
152 if (!m_prefix.empty())
153 {
154 NS_LOG_LOGIC("creating new output stream and binding it to the callback");
155 AsciiTraceHelper asciiTraceHelper;
156 std::string filename;
157 filename = asciiTraceHelper.GetFilenameFromDevice(m_prefix, dev);
158 Ptr<OutputStreamWrapper> stream = asciiTraceHelper.CreateFileStream(filename);
159
160 // note that we don't use AsciiTraceHelper to connect the trace sink, since we use a
161 // custom trace sink
162
163 // the following is inspired from YansWifiPhyHelper::EnableAsciiInternal
164 std::ostringstream oss;
165 oss.str("");
166 oss << "/NodeList/" << node->GetId() << "/DeviceList/" << devId
167 << "/$ns3::NonCommunicatingNetDevice/Phy/AveragePowerSpectralDensityReport";
169 oss.str(),
171
172 phy->Start();
173 }
174 }
175 return devices;
176}
177
180{
181 NS_LOG_FUNCTION(this);
182 return Install(NodeContainer(node));
183}
184
186SpectrumAnalyzerHelper::Install(std::string nodeName) const
187{
188 NS_LOG_FUNCTION(this);
189 Ptr<Node> node = Names::Find<Node>(nodeName);
190 return Install(node);
191}
192
193} // namespace ns3
Manage ASCII trace files for device models.
std::string GetFilenameFromDevice(std::string prefix, Ptr< NetDevice > device, bool useObjectNames=true)
Let the ascii trace helper figure out a reasonable filename to use for an ascii trace file associated...
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits.
Hold a value for an Attribute.
Definition attribute.h:59
Keep track of the current position and velocity of an object.
static Ptr< T > Find(std::string path)
Given a name path string, look to see if there's an object in the system with that associated to it.
Definition names.h:443
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
Iterator End() const
Get an iterator which indicates past-the-last Node in the container.
Iterator Begin() const
Get an iterator which refers to the first Node in the container.
This class implements a device which does not communicate, in the sense that it does not interact wit...
Ptr< Object > Create() const
Create an Object instance of the configured TypeId.
void Set(const std::string &name, const AttributeValue &value, Args &&... args)
Set an attribute to be set during construction.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition object.h:511
Smart pointer class similar to boost::intrusive_ptr.
ObjectFactory m_phy
Object factory for the phy objects.
NetDeviceContainer Install(NodeContainer c) const
Ptr< SpectrumModel > m_rxSpectrumModel
Spectrum model.
void SetPhyAttribute(std::string name, const AttributeValue &v)
void SetDeviceAttribute(std::string n1, const AttributeValue &v1)
void SetChannel(Ptr< SpectrumChannel > channel)
Set the SpectrumChannel that will be used by SpectrumPhy instances created by this helper.
ObjectFactory m_antenna
Object factory for the Antenna objects.
std::string m_prefix
Prefix for the output files.
void EnableAsciiAll(std::string prefix)
Enable ASCII output.
Ptr< SpectrumChannel > m_channel
Channel.
ObjectFactory m_device
Object factory for the NetDevice objects.
void SetRxSpectrumModel(Ptr< SpectrumModel > m)
Set the spectrum model used by the created SpectrumAnalyzer instances to represent incoming signals.
Simple SpectrumPhy implementation that averages the spectrum power density of incoming transmissions ...
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:392
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition assert.h:75
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition config.cc:943
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:271
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
auto MakeBoundCallback(R(*fnPtr)(Args...), BArgs &&... bargs)
Make Callbacks with varying number of bound arguments.
Definition callback.h:745
Time Now()
create an ns3::Time instance which contains the current simulation time.
Definition simulator.cc:294
static void WriteAveragePowerSpectralDensityReport(Ptr< OutputStreamWrapper > streamWrapper, Ptr< const SpectrumValue > avgPowerSpectralDensity)
Writes a report of the Average Power Spectral Density.
Every class exported by the ns3 library is enclosed in the ns3 namespace.