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
28NS_LOG_COMPONENT_DEFINE ("TvSpectrumTransmitterTest");
29
30using namespace ns3;
31
32const double TOLERANCE = 1e-15;
33// Bug 2094: Adjust floating point comparison epsilon based on inputs.
34// Follows http://realtimecollisiondetection.net/blog/?p=89
35double epsilon;
36
47{
48public:
56 TvSpectrumTransmitterTestCase (double startFrequency,
57 double channelBandwidth,
58 double basePsd,
61
62private:
63 virtual void DoRun (void);
72 static std::string Name (TvSpectrumTransmitter::TvType tvType,
73 double startFrequency,
74 double channelBandwidth,
75 double basePsd);
76
79 double m_basePsd;
81};
82
83
84std::string
86 double startFrequency,
87 double channelBandwidth,
88 double basePsd)
89{
90 std::ostringstream oss;
91 oss << "TV type = " << tvType << ", "
92 << "start frequency = " << startFrequency << " Hz, "
93 << "channel bandwidth = " << channelBandwidth << " Hz, "
94 << "base PSD = " << basePsd << " dBm per Hz";
95 return oss.str();
96}
97
99 double channelBandwidth,
100 double basePsd,
102 : TestCase (Name (tvType, startFrequency, channelBandwidth, basePsd)),
103 m_startFrequency (startFrequency),
104 m_channelBandwidth (channelBandwidth),
105 m_basePsd (basePsd),
106 m_tvType (tvType)
107{
108}
109
111{
112}
113
114void
116{
118
119 /* TV transmitter setup */
120 Ptr<TvSpectrumTransmitter> phy = CreateObject<TvSpectrumTransmitter>();
121 phy->SetAttribute ("StartFrequency", DoubleValue (m_startFrequency));
122 phy->SetAttribute ("ChannelBandwidth", DoubleValue (m_channelBandwidth));
123 phy->SetAttribute ("BasePsd", DoubleValue (m_basePsd));
124 phy->SetAttribute ("TvType", EnumValue (m_tvType));
125 phy->CreateTvPsd ();
126
127 /* Test max PSD value */
128 Ptr<SpectrumValue> psd = phy->GetTxPsd ();
129 Values::const_iterator psdIter = psd->ConstValuesBegin ();
130 double maxValue = 0;
131 while (psdIter != psd->ConstValuesEnd ())
132 {
133 if (*psdIter > maxValue)
134 {
135 maxValue = *psdIter;
136 }
137 ++psdIter;
138 }
139 double basePsdWattsHz = pow (10.0, (m_basePsd - 30) / 10.0); // convert dBm to W/Hz
140 if (m_tvType == TvSpectrumTransmitter::TVTYPE_8VSB) // pilot has highest PSD
141 {
142 double expectedPsd = (0.502 * basePsdWattsHz) + (21.577 * basePsdWattsHz);
143 epsilon = TOLERANCE * std::max (1.0, std::max (maxValue, expectedPsd));
144 NS_TEST_ASSERT_MSG_EQ_TOL (maxValue,
145 expectedPsd,
146 epsilon,
147 "peak PSD value (" << maxValue << ") is incorrect");
148 }
149 else // highest PSD is base PSD
150 {
151 epsilon = TOLERANCE * std::max (1.0, std::max (maxValue, basePsdWattsHz));
152 NS_TEST_ASSERT_MSG_EQ_TOL (maxValue,
153 basePsdWattsHz,
154 epsilon,
155 "peak PSD value (" << maxValue << ") is incorrect");
156 }
157
158 /* Test frequency range */
159 Bands::const_iterator bandStart = psd->ConstBandsBegin ();
160 Bands::const_iterator bandEnd = psd->ConstBandsEnd ();
161 epsilon = TOLERANCE * std::max (1.0, std::max ((*bandStart).fc, m_startFrequency));
162 NS_TEST_ASSERT_MSG_EQ_TOL ((*bandStart).fc,
164 epsilon,
165 "start frequency value (" << (*bandStart).fc << ") is incorrect");
166 epsilon = TOLERANCE * std::max (1.0, std::max ((*bandStart).fc, (m_startFrequency + m_channelBandwidth)));
167 NS_TEST_ASSERT_MSG_EQ_TOL ((*(bandEnd - 1)).fc,
169 epsilon,
170 "end frequency value (" << (*(bandEnd - 1)).fc << ") is incorrect");
171}
172
173
180{
181public:
183};
184
186 : TestSuite ("tv-spectrum-transmitter", UNIT)
187{
188 NS_LOG_INFO ("creating TvSpectrumTransmitterTestSuite");
189 for (double startFreq = 100; startFreq < 1e15; startFreq *= 10)
190 {
191 for (double bandwidth = 100; bandwidth < 1e15; bandwidth *= 10)
192 {
193 for (double psd = -100; psd <= 100; psd += 20)
194 {
196 bandwidth,
197 psd,
198 TvSpectrumTransmitter::TVTYPE_8VSB),
199 TestCase::QUICK);
200 }
201 }
202 }
203 for (double startFreq = 100; startFreq < 1e15; startFreq *= 10)
204 {
205 for (double bandwidth = 100; bandwidth < 1e15; bandwidth *= 10)
206 {
207 for (double psd = -100; psd <= 100; psd += 20)
208 {
210 bandwidth,
211 psd,
212 TvSpectrumTransmitter::TVTYPE_COFDM),
213 TestCase::QUICK);
214 }
215 }
216 }
217 for (double startFreq = 100; startFreq < 1e15; startFreq *= 10)
218 {
219 for (double bandwidth = 100; bandwidth < 1e15; bandwidth *= 10)
220 {
221 for (double psd = -100; psd <= 100; psd += 20)
222 {
224 bandwidth,
225 psd,
226 TvSpectrumTransmitter::TVTYPE_ANALOG),
227 TestCase::QUICK);
228 }
229 }
230 }
231}
232
#define max(a, b)
Definition: 80211b.c:43
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.
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.
virtual void DoRun(void)
Implementation to actually run this TestCase.
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:41
Hold variables of type enum.
Definition: enum.h:55
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
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:994
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
A suite of tests to run.
Definition: test.h:1188
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:205
#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:281
#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:323
Every class exported by the ns3 library is enclosed in the ns3 namespace.
phy
Definition: third.py:93
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.