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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Benjamin Cizdziel <ben.cizdziel@gmail.com>
7 */
8
9#include <ns3/double.h>
10#include <ns3/enum.h>
11#include <ns3/log.h>
12#include <ns3/spectrum-value.h>
13#include <ns3/test.h>
14#include <ns3/tv-spectrum-transmitter.h>
15
16NS_LOG_COMPONENT_DEFINE("TvSpectrumTransmitterTest");
17
18using namespace ns3;
19
20const double TOLERANCE = 1e-15;
21// Bug 2094: Adjust floating point comparison epsilon based on inputs.
22// Follows http://realtimecollisiondetection.net/blog/?p=89
23double epsilon;
24
25/**
26 * \ingroup spectrum-tests
27 *
28 * This test verifies the accuracy of the spectrum/PSD model in the
29 * TvSpectrumTransmitter class. To do so, it tests if the max power spectral
30 * density, start frequency, and end frequency comply with expected values.
31 * Values for TV/modulation type, start frequency, channel bandwidth, and
32 * base PSD are swept and tested for each case.
33 */
35{
36 public:
37 /**
38 * Constructor
39 * \param startFrequency Start frequency.
40 * \param channelBandwidth Channel Bandwidth.
41 * \param basePsd Base Power Spectral Density (PSD).
42 * \param tvType TV type.
43 */
44 TvSpectrumTransmitterTestCase(double startFrequency,
45 double channelBandwidth,
46 double basePsd,
49
50 private:
51 void DoRun() override;
52 /**
53 * Build the test name
54 * \param tvType TV type.
55 * \param startFrequency Start frequency.
56 * \param channelBandwidth Channel Bandwidth.
57 * \param basePsd Base Power Spectral Density (PSD).
58 * \return The test name
59 */
60 static std::string Name(TvSpectrumTransmitter::TvType tvType,
61 double startFrequency,
62 double channelBandwidth,
63 double basePsd);
64
65 double m_startFrequency; //!< Start frequency.
66 double m_channelBandwidth; //!< Channel Bandwidth.
67 double m_basePsd; //!< Base Power Spectral Density (PSD).
69};
70
71std::string
73 double startFrequency,
74 double channelBandwidth,
75 double basePsd)
76{
77 std::ostringstream oss;
78 oss << "TV type = " << tvType << ", "
79 << "start frequency = " << startFrequency << " Hz, "
80 << "channel bandwidth = " << channelBandwidth << " Hz, "
81 << "base PSD = " << basePsd << " dBm per Hz";
82 return oss.str();
83}
84
86 double channelBandwidth,
87 double basePsd,
89 : TestCase(Name(tvType, startFrequency, channelBandwidth, basePsd)),
90 m_startFrequency(startFrequency),
91 m_channelBandwidth(channelBandwidth),
92 m_basePsd(basePsd),
93 m_tvType(tvType)
94{
95}
96
100
101void
103{
105
106 /* TV transmitter setup */
108 phy->SetAttribute("StartFrequency", DoubleValue(m_startFrequency));
109 phy->SetAttribute("ChannelBandwidth", DoubleValue(m_channelBandwidth));
110 phy->SetAttribute("BasePsd", DoubleValue(m_basePsd));
111 phy->SetAttribute("TvType", EnumValue(m_tvType));
112 phy->CreateTvPsd();
113
114 /* Test max PSD value */
115 Ptr<SpectrumValue> psd = phy->GetTxPsd();
116 auto psdIter = psd->ConstValuesBegin();
117 double maxValue = 0;
118 while (psdIter != psd->ConstValuesEnd())
119 {
120 if (*psdIter > maxValue)
121 {
122 maxValue = *psdIter;
123 }
124 ++psdIter;
125 }
126 double basePsdWattsHz = pow(10.0, (m_basePsd - 30) / 10.0); // convert dBm to W/Hz
127 if (m_tvType == TvSpectrumTransmitter::TVTYPE_8VSB) // pilot has highest PSD
128 {
129 double expectedPsd = (0.502 * basePsdWattsHz) + (21.577 * basePsdWattsHz);
130 epsilon = TOLERANCE * std::max(1.0, std::max(maxValue, expectedPsd));
132 expectedPsd,
133 epsilon,
134 "peak PSD value (" << maxValue << ") is incorrect");
135 }
136 else // highest PSD is base PSD
137 {
138 epsilon = TOLERANCE * std::max(1.0, std::max(maxValue, basePsdWattsHz));
140 basePsdWattsHz,
141 epsilon,
142 "peak PSD value (" << maxValue << ") is incorrect");
143 }
144
145 /* Test frequency range */
146 auto bandStart = psd->ConstBandsBegin();
147 auto bandEnd = psd->ConstBandsEnd();
148 epsilon = TOLERANCE * std::max(1.0, std::max((*bandStart).fc, m_startFrequency));
149 NS_TEST_ASSERT_MSG_EQ_TOL((*bandStart).fc,
151 epsilon,
152 "start frequency value (" << (*bandStart).fc << ") is incorrect");
154 std::max(1.0, std::max((*bandStart).fc, (m_startFrequency + m_channelBandwidth)));
155 NS_TEST_ASSERT_MSG_EQ_TOL((*(bandEnd - 1)).fc,
157 epsilon,
158 "end frequency value (" << (*(bandEnd - 1)).fc << ") is incorrect");
159}
160
161/**
162 * \ingroup spectrum-tests
163 *
164 * Test suite for the TvSpectrumTransmitter class
165 */
171
173 : TestSuite("tv-spectrum-transmitter", Type::UNIT)
174{
175 NS_LOG_INFO("creating TvSpectrumTransmitterTestSuite");
176 for (double startFreq = 100; startFreq < 1e15; startFreq *= 10)
177 {
178 for (double bandwidth = 100; bandwidth < 1e15; bandwidth *= 10)
179 {
180 for (double psd = -100; psd <= 100; psd += 20)
181 {
183 bandwidth,
184 psd,
186 TestCase::Duration::QUICK);
187 }
188 }
189 }
190 for (double startFreq = 100; startFreq < 1e15; startFreq *= 10)
191 {
192 for (double bandwidth = 100; bandwidth < 1e15; bandwidth *= 10)
193 {
194 for (double psd = -100; psd <= 100; psd += 20)
195 {
197 bandwidth,
198 psd,
200 TestCase::Duration::QUICK);
201 }
202 }
203 }
204 for (double startFreq = 100; startFreq < 1e15; startFreq *= 10)
205 {
206 for (double bandwidth = 100; bandwidth < 1e15; bandwidth *= 10)
207 {
208 for (double psd = -100; psd <= 100; psd += 20)
209 {
211 bandwidth,
212 psd,
214 TestCase::Duration::QUICK);
215 }
216 }
217 }
218}
219
220/// 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:31
Hold variables of type enum.
Definition enum.h:52
Smart pointer class similar to boost::intrusive_ptr.
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
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:191
#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:264
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
#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:327
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.
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:155
static TvSpectrumTransmitterTestSuite g_tvSpectrumTransmitterTestSuite
Static variable for test initialization.