satellite-channel-estimation-error.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014 Magister Solutions Ltd
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: Jani Puttonen <jani.puttonen@magister.fi>
19  */
20 
22 
23 #include "satellite-utils.h"
24 
25 #include <ns3/log.h>
26 
27 #include <cmath>
28 #include <fstream>
29 
30 NS_LOG_COMPONENT_DEFINE("SatChannelEstimationError");
31 
32 namespace ns3
33 {
34 
35 NS_OBJECT_ENSURE_REGISTERED(SatChannelEstimationError);
36 
38  : m_lastSampleIndex(0),
39  m_normalRandomVariable(),
40  m_sinrsDb(),
41  m_mueCesDb(),
42  m_stdCesDb()
43 {
44  m_normalRandomVariable = CreateObject<NormalRandomVariable>();
45 }
46 
48  : m_lastSampleIndex(0),
49  m_normalRandomVariable(),
50  m_sinrsDb(),
51  m_mueCesDb(),
52  m_stdCesDb()
53 {
54  m_normalRandomVariable = CreateObject<NormalRandomVariable>();
55  ReadFile(filePathName);
56 }
57 
59 {
60 }
61 
62 TypeId
64 {
65  static TypeId tid = TypeId("ns3::SatMeasurementError")
66  .SetParent<Object>()
67  .AddConstructor<SatChannelEstimationError>();
68  return tid;
69 }
70 
71 void
73 {
74  NS_LOG_FUNCTION(this);
75 
77  Object::DoDispose();
78 }
79 
80 void
81 SatChannelEstimationError::ReadFile(std::string filePathName)
82 {
83  NS_LOG_FUNCTION(this << filePathName);
84 
85  // READ FROM THE SPECIFIED INPUT FILE
86  std::ifstream* ifs = new std::ifstream(filePathName.c_str(), std::ifstream::in);
87 
88  if (!ifs->is_open())
89  {
90  // script might be launched by test.py, try a different base path
91  delete ifs;
92  filePathName = "../../" + filePathName;
93  ifs = new std::ifstream(filePathName.c_str(), std::ifstream::in);
94 
95  if (!ifs->is_open())
96  {
97  NS_FATAL_ERROR("The file " << filePathName << " is not found.");
98  }
99  }
100 
101  // Start conditions
102  double sinrDb, mueCe, stdCe;
103 
104  // Read a row
105  *ifs >> sinrDb >> mueCe >> stdCe;
106 
107  while (ifs->good())
108  {
109  m_sinrsDb.push_back(sinrDb);
110  m_mueCesDb.push_back(mueCe);
111  m_stdCesDb.push_back(stdCe);
112 
113  // get next row
114  *ifs >> sinrDb >> mueCe >> stdCe;
115  }
116 
117  NS_ASSERT(m_sinrsDb.size() == m_mueCesDb.size());
118  NS_ASSERT(m_mueCesDb.size() == m_stdCesDb.size());
119 
120  m_lastSampleIndex = m_sinrsDb.size() - 1;
121 
122  ifs->close();
123  delete ifs;
124 }
125 
126 double
128 {
129  NS_LOG_FUNCTION(this << sinrInDb);
130 
131  // 1. Find the proper SINR grid point
132  // 2. Interpolate the mueCe
133  // 3. Interpolate the stdCe
134  // 4. Through a random number with mean and std deviation
135  // 5. Add the error from the SINR in
136  // 6. Correct with mueCe
137 
138  double mueCe(0.0);
139  double stdCe(0.0);
140 
141  // If smaller than minimum SINR
142  if (sinrInDb <= m_sinrsDb[0])
143  {
144  mueCe = m_mueCesDb[0];
145  stdCe = m_stdCesDb[0];
146  }
147  // If larger than maximum SINR
148  else if (sinrInDb >= m_sinrsDb[m_lastSampleIndex])
149  {
150  mueCe = m_mueCesDb[m_lastSampleIndex];
151  stdCe = m_stdCesDb[m_lastSampleIndex];
152  }
153  // Else find proper point and interpolate
154  else
155  {
156  for (uint32_t i = 0; i < m_sinrsDb.size(); ++i)
157  {
158  // Trigger the first bigger threshold
159  if (sinrInDb < m_sinrsDb[i])
160  {
164  mueCe = SatUtils::Interpolate(sinrInDb,
165  m_sinrsDb[i - 1],
166  m_sinrsDb[i],
167  m_mueCesDb[i - 1],
168  m_mueCesDb[i]);
169  stdCe = SatUtils::Interpolate(sinrInDb,
170  m_sinrsDb[i - 1],
171  m_sinrsDb[i],
172  m_stdCesDb[i - 1],
173  m_stdCesDb[i]);
174  break;
175  }
176  }
177  }
178 
179  // Convert standard deviation to variance
180  double varCe = pow(stdCe, 2);
181 
182  NS_LOG_INFO("mueCe: " << mueCe << ", stdCe: " << stdCe << ", varCe: " << varCe);
183 
184  // Get normal random variable error
185  double error = m_normalRandomVariable->GetValue(mueCe, varCe);
186 
187  // Add error and correct with
188  double sinrOutDb = sinrInDb + error - mueCe;
189 
190  NS_LOG_INFO("sinrIn: " << sinrInDb << ", sinrOut: " << sinrOutDb);
191 
192  return sinrOutDb;
193 }
194 
195 } // namespace ns3
std::vector< double > m_sinrsDb
SINR values.
uint32_t m_lastSampleIndex
Last sample index of the containers.
Ptr< NormalRandomVariable > m_normalRandomVariable
Normal random variable used to calculate the channel estimation error.
virtual ~SatChannelEstimationError()
Destructor for SatChannelEstimationError.
std::vector< double > m_stdCesDb
Standard deviation values.
double AddError(double sinrInDb) const
Add channel estimation error to SINR.
virtual void DoDispose()
Dispose of this class instance.
std::vector< double > m_mueCesDb
Mean values.
void ReadFile(std::string filePathName)
Read the distribution mean and STD values from file.
static TypeId GetTypeId(void)
inherited from Object
static double Interpolate(double x, double x0, double x1, double y0, double y1)
Simple linear interpolation.
SatArqSequenceNumber is handling the sequence numbers for the ARQ process.