A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
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
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
57
SpectrumAnalyzer::DoDispose
()
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;
66
SpectrumPhy::DoDispose
();
67
}
68
69
TypeId
70
SpectrumAnalyzer::GetTypeId
(
void
)
71
{
72
static
TypeId
tid =
TypeId
(
"ns3::SpectrumAnalyzer"
)
73
.
SetParent
<
SpectrumPhy
> ()
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>
96
SpectrumAnalyzer::GetDevice
()
97
{
98
return
m_netDevice
;
99
}
100
101
102
Ptr<MobilityModel>
103
SpectrumAnalyzer::GetMobility
()
104
{
105
return
m_mobility
;
106
}
107
108
109
Ptr<const SpectrumModel>
110
SpectrumAnalyzer::GetRxSpectrumModel
()
const
111
{
112
return
m_spectrumModel
;
113
}
114
115
void
116
SpectrumAnalyzer::SetDevice
(
Ptr<NetDevice>
d)
117
{
118
NS_LOG_FUNCTION
(
this
<< d);
119
m_netDevice
= d;
120
}
121
122
123
void
124
SpectrumAnalyzer::SetMobility
(
Ptr<MobilityModel>
m)
125
{
126
NS_LOG_FUNCTION
(
this
<< m);
127
m_mobility
= m;
128
}
129
130
131
void
132
SpectrumAnalyzer::SetChannel
(
Ptr<SpectrumChannel>
c)
133
{
134
NS_LOG_FUNCTION
(
this
<< c);
135
m_channel
= c;
136
}
137
138
139
Ptr<AntennaModel>
140
SpectrumAnalyzer::GetRxAntenna
()
141
{
142
return
m_antenna
;
143
}
144
145
void
146
SpectrumAnalyzer::SetAntenna
(
Ptr<AntennaModel>
a)
147
{
148
NS_LOG_FUNCTION
(
this
<< a);
149
m_antenna
= a;
150
}
151
152
153
154
void
155
SpectrumAnalyzer::StartRx
(
Ptr<SpectrumSignalParameters>
params)
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
219
SpectrumAnalyzer::SetRxSpectrumModel
(
Ptr<SpectrumModel>
f)
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
233
SpectrumAnalyzer::Start
()
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
246
SpectrumAnalyzer::Stop
()
247
{
248
m_active
=
false
;
249
}
250
251
}
// namespace ns3
src
spectrum
model
spectrum-analyzer.cc
Generated on Tue May 14 2013 11:08:32 for ns-3 by
1.8.1.2