A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
spectrum-phased-array-tx-beam-test.cc
Go to the documentation of this file.
1// Copyright (c) 2026 Tom Henderson
2//
3// SPDX-License-Identifier: NIST-Software
4
5#include "ns3/constant-position-mobility-model.h"
6#include "ns3/multi-model-spectrum-channel.h"
7#include "ns3/net-device.h"
8#include "ns3/phased-array-spectrum-propagation-loss-model.h"
9#include "ns3/propagation-delay-model.h"
10#include "ns3/simulator.h"
11#include "ns3/spectrum-phy.h"
12#include "ns3/spectrum-signal-parameters.h"
13#include "ns3/spectrum-value.h"
14#include "ns3/test.h"
15#include "ns3/uinteger.h"
16#include "ns3/uniform-planar-array.h"
17
18#include <complex>
19#include <valarray>
20
21using namespace ns3;
22
23/**
24 * @ingroup spectrum-tests
25 *
26 * Phased-array loss model whose gain is the squared magnitude of the sum of the
27 * transmitter's beamforming weights it is given, so that the received PSD reveals
28 * which beamforming vector the channel passed.
29 */
31{
32 private:
37 Ptr<const PhasedArrayModel> /* aPhasedArrayModel */,
38 Ptr<const PhasedArrayModel> /* bPhasedArrayModel */,
39 const PhasedArrayModel::ComplexVector& aBeamformingVector,
40 const PhasedArrayModel::ComplexVector& /* bBeamformingVector */) const override
41 {
42 std::complex<double> sum{0.0, 0.0};
43 for (size_t i = 0; i < aBeamformingVector.GetSize(); ++i)
44 {
45 sum += aBeamformingVector(i);
46 }
47 auto rxParams = params->Copy();
48 *(rxParams->psd) *= std::norm(sum);
49 return rxParams;
50 }
51
52 int64_t DoAssignStreams(int64_t /* stream */) override
53 {
54 return 0;
55 }
56};
57
58/**
59 * @ingroup spectrum-tests
60 *
61 * Minimal SpectrumPhy that records the PSD of the last received signal.
62 */
64{
65 public:
66 /**
67 * Constructor.
68 *
69 * @param spectrumModel The spectrum model used for reception.
70 * @param antenna The phased array of this PHY.
71 */
73 : m_spectrumModel(spectrumModel),
74 m_antenna(antenna)
75 {
76 }
77
78 void SetDevice(Ptr<NetDevice> d) override
79 {
80 m_device = d;
81 }
82
83 Ptr<NetDevice> GetDevice() const override
84 {
85 return m_device;
86 }
87
89 {
90 m_mobility = m;
91 }
92
94 {
95 return m_mobility;
96 }
97
99 {
100 m_channel = c;
101 }
102
104 {
105 return m_spectrumModel;
106 }
107
108 Ptr<Object> GetAntenna() const override
109 {
110 return m_antenna;
111 }
112
114 {
115 m_rxPsd = params->psd;
116 ++m_rxCount;
117 }
118
119 Ptr<SpectrumValue> m_rxPsd; ///< PSD of the last received signal
120 uint32_t m_rxCount{0}; ///< Number of received signals
121
122 private:
123 Ptr<const SpectrumModel> m_spectrumModel; ///< Receive spectrum model
126 Ptr<MobilityModel> m_mobility; ///< Mobility model
128};
129
130/**
131 * @ingroup spectrum-tests
132 *
133 * Check that a signal is received with the gain of the beamforming vector the
134 * transmitter had when the transmission started, even if that vector is
135 * changed before the signal arrives at the receiver.
136 *
137 * Two configurations are run. With a propagation delay model, the receiver's
138 * StartRx event is scheduled after a positive delay and the transmitter's array
139 * is re-steered while the signal is in flight. Without a propagation delay
140 * model, StartRx is scheduled with zero delay but is still a separate event; the
141 * array is re-steered by another event at the same timestamp that runs between
142 * StartTx and StartRx. Both cases fail if the channel reads the transmitter's
143 * array at reception time instead of using the vector captured at transmission.
144 *
145 * The loss model used here returns |sum of the transmitter's weights|^2 as the
146 * gain, so the vector in use at transmission (all ones, gain numElems^2) and the
147 * re-steered vector (alternating +1/-1, gain 0) are distinguishable from the
148 * received PSD.
149 */
151{
152 public:
153 /**
154 * Constructor.
155 *
156 * @param withDelay Whether a propagation delay model is installed on the channel.
157 */
158 SpectrumPhasedArrayTxBeamTestCase(bool withDelay);
159
160 private:
161 void DoRun() override;
162
163 bool m_withDelay; ///< Whether a propagation delay model is installed on the channel
164};
165
167 : TestCase(std::string("TX beamforming vector at transmission time, ") +
168 (withDelay ? "with" : "without") + " propagation delay"),
169 m_withDelay(withDelay)
170{
171}
172
173void
175{
176 BandInfo bandInfo;
177 bandInfo.fl = 1.99e9;
178 bandInfo.fc = 2.0e9;
179 bandInfo.fh = 2.01e9;
180 Bands bands{bandInfo};
181 Ptr<SpectrumModel> spectrumModel = Create<SpectrumModel>(bands);
182
183 auto txAntenna = CreateObjectWithAttributes<UniformPlanarArray>("NumRows",
184 UintegerValue(2),
185 "NumColumns",
186 UintegerValue(2));
187 auto rxAntenna = CreateObjectWithAttributes<UniformPlanarArray>("NumRows",
188 UintegerValue(2),
189 "NumColumns",
190 UintegerValue(2));
191 const size_t numElems = txAntenna->GetNumElems();
192
193 // Weights summing to numElems (loss model gain numElems^2) for the transmission, and
194 // weights summing to zero (gain 0) for the re-steered array.
196 std::valarray<std::complex<double>>(std::complex<double>(1.0, 0.0), numElems));
197 std::valarray<std::complex<double>> alternating(numElems);
198 for (size_t i = 0; i < numElems; ++i)
199 {
200 alternating[i] = (i % 2 == 0) ? 1.0 : -1.0;
201 }
202 PhasedArrayModel::ComplexVector laterBeam(alternating);
203 txAntenna->SetBeamformingVector(txBeam);
204 rxAntenna->SetBeamformingVector(txBeam);
205
207 txMobility->SetPosition(Vector(0, 0, 0));
209 rxMobility->SetPosition(Vector(3000, 0, 0)); // 10 us at the speed of light
210
211 auto txPhy = CreateObject<TxBeamTestPhy>(spectrumModel, txAntenna);
212 txPhy->SetMobility(txMobility);
213 auto rxPhy = CreateObject<TxBeamTestPhy>(spectrumModel, rxAntenna);
214 rxPhy->SetMobility(rxMobility);
215
217 channel->AddPhasedArraySpectrumPropagationLossModel(CreateObject<TxBeamGainLossModel>());
218 if (m_withDelay)
219 {
220 channel->SetPropagationDelayModel(CreateObject<ConstantSpeedPropagationDelayModel>());
221 }
222 channel->AddRx(rxPhy);
223 txPhy->SetChannel(channel);
224 rxPhy->SetChannel(channel);
225
226 auto txPsd = Create<SpectrumValue>(spectrumModel);
227 (*txPsd)[0] = 1.0;
228 auto params = Create<SpectrumSignalParameters>();
229 params->psd = txPsd;
230 params->duration = MicroSeconds(1);
231 params->txPhy = txPhy;
232
233 const Time txTime = MilliSeconds(1);
234 Simulator::Schedule(txTime, [&]() { channel->StartTx(params); });
235 // Re-steer the transmitter's array while the signal is in flight: halfway through
236 // the propagation delay, or in the same instant when there is no delay (the
237 // reception is then a separate event at the same timestamp).
238 const Time steerTime = m_withDelay ? txTime + MicroSeconds(5) : txTime;
239 Simulator::Schedule(steerTime, [&]() { txAntenna->SetBeamformingVector(laterBeam); });
241
242 NS_TEST_ASSERT_MSG_EQ(rxPhy->m_rxCount, 1, "Expected one reception");
243 NS_TEST_ASSERT_MSG_EQ_TOL((*rxPhy->m_rxPsd)[0],
244 static_cast<double>(numElems * numElems),
245 1e-9,
246 "Received PSD does not reflect the beamforming vector in use "
247 "when the transmission started");
248 NS_TEST_ASSERT_MSG_EQ(txAntenna->GetBeamformingVector() == laterBeam,
249 true,
250 "The channel modified the transmitter's current beamforming vector");
252}
253
254/**
255 * @ingroup spectrum-tests
256 *
257 * Test suite for the evaluation of the transmitter's beamforming vector at transmission time.
258 */
264
271
272/// Static variable for test initialization
Check that a signal is received with the gain of the beamforming vector the transmitter had when the ...
void DoRun() override
Implementation to actually run this TestCase.
bool m_withDelay
Whether a propagation delay model is installed on the channel.
Test suite for the evaluation of the transmitter's beamforming vector at transmission time.
Phased-array loss model whose gain is the squared magnitude of the sum of the transmitter's beamformi...
int64_t DoAssignStreams(int64_t) override
Assign a fixed random variable stream number to the random variables used by this model.
Ptr< SpectrumSignalParameters > DoCalcRxPowerSpectralDensity(Ptr< const SpectrumSignalParameters > params, Ptr< const MobilityModel >, Ptr< const MobilityModel >, Ptr< const PhasedArrayModel >, Ptr< const PhasedArrayModel >, const PhasedArrayModel::ComplexVector &aBeamformingVector, const PhasedArrayModel::ComplexVector &) const override
Ptr< const SpectrumModel > GetRxSpectrumModel() const override
Ptr< MobilityModel > m_mobility
Mobility model.
Ptr< NetDevice > GetDevice() const override
Get the associated NetDevice instance.
Ptr< NetDevice > m_device
Device.
Ptr< SpectrumChannel > m_channel
Channel.
void SetDevice(Ptr< NetDevice > d) override
Set the associated NetDevice instance.
TxBeamTestPhy(Ptr< const SpectrumModel > spectrumModel, Ptr< PhasedArrayModel > antenna)
Constructor.
Ptr< PhasedArrayModel > m_antenna
Phased array.
Ptr< const SpectrumModel > m_spectrumModel
Receive spectrum model.
Ptr< MobilityModel > GetMobility() const override
Get the associated MobilityModel instance.
void SetMobility(Ptr< MobilityModel > m) override
Set the mobility model associated with this device.
void SetChannel(Ptr< SpectrumChannel > c) override
Set the channel attached to this device.
Ptr< SpectrumValue > m_rxPsd
PSD of the last received signal.
void StartRx(Ptr< SpectrumSignalParameters > params) override
Notify the SpectrumPhy instance of an incoming signal.
uint32_t m_rxCount
Number of received signals.
Ptr< Object > GetAntenna() const override
Get the AntennaModel used by this SpectrumPhy instance for transmission and/or reception.
ComplexMatrixArray ComplexVector
the underlying Valarray
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:580
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:125
static void Run()
Run the simulation.
Definition simulator.cc:161
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:296
@ QUICK
Fast test.
Definition test.h:1057
TestCase(const TestCase &)=delete
Caller graph was not generated because of its size.
Type
Type of test.
Definition test.h:1271
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Definition test.cc:494
Simulation virtual time values and global simulation resolution.
Definition nstime.h:95
Hold an unsigned integer type.
Definition uinteger.h:34
size_t GetSize() const
Definition val-array.h:394
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:627
Ptr< T > CreateObjectWithAttributes(Args... args)
Allocate an Object on the heap and initialize with a set of attributes.
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:492
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition test.h:133
#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:326
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1347
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1330
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::vector< BandInfo > Bands
Container of BandInfo.
STL namespace.
static SpectrumPhasedArrayTxBeamTestSuite g_spectrumPhasedArrayTxBeamTestSuite
Static variable for test initialization.
The building block of a SpectrumModel.
double fc
center frequency
double fl
lower limit of subband
double fh
upper limit of subband