A Discrete-Event Network Simulator
API
tv-spectrum-transmitter-test.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014 University of Washington
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: Benjamin Cizdziel <ben.cizdziel@gmail.com>
19  */
20 
21 #include <ns3/test.h>
22 #include <ns3/log.h>
23 #include <ns3/spectrum-value.h>
24 #include <ns3/enum.h>
25 #include <ns3/double.h>
26 #include <ns3/tv-spectrum-transmitter.h>
27 
35 NS_LOG_COMPONENT_DEFINE ("TvSpectrumTransmitterTest");
36 
37 using namespace ns3;
38 
39 const double TOLERANCE = 1e-15;
40 // Bug 2094: Adjust floating point comparison epsilon based on inputs.
41 // Follows http://realtimecollisiondetection.net/blog/?p=89
42 double epsilon;
43 
45 {
46 public:
47  TvSpectrumTransmitterTestCase (double startFrequency,
48  double channelBandwidth,
49  double basePsd,
52 
53 private:
54  virtual void DoRun (void);
55  static std::string Name (TvSpectrumTransmitter::TvType tvType,
56  double startFrequency,
57  double channelBandwidth,
58  double basePsd);
59 
62  double m_basePsd;
64 };
65 
66 
67 std::string
69  double startFrequency,
70  double channelBandwidth,
71  double basePsd)
72 {
73  std::ostringstream oss;
74  oss << "TV type = " << tvType << ", "
75  << "start frequency = " << startFrequency << " Hz, "
76  << "channel bandwidth = " << channelBandwidth << " Hz, "
77  << "base PSD = " << basePsd << " dBm per Hz";
78  return oss.str();
79 }
80 
82  double channelBandwidth,
83  double basePsd,
85  : TestCase (Name (tvType, startFrequency, channelBandwidth, basePsd)),
86  m_startFrequency (startFrequency),
87  m_channelBandwidth (channelBandwidth),
88  m_basePsd (basePsd),
89  m_tvType (tvType)
90 {
91 }
92 
94 {
95 }
96 
97 void
99 {
101 
102  /* TV transmitter setup */
103  Ptr<TvSpectrumTransmitter> phy = CreateObject<TvSpectrumTransmitter>();
104  phy->SetAttribute ("StartFrequency", DoubleValue (m_startFrequency));
105  phy->SetAttribute ("ChannelBandwidth", DoubleValue (m_channelBandwidth));
106  phy->SetAttribute ("BasePsd", DoubleValue (m_basePsd));
107  phy->SetAttribute ("TvType", EnumValue (m_tvType));
108  phy->CreateTvPsd ();
109 
110  /* Test max PSD value */
111  Ptr<SpectrumValue> psd = phy->GetTxPsd ();
112  Values::const_iterator psdIter = psd->ConstValuesBegin ();
113  double maxValue = 0;
114  while (psdIter != psd->ConstValuesEnd ())
115  {
116  if (*psdIter > maxValue)
117  {
118  maxValue = *psdIter;
119  }
120  ++psdIter;
121  }
122  double basePsdWattsHz = pow (10.0, (m_basePsd - 30) / 10.0); // convert dBm to W/Hz
123  if (m_tvType == TvSpectrumTransmitter::TVTYPE_8VSB) // pilot has highest PSD
124  {
125  double expectedPsd = (0.502 * basePsdWattsHz) + (21.577 * basePsdWattsHz);
126  epsilon = TOLERANCE * std::max (1.0, std::max (maxValue, expectedPsd));
127  NS_TEST_ASSERT_MSG_EQ_TOL (maxValue,
128  expectedPsd,
129  epsilon,
130  "peak PSD value (" << maxValue << ") is incorrect");
131  }
132  else // highest PSD is base PSD
133  {
134  epsilon = TOLERANCE * std::max (1.0, std::max (maxValue, basePsdWattsHz));
135  NS_TEST_ASSERT_MSG_EQ_TOL (maxValue,
136  basePsdWattsHz,
137  epsilon,
138  "peak PSD value (" << maxValue << ") is incorrect");
139  }
140 
141  /* Test frequency range */
142  Bands::const_iterator bandStart = psd->ConstBandsBegin ();
143  Bands::const_iterator bandEnd = psd->ConstBandsEnd ();
144  epsilon = TOLERANCE * std::max (1.0, std::max ((*bandStart).fc, m_startFrequency));
145  NS_TEST_ASSERT_MSG_EQ_TOL ((*bandStart).fc,
147  epsilon,
148  "start frequency value (" << (*bandStart).fc << ") is incorrect");
149  epsilon = TOLERANCE * std::max (1.0, std::max ((*bandStart).fc, (m_startFrequency + m_channelBandwidth)));
150  NS_TEST_ASSERT_MSG_EQ_TOL ((*(bandEnd - 1)).fc,
152  epsilon,
153  "end frequency value (" << (*(bandEnd - 1)).fc << ") is incorrect");
154 }
155 
156 
158 {
159 public:
161 };
162 
164  : TestSuite ("tv-spectrum-transmitter", UNIT)
165 {
166  NS_LOG_INFO ("creating TvSpectrumTransmitterTestSuite");
167  for (double startFreq = 100; startFreq < 1e15; startFreq *= 10)
168  {
169  for (double bandwidth = 100; bandwidth < 1e15; bandwidth *= 10)
170  {
171  for (double psd = -100; psd <= 100; psd += 20)
172  {
174  bandwidth,
175  psd,
176  TvSpectrumTransmitter::TVTYPE_8VSB),
177  TestCase::QUICK);
178  }
179  }
180  }
181  for (double startFreq = 100; startFreq < 1e15; startFreq *= 10)
182  {
183  for (double bandwidth = 100; bandwidth < 1e15; bandwidth *= 10)
184  {
185  for (double psd = -100; psd <= 100; psd += 20)
186  {
188  bandwidth,
189  psd,
190  TvSpectrumTransmitter::TVTYPE_COFDM),
191  TestCase::QUICK);
192  }
193  }
194  }
195  for (double startFreq = 100; startFreq < 1e15; startFreq *= 10)
196  {
197  for (double bandwidth = 100; bandwidth < 1e15; bandwidth *= 10)
198  {
199  for (double psd = -100; psd <= 100; psd += 20)
200  {
202  bandwidth,
203  psd,
204  TvSpectrumTransmitter::TVTYPE_ANALOG),
205  TestCase::QUICK);
206  }
207  }
208  }
209 }
210 
Values::const_iterator ConstValuesEnd() const
virtual void DoRun(void)
Implementation to actually run this TestCase.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
static std::string Name(TvSpectrumTransmitter::TvType tvType, double startFrequency, double channelBandwidth, double basePsd)
A suite of tests to run.
Definition: test.h:1342
static TvSpectrumTransmitterTestSuite g_tvSpectrumTransmitterTestSuite
virtual void CreateTvPsd()
Creates power spectral density (PSD) spectrum of the TV transmitter and sets it for transmission...
TvSpectrumTransmitter::TvType m_tvType
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
TvType
types of TV transmitters: analog, digital 8-VSB, or digital COFDM
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:277
encapsulates test code
Definition: test.h:1155
const double TOLERANCE
Ptr< SpectrumValue > GetTxPsd() const
Get the power spectral density of the TV transmitter's signal.
static std::string Name(std::string str, uint32_t totalStreamSize, uint32_t sourceWriteSize, uint32_t serverReadSize, uint32_t serverWriteSize, uint32_t sourceReadSize, bool useIpv6)
Definition: tcp-test.cc:166
tuple phy
Definition: third.py:86
Hold variables of type enum.
Definition: enum.h:54
#define max(a, b)
Definition: 80211b.c:45
Bands::const_iterator ConstBandsEnd() const
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
#define NS_TEST_ASSERT_MSG_EQ_TOL(actual, limit, tol, msg)
Test that actual and expected (limit) values are equal to plus or minus some tolerance and report and...
Definition: test.h:380
Bands::const_iterator ConstBandsBegin() const
Every class exported by the ns3 library is enclosed in the ns3 namespace.
TvSpectrumTransmitterTestCase(double startFrequency, double channelBandwidth, double basePsd, TvSpectrumTransmitter::TvType tvType)
static const double TOLERANCE
Tolerance used to check reciprocal of two numbers.
Values::const_iterator ConstValuesBegin() const
This class can be used to hold variables of floating point type such as 'double' or 'float'...
Definition: double.h:41
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:185