A Discrete-Event Network Simulator
API
spectrum-converter.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 CTTC
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Nicola Baldo <nbaldo@cttc.es>
19  */
20 
21 #include <ns3/spectrum-converter.h>
22 #include <ns3/assert.h>
23 #include <ns3/log.h>
24 #include <algorithm>
25 
26 
27 
28 namespace ns3 {
29 
30 NS_LOG_COMPONENT_DEFINE ("SpectrumConverter");
31 
33 {
34 }
35 
37 {
38  NS_LOG_FUNCTION (this);
39  m_fromSpectrumModel = fromSpectrumModel;
40  m_toSpectrumModel = toSpectrumModel;
41 
42  size_t rowPtr = 0;
43  for (Bands::const_iterator toit = toSpectrumModel->Begin (); toit != toSpectrumModel->End (); ++toit)
44  {
45  size_t colInd = 0;
46  for (Bands::const_iterator fromit = fromSpectrumModel->Begin (); fromit != fromSpectrumModel->End (); ++fromit)
47  {
48  double c = GetCoefficient (*fromit, *toit);
49  NS_LOG_LOGIC ("(" << fromit->fl << "," << fromit->fh << ")"
50  << " --> " <<
51  "(" << toit->fl << "," << toit->fh << ")"
52  << " = " << c);
53  if (c > 0)
54  {
55  m_conversionMatrix.push_back (c);
56  m_conversionColInd.push_back (colInd);
57  rowPtr++;
58  }
59  colInd++;
60  }
61  m_conversionRowPtr.push_back (rowPtr);
62  }
63 
64 }
65 
66 
67 double SpectrumConverter::GetCoefficient (const BandInfo& from, const BandInfo& to) const
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 
76 
77 
80 {
81  NS_ASSERT ( *(fvvf->GetSpectrumModel ()) == *m_fromSpectrumModel);
82 
83  Ptr<SpectrumValue> tvvf = Create<SpectrumValue> (m_toSpectrumModel);
84 
85  Values::iterator tvit = tvvf->ValuesBegin ();
86  size_t i = 0; // Index of conversion coefficient
87 
88  for (std::vector<size_t>::const_iterator convIt = m_conversionRowPtr.begin ();
89  convIt != m_conversionRowPtr.end ();
90  ++convIt)
91  {
92  double sum = 0;
93  while (i < *convIt)
94  {
95  sum += (*fvvf)[m_conversionColInd.at (i)] * m_conversionMatrix.at (i);
96  i++;
97  }
98  *tvit = sum;
99  ++tvit;
100  }
101 
102  return tvvf;
103 }
104 
105 
106 
107 
108 
109 } // namespace ns3
#define min(a, b)
Definition: 80211b.c:42
#define max(a, b)
Definition: 80211b.c:43
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:67
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:289
#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