A Discrete-Event Network Simulator
API
tv-trans-example.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 University of Washington
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Benjamin Cizdziel <ben.cizdziel@gmail.com>
18  */
19 
20 #include <ns3/core-module.h>
21 #include <ns3/mobility-module.h>
22 #include <ns3/spectrum-analyzer-helper.h>
23 #include <ns3/spectrum-helper.h>
24 #include <ns3/tv-spectrum-transmitter-helper.h>
25 
26 #include <iostream>
27 #include <stdlib.h>
28 
29 using namespace ns3;
30 
42 int
43 main(int argc, char** argv)
44 {
45  CommandLine cmd(__FILE__);
46  cmd.Parse(argc, argv);
47 
48  /* nodes and positions */
49  NodeContainer tvTransmitterNodes;
50  NodeContainer spectrumAnalyzerNodes;
51  NodeContainer allNodes;
52  tvTransmitterNodes.Create(2);
53  spectrumAnalyzerNodes.Create(1);
54  allNodes.Add(tvTransmitterNodes);
55  allNodes.Add(spectrumAnalyzerNodes);
57  Ptr<ListPositionAllocator> nodePositionList = CreateObject<ListPositionAllocator>();
58  nodePositionList->Add(
59  Vector(128000.0, 0.0, 0.0)); // TV Transmitter 1; 128 km away from spectrum analyzer
60  nodePositionList->Add(
61  Vector(0.0, 24000.0, 0.0)); // TV Transmitter 2; 24 km away from spectrum analyzer
62  nodePositionList->Add(Vector(0.0, 0.0, 0.0)); // Spectrum Analyzer
63  mobility.SetPositionAllocator(nodePositionList);
64  mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
65  mobility.Install(allNodes);
66 
67  /* channel and propagation */
69  channelHelper.SetChannel("ns3::MultiModelSpectrumChannel");
70  // constant path loss added just to show capability to set different propagation loss models
71  // FriisSpectrumPropagationLossModel already added by default in SpectrumChannelHelper
72  channelHelper.AddSpectrumPropagationLoss("ns3::ConstantSpectrumPropagationLossModel");
73  Ptr<SpectrumChannel> channel = channelHelper.Create();
74 
75  /* TV transmitter setup */
76  TvSpectrumTransmitterHelper tvTransHelper;
77  tvTransHelper.SetChannel(channel);
78  tvTransHelper.SetAttribute("StartFrequency", DoubleValue(524e6));
79  tvTransHelper.SetAttribute("ChannelBandwidth", DoubleValue(6e6));
80  tvTransHelper.SetAttribute("StartingTime", TimeValue(Seconds(0)));
81  tvTransHelper.SetAttribute("TransmitDuration", TimeValue(Seconds(0.2)));
82  // 22.22 dBm/Hz from 1000 kW ERP transmit power, flat 6 MHz PSD spectrum assumed for this
83  // approximation
84  tvTransHelper.SetAttribute("BasePsd", DoubleValue(22.22));
86  tvTransHelper.SetAttribute("Antenna", StringValue("ns3::IsotropicAntennaModel"));
87  tvTransHelper.InstallAdjacent(tvTransmitterNodes);
88 
89  /* frequency range for spectrum analyzer */
90  std::vector<double> freqs;
91  freqs.reserve(200);
92  for (int i = 0; i < 200; ++i)
93  {
94  freqs.push_back((i + 5200) * 1e5);
95  }
96  Ptr<SpectrumModel> spectrumAnalyzerFreqModel = Create<SpectrumModel>(freqs);
97 
98  /* spectrum analyzer setup */
99  SpectrumAnalyzerHelper spectrumAnalyzerHelper;
100  spectrumAnalyzerHelper.SetChannel(channel);
101  spectrumAnalyzerHelper.SetRxSpectrumModel(spectrumAnalyzerFreqModel);
102  spectrumAnalyzerHelper.SetPhyAttribute("NoisePowerSpectralDensity",
103  DoubleValue(1e-15)); // -120 dBm/Hz
104  spectrumAnalyzerHelper.EnableAsciiAll("spectrum-analyzer-tv-sim");
105  NetDeviceContainer spectrumAnalyzerDevices =
106  spectrumAnalyzerHelper.Install(spectrumAnalyzerNodes);
107 
108  Simulator::Stop(Seconds(0.4));
109 
110  Simulator::Run();
111 
113 
114  std::cout << "simulation done!" << std::endl;
115  std::cout << "see spectrum analyzer output file" << std::endl;
116 
117  return 0;
118 }
Parse command-line arguments.
Definition: command-line.h:232
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
Hold variables of type enum.
Definition: enum.h:62
Helper class used to assign positions and mobility models to nodes.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void Add(const NodeContainer &nc)
Append the contents of another NodeContainer to the end of this container.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
Class to allow the Spectrum Analysis.
NetDeviceContainer Install(NodeContainer c) const
void SetPhyAttribute(std::string name, const AttributeValue &v)
void SetChannel(Ptr< SpectrumChannel > channel)
Set the SpectrumChannel that will be used by SpectrumPhy instances created by this helper.
void EnableAsciiAll(std::string prefix)
Enable ASCII output.
void SetRxSpectrumModel(Ptr< SpectrumModel > m)
Set the spectrum model used by the created SpectrumAnalyzer instances to represent incoming signals.
Setup a SpectrumChannel.
Ptr< SpectrumChannel > Create() const
static SpectrumChannelHelper Default()
Setup a default SpectrumChannel.
void AddSpectrumPropagationLoss(std::string name, Ts &&... args)
void SetChannel(std::string type, Ts &&... args)
Hold variables of type string.
Definition: string.h:56
Helper class which uses TvSpectrumTransmitter class to create customizable TV transmitter(s) that tra...
void SetChannel(Ptr< SpectrumChannel > c)
Set the spectrum channel for the device(s) to transmit on.
NetDeviceContainer InstallAdjacent(NodeContainer nodes)
Set up and start the TV Transmitter's transmission on the spectrum channel.
void SetAttribute(std::string name, const AttributeValue &val)
Set attribute for each TvSpectrumTransmitter instance to be created.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:40
channel
Definition: third.py:88
mobility
Definition: third.py:105