OLD | NEW |
(Empty) | |
| 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 #include <ns3/spectrum-analyzer.h> |
| 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 |
| 28 NS_LOG_COMPONENT_DEFINE ("SpectrumAnalyzer"); |
| 29 |
| 30 namespace ns3 { |
| 31 |
| 32 NS_OBJECT_ENSURE_REGISTERED (SpectrumAnalyzer); |
| 33 |
| 34 SpectrumAnalyzer::SpectrumAnalyzer () |
| 35 : m_mobility (0), |
| 36 m_netDevice (0), |
| 37 m_channel (0), |
| 38 m_spectrumModel (0), |
| 39 m_sumPowerSpectralDensity (0),···· |
| 40 m_resolution (MilliSeconds (50)), |
| 41 m_active (false) |
| 42 { |
| 43 NS_LOG_FUNCTION (this); |
| 44 } |
| 45 |
| 46 |
| 47 |
| 48 SpectrumAnalyzer::~SpectrumAnalyzer () |
| 49 { |
| 50 NS_LOG_FUNCTION (this); |
| 51 } |
| 52 |
| 53 |
| 54 TypeId |
| 55 SpectrumAnalyzer::GetTypeId (void) |
| 56 { |
| 57 static TypeId tid = TypeId ("ns3::SpectrumAnalyzer") |
| 58 .SetParent<SpectrumPhy> () |
| 59 .AddConstructor<SpectrumAnalyzer> () |
| 60 .AddAttribute ("Resolution", |
| 61 "the lengh of the time interval over which the power spectral
" |
| 62 "density of incoming signals is averaged", |
| 63 TimeValue (MilliSeconds (1)), |
| 64 MakeTimeAccessor (&SpectrumAnalyzer::m_resolution), |
| 65 MakeTimeChecker ()) |
| 66 .AddAttribute ("NoisePowerSpectralDensity", |
| 67 "the power spectral density of the measuring instrument noise
, in Watt/Hz. Mostly useful to make spectrograms look more similar to those obta
ined by real devices. Defaults to the value for thermal noise at 300K.", |
| 68 DoubleValue (1.38e-23*300), |
| 69 MakeDoubleAccessor (&SpectrumAnalyzer::m_noisePowerSpectralDe
nsity), |
| 70 MakeDoubleChecker<double> ()) |
| 71 .AddTraceSource ("AveragePowerSpectralDensityReport", |
| 72 "Trace fired whenever a new value for the average Power Spe
ctral Density is calculated", |
| 73 MakeTraceSourceAccessor (&SpectrumAnalyzer::m_averagePowerS
pectralDensityReportTrace)) |
| 74 ; |
| 75 return tid; |
| 76 } |
| 77 |
| 78 |
| 79 |
| 80 Ptr<Object> |
| 81 SpectrumAnalyzer::GetDevice () |
| 82 { |
| 83 return m_netDevice; |
| 84 } |
| 85 |
| 86 |
| 87 Ptr<Object> |
| 88 SpectrumAnalyzer::GetMobility () |
| 89 { |
| 90 return m_mobility; |
| 91 } |
| 92 |
| 93 |
| 94 Ptr<const SpectrumModel>· |
| 95 SpectrumAnalyzer::GetRxSpectrumModel () const |
| 96 { |
| 97 return m_spectrumModel; |
| 98 } |
| 99 |
| 100 void |
| 101 SpectrumAnalyzer::SetDevice (Ptr<Object> d) |
| 102 { |
| 103 NS_LOG_FUNCTION (this << d); |
| 104 m_netDevice = d; |
| 105 } |
| 106 |
| 107 |
| 108 void |
| 109 SpectrumAnalyzer::SetMobility (Ptr<Object> m) |
| 110 { |
| 111 NS_LOG_FUNCTION (this << m); |
| 112 m_mobility = m; |
| 113 } |
| 114 |
| 115 |
| 116 void |
| 117 SpectrumAnalyzer::SetChannel (Ptr<SpectrumChannel> c) |
| 118 { |
| 119 NS_LOG_FUNCTION (this << c); |
| 120 m_channel = c; |
| 121 } |
| 122 |
| 123 |
| 124 |
| 125 |
| 126 void |
| 127 SpectrumAnalyzer::StartRx (Ptr<Packet> p,· |
| 128 Ptr <const SpectrumValue> rxPowerSpectralDensity,· |
| 129 SpectrumType st,· |
| 130 Time duration) |
| 131 { |
| 132 NS_LOG_FUNCTION ( this << st << duration << *rxPowerSpectralDensity); |
| 133 AddSignal (rxPowerSpectralDensity); |
| 134 Simulator::Schedule (duration, &SpectrumAnalyzer::SubtractSignal, this, rxPowe
rSpectralDensity); |
| 135 } |
| 136 |
| 137 |
| 138 void |
| 139 SpectrumAnalyzer::AddSignal (Ptr<const SpectrumValue> psd) |
| 140 { |
| 141 NS_LOG_FUNCTION (this << *psd); |
| 142 UpdateEnergyReceivedSoFar (); |
| 143 (*m_sumPowerSpectralDensity) += (*psd); |
| 144 } |
| 145 |
| 146 void |
| 147 SpectrumAnalyzer::SubtractSignal (Ptr<const SpectrumValue> psd) |
| 148 {· |
| 149 NS_LOG_FUNCTION (this << *psd); |
| 150 UpdateEnergyReceivedSoFar (); |
| 151 (*m_sumPowerSpectralDensity) -= (*psd); |
| 152 } |
| 153 |
| 154 void |
| 155 SpectrumAnalyzer::UpdateEnergyReceivedSoFar () |
| 156 { |
| 157 NS_LOG_FUNCTION (this); |
| 158 if (m_lastChangeTime < Now ()) |
| 159 { |
| 160 (*m_energySpectralDensity) += (*m_sumPowerSpectralDensity) * ((Now () - m_
lastChangeTime).GetSeconds ()); |
| 161 m_lastChangeTime = Now (); |
| 162 } |
| 163 else |
| 164 { |
| 165 NS_ASSERT (m_lastChangeTime == Now ()); |
| 166 } |
| 167 } |
| 168 |
| 169 void |
| 170 SpectrumAnalyzer::GenerateReport () |
| 171 { |
| 172 NS_LOG_FUNCTION (this); |
| 173 |
| 174 UpdateEnergyReceivedSoFar (); |
| 175 Ptr<SpectrumValue> avgPowerSpectralDensity = Create<SpectrumValue> (m_sumPower
SpectralDensity->GetSpectrumModel ()); |
| 176 (*avgPowerSpectralDensity) = (*m_energySpectralDensity) / m_resolution.GetSeco
nds ();·· |
| 177 (*avgPowerSpectralDensity) += m_noisePowerSpectralDensity; |
| 178 (*m_energySpectralDensity) = 0; |
| 179 |
| 180 NS_LOG_INFO ("generating report"); |
| 181 m_averagePowerSpectralDensityReportTrace (avgPowerSpectralDensity); |
| 182 |
| 183 *avgPowerSpectralDensity = 0; |
| 184 |
| 185 if (m_active) |
| 186 { |
| 187 Simulator::Schedule (m_resolution, &SpectrumAnalyzer::GenerateReport, this
); |
| 188 } |
| 189 } |
| 190 |
| 191 |
| 192 |
| 193 void |
| 194 SpectrumAnalyzer::SetRxSpectrumModel (Ptr<SpectrumModel> f) |
| 195 { |
| 196 NS_LOG_FUNCTION (this << f); |
| 197 m_spectrumModel = f; |
| 198 NS_ASSERT (!m_sumPowerSpectralDensity); |
| 199 m_sumPowerSpectralDensity = Create<SpectrumValue> (f); |
| 200 m_energySpectralDensity = Create<SpectrumValue> (f); |
| 201 NS_ASSERT (m_sumPowerSpectralDensity); |
| 202 } |
| 203 |
| 204 |
| 205 |
| 206 |
| 207 void |
| 208 SpectrumAnalyzer::Start () |
| 209 { |
| 210 NS_LOG_FUNCTION (this); |
| 211 if (!m_active) |
| 212 { |
| 213 NS_LOG_LOGIC ("activating"); |
| 214 m_active = true; |
| 215 Simulator::Schedule (m_resolution, &SpectrumAnalyzer::GenerateReport, this
); |
| 216 } |
| 217 } |
| 218 |
| 219 |
| 220 void |
| 221 SpectrumAnalyzer::Stop () |
| 222 { |
| 223 m_active = false; |
| 224 } |
| 225 |
| 226 } // namespace ns3 |
OLD | NEW |