A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
spectrum-model.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 CTTC
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Nicola Baldo <nbaldo@cttc.es>
7 */
8
9#include "spectrum-model.h"
10
11#include "ns3/assert.h"
12#include "ns3/log.h"
13
14#include <algorithm>
15#include <cmath>
16#include <cstddef>
17#include <limits>
18
19namespace ns3
20{
21
22NS_LOG_COMPONENT_DEFINE("SpectrumModel");
23
24bool
25operator==(const SpectrumModel& lhs, const SpectrumModel& rhs)
26{
27 return (lhs.m_uid == rhs.m_uid);
28}
29
31
32SpectrumModel::SpectrumModel(const std::vector<double>& centerFreqs)
33{
34 NS_LOG_FUNCTION(this << centerFreqs);
35 NS_ASSERT(centerFreqs.size() > 1);
36 m_bands.reserve(centerFreqs.size());
37 for (auto it = centerFreqs.begin(); it != centerFreqs.end(); ++it)
38 {
39 BandInfo e;
40 e.fc = *it;
41 if (it == centerFreqs.begin())
42 {
43 double delta = ((*(it + 1)) - (*it)) / 2;
44 e.fl = *it - delta;
45 e.fh = *it + delta;
46 }
47 else if (it == centerFreqs.end() - 1)
48 {
49 double delta = ((*it) - (*(it - 1))) / 2;
50 e.fl = *it - delta;
51 e.fh = *it + delta;
52 }
53 else
54 {
55 e.fl = ((*it) + (*(it - 1))) / 2;
56 e.fh = ((*(it + 1)) + (*it)) / 2;
57 }
58 m_bands.push_back(e);
59 }
60 InitModel();
61}
62
64{
65 NS_LOG_FUNCTION(this);
66 m_bands = bands;
67 InitModel();
68}
69
71 : m_bands(std::move(bands))
72{
73 NS_LOG_FUNCTION(this);
74 InitModel();
75}
76
77void
79{
80 m_uid = ++m_uidCount;
81 NS_LOG_INFO("creating new SpectrumModel, m_uid=" << m_uid);
82 // sort bands by increasing frequency
83 std::sort(m_bands.begin(), m_bands.end());
84 // check if bands are contiguous, i.e. if the upper limit of a band is equal to the lower limit
85 // of the next band
86 m_contiguousBands = true;
87 if (!m_bands.empty())
88 {
89 auto it = m_bands.cbegin();
90 auto currentFh = it->fh;
91 for (++it; it != m_bands.cend(); ++it)
92 {
93 if (it->fl > currentFh + std::numeric_limits<double>::epsilon())
94 {
95 m_contiguousBands = false;
96 break;
97 }
98 if (it->fh > currentFh)
99 {
100 currentFh = it->fh;
101 }
102 }
103 }
104 // check if all bands have the same size
105 const auto bandSize = m_bands.cbegin()->fh - m_bands.cbegin()->fl;
107 (m_bands.size() == 1) ||
108 std::all_of(m_bands.cbegin(), m_bands.cend(), [bandSize](const auto& b) {
109 return std::abs((b.fh - b.fl) - bandSize) <= std::numeric_limits<double>::epsilon();
110 });
111}
112
113Bands::const_iterator
115{
116 return m_bands.begin();
117}
118
119Bands::const_iterator
121{
122 return m_bands.end();
123}
124
125size_t
127{
128 return m_bands.size();
129}
130
133{
134 return m_uid;
135}
136
137bool
139{
141 {
142 return (m_bands.rbegin()->fh <= other.m_bands.begin()->fl) ||
143 (other.m_bands.rbegin()->fh <= m_bands.begin()->fl);
144 }
145 for (auto myIt = Begin(); myIt != End(); ++myIt)
146 {
147 for (auto otherIt = other.Begin(); otherIt != other.End(); ++otherIt)
148 {
149 if (!(myIt->fh <= otherIt->fl || otherIt->fh <= myIt->fl))
150 {
151 return false;
152 }
153 }
154 }
155 return true;
156}
157
158bool
160{
162 {
163 // same band size, only one band has to be checked
164 const auto bandBandWidth = m_bands.begin()->fh - m_bands.begin()->fl;
165 const auto otherBandWidth = other.m_bands.begin()->fh - other.m_bands.begin()->fl;
166 return std::abs(bandBandWidth - otherBandWidth) <= std::numeric_limits<double>::epsilon();
167 }
168 auto myIt = Begin();
169 auto otherIt = other.Begin();
170 while (myIt != End() && otherIt != other.End())
171 {
172 while (otherIt != other.End() && otherIt->fh <= myIt->fl)
173 {
174 ++otherIt;
175 }
176 if (otherIt == other.End())
177 {
178 break;
179 }
180 if (std::abs(myIt->fl - otherIt->fl) > std::numeric_limits<double>::epsilon() ||
181 std::abs(myIt->fh - otherIt->fh) > std::numeric_limits<double>::epsilon())
182 {
183 return false;
184 }
185 ++myIt;
186 ++otherIt;
187 }
188 return true;
189}
190
191} // namespace ns3
void InitModel()
Initialize internal variables.
bool IsOrthogonal(const SpectrumModel &other) const
Check if another SpectrumModel has bands orthogonal to our bands.
static SpectrumModelUid_t m_uidCount
counter to assign m_uids
bool m_contiguousBands
Whether the bands are contiguous (i.e., no gap between adjacent bands).
SpectrumModelUid_t m_uid
unique id for a given set of frequencies
SpectrumModel(const std::vector< double > &centerFreqs)
This constructs a SpectrumModel based on a given set of frequencies, which is assumed to be sorted by...
Bands::const_iterator End() const
Const Iterator to the model Bands container end, i.e.
size_t GetNumBands() const
bool IsAligned(const SpectrumModel &other) const
Check if another SpectrumModel is aligned with our bands.
Bands::const_iterator Begin() const
Const Iterator to the model Bands container start, i.e.
SpectrumModelUid_t GetUid() const
bool m_uniqueBandSize
Whether all bands have the same size.
Bands m_bands
Actual definition of frequency bands within this SpectrumModel.
#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_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:194
#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:267
Every class exported by the ns3 library is enclosed in the ns3 namespace.
bool operator==(const EventId &a, const EventId &b)
Definition event-id.h:167
std::vector< BandInfo > Bands
Container of BandInfo.
uint32_t SpectrumModelUid_t
Uid for SpectrumModels.
STL namespace.
The building block of a SpectrumModel.
double fc
center frequency
double fl
lower limit of subband
double fh
upper limit of subband