A Discrete-Event Network Simulator
API
spectrum-converter.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009 CTTC
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Nicola Baldo <nbaldo@cttc.es>
18  */
19 
20 #include "spectrum-converter.h"
21 
22 #include <ns3/assert.h>
23 #include <ns3/log.h>
24 
25 #include <algorithm>
26 
27 namespace ns3
28 {
29 
30 NS_LOG_COMPONENT_DEFINE("SpectrumConverter");
31 
33 {
34 }
35 
37  Ptr<const SpectrumModel> toSpectrumModel)
38 {
39  NS_LOG_FUNCTION(this);
40  m_fromSpectrumModel = fromSpectrumModel;
41  m_toSpectrumModel = toSpectrumModel;
42 
43  size_t rowPtr = 0;
44  for (auto toit = toSpectrumModel->Begin(); toit != toSpectrumModel->End(); ++toit)
45  {
46  size_t colInd = 0;
47  for (auto fromit = fromSpectrumModel->Begin(); fromit != fromSpectrumModel->End(); ++fromit)
48  {
49  double c = GetCoefficient(*fromit, *toit);
50  NS_LOG_LOGIC("(" << fromit->fl << "," << fromit->fh << ")"
51  << " --> "
52  << "(" << toit->fl << "," << toit->fh << ")"
53  << " = " << c);
54  if (c > 0)
55  {
56  m_conversionMatrix.push_back(c);
57  m_conversionColInd.push_back(colInd);
58  rowPtr++;
59  }
60  colInd++;
61  }
62  m_conversionRowPtr.push_back(rowPtr);
63  }
64 }
65 
66 double
68 {
69  NS_LOG_FUNCTION(this);
70  double coeff = std::min(from.fh, to.fh) - std::max(from.fl, to.fl);
71  coeff = std::max(0.0, coeff);
72  coeff = std::min(1.0, coeff / (to.fh - to.fl));
73  return coeff;
74 }
75 
78 {
79  NS_ASSERT(*(fvvf->GetSpectrumModel()) == *m_fromSpectrumModel);
80 
81  Ptr<SpectrumValue> tvvf = Create<SpectrumValue>(m_toSpectrumModel);
82 
83  auto tvit = tvvf->ValuesBegin();
84  size_t i = 0; // Index of conversion coefficient
85 
86  for (auto convIt = m_conversionRowPtr.begin(); convIt != m_conversionRowPtr.end(); ++convIt)
87  {
88  double sum = 0;
89  while (i < *convIt)
90  {
91  sum += (*fvvf)[m_conversionColInd.at(i)] * m_conversionMatrix.at(i);
92  i++;
93  }
94  *tvit = sum;
95  ++tvit;
96  }
97 
98  return tvvf;
99 }
100 
101 } // namespace ns3
#define min(a, b)
Definition: 80211b.c:41
#define max(a, b)
Definition: 80211b.c:42
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
double GetCoefficient(const BandInfo &from, const BandInfo &to) const
Calculate the coefficient for value conversion between elements.
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
Bands::const_iterator End() const
Const Iterator to the model Bands container end.
Bands::const_iterator Begin() const
Const Iterator to the model Bands container start.
Values::iterator ValuesBegin()
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Every class exported by the ns3 library is enclosed in the ns3 namespace.
The building block of a SpectrumModel.
double fl
lower limit of subband
double fh
upper limit of subband