A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 NS_LOG_COMPONENT_DEFINE ("SpectrumConverter");
29 
30 namespace ns3 {
31 
33 {
34 }
35 
37 {
38  NS_LOG_FUNCTION (this);
39  m_fromSpectrumModel = fromSpectrumModel;
40  m_toSpectrumModel = toSpectrumModel;
41 
42  for (Bands::const_iterator toit = toSpectrumModel->Begin (); toit != toSpectrumModel->End (); ++toit)
43  {
44  std::vector<double> coeffs;
45 
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  coeffs.push_back (c);
54  }
55 
56  m_conversionMatrix.push_back (coeffs);
57  }
58 
59 }
60 
61 
62 double SpectrumConverter::GetCoefficient (const BandInfo& from, const BandInfo& to) const
63 {
64  NS_LOG_FUNCTION (this);
65  double coeff = std::min (from.fh, to.fh) - std::max (from.fl, to.fl);
66  coeff = std::max (0.0, coeff);
67  coeff = std::min (1.0, coeff / (to.fh - to.fl));
68  return coeff;
69 }
70 
71 
72 
75 {
76  NS_ASSERT ( *(fvvf->GetSpectrumModel ()) == *m_fromSpectrumModel);
77 
78  Ptr<SpectrumValue> tvvf = Create<SpectrumValue> (m_toSpectrumModel);
79 
80  Values::iterator tvit = tvvf->ValuesBegin ();
81 
82 
83  for (std::vector<std::vector<double> >::const_iterator toit = m_conversionMatrix.begin ();
84  toit != m_conversionMatrix.end ();
85  ++toit)
86  {
87  NS_ASSERT (tvit != tvvf->ValuesEnd ());
88  Values::const_iterator fvit = fvvf->ConstValuesBegin ();
89 
90  double sum = 0;
91  for (std::vector<double>::const_iterator fromit = toit->begin ();
92  fromit != toit->end ();
93  ++fromit)
94  {
95  NS_ASSERT (fvit != fvvf->ConstValuesEnd ());
96  sum += (*fvit) * (*fromit);
97  ++fvit;
98  }
99  *tvit = sum;
100  ++tvit;
101  }
102 
103  return tvvf;
104 }
105 
106 
107 
108 
109 
110 } // namespace ns3
Ptr< const SpectrumModel > m_toSpectrumModel
double GetCoefficient(const BandInfo &from, const BandInfo &to) const
Calculate the coefficient for value conversion between elements.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:61
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
Values::iterator ValuesEnd()
Bands::const_iterator End() const
std::vector< std::vector< double > > m_conversionMatrix
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:233
double fl
lower limit of subband
Ptr< SpectrumValue > Convert(Ptr< const SpectrumValue > vvf) const
Convert a particular ValueVsFreq instance to.
Values::iterator ValuesBegin()
Bands::const_iterator Begin() const
Ptr< const SpectrumModel > m_fromSpectrumModel
double fh
upper limit of subband
The building block of a SpectrumModel.