A Discrete-Event Network Simulator
API
tv-spectrum-transmitter-test.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/double.h>
21 #include <ns3/enum.h>
22 #include <ns3/log.h>
23 #include <ns3/spectrum-value.h>
24 #include <ns3/test.h>
25 #include <ns3/tv-spectrum-transmitter.h>
26 
27 NS_LOG_COMPONENT_DEFINE("TvSpectrumTransmitterTest");
28 
29 using namespace ns3;
30 
31 const double TOLERANCE = 1e-15;
32 // Bug 2094: Adjust floating point comparison epsilon based on inputs.
33 // Follows http://realtimecollisiondetection.net/blog/?p=89
34 double epsilon;
35 
46 {
47  public:
55  TvSpectrumTransmitterTestCase(double startFrequency,
56  double channelBandwidth,
57  double basePsd,
60 
61  private:
62  void DoRun() override;
71  static std::string Name(TvSpectrumTransmitter::TvType tvType,
72  double startFrequency,
73  double channelBandwidth,
74  double basePsd);
75 
78  double m_basePsd;
80 };
81 
82 std::string
84  double startFrequency,
85  double channelBandwidth,
86  double basePsd)
87 {
88  std::ostringstream oss;
89  oss << "TV type = " << tvType << ", "
90  << "start frequency = " << startFrequency << " Hz, "
91  << "channel bandwidth = " << channelBandwidth << " Hz, "
92  << "base PSD = " << basePsd << " dBm per Hz";
93  return oss.str();
94 }
95 
97  double channelBandwidth,
98  double basePsd,
100  : TestCase(Name(tvType, startFrequency, channelBandwidth, basePsd)),
101  m_startFrequency(startFrequency),
102  m_channelBandwidth(channelBandwidth),
103  m_basePsd(basePsd),
104  m_tvType(tvType)
105 {
106 }
107 
109 {
110 }
111 
112 void
114 {
116 
117  /* TV transmitter setup */
118  Ptr<TvSpectrumTransmitter> phy = CreateObject<TvSpectrumTransmitter>();
119  phy->SetAttribute("StartFrequency", DoubleValue(m_startFrequency));
120  phy->SetAttribute("ChannelBandwidth", DoubleValue(m_channelBandwidth));
121  phy->SetAttribute("BasePsd", DoubleValue(m_basePsd));
122  phy->SetAttribute("TvType", EnumValue(m_tvType));
123  phy->CreateTvPsd();
124 
125  /* Test max PSD value */
126  Ptr<SpectrumValue> psd = phy->GetTxPsd();
127  auto psdIter = psd->ConstValuesBegin();
128  double maxValue = 0;
129  while (psdIter != psd->ConstValuesEnd())
130  {
131  if (*psdIter > maxValue)
132  {
133  maxValue = *psdIter;
134  }
135  ++psdIter;
136  }
137  double basePsdWattsHz = pow(10.0, (m_basePsd - 30) / 10.0); // convert dBm to W/Hz
138  if (m_tvType == TvSpectrumTransmitter::TVTYPE_8VSB) // pilot has highest PSD
139  {
140  double expectedPsd = (0.502 * basePsdWattsHz) + (21.577 * basePsdWattsHz);
141  epsilon = TOLERANCE * std::max(1.0, std::max(maxValue, expectedPsd));
142  NS_TEST_ASSERT_MSG_EQ_TOL(maxValue,
143  expectedPsd,
144  epsilon,
145  "peak PSD value (" << maxValue << ") is incorrect");
146  }
147  else // highest PSD is base PSD
148  {
149  epsilon = TOLERANCE * std::max(1.0, std::max(maxValue, basePsdWattsHz));
150  NS_TEST_ASSERT_MSG_EQ_TOL(maxValue,
151  basePsdWattsHz,
152  epsilon,
153  "peak PSD value (" << maxValue << ") is incorrect");
154  }
155 
156  /* Test frequency range */
157  auto bandStart = psd->ConstBandsBegin();
158  auto bandEnd = psd->ConstBandsEnd();
159  epsilon = TOLERANCE * std::max(1.0, std::max((*bandStart).fc, m_startFrequency));
160  NS_TEST_ASSERT_MSG_EQ_TOL((*bandStart).fc,
162  epsilon,
163  "start frequency value (" << (*bandStart).fc << ") is incorrect");
164  epsilon = TOLERANCE *
165  std::max(1.0, std::max((*bandStart).fc, (m_startFrequency + m_channelBandwidth)));
166  NS_TEST_ASSERT_MSG_EQ_TOL((*(bandEnd - 1)).fc,
168  epsilon,
169  "end frequency value (" << (*(bandEnd - 1)).fc << ") is incorrect");
170 }
171 
178 {
179  public:
181 };
182 
184  : TestSuite("tv-spectrum-transmitter", UNIT)
185 {
186  NS_LOG_INFO("creating TvSpectrumTransmitterTestSuite");
187  for (double startFreq = 100; startFreq < 1e15; startFreq *= 10)
188  {
189  for (double bandwidth = 100; bandwidth < 1e15; bandwidth *= 10)
190  {
191  for (double psd = -100; psd <= 100; psd += 20)
192  {
194  bandwidth,
195  psd,
196  TvSpectrumTransmitter::TVTYPE_8VSB),
197  TestCase::QUICK);
198  }
199  }
200  }
201  for (double startFreq = 100; startFreq < 1e15; startFreq *= 10)
202  {
203  for (double bandwidth = 100; bandwidth < 1e15; bandwidth *= 10)
204  {
205  for (double psd = -100; psd <= 100; psd += 20)
206  {
208  bandwidth,
209  psd,
210  TvSpectrumTransmitter::TVTYPE_COFDM),
211  TestCase::QUICK);
212  }
213  }
214  }
215  for (double startFreq = 100; startFreq < 1e15; startFreq *= 10)
216  {
217  for (double bandwidth = 100; bandwidth < 1e15; bandwidth *= 10)
218  {
219  for (double psd = -100; psd <= 100; psd += 20)
220  {
222  bandwidth,
223  psd,
224  TvSpectrumTransmitter::TVTYPE_ANALOG),
225  TestCase::QUICK);
226  }
227  }
228  }
229 }
230 
#define max(a, b)
Definition: 80211b.c:42
This test verifies the accuracy of the spectrum/PSD model in the TvSpectrumTransmitter class.
TvSpectrumTransmitterTestCase(double startFrequency, double channelBandwidth, double basePsd, TvSpectrumTransmitter::TvType tvType)
Constructor.
void DoRun() override
Implementation to actually run this TestCase.
TvSpectrumTransmitter::TvType m_tvType
TV type.
double m_basePsd
Base Power Spectral Density (PSD).
static std::string Name(TvSpectrumTransmitter::TvType tvType, double startFrequency, double channelBandwidth, double basePsd)
Build the test name.
Test suite for the TvSpectrumTransmitter class.
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
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
Values::const_iterator ConstValuesBegin() const
Bands::const_iterator ConstBandsEnd() const
Bands::const_iterator ConstBandsBegin() const
Values::const_iterator ConstValuesEnd() const
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
TvType
types of TV transmitters: analog, digital 8-VSB, or digital COFDM
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
#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:337
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static const double TOLERANCE
Tolerance used to check reciprocal of two numbers.
phy
Definition: third.py:89
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
const double TOLERANCE
static TvSpectrumTransmitterTestSuite g_tvSpectrumTransmitterTestSuite
Static variable for test initialization.