A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
spectrum-analyzer.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 CTTC
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 * Author: Nicola Baldo <nbaldo@cttc.es>
18 */
19
20#include "spectrum-analyzer.h"
21
22#include <ns3/antenna-model.h>
23#include <ns3/double.h>
24#include <ns3/log.h>
25#include <ns3/object-factory.h>
26#include <ns3/simulator.h>
27#include <ns3/trace-source-accessor.h>
28
29namespace ns3
30{
31
32NS_LOG_COMPONENT_DEFINE("SpectrumAnalyzer");
33
34NS_OBJECT_ENSURE_REGISTERED(SpectrumAnalyzer);
35
37 : m_mobility(nullptr),
38 m_netDevice(nullptr),
39 m_channel(nullptr),
40 m_spectrumModel(nullptr),
41 m_sumPowerSpectralDensity(nullptr),
42 m_resolution(MilliSeconds(50)),
43 m_active(false)
44{
45 NS_LOG_FUNCTION(this);
46}
47
49{
50 NS_LOG_FUNCTION(this);
51}
52
53void
55{
56 NS_LOG_FUNCTION(this);
57 m_mobility = nullptr;
58 m_netDevice = nullptr;
59 m_channel = nullptr;
60 m_spectrumModel = nullptr;
64}
65
68{
69 static TypeId tid =
70 TypeId("ns3::SpectrumAnalyzer")
72 .SetGroupName("Spectrum")
73 .AddConstructor<SpectrumAnalyzer>()
74 .AddAttribute("Resolution",
75 "The length of the time interval over which the "
76 "power spectral density of incoming signals is averaged",
80 .AddAttribute("NoisePowerSpectralDensity",
81 "The power spectral density of the measuring instrument "
82 "noise, in Watt/Hz. Mostly useful to make spectrograms "
83 "look more similar to those obtained by real devices. "
84 "Defaults to the value for thermal noise at 300K.",
85 DoubleValue(1.38e-23 * 300),
87 MakeDoubleChecker<double>())
88 .AddTraceSource("AveragePowerSpectralDensityReport",
89 "Trace fired whenever a new value for the average "
90 "Power Spectral Density is calculated",
93 "ns3::SpectrumValue::TracedCallback");
94 return tid;
95}
96
99{
100 return m_netDevice;
101}
102
105{
106 return m_mobility;
107}
108
111{
112 return m_spectrumModel;
113}
114
115void
117{
118 NS_LOG_FUNCTION(this << d);
119 m_netDevice = d;
120}
121
122void
124{
125 NS_LOG_FUNCTION(this << m);
126 m_mobility = m;
127}
128
129void
131{
132 NS_LOG_FUNCTION(this << c);
133 m_channel = c;
134}
135
138{
139 return m_antenna;
140}
141
142void
144{
145 NS_LOG_FUNCTION(this << a);
146 m_antenna = a;
147}
148
149void
151{
152 NS_LOG_FUNCTION(this << params);
153 AddSignal(params->psd);
154 Simulator::Schedule(params->duration, &SpectrumAnalyzer::SubtractSignal, this, params->psd);
155}
156
157void
159{
160 NS_LOG_FUNCTION(this << *psd);
162 (*m_sumPowerSpectralDensity) += (*psd);
163}
164
165void
167{
168 NS_LOG_FUNCTION(this << *psd);
170 (*m_sumPowerSpectralDensity) -= (*psd);
171}
172
173void
175{
176 NS_LOG_FUNCTION(this);
177 if (m_lastChangeTime < Now())
178 {
179 (*m_energySpectralDensity) +=
180 (*m_sumPowerSpectralDensity) * ((Now() - m_lastChangeTime).GetSeconds());
182 }
183 else
184 {
186 }
187}
188
189void
191{
192 NS_LOG_FUNCTION(this);
193
195 Ptr<SpectrumValue> avgPowerSpectralDensity =
196 Create<SpectrumValue>(m_sumPowerSpectralDensity->GetSpectrumModel());
197 (*avgPowerSpectralDensity) = (*m_energySpectralDensity) / m_resolution.GetSeconds();
198 (*avgPowerSpectralDensity) += m_noisePowerSpectralDensity;
199 (*m_energySpectralDensity) = 0;
200
201 NS_LOG_INFO("generating report");
202 m_averagePowerSpectralDensityReportTrace(avgPowerSpectralDensity);
203
204 *avgPowerSpectralDensity = 0;
205
206 if (m_active)
207 {
209 }
210}
211
212void
214{
215 NS_LOG_FUNCTION(this << f);
216 m_spectrumModel = f;
218 m_sumPowerSpectralDensity = Create<SpectrumValue>(f);
219 m_energySpectralDensity = Create<SpectrumValue>(f);
221}
222
223void
225{
226 NS_LOG_FUNCTION(this);
227 if (!m_active)
228 {
229 NS_LOG_LOGIC("activating");
230 m_active = true;
232 }
233}
234
235void
237{
238 m_active = false;
239}
240
241} // namespace ns3
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
virtual void DoDispose()
Destructor implementation.
Definition: object.cc:444
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
Simple SpectrumPhy implementation that averages the spectrum power density of incoming transmissions ...
TracedCallback< Ptr< const SpectrumValue > > m_averagePowerSpectralDensityReportTrace
TracedCallback - average power spectral density report.
void SetRxSpectrumModel(Ptr< SpectrumModel > m)
Set the spectrum model used by the SpectrumAnalyzer to represent incoming signals.
virtual void Start()
Start the spectrum analyzer.
static TypeId GetTypeId()
Get the type ID.
void DoDispose() override
Destructor implementation.
double m_noisePowerSpectralDensity
Noise power spectral density.
void SetAntenna(Ptr< AntennaModel > a)
Set the AntennaModel to be used.
virtual void Stop()
Stop the spectrum analyzer.
void SetMobility(Ptr< MobilityModel > m) override
Set the mobility model associated with this device.
Ptr< Object > GetAntenna() const override
Get the AntennaModel used by this SpectrumPhy instance for transmission and/or reception.
void StartRx(Ptr< SpectrumSignalParameters > params) override
Notify the SpectrumPhy instance of an incoming signal.
bool m_active
True if the analyzer is active.
Ptr< SpectrumChannel > m_channel
Pointer to the channel to be analyzed.
Time m_resolution
Time resolution.
Ptr< AntennaModel > m_antenna
Pointer to the Antenna model.
Ptr< NetDevice > GetDevice() const override
Get the associated NetDevice instance.
Ptr< MobilityModel > GetMobility() const override
Get the associated MobilityModel instance.
virtual void GenerateReport()
Generates a report of the data collected so far.
Ptr< SpectrumModel > m_spectrumModel
Spectrum model.
Time m_lastChangeTime
When the last update happened.
void AddSignal(Ptr< const SpectrumValue > psd)
Adds a signal to the data collected.
Ptr< SpectrumValue > m_sumPowerSpectralDensity
Sum of the received PSD.
Ptr< const SpectrumModel > GetRxSpectrumModel() const override
Ptr< SpectrumValue > m_energySpectralDensity
Energy spectral density.
void UpdateEnergyReceivedSoFar()
Updates the data about the received Energy.
void SetChannel(Ptr< SpectrumChannel > c) override
Set the channel attached to this device.
void SubtractSignal(Ptr< const SpectrumValue > psd)
Removes a signal to the data collected.
Ptr< MobilityModel > m_mobility
Pointer to the mobility model.
void SetDevice(Ptr< NetDevice > d) override
Set the associated NetDevice instance.
Ptr< NetDevice > m_netDevice
Pointer to the NetDevice using this object.
Abstract base class for Spectrum-aware PHY layers.
Definition: spectrum-phy.h:46
Ptr< const SpectrumModel > GetSpectrumModel() const
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:403
AttributeValue implementation for Time.
Definition: nstime.h:1413
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Definition: double.h:43
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition: nstime.h:1434
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition: nstime.h:1414
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Time Now()
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:305
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Every class exported by the ns3 library is enclosed in the ns3 namespace.