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 #include <string>
30 
31 NS_LOG_COMPONENT_DEFINE("SatChannelEstimationError");
32 
33 namespace ns3
34 {
35 
36 NS_OBJECT_ENSURE_REGISTERED(SatChannelEstimationError);
37 
39  : m_lastSampleIndex(0),
40  m_normalRandomVariable(),
41  m_sinrsDb(),
42  m_mueCesDb(),
43  m_stdCesDb()
44 {
45  m_normalRandomVariable = CreateObject<NormalRandomVariable>();
46 }
47 
49  : m_lastSampleIndex(0),
50  m_normalRandomVariable(),
51  m_sinrsDb(),
52  m_mueCesDb(),
53  m_stdCesDb()
54 {
55  m_normalRandomVariable = CreateObject<NormalRandomVariable>();
56  ReadFile(filePathName);
57 }
58 
60 {
61 }
62 
63 TypeId
65 {
66  static TypeId tid = TypeId("ns3::SatMeasurementError")
67  .SetParent<Object>()
68  .AddConstructor<SatChannelEstimationError>();
69  return tid;
70 }
71 
72 void
74 {
75  NS_LOG_FUNCTION(this);
76 
77  m_normalRandomVariable = nullptr;
78  Object::DoDispose();
79 }
80 
81 void
82 SatChannelEstimationError::ReadFile(std::string filePathName)
83 {
84  NS_LOG_FUNCTION(this << filePathName);
85 
86  // READ FROM THE SPECIFIED INPUT FILE
87  std::ifstream* ifs = new std::ifstream(filePathName.c_str(), std::ifstream::in);
88 
89  if (!ifs->is_open())
90  {
91  // script might be launched by test.py, try a different base path
92  delete ifs;
93  filePathName = "../../" + filePathName;
94  ifs = new std::ifstream(filePathName.c_str(), std::ifstream::in);
95 
96  if (!ifs->is_open())
97  {
98  NS_FATAL_ERROR("The file " << filePathName << " is not found.");
99  }
100  }
101 
102  // Start conditions
103  double sinrDb, mueCe, stdCe;
104 
105  // Read a row
106  *ifs >> sinrDb >> mueCe >> stdCe;
107 
108  while (ifs->good())
109  {
110  m_sinrsDb.push_back(sinrDb);
111  m_mueCesDb.push_back(mueCe);
112  m_stdCesDb.push_back(stdCe);
113 
114  // get next row
115  *ifs >> sinrDb >> mueCe >> stdCe;
116  }
117 
118  NS_ASSERT(m_sinrsDb.size() == m_mueCesDb.size());
119  NS_ASSERT(m_mueCesDb.size() == m_stdCesDb.size());
120 
121  m_lastSampleIndex = m_sinrsDb.size() - 1;
122 
123  ifs->close();
124  delete ifs;
125 }
126 
127 double
129 {
130  NS_LOG_FUNCTION(this << sinrInDb);
131 
132  // 1. Find the proper SINR grid point
133  // 2. Interpolate the mueCe
134  // 3. Interpolate the stdCe
135  // 4. Through a random number with mean and std deviation
136  // 5. Add the error from the SINR in
137  // 6. Correct with mueCe
138 
139  double mueCe(0.0);
140  double stdCe(0.0);
141 
142  // If smaller than minimum SINR
143  if (sinrInDb <= m_sinrsDb[0])
144  {
145  mueCe = m_mueCesDb[0];
146  stdCe = m_stdCesDb[0];
147  }
148  // If larger than maximum SINR
149  else if (sinrInDb >= m_sinrsDb[m_lastSampleIndex])
150  {
151  mueCe = m_mueCesDb[m_lastSampleIndex];
152  stdCe = m_stdCesDb[m_lastSampleIndex];
153  }
154  // Else find proper point and interpolate
155  else
156  {
157  for (uint32_t i = 0; i < m_sinrsDb.size(); ++i)
158  {
159  // Trigger the first bigger threshold
160  if (sinrInDb < m_sinrsDb[i])
161  {
165  mueCe = SatUtils::Interpolate(sinrInDb,
166  m_sinrsDb[i - 1],
167  m_sinrsDb[i],
168  m_mueCesDb[i - 1],
169  m_mueCesDb[i]);
170  stdCe = SatUtils::Interpolate(sinrInDb,
171  m_sinrsDb[i - 1],
172  m_sinrsDb[i],
173  m_stdCesDb[i - 1],
174  m_stdCesDb[i]);
175  break;
176  }
177  }
178  }
179 
180  // Convert standard deviation to variance
181  double varCe = pow(stdCe, 2);
182 
183  NS_LOG_INFO("mueCe: " << mueCe << ", stdCe: " << stdCe << ", varCe: " << varCe);
184 
185  // Get normal random variable error
186  double error = m_normalRandomVariable->GetValue(mueCe, varCe);
187 
188  // Add error and correct with
189  double sinrOutDb = sinrInDb + error - mueCe;
190 
191  NS_LOG_INFO("sinrIn: " << sinrInDb << ", sinrOut: " << sinrOutDb);
192 
193  return sinrOutDb;
194 }
195 
196 } // 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.