A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
27NS_LOG_COMPONENT_DEFINE("TvSpectrumTransmitterTest");
28
29using namespace ns3;
30
31const double TOLERANCE = 1e-15;
32// Bug 2094: Adjust floating point comparison epsilon based on inputs.
33// Follows http://realtimecollisiondetection.net/blog/?p=89
34double epsilon;
35
36/**
37 * \ingroup spectrum-tests
38 *
39 * This test verifies the accuracy of the spectrum/PSD model in the
40 * TvSpectrumTransmitter class. To do so, it tests if the max power spectral
41 * density, start frequency, and end frequency comply with expected values.
42 * Values for TV/modulation type, start frequency, channel bandwidth, and
43 * base PSD are swept and tested for each case.
44 */
46{
47 public:
48 /**
49 * Constructor
50 * \param startFrequency Start frequency.
51 * \param channelBandwidth Channel Bandwidth.
52 * \param basePsd Base Power Spectral Density (PSD).
53 * \param tvType TV type.
54 */
55 TvSpectrumTransmitterTestCase(double startFrequency,
56 double channelBandwidth,
57 double basePsd,
60
61 private:
62 void DoRun() override;
63 /**
64 * Build the test name
65 * \param tvType TV type.
66 * \param startFrequency Start frequency.
67 * \param channelBandwidth Channel Bandwidth.
68 * \param basePsd Base Power Spectral Density (PSD).
69 * \return The test name
70 */
71 static std::string Name(TvSpectrumTransmitter::TvType tvType,
72 double startFrequency,
73 double channelBandwidth,
74 double basePsd);
75
76 double m_startFrequency; //!< Start frequency.
77 double m_channelBandwidth; //!< Channel Bandwidth.
78 double m_basePsd; //!< Base Power Spectral Density (PSD).
80};
81
82std::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
112void
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));
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));
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");
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
172/**
173 * \ingroup spectrum-tests
174 *
175 * Test suite for the TvSpectrumTransmitter class
176 */
178{
179 public:
181};
182
184 : TestSuite("tv-spectrum-transmitter", Type::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,
197 TestCase::Duration::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,
211 TestCase::Duration::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,
225 TestCase::Duration::QUICK);
226 }
227 }
228 }
229}
230
231/// Static variable for test initialization
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
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1268
Type
Type of test.
Definition: test.h:1275
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:338
Every class exported by the ns3 library is enclosed in the ns3 namespace.
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.