A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
spectrum-converter.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
10
11#include "ns3/assert.h"
12#include "ns3/log.h"
13
14#include <algorithm>
15
16namespace ns3
17{
18
19NS_LOG_COMPONENT_DEFINE("SpectrumConverter");
20
21namespace
22{
23
24/**
25 * This method converts the transmitted PSD from the TX SpectrumModel to the RX SpectrumModel in the
26 * case the two models are fully aligned, i.e., each band in the TX model has a corresponding band
27 * in the RX model with the same boundaries and hence the conversion is just a matter of copying
28 * values in the overlapping bands and setting to zero the values in the non-overlapping bands.
29 *
30 * @param txPsd the transmitted PSD to be converted
31 * @param txSpectrumModel the SpectrumModel over which the transmitted PSD is defined
32 * @param rxSpectrumModel the SpectrumModel over which the converted PSD should be defined
33 * @return the converted PSD
34 */
37 Ptr<const SpectrumModel> txSpectrumModel,
38 Ptr<const SpectrumModel> rxSpectrumModel)
39{
40 // Create a new SpectrumValue with zeroes
41 auto convertedPsd = Create<SpectrumValue>(rxSpectrumModel);
42
43 // Copy values in the overlapping bands
44 auto fromIt = txPsd->ConstValuesBegin();
45 auto toIt = convertedPsd->ValuesBegin();
46 auto fromBandIt = txSpectrumModel->Begin();
47 auto toBandIt = rxSpectrumModel->Begin();
48
49 while (fromBandIt != txSpectrumModel->End() && toBandIt != rxSpectrumModel->End())
50 {
51 if (fromBandIt->fh <= toBandIt->fl)
52 {
53 ++fromBandIt;
54 ++fromIt;
55 }
56 else if (toBandIt->fh <= fromBandIt->fl)
57 {
58 ++toBandIt;
59 ++toIt;
60 }
61 else
62 {
63 // overlapping bands
64 *toIt = *fromIt;
65 ++fromBandIt;
66 ++fromIt;
67 ++toBandIt;
68 ++toIt;
69 }
70 }
71
72 return convertedPsd;
73}
74
75} // namespace
76
78 Ptr<const SpectrumModel> toSpectrumModel)
79{
80 NS_LOG_FUNCTION(this);
81 m_fromSpectrumModel = fromSpectrumModel;
82 m_toSpectrumModel = toSpectrumModel;
83
84 if (fromSpectrumModel->IsOrthogonal(*toSpectrumModel) ||
85 fromSpectrumModel->IsAligned(*toSpectrumModel))
86 {
87 // no conversion matrix needed
88 return;
89 }
90
91 size_t rowPtr = 0;
92 m_conversionMatrix.reserve(fromSpectrumModel->GetNumBands() * toSpectrumModel->GetNumBands());
93 m_conversionColInd.reserve(fromSpectrumModel->GetNumBands() * toSpectrumModel->GetNumBands());
94 m_conversionRowPtr.reserve(toSpectrumModel->GetNumBands());
95 for (auto toit = toSpectrumModel->Begin(); toit != toSpectrumModel->End(); ++toit)
96 {
97 size_t colInd = 0;
98 auto bandInv = 1.0 / (toit->fh - toit->fl);
99 for (auto fromit = fromSpectrumModel->Begin(); fromit != fromSpectrumModel->End(); ++fromit)
100 {
101 if (fromit->fh <= toit->fl || toit->fh <= fromit->fl)
102 {
103 ++colInd;
104 continue;
105 }
106 const auto maxLow = (fromit->fl > toit->fl) ? fromit->fl : toit->fl;
107 const auto minHigh = (fromit->fh < toit->fh) ? fromit->fh : toit->fh;
108 const auto coeff = (minHigh - maxLow) * bandInv;
109 const auto c = (coeff > 1.0) ? 1.0 : coeff;
110 NS_LOG_LOGIC("(" << fromit->fl << "," << fromit->fh << ")"
111 << " --> "
112 << "(" << toit->fl << "," << toit->fh << ")"
113 << " = " << c);
114 if (c > 0)
115 {
116 m_conversionMatrix.push_back(c);
117 m_conversionColInd.push_back(colInd);
118 ++rowPtr;
119 }
120 ++colInd;
121 }
122 m_conversionRowPtr.push_back(rowPtr);
123 }
124}
125
128{
129 if (m_fromSpectrumModel->IsOrthogonal(*m_toSpectrumModel))
130 {
131 // orthogonal models, return zeroed SpectrumValue
133 }
134
135 if (m_fromSpectrumModel->IsAligned(*m_toSpectrumModel))
136 {
137 // aligned models, simply opy values in overlapping bands
138 return ConvertAlignedSpectrumModels(fvvf, m_fromSpectrumModel, m_toSpectrumModel);
139 }
140
141 NS_ASSERT(*(fvvf->GetSpectrumModel()) == *m_fromSpectrumModel);
142
144
145 auto tvit = tvvf->ValuesBegin();
146 size_t i = 0; // Index of conversion coefficient
147
148 for (auto convIt = m_conversionRowPtr.begin(); convIt != m_conversionRowPtr.end(); ++convIt)
149 {
150 double sum = 0;
151 while (i < *convIt)
152 {
153 sum += (*fvvf)[m_conversionColInd[i]] * m_conversionMatrix[i];
154 ++i;
155 }
156 *tvit = sum;
157 ++tvit;
158 }
159
160 return tvvf;
161}
162
163} // namespace ns3
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
std::vector< double > m_conversionMatrix
matrix of conversion coefficients stored in Compressed Row Storage format
Ptr< const SpectrumModel > m_fromSpectrumModel
the SpectrumModel this SpectrumConverter instance can convert from
SpectrumConverter(Ptr< const SpectrumModel > fromSpectrumModel, Ptr< const SpectrumModel > toSpectrumModel)
Create a SpectrumConverter class that will be able to convert ValueVsFreq instances defined over one ...
Ptr< const SpectrumModel > m_toSpectrumModel
the SpectrumModel this SpectrumConverter instance can convert to
Ptr< SpectrumValue > Convert(Ptr< const SpectrumValue > vvf) const
Convert a particular ValueVsFreq instance to.
std::vector< size_t > m_conversionRowPtr
offset of rows in m_conversionMatrix
std::vector< size_t > m_conversionColInd
column of each non-zero element in m_conversionMatrix
#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_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:274
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:492
Ptr< SpectrumValue > ConvertAlignedSpectrumModels(Ptr< const SpectrumValue > txPsd, Ptr< const SpectrumModel > txSpectrumModel, Ptr< const SpectrumModel > rxSpectrumModel)
This method converts the transmitted PSD from the TX SpectrumModel to the RX SpectrumModel in the cas...
Every class exported by the ns3 library is enclosed in the ns3 namespace.