A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
spectrum-analyzer.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 CTTC
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Nicola Baldo <nbaldo@cttc.es>
19  */
20 
21 
22 #include <ns3/object-factory.h>
23 #include <ns3/log.h>
24 #include <ns3/double.h>
25 #include <ns3/simulator.h>
26 #include <ns3/trace-source-accessor.h>
27 #include <ns3/antenna-model.h>
28 
29 #include "spectrum-analyzer.h"
30 
31 NS_LOG_COMPONENT_DEFINE ("SpectrumAnalyzer");
32 
33 namespace ns3 {
34 
35 NS_OBJECT_ENSURE_REGISTERED (SpectrumAnalyzer);
36 
38  : m_mobility (0),
39  m_netDevice (0),
40  m_channel (0),
41  m_spectrumModel (0),
42  m_sumPowerSpectralDensity (0),
43  m_resolution (MilliSeconds (50)),
44  m_active (false)
45 {
46  NS_LOG_FUNCTION (this);
47 }
48 
49 
50 
52 {
53  NS_LOG_FUNCTION (this);
54 }
55 
56 void
58 {
59  NS_LOG_FUNCTION (this);
60  m_mobility = 0;
61  m_netDevice = 0;
62  m_channel = 0;
63  m_spectrumModel = 0;
67 }
68 
69 TypeId
71 {
72  static TypeId tid = TypeId ("ns3::SpectrumAnalyzer")
74  .AddConstructor<SpectrumAnalyzer> ()
75  .AddAttribute ("Resolution",
76  "the lengh of the time interval over which the power spectral "
77  "density of incoming signals is averaged",
78  TimeValue (MilliSeconds (1)),
79  MakeTimeAccessor (&SpectrumAnalyzer::m_resolution),
80  MakeTimeChecker ())
81  .AddAttribute ("NoisePowerSpectralDensity",
82  "the power spectral density of the measuring instrument noise, in Watt/Hz. Mostly useful to make spectrograms look more similar to those obtained by real devices. Defaults to the value for thermal noise at 300K.",
83  DoubleValue (1.38e-23 * 300),
85  MakeDoubleChecker<double> ())
86  .AddTraceSource ("AveragePowerSpectralDensityReport",
87  "Trace fired whenever a new value for the average Power Spectral Density is calculated",
89  ;
90  return tid;
91 }
92 
93 
94 
97 {
98  return m_netDevice;
99 }
100 
101 
104 {
105  return m_mobility;
106 }
107 
108 
111 {
112  return m_spectrumModel;
113 }
114 
115 void
117 {
118  NS_LOG_FUNCTION (this << d);
119  m_netDevice = d;
120 }
121 
122 
123 void
125 {
126  NS_LOG_FUNCTION (this << m);
127  m_mobility = m;
128 }
129 
130 
131 void
133 {
134  NS_LOG_FUNCTION (this << c);
135  m_channel = c;
136 }
137 
138 
141 {
142  return m_antenna;
143 }
144 
145 void
147 {
148  NS_LOG_FUNCTION (this << a);
149  m_antenna = a;
150 }
151 
152 
153 
154 void
156 {
157  NS_LOG_FUNCTION ( this << params);
158  AddSignal (params->psd);
159  Simulator::Schedule (params->duration, &SpectrumAnalyzer::SubtractSignal, this, params->psd);
160 }
161 
162 
163 void
165 {
166  NS_LOG_FUNCTION (this << *psd);
168  (*m_sumPowerSpectralDensity) += (*psd);
169 }
170 
171 void
173 {
174  NS_LOG_FUNCTION (this << *psd);
176  (*m_sumPowerSpectralDensity) -= (*psd);
177 }
178 
179 void
181 {
182  NS_LOG_FUNCTION (this);
183  if (m_lastChangeTime < Now ())
184  {
185  (*m_energySpectralDensity) += (*m_sumPowerSpectralDensity) * ((Now () - m_lastChangeTime).GetSeconds ());
186  m_lastChangeTime = Now ();
187  }
188  else
189  {
191  }
192 }
193 
194 void
196 {
197  NS_LOG_FUNCTION (this);
198 
200  Ptr<SpectrumValue> avgPowerSpectralDensity = Create<SpectrumValue> (m_sumPowerSpectralDensity->GetSpectrumModel ());
201  (*avgPowerSpectralDensity) = (*m_energySpectralDensity) / m_resolution.GetSeconds ();
202  (*avgPowerSpectralDensity) += m_noisePowerSpectralDensity;
203  (*m_energySpectralDensity) = 0;
204 
205  NS_LOG_INFO ("generating report");
206  m_averagePowerSpectralDensityReportTrace (avgPowerSpectralDensity);
207 
208  *avgPowerSpectralDensity = 0;
209 
210  if (m_active)
211  {
213  }
214 }
215 
216 
217 
218 void
220 {
221  NS_LOG_FUNCTION (this << f);
222  m_spectrumModel = f;
224  m_sumPowerSpectralDensity = Create<SpectrumValue> (f);
225  m_energySpectralDensity = Create<SpectrumValue> (f);
227 }
228 
229 
230 
231 
232 void
234 {
235  NS_LOG_FUNCTION (this);
236  if (!m_active)
237  {
238  NS_LOG_LOGIC ("activating");
239  m_active = true;
241  }
242 }
243 
244 
245 void
247 {
248  m_active = false;
249 }
250 
251 } // namespace ns3
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:60
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
Abstract base class for Spectrum-aware PHY layers.
Definition: spectrum-phy.h:45
Ptr< NetDevice > m_netDevice
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register the class in the ns-3 factory.
Definition: object-base.h:38
void StartRx(Ptr< SpectrumSignalParameters > params)
Notify the SpectrumPhy instance of an incoming signal.
Ptr< NetDevice > GetDevice()
get the associated NetDevice instance
Ptr< AntennaModel > m_antenna
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:61
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
virtual void Start()
Start the spectrum analyzer.
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Definition: object.cc:335
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:223
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:825
void SetChannel(Ptr< SpectrumChannel > c)
Set the channel attached to this device.
double GetSeconds(void) const
Definition: nstime.h:272
Ptr< SpectrumChannel > m_channel
hold objects of type ns3::Time
Definition: nstime.h:1008
Ptr< const SpectrumModel > GetSpectrumModel() const
static TypeId GetTypeId(void)
virtual void Stop()
Stop the spectrum analyzer.
void SetRxSpectrumModel(Ptr< SpectrumModel > m)
Set the spectrum model used by the SpectrumAnalyzer to represent incoming signals.
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:233
void DoDispose()
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
void SetDevice(Ptr< NetDevice > d)
set the associated NetDevice instance
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
void AddSignal(Ptr< const SpectrumValue > psd)
TracedCallback< Ptr< const SpectrumValue > > m_averagePowerSpectralDensityReportTrace
Ptr< SpectrumValue > m_sumPowerSpectralDensity
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:287
Ptr< SpectrumModel > m_spectrumModel
Ptr< MobilityModel > GetMobility()
get the associated MobilityModel instance
virtual void GenerateReport()
void SetMobility(Ptr< MobilityModel > m)
Set the mobility model associated with this device.
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:441
Ptr< const SpectrumModel > GetRxSpectrumModel() const
Ptr< AntennaModel > GetRxAntenna()
get the AntennaModel used by the NetDevice for reception
Hold a floating point type.
Definition: double.h:41
void SubtractSignal(Ptr< const SpectrumValue > psd)
a unique identifier for an interface.
Definition: type-id.h:49
Ptr< MobilityModel > m_mobility
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
Ptr< SpectrumValue > m_energySpectralDensity
void SetAntenna(Ptr< AntennaModel > a)
set the AntennaModel to be used