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  ;
37 
39  : m_mobility (0),
40  m_netDevice (0),
41  m_channel (0),
42  m_spectrumModel (0),
43  m_sumPowerSpectralDensity (0),
44  m_resolution (MilliSeconds (50)),
45  m_active (false)
46 {
47  NS_LOG_FUNCTION (this);
48 }
49 
50 
51 
53 {
54  NS_LOG_FUNCTION (this);
55 }
56 
57 void
59 {
60  NS_LOG_FUNCTION (this);
61  m_mobility = 0;
62  m_netDevice = 0;
63  m_channel = 0;
64  m_spectrumModel = 0;
68 }
69 
70 TypeId
72 {
73  static TypeId tid = TypeId ("ns3::SpectrumAnalyzer")
75  .AddConstructor<SpectrumAnalyzer> ()
76  .AddAttribute ("Resolution",
77  "the lengh of the time interval over which the power spectral "
78  "density of incoming signals is averaged",
79  TimeValue (MilliSeconds (1)),
80  MakeTimeAccessor (&SpectrumAnalyzer::m_resolution),
81  MakeTimeChecker ())
82  .AddAttribute ("NoisePowerSpectralDensity",
83  "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.",
84  DoubleValue (1.38e-23 * 300),
86  MakeDoubleChecker<double> ())
87  .AddTraceSource ("AveragePowerSpectralDensityReport",
88  "Trace fired whenever a new value for the average Power Spectral Density is calculated",
90  ;
91  return tid;
92 }
93 
94 
95 
98 {
99  return m_netDevice;
100 }
101 
102 
105 {
106  return m_mobility;
107 }
108 
109 
112 {
113  return m_spectrumModel;
114 }
115 
116 void
118 {
119  NS_LOG_FUNCTION (this << d);
120  m_netDevice = d;
121 }
122 
123 
124 void
126 {
127  NS_LOG_FUNCTION (this << m);
128  m_mobility = m;
129 }
130 
131 
132 void
134 {
135  NS_LOG_FUNCTION (this << c);
136  m_channel = c;
137 }
138 
139 
142 {
143  return m_antenna;
144 }
145 
146 void
148 {
149  NS_LOG_FUNCTION (this << a);
150  m_antenna = a;
151 }
152 
153 
154 
155 void
157 {
158  NS_LOG_FUNCTION ( this << params);
159  AddSignal (params->psd);
160  Simulator::Schedule (params->duration, &SpectrumAnalyzer::SubtractSignal, this, params->psd);
161 }
162 
163 
164 void
166 {
167  NS_LOG_FUNCTION (this << *psd);
169  (*m_sumPowerSpectralDensity) += (*psd);
170 }
171 
172 void
174 {
175  NS_LOG_FUNCTION (this << *psd);
177  (*m_sumPowerSpectralDensity) -= (*psd);
178 }
179 
180 void
182 {
183  NS_LOG_FUNCTION (this);
184  if (m_lastChangeTime < Now ())
185  {
186  (*m_energySpectralDensity) += (*m_sumPowerSpectralDensity) * ((Now () - m_lastChangeTime).GetSeconds ());
187  m_lastChangeTime = Now ();
188  }
189  else
190  {
192  }
193 }
194 
195 void
197 {
198  NS_LOG_FUNCTION (this);
199 
201  Ptr<SpectrumValue> avgPowerSpectralDensity = Create<SpectrumValue> (m_sumPowerSpectralDensity->GetSpectrumModel ());
202  (*avgPowerSpectralDensity) = (*m_energySpectralDensity) / m_resolution.GetSeconds ();
203  (*avgPowerSpectralDensity) += m_noisePowerSpectralDensity;
204  (*m_energySpectralDensity) = 0;
205 
206  NS_LOG_INFO ("generating report");
207  m_averagePowerSpectralDensityReportTrace (avgPowerSpectralDensity);
208 
209  *avgPowerSpectralDensity = 0;
210 
211  if (m_active)
212  {
214  }
215 }
216 
217 
218 
219 void
221 {
222  NS_LOG_FUNCTION (this << f);
223  m_spectrumModel = f;
225  m_sumPowerSpectralDensity = Create<SpectrumValue> (f);
226  m_energySpectralDensity = Create<SpectrumValue> (f);
228 }
229 
230 
231 
232 
233 void
235 {
236  NS_LOG_FUNCTION (this);
237  if (!m_active)
238  {
239  NS_LOG_LOGIC ("activating");
240  m_active = true;
242  }
243 }
244 
245 
246 void
248 {
249  m_active = false;
250 }
251 
252 } // namespace ns3
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
Abstract base class for Spectrum-aware PHY layers.
Definition: spectrum-phy.h:45
Ptr< NetDevice > m_netDevice
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)
Definition: assert.h:64
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
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:336
#define NS_LOG_INFO(msg)
Definition: log.h:298
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:824
void SetChannel(Ptr< SpectrumChannel > c)
Set the channel attached to this device.
double GetSeconds(void) const
Definition: nstime.h:274
Ptr< SpectrumChannel > m_channel
hold objects of type ns3::Time
Definition: nstime.h:961
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.
NS_LOG_COMPONENT_DEFINE("SpectrumAnalyzer")
#define NS_LOG_LOGIC(msg)
Definition: log.h:368
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:452
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:611
Ptr< SpectrumValue > m_energySpectralDensity
void SetAntenna(Ptr< AntennaModel > a)
set the AntennaModel to be used