A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
wifi-spectrum-value-helper.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 CTTC
3 * Copyright (c) 2010 TELEMATICS LAB, DEE - Politecnico di Bari
4 * Copyright (c) 2017 Orange Labs
5 *
6 * SPDX-License-Identifier: GPL-2.0-only
7 *
8 * Authors: Nicola Baldo <nbaldo@cttc.es>
9 * Giuseppe Piro <g.piro@poliba.it>
10 * Rediet <getachew.redieteab@orange.com>
11 */
12
14
15#include "wifi-utils.h"
16
17#include "ns3/assert.h"
18#include "ns3/fatal-error.h"
19#include "ns3/log.h"
20
21#include <algorithm>
22#include <cmath>
23#include <iterator>
24#include <map>
25#include <numeric>
26#include <optional>
27#include <sstream>
28
29namespace
30{
31
32/**
33 * Lambda to print a vector of frequencies.
34 */
35auto printFrequencies = [](const std::vector<ns3::MHz_u>& v) {
36 std::stringstream ss;
37 for (const auto& centerFrequency : v)
38 {
39 ss << centerFrequency << " ";
40 }
41 return ss.str();
42};
43} // namespace
44
45namespace ns3
46{
47
48NS_LOG_COMPONENT_DEFINE("WifiSpectrumValueHelper");
49
50///< Wifi Spectrum Model structure
52{
53 std::vector<MHz_u> centerFrequencies; ///< center frequency per segment
54 MHz_u channelWidth; ///< channel width
55 Hz_u carrierSpacing; ///< carrier spacing
56 MHz_u guardBandwidth; ///< guard band width
57};
58
59/**
60 * Less than operator
61 * @param lhs the left hand side wifi spectrum to compare
62 * @param rhs the right hand side wifi spectrum to compare
63 * @returns true if the left hand side spectrum is less than the right hand side spectrum
64 */
65bool
67{
68 return std::tie(lhs.centerFrequencies,
69 lhs.channelWidth,
71 lhs.guardBandwidth) < std::tie(rhs.centerFrequencies,
72 rhs.channelWidth,
74 rhs.guardBandwidth);
75 // TODO: replace with default spaceship operator, but it seems currently not working with all
76 // compilers
77}
78
79static std::map<WifiSpectrumModelId, Ptr<SpectrumModel>>
80 g_wifiSpectrumModelMap; ///< static initializer for the class
81
83WifiSpectrumValueHelper::GetSpectrumModel(const std::vector<MHz_u>& centerFrequencies,
84 MHz_u channelWidth,
85 Hz_u carrierSpacing,
86 MHz_u guardBandwidth)
87{
88 NS_LOG_FUNCTION(printFrequencies(centerFrequencies)
89 << channelWidth << carrierSpacing << guardBandwidth);
90 NS_ASSERT_MSG(centerFrequencies.size() <= 2,
91 "Spectrum model does not support more than 2 segments");
92 if (centerFrequencies.size() != 1)
93 {
94 NS_ASSERT_MSG(centerFrequencies.front() != centerFrequencies.back(),
95 "Center frequency of each segment shall be different");
96 }
97 // assume all frequency segments have the same width, hence split the guard bandwidth
98 // accordingly over the segments
99 guardBandwidth /= centerFrequencies.size();
101 WifiSpectrumModelId key{centerFrequencies, channelWidth, carrierSpacing, guardBandwidth};
102 if (const auto it = g_wifiSpectrumModelMap.find(key); it != g_wifiSpectrumModelMap.cend())
103 {
104 ret = it->second;
105 }
106 else
107 {
108 Bands bands;
109 const auto [minCenterFrequency, maxCenterFrequency] =
110 std::minmax_element(centerFrequencies.cbegin(), centerFrequencies.cend());
111 const auto separationWidth =
112 (minCenterFrequency == maxCenterFrequency)
113 ? MHz_u{0}
114 : (*maxCenterFrequency - *minCenterFrequency - (channelWidth / 2));
115 NS_ASSERT(separationWidth == MHz_u{0} || centerFrequencies.size() == 2);
116 const auto bandwidth = MHzToHz((channelWidth + (2 * guardBandwidth) + separationWidth));
117 // For OFDM, the center subcarrier is null (at center frequency)
118 uint32_t numBands = std::ceil(bandwidth / carrierSpacing);
119 NS_ASSERT(numBands > 0);
120 if (numBands % 2 == 0)
121 {
122 // round up to the nearest odd number of subbands so that bands
123 // are symmetric around center frequency
124 numBands += 1;
125 }
126 NS_ASSERT_MSG(numBands % 2 == 1, "Number of bands should be odd");
127 NS_LOG_DEBUG("Num bands " << numBands << " band bandwidth " << carrierSpacing);
128
129 // The lowest frequency is obtained from the minimum center frequency among the segment(s).
130 // Then, we subtract half the channel width to retrieve the starting frequency of the
131 // operating channel. If the channel is made of 2 segments, since the channel width is the
132 // total width, only a quarter of the channel width has to be subtracted. Finally, we
133 // remove the guard band width to get the center frequency of the first band and half the
134 // carrier spacing to get the effective starting frequency of the first band.
135 const auto startingFrequency = MHzToHz(*minCenterFrequency) -
136 (MHzToHz(channelWidth) / (2 * centerFrequencies.size())) -
137 MHzToHz(guardBandwidth) - (carrierSpacing / 2);
138 for (size_t i = 0; i < numBands; i++)
139 {
140 BandInfo info;
141 auto f = startingFrequency + (i * carrierSpacing);
142 info.fl = f;
143 f += carrierSpacing / 2;
144 info.fc = f;
145 f += carrierSpacing / 2;
146 info.fh = f;
147 NS_LOG_DEBUG("creating band " << i << " (" << info.fl << ":" << info.fc << ":"
148 << info.fh << ")");
149 bands.push_back(info);
150 }
151 ret = Create<SpectrumModel>(std::move(bands));
153 }
154 NS_LOG_LOGIC("returning SpectrumModel::GetUid () == " << ret->GetUid());
155 return ret;
156}
157
158// Power allocated to 71 center subbands out of 135 total subbands in the band
161 Watt_u txPower,
162 MHz_u guardBandwidth)
163{
164 NS_LOG_FUNCTION(centerFrequency << txPower << +guardBandwidth);
165 MHz_u channelWidth{22}; // DSSS channels are 22 MHz wide
166 Hz_u carrierSpacing{312500};
168 GetSpectrumModel({centerFrequency}, channelWidth, carrierSpacing, guardBandwidth));
169 auto vit = c->ValuesBegin();
170 auto bit = c->ConstBandsBegin();
171 auto nGuardBands =
172 static_cast<uint32_t>(((MHzToHz(2 * guardBandwidth)) / carrierSpacing) + 0.5);
173 auto nAllocatedBands = static_cast<uint32_t>((MHzToHz(channelWidth) / carrierSpacing) + 0.5);
174 NS_ASSERT(c->GetSpectrumModel()->GetNumBands() == (nAllocatedBands + nGuardBands + 1));
175 // Evenly spread power across 22 MHz
176 const auto txPowerPerBand = txPower / nAllocatedBands;
177 const auto psd = txPowerPerBand / (bit->fh - bit->fl);
178 for (size_t i = 0; i < c->GetSpectrumModel()->GetNumBands(); i++, vit++, bit++)
179 {
180 if ((i >= (nGuardBands / 2)) && (i <= ((nGuardBands / 2) + nAllocatedBands - 1)))
181 {
182 *vit = psd;
183 }
184 }
185 return c;
186}
187
190 MHz_u channelWidth,
191 Watt_u txPower,
192 MHz_u guardBandwidth,
193 dBr_u minInnerBand,
194 dBr_u minOuterBand,
195 dBr_u lowestPoint)
196{
197 NS_LOG_FUNCTION(centerFrequency << channelWidth << txPower << guardBandwidth << minInnerBand
198 << minOuterBand << lowestPoint);
199 Hz_u carrierSpacing{0};
200 uint32_t innerSlopeWidth = 0;
201 switch (static_cast<uint16_t>(channelWidth))
202 {
203 case 20:
204 carrierSpacing = Hz_u{312500};
205 innerSlopeWidth =
206 static_cast<uint32_t>((Hz_u{2e6} / carrierSpacing) + 0.5); // [-11;-9] & [9;11]
207 break;
208 case 10:
209 carrierSpacing = Hz_u{156250};
210 innerSlopeWidth =
211 static_cast<uint32_t>((Hz_u{1e6} / carrierSpacing) + 0.5); // [-5.5;-4.5] & [4.5;5.5]
212 break;
213 case 5:
214 carrierSpacing = Hz_u{78125};
215 innerSlopeWidth =
216 static_cast<uint32_t>((Hz_u{5e5} / carrierSpacing) + 0.5); // [-2.75;-2.5] & [2.5;2.75]
217 break;
218 default:
219 NS_FATAL_ERROR("Channel width " << channelWidth << " should be correctly set.");
220 return nullptr;
221 }
222
223 auto c = Create<SpectrumValue>(
224 GetSpectrumModel({centerFrequency}, channelWidth, carrierSpacing, guardBandwidth));
225 auto nGuardBands =
226 static_cast<uint32_t>(((MHzToHz(2 * guardBandwidth)) / carrierSpacing) + 0.5);
227 auto nAllocatedBands = static_cast<uint32_t>((MHzToHz(channelWidth) / carrierSpacing) + 0.5);
228 NS_ASSERT_MSG(c->GetSpectrumModel()->GetNumBands() == (nAllocatedBands + nGuardBands + 1),
229 "Unexpected number of bands " << c->GetSpectrumModel()->GetNumBands());
230 // 52 subcarriers (48 data + 4 pilot)
231 // skip guard band and 6 subbands, then place power in 26 subbands, then
232 // skip the center subband, then place power in 26 subbands, then skip
233 // the final 6 subbands and the guard band.
234 const auto txPowerPerBand = txPower / 52;
235 NS_LOG_DEBUG("Power per band " << txPowerPerBand << "W");
236 uint32_t start1 = (nGuardBands / 2) + 6;
237 uint32_t stop1 = start1 + 26 - 1;
238 uint32_t start2 = stop1 + 2;
239 uint32_t stop2 = start2 + 26 - 1;
240
241 // Build transmit spectrum mask
242 std::vector<WifiSpectrumBandIndices> subBands{
243 std::make_pair(start1, stop1),
244 std::make_pair(start2, stop2),
245 };
246 WifiSpectrumBandIndices maskBand(0, nAllocatedBands + nGuardBands);
248 {subBands},
249 maskBand,
250 txPowerPerBand,
251 nGuardBands,
252 innerSlopeWidth,
253 minInnerBand,
254 minOuterBand,
255 lowestPoint);
256 NormalizeSpectrumMask(c, txPower);
257 NS_ASSERT_MSG(std::abs(txPower - Integral(*c)) < 1e-6, "Power allocation failed");
258 return c;
259}
260
263 const std::vector<MHz_u>& centerFrequencies,
264 MHz_u channelWidth,
265 Watt_u txPower,
266 MHz_u guardBandwidth,
267 dBr_u minInnerBand,
268 dBr_u minOuterBand,
269 dBr_u lowestPoint,
270 const std::vector<bool>& puncturedSubchannels)
271{
272 NS_ASSERT_MSG(centerFrequencies.size() == 1 ||
273 (channelWidth == MHz_u{160} && centerFrequencies.size() <= 2),
274 "PSD for non-contiguous channels is only possible when the total width is 160 "
275 "MHz and cannot be made of more than 2 segments");
276 NS_LOG_FUNCTION(printFrequencies(centerFrequencies)
277 << channelWidth << txPower << guardBandwidth << minInnerBand << minOuterBand
278 << lowestPoint);
279 const Hz_u carrierSpacing{312500};
281 GetSpectrumModel(centerFrequencies, channelWidth, carrierSpacing, guardBandwidth));
282 // assume all frequency segments have the same width, hence split the guard bandwidth
283 // accordingly over the segments
284 guardBandwidth /= centerFrequencies.size();
285 const auto nGuardBands =
286 static_cast<uint32_t>(((MHzToHz(2 * guardBandwidth)) / carrierSpacing) + 0.5);
287 const auto nAllocatedBands =
288 static_cast<uint32_t>((MHzToHz(channelWidth) / carrierSpacing) + 0.5);
289 const auto separationWidth = std::abs(centerFrequencies.back() - centerFrequencies.front());
290 const auto unallocatedWidth =
291 separationWidth > Hz_u{0} ? (separationWidth - (channelWidth / 2)) : 0;
292 const auto nUnallocatedBands =
293 static_cast<uint32_t>((MHzToHz(unallocatedWidth) / carrierSpacing) + 0.5);
294 NS_ASSERT_MSG(c->GetSpectrumModel()->GetNumBands() ==
295 (nAllocatedBands + nGuardBands + nUnallocatedBands + 1),
296 "Unexpected number of bands " << c->GetSpectrumModel()->GetNumBands());
297 auto num20MhzBands = Count20MHzSubchannels(channelWidth);
298 std::size_t numAllocatedSubcarriersPer20MHz = 52;
299 NS_ASSERT(puncturedSubchannels.empty() || (puncturedSubchannels.size() == num20MhzBands));
300 const auto txPowerPerBand = (txPower / numAllocatedSubcarriersPer20MHz) / num20MhzBands;
301 NS_LOG_DEBUG("Power per band " << txPowerPerBand << "W");
302
303 std::size_t numSubcarriersPer20MHz = MHzToHz(MHz_u{20}) / carrierSpacing;
304 std::size_t numUnallocatedSubcarriersPer20MHz =
305 numSubcarriersPer20MHz - numAllocatedSubcarriersPer20MHz;
306 std::vector<std::vector<WifiSpectrumBandIndices>> subBandsPerSegment(
307 centerFrequencies.size()); // list of data/pilot-containing subBands (sent at 0dBr)
308 for (std::size_t i = 0; i < centerFrequencies.size(); ++i)
309 {
310 subBandsPerSegment.at(i).resize(
311 num20MhzBands * 2 / centerFrequencies.size()); // the center subcarrier is skipped,
312 // hence 2 subbands per 20 MHz subchannel
313 }
314 std::vector<std::vector<WifiSpectrumBandIndices>> puncturedBandsPerSegment;
315 uint32_t start = (nGuardBands / 2) + (numUnallocatedSubcarriersPer20MHz / 2);
316 uint32_t stop;
317 uint8_t index = 0;
318 for (auto& subBands : subBandsPerSegment)
319 {
320 puncturedBandsPerSegment.emplace_back();
321 for (auto it = subBands.begin(); it != subBands.end();)
322 {
323 stop = start + (numAllocatedSubcarriersPer20MHz / 2) - 1;
324 *it = std::make_pair(start, stop);
325 ++it;
326 uint32_t puncturedStart = start;
327 start = stop + 2; // skip center subcarrier
328 stop = start + (numAllocatedSubcarriersPer20MHz / 2) - 1;
329 *it = std::make_pair(start, stop);
330 ++it;
331 start = stop + numUnallocatedSubcarriersPer20MHz;
332 uint32_t puncturedStop = stop;
333 if (!puncturedSubchannels.empty() && puncturedSubchannels.at(index++))
334 {
335 puncturedBandsPerSegment.back().emplace_back(puncturedStart, puncturedStop);
336 }
337 }
338 start += nUnallocatedBands;
339 }
340
341 // Prepare spectrum mask specific variables
342 auto innerSlopeWidth = static_cast<uint32_t>(
343 (Hz_u{2e6} / carrierSpacing) +
344 0.5); // size in number of subcarriers of the 0dBr<->20dBr slope (2MHz for HT/VHT)
345 WifiSpectrumBandIndices maskBand(0, nAllocatedBands + nGuardBands + nUnallocatedBands);
346 const auto puncturedSlopeWidth =
347 static_cast<uint32_t>((Hz_u{500e3} / carrierSpacing) +
348 0.5); // size in number of subcarriers of the punctured slope band
349
350 // Build transmit spectrum mask
352 subBandsPerSegment,
353 maskBand,
354 txPowerPerBand,
355 nGuardBands,
356 innerSlopeWidth,
357 minInnerBand,
358 minOuterBand,
359 lowestPoint,
360 puncturedBandsPerSegment,
361 puncturedSlopeWidth);
362 NormalizeSpectrumMask(c, txPower);
363 NS_ASSERT_MSG(std::abs(txPower - Integral(*c)) < 1e-6, "Power allocation failed");
364 return c;
365}
366
369 const std::vector<MHz_u>& centerFrequencies,
370 MHz_u channelWidth,
371 Watt_u txPower,
372 MHz_u guardBandwidth,
373 dBr_u minInnerBand,
374 dBr_u minOuterBand,
375 dBr_u lowestPoint)
376{
377 NS_ASSERT_MSG(centerFrequencies.size() == 1 ||
378 (channelWidth == MHz_u{160} && centerFrequencies.size() <= 2),
379 "PSD for non-contiguous channels is only possible when the total width is 160 "
380 "MHz and cannot be made of more than 2 segments");
381 NS_LOG_FUNCTION(printFrequencies(centerFrequencies)
382 << channelWidth << txPower << guardBandwidth << minInnerBand << minOuterBand
383 << lowestPoint);
384 const Hz_u carrierSpacing{312500};
386 GetSpectrumModel(centerFrequencies, channelWidth, carrierSpacing, guardBandwidth));
387 // assume all frequency segments have the same width, hence split the guard bandwidth
388 // accordingly over the segments
389 guardBandwidth /= centerFrequencies.size();
390 const auto nGuardBands =
391 static_cast<uint32_t>(((MHzToHz(2 * guardBandwidth)) / carrierSpacing) + 0.5);
392 const auto nAllocatedBands =
393 static_cast<uint32_t>((MHzToHz(channelWidth) / carrierSpacing) + 0.5);
394 const auto separationWidth = std::abs(centerFrequencies.back() - centerFrequencies.front());
395 const auto unallocatedWidth =
396 separationWidth > Hz_u{0} ? (separationWidth - (channelWidth / 2)) : 0;
397 const auto nUnallocatedBands =
398 static_cast<uint32_t>((MHzToHz(unallocatedWidth) / carrierSpacing) + 0.5);
399 NS_ASSERT_MSG(c->GetSpectrumModel()->GetNumBands() ==
400 (nAllocatedBands + nGuardBands + nUnallocatedBands + 1),
401 "Unexpected number of bands " << c->GetSpectrumModel()->GetNumBands());
402 auto num20MhzBands = Count20MHzSubchannels(channelWidth);
403 std::size_t numAllocatedSubcarriersPer20MHz = 56;
404 const auto txPowerPerBand = (txPower / numAllocatedSubcarriersPer20MHz) / num20MhzBands;
405 NS_LOG_DEBUG("Power per band " << txPowerPerBand << "W");
406
407 std::size_t numSubcarriersPer20MHz = MHzToHz(MHz_u{20}) / carrierSpacing;
408 std::size_t numUnallocatedSubcarriersPer20MHz =
409 numSubcarriersPer20MHz - numAllocatedSubcarriersPer20MHz;
410 std::vector<std::vector<WifiSpectrumBandIndices>> subBandsPerSegment(
411 centerFrequencies.size()); // list of data/pilot-containing subBands (sent at 0dBr)
412 for (std::size_t i = 0; i < centerFrequencies.size(); ++i)
413 {
414 subBandsPerSegment.at(i).resize(
415 num20MhzBands * 2 / centerFrequencies.size()); // the center subcarrier is skipped,
416 // hence 2 subbands per 20 MHz subchannel
417 }
418 uint32_t start = (nGuardBands / 2) + (numUnallocatedSubcarriersPer20MHz / 2);
419 uint32_t stop;
420 for (auto& subBands : subBandsPerSegment)
421 {
422 for (auto it = subBands.begin(); it != subBands.end();)
423 {
424 stop = start + (numAllocatedSubcarriersPer20MHz / 2) - 1;
425 *it = std::make_pair(start, stop);
426 ++it;
427 start = stop + 2; // skip center subcarrier
428 stop = start + (numAllocatedSubcarriersPer20MHz / 2) - 1;
429 *it = std::make_pair(start, stop);
430 ++it;
431 start = stop + numUnallocatedSubcarriersPer20MHz;
432 }
433 start += nUnallocatedBands;
434 }
435
436 // Prepare spectrum mask specific variables
437 auto innerSlopeWidth = static_cast<uint32_t>(
438 (Hz_u{2e6} / carrierSpacing) +
439 0.5); // size in number of subcarriers of the inner band (2MHz for HT/VHT)
440 WifiSpectrumBandIndices maskBand(0, nAllocatedBands + nGuardBands + nUnallocatedBands);
441
442 // Build transmit spectrum mask
444 subBandsPerSegment,
445 maskBand,
446 txPowerPerBand,
447 nGuardBands,
448 innerSlopeWidth,
449 minInnerBand,
450 minOuterBand,
451 lowestPoint);
452 NormalizeSpectrumMask(c, txPower);
453 NS_ASSERT_MSG(std::abs(txPower - Integral(*c)) < 1e-6, "Power allocation failed");
454 return c;
455}
456
459 MHz_u centerFrequency,
460 MHz_u channelWidth,
461 Watt_u txPower,
462 MHz_u guardBandwidth,
463 dBr_u minInnerBand,
464 dBr_u minOuterBand,
465 dBr_u lowestPoint,
466 const std::vector<bool>& puncturedSubchannels)
467{
468 return CreateHeOfdmTxPowerSpectralDensity(std::vector<MHz_u>{centerFrequency},
469 channelWidth,
470 txPower,
471 guardBandwidth,
472 minInnerBand,
473 minOuterBand,
474 lowestPoint,
475 puncturedSubchannels);
476}
477
480 const std::vector<MHz_u>& centerFrequencies,
481 MHz_u channelWidth,
482 Watt_u txPower,
483 MHz_u guardBandwidth,
484 dBr_u minInnerBand,
485 dBr_u minOuterBand,
486 dBr_u lowestPoint,
487 const std::vector<bool>& puncturedSubchannels)
488{
490 centerFrequencies.size() == 1 || channelWidth == MHz_u{160},
491 "PSD for non-contiguous channels is only possible when the total width is 160 MHz");
493 (channelWidth != 160) || (centerFrequencies.size() <= 2),
494 "It is not possible to create a PSD made of more than 2 segments for a width of 160 MHz");
495 NS_LOG_FUNCTION(printFrequencies(centerFrequencies)
496 << channelWidth << txPower << guardBandwidth << minInnerBand << minOuterBand
497 << lowestPoint);
498 const Hz_u carrierSpacing{78125};
500 GetSpectrumModel(centerFrequencies, channelWidth, carrierSpacing, guardBandwidth));
501 // assume all frequency segments have the same width, hence split the guard bandwidth
502 // accordingly over the segments
503 guardBandwidth /= centerFrequencies.size();
504 const auto nGuardBands =
505 static_cast<uint32_t>(((MHzToHz(2 * guardBandwidth)) / carrierSpacing) + 0.5);
506 const auto separationWidth = std::abs(centerFrequencies.back() - centerFrequencies.front());
507 const auto unallocatedWidth =
508 separationWidth > Hz_u{0} ? (separationWidth - (channelWidth / 2)) : 0;
509 const auto nUnallocatedBands =
510 static_cast<uint32_t>((MHzToHz(unallocatedWidth) / carrierSpacing) + 0.5);
511 const auto nAllocatedBands =
512 static_cast<uint32_t>((MHzToHz(channelWidth) / carrierSpacing) + 0.5);
513 NS_ASSERT_MSG(c->GetSpectrumModel()->GetNumBands() ==
514 (nAllocatedBands + nGuardBands + nUnallocatedBands + 1),
515 "Unexpected number of bands " << c->GetSpectrumModel()->GetNumBands());
516 Watt_u txPowerPerBand{0.0};
517 // Prepare spectrum mask specific variables
518 auto innerSlopeWidth =
519 static_cast<uint32_t>((MHzToHz(MHz_u{1}) / carrierSpacing) +
520 0.5); // size in number of subcarriers of the inner band
521 std::vector<std::vector<WifiSpectrumBandIndices>> subBandsPerSegment(
522 centerFrequencies.size()); // list of data/pilot-containing subBands (sent at 0dBr)
523 WifiSpectrumBandIndices maskBand(0, nAllocatedBands + nGuardBands + nUnallocatedBands);
524 std::size_t skippedSubbands{0};
525 std::size_t numAllocatedSubbands{0};
526 std::size_t numDc{0};
527 switch (static_cast<uint16_t>(channelWidth))
528 {
529 case 20:
530 // 242 subcarriers (234 data + 8 pilot)
531 txPowerPerBand = txPower / 242;
532 innerSlopeWidth = static_cast<uint32_t>((Hz_u{5e5} / carrierSpacing) +
533 0.5); // [-10.25;-9.75] & [9.75;10.25]
534 skippedSubbands = 6;
535 numAllocatedSubbands = 121;
536 numDc = 3;
537 break;
538 case 40:
539 // 484 subcarriers (468 data + 16 pilot)
540 txPowerPerBand = txPower / 484;
541 skippedSubbands = 12;
542 numAllocatedSubbands = 242;
543 numDc = 5;
544 break;
545 case 80:
546 case 160: // 2 x 80 MHz
547 case 320: // 4 x 80 MHz
548 // 996 subcarriers (980 data + 16 pilot) per 80 MHz band
549 txPowerPerBand = txPower / (996 * (channelWidth / 80));
550 skippedSubbands = 12;
551 numAllocatedSubbands = 498;
552 numDc = 5;
553 break;
554 default:
555 NS_FATAL_ERROR("ChannelWidth " << channelWidth << " unsupported");
556 break;
557 }
558
559 // skip the guard band and skipped subbands, then place power in allocated subbands, then skip
560 // DC subbands, then place power in allocated subbands, then skip the final 11 subbands and the
561 // guard band.
562 auto start = (nGuardBands / 2) + skippedSubbands;
563 auto stop = start + numAllocatedSubbands - 1;
564 subBandsPerSegment.at(0).emplace_back(start, stop);
565 start = stop + numDc + 1;
566 stop = start + numAllocatedSubbands - 1;
567 subBandsPerSegment.at(0).emplace_back(start, stop);
568 if (channelWidth >= 160)
569 {
570 for (std::size_t i = 1; i < (channelWidth / 80); ++i)
571 {
572 start = subBandsPerSegment.front().back().second + (2 * skippedSubbands) +
573 nUnallocatedBands;
574 auto stop = start + numAllocatedSubbands - 1;
575 subBandsPerSegment.back().emplace_back(start, stop);
576 start = stop + numDc + 1;
577 stop = start + numAllocatedSubbands - 1;
578 subBandsPerSegment.back().emplace_back(start, stop);
579 }
580 }
581
582 // Create punctured bands
583 auto puncturedSlopeWidth =
584 static_cast<uint32_t>((Hz_u{500e3} / carrierSpacing) +
585 0.5); // size in number of subcarriers of the punctured slope band
586 std::vector<std::vector<WifiSpectrumBandIndices>> puncturedBandsPerSegment;
587 std::size_t subcarriersPerSuband = (MHzToHz(MHz_u{20}) / carrierSpacing);
588 start = (nGuardBands / 2);
589 stop = start + subcarriersPerSuband - 1;
590 if (!puncturedSubchannels.empty())
591 {
592 for (std::size_t i = 0; i < subBandsPerSegment.size(); ++i)
593 {
594 puncturedBandsPerSegment.emplace_back();
595 }
596 }
597 std::size_t prevPsdIndex = 0;
598 for (std::size_t i = 0; i < puncturedSubchannels.size(); ++i)
599 {
600 std::size_t psdIndex = (puncturedBandsPerSegment.size() == 1)
601 ? 0
602 : ((i < (puncturedSubchannels.size() / 2)) ? 0 : 1);
603 if (psdIndex != prevPsdIndex)
604 {
605 start += nUnallocatedBands;
606 stop += nUnallocatedBands;
607 }
608 if (puncturedSubchannels.at(i))
609 {
610 puncturedBandsPerSegment.at(psdIndex).emplace_back(start, stop);
611 }
612 start = stop + 1;
613 stop = start + subcarriersPerSuband - 1;
614 prevPsdIndex = psdIndex;
615 }
616
617 // Build transmit spectrum mask
619 subBandsPerSegment,
620 maskBand,
621 txPowerPerBand,
622 nGuardBands,
623 innerSlopeWidth,
624 minInnerBand,
625 minOuterBand,
626 lowestPoint,
627 puncturedBandsPerSegment,
628 puncturedSlopeWidth);
629 NormalizeSpectrumMask(c, txPower);
630 NS_ASSERT_MSG(std::abs(txPower - Integral(*c)) < 1e-6, "Power allocation failed");
631 return c;
632}
633
636 const std::vector<MHz_u>& centerFrequencies,
637 MHz_u channelWidth,
638 Watt_u txPower,
639 MHz_u guardBandwidth,
640 const std::vector<WifiSpectrumBandIndices>& ru)
641{
642 auto printRuIndices = [](const std::vector<WifiSpectrumBandIndices>& v) {
643 std::stringstream ss;
644 for (const auto& [start, stop] : v)
645 {
646 ss << start << "-" << stop << " ";
647 }
648 return ss.str();
649 };
650 NS_LOG_FUNCTION(printFrequencies(centerFrequencies)
651 << channelWidth << txPower << guardBandwidth << printRuIndices(ru));
652 const Hz_u carrierSpacing{78125};
654 GetSpectrumModel(centerFrequencies, channelWidth, carrierSpacing, guardBandwidth));
655
656 // Build spectrum mask
657 auto vit = c->ValuesBegin();
658 auto bit = c->ConstBandsBegin();
659 const auto numSubcarriers =
660 std::accumulate(ru.cbegin(), ru.cend(), 0, [](uint32_t sum, const auto& p) {
661 return sum + (p.second - p.first) + 1;
662 });
663 const auto txPowerPerBand = (txPower / numSubcarriers); // FIXME: null subcarriers
664 uint32_t numBands = c->GetSpectrumModel()->GetNumBands();
665 const auto psd = txPowerPerBand / (bit->fh - bit->fl);
666 for (size_t i = 0; i < numBands; i++, vit++, bit++)
667 {
668 const auto allocated = std::any_of(ru.cbegin(), ru.cend(), [i](const auto& p) {
669 return (i >= p.first && i <= p.second);
670 });
671 *vit = allocated ? psd : 0.0;
672 }
673
674 return c;
675}
676
677void
680 const std::vector<std::vector<WifiSpectrumBandIndices>>& allocatedSubBandsPerSegment,
681 const WifiSpectrumBandIndices& maskBand,
682 Watt_u txPowerPerBand,
683 uint32_t nGuardBands,
684 uint32_t innerSlopeWidth,
685 dBr_u minInnerBand,
686 dBr_u minOuterBand,
687 dBr_u lowestPoint,
688 const std::vector<std::vector<WifiSpectrumBandIndices>>& puncturedBandsPerSegment,
689 uint32_t puncturedSlopeWidth)
690{
691 NS_ASSERT_MSG(allocatedSubBandsPerSegment.size() <= 2,
692 "Only PSDs for up to 2 frequency segments are supported");
693 NS_ASSERT(puncturedBandsPerSegment.empty() ||
694 (puncturedBandsPerSegment.size() == allocatedSubBandsPerSegment.size()));
695 NS_LOG_FUNCTION(c << allocatedSubBandsPerSegment.front().front().first
696 << allocatedSubBandsPerSegment.front().back().second << maskBand.first
697 << maskBand.second << txPowerPerBand << nGuardBands << innerSlopeWidth
698 << minInnerBand << minOuterBand << lowestPoint << puncturedSlopeWidth);
699 uint32_t numSubBands = allocatedSubBandsPerSegment.front().size();
700 uint32_t numBands = c->GetSpectrumModel()->GetNumBands();
701 uint32_t numMaskBands = maskBand.second - maskBand.first + 1;
702 NS_ASSERT(numSubBands && numBands && numMaskBands);
703 NS_LOG_LOGIC("Power per band " << txPowerPerBand << "W");
704
705 // Different power levels
706 dBm_u txPowerRef{10.0 * std::log10(txPowerPerBand * 1000.0)};
707 dBm_u txPowerInnerBandMin{txPowerRef + minInnerBand};
708 dBm_u txPowerMiddleBandMin{txPowerRef + minOuterBand};
709 dBm_u txPowerOuterBandMin{txPowerRef +
710 lowestPoint}; // TODO also take into account dBm/MHz constraints
711
712 // Different widths (in number of bands)
713 uint32_t outerSlopeWidth =
714 nGuardBands / 4; // nGuardBands is the total left+right guard band. The left/right outer
715 // part is half of the left/right guard band.
716 uint32_t middleSlopeWidth = outerSlopeWidth - (innerSlopeWidth / 2);
717
718 std::vector<WifiSpectrumBandIndices> outerBandsLeft;
719 std::vector<WifiSpectrumBandIndices> middleBandsLeft;
720 std::vector<WifiSpectrumBandIndices> flatJunctionsLeft;
721 std::vector<WifiSpectrumBandIndices> innerBandsLeft;
722 std::vector<WifiSpectrumBandIndices> allocatedSubBands;
723 std::vector<WifiSpectrumBandIndices> innerBandsRight;
724 std::vector<WifiSpectrumBandIndices> flatJunctionsRight;
725 std::vector<WifiSpectrumBandIndices> middleBandsRight;
726 std::vector<WifiSpectrumBandIndices> outerBandsRight;
727 std::optional<WifiSpectrumBandIndices> betweenPsdsBand;
728
729 allocatedSubBands.emplace_back(allocatedSubBandsPerSegment.front().front().first,
730 allocatedSubBandsPerSegment.front().back().second);
731 outerBandsLeft.emplace_back(maskBand.first, // to handle cases where allocated channel is
732 // under WifiPhy configured channel width.
733 maskBand.first + outerSlopeWidth - 1);
734 middleBandsLeft.emplace_back(outerBandsLeft.front().second + 1,
735 outerBandsLeft.front().second + middleSlopeWidth);
736 innerBandsLeft.emplace_back(allocatedSubBands.front().first - innerSlopeWidth,
737 allocatedSubBands.front().first -
738 1); // better to place slope based on allocated subcarriers
739 flatJunctionsLeft.emplace_back(middleBandsLeft.front().second + 1,
740 innerBandsLeft.front().first -
741 1); // in order to handle shift due to guard subcarriers
742 uint32_t flatJunctionWidth =
743 flatJunctionsLeft.front().second - flatJunctionsLeft.front().first + 1;
744 innerBandsRight.emplace_back(allocatedSubBands.front().second + 1,
745 allocatedSubBands.front().second + innerSlopeWidth);
746 flatJunctionsRight.emplace_back(innerBandsRight.front().second + 1,
747 innerBandsRight.front().second + flatJunctionWidth);
748 middleBandsRight.emplace_back(flatJunctionsRight.front().second + 1,
749 flatJunctionsRight.front().second + middleSlopeWidth);
750 outerBandsRight.emplace_back(middleBandsRight.front().second + 1,
751 middleBandsRight.front().second + outerSlopeWidth);
752
753 if (allocatedSubBandsPerSegment.size() > 1)
754 {
755 const auto offset = (((allocatedSubBandsPerSegment.front().back().second -
756 allocatedSubBandsPerSegment.front().front().first) /
757 2) +
758 (allocatedSubBandsPerSegment.back().front().first -
759 allocatedSubBandsPerSegment.front().back().second) +
760 ((allocatedSubBandsPerSegment.back().back().second -
761 allocatedSubBandsPerSegment.back().front().first) /
762 2));
763 outerBandsLeft.emplace_back(outerBandsLeft.front().first + offset,
764 outerBandsLeft.front().second + offset);
765 middleBandsLeft.emplace_back(middleBandsLeft.front().first + offset,
766 middleBandsLeft.front().second + offset);
767 flatJunctionsLeft.emplace_back(flatJunctionsLeft.front().first + offset,
768 flatJunctionsLeft.front().second + offset);
769 innerBandsLeft.emplace_back(innerBandsLeft.front().first + offset,
770 innerBandsLeft.front().second + offset);
771 allocatedSubBands.emplace_back(allocatedSubBands.front().first + offset,
772 allocatedSubBands.front().second + offset);
773 innerBandsRight.emplace_back(innerBandsRight.front().first + offset,
774 innerBandsRight.front().second + offset);
775 flatJunctionsRight.emplace_back(flatJunctionsRight.front().first + offset,
776 flatJunctionsRight.front().second + offset);
777 middleBandsRight.emplace_back(middleBandsRight.front().first + offset,
778 middleBandsRight.front().second + offset);
779 outerBandsRight.emplace_back(outerBandsRight.front().first + offset,
780 outerBandsRight.front().second + offset);
781 betweenPsdsBand.emplace(middleBandsRight.front().first, middleBandsLeft.back().second);
782 }
783
784 std::ostringstream ss;
785 for (std::size_t i = 0; i < allocatedSubBandsPerSegment.size(); ++i)
786 {
787 if (allocatedSubBandsPerSegment.size() > 1)
788 {
789 ss << "PSD" << i + 1 << ": ";
790 }
791 ss << "outerBandLeft=[" << outerBandsLeft.at(i).first << ";" << outerBandsLeft.at(i).second
792 << "] "
793 << "middleBandLeft=[" << middleBandsLeft.at(i).first << ";"
794 << middleBandsLeft.at(i).second << "] "
795 << "flatJunctionLeft=[" << flatJunctionsLeft.at(i).first << ";"
796 << flatJunctionsLeft.at(i).second << "] "
797 << "innerBandLeft=[" << innerBandsLeft.at(i).first << ";" << innerBandsLeft.at(i).second
798 << "] "
799 << "allocatedBand=[" << allocatedSubBands.at(i).first << ";"
800 << allocatedSubBands.at(i).second << "] ";
801 if (!puncturedBandsPerSegment.empty() && !puncturedBandsPerSegment.at(i).empty())
802 {
803 ss << "puncturedBands=[" << puncturedBandsPerSegment.at(i).front().first << ";"
804 << puncturedBandsPerSegment.at(i).back().second << "] ";
805 }
806 ss << "innerBandRight=[" << innerBandsRight.at(i).first << ";"
807 << innerBandsRight.at(i).second << "] "
808 << "flatJunctionRight=[" << flatJunctionsRight.at(i).first << ";"
809 << flatJunctionsRight.at(i).second << "] "
810 << "middleBandRight=[" << middleBandsRight.at(i).first << ";"
811 << middleBandsRight.at(i).second << "] "
812 << "outerBandRight=[" << outerBandsRight.at(i).first << ";"
813 << outerBandsRight.at(i).second << "] ";
814 }
815 if (allocatedSubBandsPerSegment.size() > 1)
816 {
817 ss << "=> PSD: "
818 << "outerBandLeft=[" << outerBandsLeft.front().first << ";"
819 << outerBandsLeft.front().second << "] "
820 << "middleBandLeft=[" << middleBandsLeft.front().first << ";"
821 << middleBandsLeft.front().second << "] "
822 << "flatJunctionLeft=[" << flatJunctionsLeft.front().first << ";"
823 << flatJunctionsLeft.front().second << "] "
824 << "innerBandLeft=[" << innerBandsLeft.front().first << ";"
825 << innerBandsLeft.front().second << "] "
826 << "allocatedBandInPsd1=[" << allocatedSubBands.front().first << ";"
827 << allocatedSubBands.front().second << "] ";
828 if (!puncturedBandsPerSegment.empty() && !puncturedBandsPerSegment.front().empty())
829 {
830 ss << "puncturedBandsInPsd1=[" << puncturedBandsPerSegment.front().front().first << ";"
831 << puncturedBandsPerSegment.front().back().second << "] ";
832 }
833 ss << "flatJunctionRightPsd1=[" << flatJunctionsRight.front().first << ";"
834 << flatJunctionsRight.front().second << "] "
835 << "linearSum=[" << betweenPsdsBand->first << ";" << betweenPsdsBand->second << "] "
836 << "flatJunctionLeftPsd2=[" << flatJunctionsLeft.back().first << ";"
837 << flatJunctionsLeft.back().second << "] "
838 << "innerBandLeftPsd2=[" << innerBandsLeft.back().first << ";"
839 << innerBandsLeft.back().second << "] "
840 << "allocatedBandInPsd2=[" << allocatedSubBands.back().first << ";"
841 << allocatedSubBands.back().second << "] ";
842 if (!puncturedBandsPerSegment.empty() && !puncturedBandsPerSegment.back().empty())
843 {
844 ss << "puncturedBandsInPsd2=[" << puncturedBandsPerSegment.back().front().first << ";"
845 << puncturedBandsPerSegment.back().back().second << "] ";
846 }
847 ss << "innerBandRight=[" << innerBandsRight.back().first << ";"
848 << innerBandsRight.back().second << "] "
849 << "flatJunctionRight=[" << flatJunctionsRight.back().first << ";"
850 << flatJunctionsRight.back().second << "] "
851 << "middleBandRight=[" << middleBandsRight.back().first << ";"
852 << middleBandsRight.back().second << "] "
853 << "outerBandRight=[" << outerBandsRight.back().first << ";"
854 << outerBandsRight.back().second << "] ";
855 }
856 NS_LOG_DEBUG(ss.str());
857 NS_ASSERT(maskBand.second == outerBandsRight.back().second);
858 NS_ASSERT(numMaskBands ==
859 ((allocatedSubBandsPerSegment.back().back().second -
860 allocatedSubBandsPerSegment.front().front().first +
861 1) // equivalent to allocatedBand (includes notches and DC)
862 + 2 * (innerSlopeWidth + middleSlopeWidth + outerSlopeWidth + flatJunctionWidth)));
863
864 // Different slopes
865 double innerSlope = (-1.0 * minInnerBand) / innerSlopeWidth;
866 double middleSlope = (-1.0 * (minOuterBand - minInnerBand)) / middleSlopeWidth;
867 double outerSlope = (txPowerMiddleBandMin - txPowerOuterBandMin) / outerSlopeWidth;
868 double puncturedSlope = (-1.0 * minInnerBand) / puncturedSlopeWidth;
869
870 // Build spectrum mask
871 Watt_u previousTxPower{0.0};
872 std::vector<Watt_u> txPowerValues(numBands);
873 NS_ASSERT(txPowerValues.size() == numBands);
874 for (size_t i = 0; i < numBands; ++i)
875 {
876 size_t psdIndex =
877 (allocatedSubBandsPerSegment.size() == 1) ? 0 : ((i < (numBands / 2)) ? 0 : 1);
878 Watt_u txPower{0.0};
879 if (i < maskBand.first || i > maskBand.second) // outside the spectrum mask
880 {
881 txPower = Watt_u{0.0};
882 }
883 else if (betweenPsdsBand.has_value() &&
884 (i <= betweenPsdsBand->second && i >= betweenPsdsBand->first))
885 {
886 // value for PSD mask 1
887 std::vector<Watt_u> txPowerWPsds(2);
888 if (i <= middleBandsRight.at(0).second && i >= middleBandsRight.at(0).first)
889 {
890 txPowerWPsds.at(0) =
891 DbmToW(txPowerInnerBandMin -
892 ((i - middleBandsRight.at(0).first + 1) *
893 middleSlope)); // +1 so as to be symmetric with left slope
894 }
895 else if (i <= outerBandsRight.at(0).second && i >= outerBandsRight.at(0).first)
896 {
897 txPowerWPsds.at(0) =
898 DbmToW(txPowerMiddleBandMin -
899 ((i - outerBandsRight.at(0).first + 1) *
900 outerSlope)); // +1 so as to be symmetric with left slope
901 }
902 else if (i > outerBandsRight.at(0).second)
903 {
904 txPower = DbmToW(txPowerOuterBandMin);
905 }
906 else
907 {
908 NS_ASSERT(false);
909 }
910
911 // value for PSD mask 2
912 if (i < outerBandsLeft.at(1).first)
913 {
914 txPower = DbmToW(txPowerOuterBandMin);
915 }
916 else if (i <= outerBandsLeft.at(1).second && i >= outerBandsLeft.at(1).first)
917 {
918 txPowerWPsds.at(1) =
919 DbmToW(txPowerOuterBandMin + ((i - outerBandsLeft.at(1).first) * outerSlope));
920 }
921 else if (i <= middleBandsLeft.at(1).second && i >= middleBandsLeft.at(1).first)
922 {
923 txPowerWPsds.at(1) = DbmToW(txPowerMiddleBandMin +
924 ((i - middleBandsLeft.at(1).first) * middleSlope));
925 }
926 else
927 {
928 NS_ASSERT(false);
929 }
930
931 txPower = std::accumulate(txPowerWPsds.cbegin(), txPowerWPsds.cend(), Watt_u{0.0});
932 txPower = std::max(DbmToW(txPowerRef - 25.0), txPower);
933 txPower = std::min(DbmToW(txPowerRef - 20.0), txPower);
934 }
935 else if (i <= outerBandsLeft.at(psdIndex).second &&
936 i >= outerBandsLeft.at(psdIndex)
937 .first) // better to put greater first (less computation)
938 {
939 txPower = DbmToW(txPowerOuterBandMin +
940 ((i - outerBandsLeft.at(psdIndex).first) * outerSlope));
941 }
942 else if (i <= middleBandsLeft.at(psdIndex).second &&
943 i >= middleBandsLeft.at(psdIndex).first)
944 {
945 txPower = DbmToW(txPowerMiddleBandMin +
946 ((i - middleBandsLeft.at(psdIndex).first) * middleSlope));
947 }
948 else if ((i <= flatJunctionsLeft.at(psdIndex).second &&
949 i >= flatJunctionsLeft.at(psdIndex).first) ||
950 (i <= flatJunctionsRight.at(psdIndex).second &&
951 i >= flatJunctionsRight.at(psdIndex).first))
952 {
953 txPower = DbmToW(txPowerInnerBandMin);
954 }
955 else if (i <= innerBandsLeft.at(psdIndex).second && i >= innerBandsLeft.at(psdIndex).first)
956 {
957 txPower = (!puncturedBandsPerSegment.empty() &&
958 !puncturedBandsPerSegment.at(psdIndex).empty() &&
959 (puncturedBandsPerSegment.at(psdIndex).front().first <=
960 allocatedSubBandsPerSegment.at(psdIndex).front().first))
961 ? DbmToW(txPowerInnerBandMin)
962 : // first 20 MHz band is punctured
963 DbmToW(txPowerInnerBandMin +
964 ((i - innerBandsLeft.at(psdIndex).first) * innerSlope));
965 }
966 else if ((i <= allocatedSubBandsPerSegment.at(psdIndex).back().second &&
967 i >= allocatedSubBandsPerSegment.at(psdIndex).front().first)) // roughly in
968 // allocated band
969 {
970 bool insideSubBand = false;
971 for (uint32_t j = 0; !insideSubBand && j < numSubBands;
972 j++) // continue until inside a sub-band
973 {
974 insideSubBand = (i <= allocatedSubBandsPerSegment.at(psdIndex)[j].second) &&
975 (i >= allocatedSubBandsPerSegment.at(psdIndex)[j].first);
976 }
977 if (insideSubBand)
978 {
979 bool insidePuncturedSubBand = false;
980 std::size_t j = 0;
981 std::size_t puncturedBandSize = !puncturedBandsPerSegment.empty()
982 ? puncturedBandsPerSegment.at(psdIndex).size()
983 : 0;
984 for (; !insidePuncturedSubBand && j < puncturedBandSize;
985 j++) // continue until inside a punctured sub-band
986 {
987 insidePuncturedSubBand =
988 (i <= puncturedBandsPerSegment.at(psdIndex).at(j).second) &&
989 (i >= puncturedBandsPerSegment.at(psdIndex).at(j).first);
990 }
991 if (insidePuncturedSubBand)
992 {
993 uint32_t startPuncturedSlope =
994 (puncturedBandsPerSegment.at(psdIndex)
995 .at(puncturedBandsPerSegment.at(psdIndex).size() - 1)
996 .second -
997 puncturedSlopeWidth); // only consecutive subchannels can be punctured
998 if (i >= startPuncturedSlope)
999 {
1000 txPower = DbmToW(txPowerInnerBandMin +
1001 ((i - startPuncturedSlope) * puncturedSlope));
1002 }
1003 else
1004 {
1005 txPower = std::max(
1006 DbmToW(txPowerInnerBandMin),
1007 DbmToW(txPowerRef -
1008 ((i - puncturedBandsPerSegment.at(psdIndex).at(0).first) *
1009 puncturedSlope)));
1010 }
1011 }
1012 else
1013 {
1014 txPower = txPowerPerBand;
1015 }
1016 }
1017 else
1018 {
1019 txPower = DbmToW(txPowerInnerBandMin);
1020 }
1021 }
1022 else if (i <= innerBandsRight.at(psdIndex).second &&
1023 i >= innerBandsRight.at(psdIndex).first)
1024 {
1025 // take min to handle the case where last 20 MHz band is punctured
1026 txPower = std::min(
1027 previousTxPower,
1028 DbmToW(txPowerRef - ((i - innerBandsRight.at(psdIndex).first + 1) *
1029 innerSlope))); // +1 so as to be symmetric with left slope
1030 }
1031 else if (i <= middleBandsRight.at(psdIndex).second &&
1032 i >= middleBandsRight.at(psdIndex).first)
1033 {
1034 txPower = DbmToW(txPowerInnerBandMin -
1035 ((i - middleBandsRight.at(psdIndex).first + 1) *
1036 middleSlope)); // +1 so as to be symmetric with left slope
1037 }
1038 else if (i <= outerBandsRight.at(psdIndex).second &&
1039 i >= outerBandsRight.at(psdIndex).first)
1040 {
1041 txPower = DbmToW(txPowerMiddleBandMin -
1042 ((i - outerBandsRight.at(psdIndex).first + 1) *
1043 outerSlope)); // +1 so as to be symmetric with left slope
1044 }
1045 else
1046 {
1047 NS_FATAL_ERROR("Should have handled all cases");
1048 }
1049 NS_LOG_LOGIC(i << " -> " << (10 * std::log10(txPower / txPowerPerBand)));
1050 previousTxPower = txPower;
1051 txPowerValues.at(i) = txPower;
1052 }
1053
1054 // fill in spectrum mask
1055 auto vit = c->ValuesBegin();
1056 auto bit = c->ConstBandsBegin();
1057 const auto invBandwidth = 1 / (bit->fh - bit->fl);
1058 for (auto txPowerValue : txPowerValues)
1059 {
1060 *vit = txPowerValue * invBandwidth;
1061 vit++;
1062 bit++;
1063 }
1064
1065 for (const auto& allocatedSubBands : allocatedSubBandsPerSegment)
1066 {
1067 NS_LOG_INFO("Added signal power to subbands " << allocatedSubBands.front().first << "-"
1068 << allocatedSubBands.back().second);
1069 }
1070}
1071
1072void
1074{
1075 NS_LOG_FUNCTION(c << txPower);
1076 // Normalize power so that total signal power equals transmit power
1077 Watt_u currentTxPower{Integral(*c)};
1078 double normalizationRatio [[maybe_unused]] = currentTxPower / txPower;
1079 double invNormalizationRatio = txPower / currentTxPower;
1080 NS_LOG_LOGIC("Current power: " << currentTxPower << "W vs expected power: " << txPower << "W"
1081 << " -> ratio (C/E) = " << normalizationRatio);
1082 auto vit = c->ValuesBegin();
1083 for (size_t i = 0; i < c->GetSpectrumModel()->GetNumBands(); i++, vit++)
1084 {
1085 *vit = (*vit) * invNormalizationRatio;
1086 }
1087}
1088
1089Watt_u
1091 const std::vector<WifiSpectrumBandIndices>& segments)
1092{
1093 auto powerWattPerHertz{0.0};
1094 auto bandIt = psd->ConstBandsBegin() + segments.front().first; // all bands have same width
1095 const auto bandWidth = (bandIt->fh - bandIt->fl);
1096 NS_ASSERT_MSG(bandWidth >= 0.0,
1097 "Invalid width for subband [" << bandIt->fl << ";" << bandIt->fh << "]");
1098 for (const auto& [start, stop] : segments)
1099 {
1100 auto valueIt = psd->ConstValuesBegin() + start;
1101 auto end = psd->ConstValuesBegin() + stop;
1102 uint32_t index [[maybe_unused]] = 0;
1103 while (valueIt <= end)
1104 {
1105 NS_ASSERT_MSG(*valueIt >= 0.0,
1106 "Invalid power value " << *valueIt << " in subband " << index);
1107 powerWattPerHertz += *valueIt;
1108 ++valueIt;
1109 ++index;
1110 }
1111 }
1112 const Watt_u power{powerWattPerHertz * bandWidth};
1113 NS_ASSERT_MSG(power >= 0.0, "Invalid calculated power " << power);
1114 return power;
1115}
1116
1117bool
1118operator<(const FrequencyRange& left, const FrequencyRange& right)
1119{
1120 return left.minFrequency < right.minFrequency;
1121}
1122
1123bool
1124operator==(const FrequencyRange& left, const FrequencyRange& right)
1125{
1126 return (left.minFrequency == right.minFrequency) && (left.maxFrequency == right.maxFrequency);
1127}
1128
1129bool
1130operator!=(const FrequencyRange& left, const FrequencyRange& right)
1131{
1132 return !(left == right);
1133}
1134
1135std::ostream&
1136operator<<(std::ostream& os, const FrequencyRange& freqRange)
1137{
1138 os << "[" << freqRange.minFrequency << " MHz - " << freqRange.maxFrequency << " MHz]";
1139 return os;
1140}
1141
1142} // namespace ns3
Smart pointer class similar to boost::intrusive_ptr.
static Ptr< SpectrumValue > CreateHtOfdmTxPowerSpectralDensity(const std::vector< MHz_u > &centerFrequencies, MHz_u channelWidth, Watt_u txPower, MHz_u guardBandwidth, dBr_u minInnerBand=dBr_u{-20}, dBr_u minOuterband=dBr_u{-28}, dBr_u lowestPoint=dBr_u{-40})
Create a transmit power spectral density corresponding to OFDM High Throughput (HT/VHT) (802....
static Ptr< SpectrumValue > CreateDsssTxPowerSpectralDensity(MHz_u centerFrequency, Watt_u txPower, MHz_u guardBandwidth)
Create a transmit power spectral density corresponding to DSSS.
static Ptr< SpectrumValue > CreateDuplicated20MhzTxPowerSpectralDensity(const std::vector< MHz_u > &centerFrequencies, MHz_u channelWidth, Watt_u txPower, MHz_u guardBandwidth, dBr_u minInnerBand=dBr_u{-20}, dBr_u minOuterband=dBr_u{-28}, dBr_u lowestPoint=dBr_u{-40}, const std::vector< bool > &puncturedSubchannels={})
Create a transmit power spectral density corresponding to OFDM duplicated over multiple 20 MHz subcha...
static void CreateSpectrumMaskForOfdm(Ptr< SpectrumValue > c, const std::vector< std::vector< WifiSpectrumBandIndices > > &allocatedSubBandsPerSegment, const WifiSpectrumBandIndices &maskBand, Watt_u txPowerPerBand, uint32_t nGuardBands, uint32_t innerSlopeWidth, dBr_u minInnerBand, dBr_u minOuterband, dBr_u lowestPoint, const std::vector< std::vector< WifiSpectrumBandIndices > > &puncturedSubBands={}, uint32_t puncturedSlopeWidth=0)
Create a transmit power spectral density corresponding to OFDM transmit spectrum mask requirements fo...
static Ptr< SpectrumModel > GetSpectrumModel(const std::vector< MHz_u > &centerFrequencies, MHz_u channelWidth, Hz_u carrierSpacing, MHz_u guardBandwidth)
Return a SpectrumModel instance corresponding to the center frequency and channel width.
static Watt_u GetBandPowerW(Ptr< SpectrumValue > psd, const std::vector< WifiSpectrumBandIndices > &segments)
Calculate the power of the specified band composed of uniformly-sized sub-bands.
static Ptr< SpectrumValue > CreateHeMuOfdmTxPowerSpectralDensity(const std::vector< MHz_u > &centerFrequencies, MHz_u channelWidth, Watt_u txPower, MHz_u guardBandwidth, const std::vector< WifiSpectrumBandIndices > &ru)
Create a transmit power spectral density corresponding to the OFDMA part of HE TB PPDUs for a given R...
static Ptr< SpectrumValue > CreateOfdmTxPowerSpectralDensity(MHz_u centerFrequency, MHz_u channelWidth, Watt_u txPower, MHz_u guardBandwidth, dBr_u minInnerBand=dBr_u{-20}, dBr_u minOuterband=dBr_u{-28}, dBr_u lowestPoint=dBr_u{-40})
Create a transmit power spectral density corresponding to OFDM (802.11a/g).
static Ptr< SpectrumValue > CreateHeOfdmTxPowerSpectralDensity(MHz_u centerFrequency, MHz_u channelWidth, Watt_u txPower, MHz_u guardBandwidth, dBr_u minInnerBand=dBr_u{-20}, dBr_u minOuterband=dBr_u{-28}, dBr_u lowestPoint=dBr_u{-40}, const std::vector< bool > &puncturedSubchannels={})
Create a transmit power spectral density corresponding to OFDM High Efficiency (HE/EHT) (802....
static void NormalizeSpectrumMask(Ptr< SpectrumValue > c, Watt_u txPower)
Normalize the transmit spectrum mask generated by CreateSpectrumMaskForOfdm so that the total transmi...
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition assert.h:75
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:271
#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 > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
auto printFrequencies
Lambda to print a vector of frequencies.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
bool operator!=(Callback< R, Args... > a, Callback< R, Args... > b)
Inequality test.
Definition callback.h:658
bool operator==(const EventId &a, const EventId &b)
Definition event-id.h:155
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition angles.cc:148
double Integral(const SpectrumValue &arg)
std::vector< BandInfo > Bands
Container of BandInfo.
std::size_t Count20MHzSubchannels(MHz_u channelWidth)
Return the number of 20 MHz subchannels covering the channel width.
Definition wifi-utils.h:138
bool operator<(const EventId &a, const EventId &b)
Definition event-id.h:168
static std::map< WifiSpectrumModelId, Ptr< SpectrumModel > > g_wifiSpectrumModelMap
static initializer for the class
Watt_u DbmToW(dBm_u val)
Convert from dBm to Watts.
Definition wifi-utils.cc:32
Hz_u MHzToHz(MHz_u val)
Convert from MHz to Hz.
Definition wifi-utils.h:113
double Watt_u
Watt weak type.
Definition wifi-units.h:25
std::pair< uint32_t, uint32_t > WifiSpectrumBandIndices
typedef for a pair of start and stop sub-band indices
The building block of a SpectrumModel.
double fc
center frequency
double fl
lower limit of subband
double fh
upper limit of subband
Struct defining a frequency range between minFrequency and maxFrequency.
MHz_u minFrequency
the minimum frequency
MHz_u maxFrequency
the maximum frequency
Wifi Spectrum Model structure.
std::vector< MHz_u > centerFrequencies
center frequency per segment