A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
spectrum-analyzer.cc
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 SpectrumAnalyzer::SpectrumAnalyzer ()
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 
51 SpectrumAnalyzer::~SpectrumAnalyzer ()
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;
64  m_sumPowerSpectralDensity = 0;
65  m_energySpectralDensity = 0;
67 }
68 
69 TypeId
70 SpectrumAnalyzer::GetTypeId (void)
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),
84  MakeDoubleAccessor (&SpectrumAnalyzer::m_noisePowerSpectralDensity),
85  MakeDoubleChecker<double> ())
86  .AddTraceSource ("AveragePowerSpectralDensityReport",
87  "Trace fired whenever a new value for the average Power Spectral Density is calculated",
88  MakeTraceSourceAccessor (&SpectrumAnalyzer::m_averagePowerSpectralDensityReportTrace))
89  ;
90  return tid;
91 }
92 
93 
94 
95 Ptr<NetDevice>
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
164 SpectrumAnalyzer::AddSignal (Ptr<const SpectrumValue> psd)
165 {
166  NS_LOG_FUNCTION (this << *psd);
167  UpdateEnergyReceivedSoFar ();
168  (*m_sumPowerSpectralDensity) += (*psd);
169 }
170 
171 void
172 SpectrumAnalyzer::SubtractSignal (Ptr<const SpectrumValue> psd)
173 {
174  NS_LOG_FUNCTION (this << *psd);
175  UpdateEnergyReceivedSoFar ();
176  (*m_sumPowerSpectralDensity) -= (*psd);
177 }
178 
179 void
180 SpectrumAnalyzer::UpdateEnergyReceivedSoFar ()
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  {
190  NS_ASSERT (m_lastChangeTime == Now ());
191  }
192 }
193 
194 void
195 SpectrumAnalyzer::GenerateReport ()
196 {
197  NS_LOG_FUNCTION (this);
198 
199  UpdateEnergyReceivedSoFar ();
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  {
212  Simulator::Schedule (m_resolution, &SpectrumAnalyzer::GenerateReport, this);
213  }
214 }
215 
216 
217 
218 void
220 {
221  NS_LOG_FUNCTION (this << f);
222  m_spectrumModel = f;
223  NS_ASSERT (!m_sumPowerSpectralDensity);
224  m_sumPowerSpectralDensity = Create<SpectrumValue> (f);
225  m_energySpectralDensity = Create<SpectrumValue> (f);
226  NS_ASSERT (m_sumPowerSpectralDensity);
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;
240  Simulator::Schedule (m_resolution, &SpectrumAnalyzer::GenerateReport, this);
241  }
242 }
243 
244 
245 void
247 {
248  m_active = false;
249 }
250 
251 } // namespace ns3
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:311
void StartRx(Ptr< SpectrumSignalParameters > params)
Ptr< NetDevice > GetDevice()
#define NS_ASSERT(condition)
Definition: assert.h:64
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
virtual void DoDispose(void)
Definition: object.cc:335
#define NS_LOG_INFO(msg)
Definition: log.h:264
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Definition: simulator.h:820
void SetChannel(Ptr< SpectrumChannel > c)
double GetSeconds(void) const
Definition: nstime.h:262
hold objects of type ns3::Time
Definition: nstime.h:700
void SetRxSpectrumModel(Ptr< SpectrumModel > m)
#define NS_LOG_LOGIC(msg)
Definition: log.h:334
void SetDevice(Ptr< NetDevice > d)
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:286
Ptr< MobilityModel > GetMobility()
void SetMobility(Ptr< MobilityModel > m)
Time MilliSeconds(uint64_t ms)
create ns3::Time instances in units of milliseconds.
Definition: nstime.h:601
Ptr< const SpectrumModel > GetRxSpectrumModel() const
Ptr< AntennaModel > GetRxAntenna()
Hold an floating point type.
Definition: double.h:41
a unique identifier for an interface.
Definition: type-id.h:44
TypeId SetParent(TypeId tid)
Definition: type-id.cc:471
void SetAntenna(Ptr< AntennaModel > a)