DESERT 3.5.1
Loading...
Searching...
No Matches
uwphysicalfromdb.cpp
Go to the documentation of this file.
1//
2// Copyright (c) 2017 Regents of the SIGNET lab, University of Padova.
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions
7// are met:
8// 1. Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright
11// notice, this list of conditions and the following disclaimer in the
12// documentation and/or other materials provided with the distribution.
13// 3. Neither the name of the University of Padova (SIGNET lab) nor the
14// names of its contributors may be used to endorse or promote products
15// derived from this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28//
29
39#include "uwphysicalfromdb.h"
40
41static class UnderwaterPhysicalfromdbClass : public TclClass
42{
43public:
45 : TclClass("Module/UW/PHYSICALFROMDB")
46 {
47 }
48 TclObject *
49 create(int, const char *const *)
50 {
51 return (new UnderwaterPhysicalfromdb);
52 }
54
56 : tau_index(1)
57{
58 bind("tau_index_", &tau_index);
59 path_gainmaps = "";
61}
62
63int
64UnderwaterPhysicalfromdb::command(int argc, const char *const *argv)
65{
66 if (argc == 3) {
67 if (strcasecmp(argv[1], "setPathGainmaps") == 0) {
68 string tmp_ = ((char *) argv[2]);
69 path_gainmaps = new char[tmp_.length() + 1];
70 strcpy(path_gainmaps, tmp_.c_str());
71 if (path_ == NULL) {
73 "Empty string for the path_gainmaps_ file name");
74 return TCL_ERROR;
75 }
76 return TCL_OK;
77 } else if (strcasecmp(argv[1], "setPathSelfInterference") == 0) {
78 string tmp_ = ((char *) argv[2]);
79 path_selfinterference = new char[tmp_.length() + 1];
81 if (path_ == NULL) {
83 "Empty string for the path_selfinterference_ file "
84 "name");
85 return TCL_ERROR;
86 }
87 return TCL_OK;
88 }
89 }
91} /* UnderwaterPhysicalfromdb::command */
92
93double
94UnderwaterPhysicalfromdb::getPER(double _snr, int _nbits, Packet *p)
95{
96 hdr_MPhy *ph = HDR_MPHY(p);
97
98 double x_ = (ph->srcPosition)->getX();
99 double y_ = (ph->srcPosition)->getY();
100 double z_ = (ph->srcPosition)->getZ();
101 double x_dst_ = (ph->dstPosition)->getX();
102 double y_dst_ = (ph->dstPosition)->getY();
103 double z_dst_ = (ph->dstPosition)->getZ();
104 double depth_src_ = z_;
105 double depth_dst_ = z_dst_;
106 double dist_dst_ =
107 sqrt((x_ - x_dst_) * (x_ - x_dst_) + (y_ - y_dst_) * (y_ - y_dst_));
108
109 double gain_ =
111 double self_interference_ = pow(10,
113 // gain_ = gain_ * pow(frequency_correction_factor_, - dist_dst_);
114
115 double snir_;
116 if ((ph->Pn + ph->Pi + self_interference_) != 0) {
117 ph->Pi += self_interference_; // Add the self interference to the
118 // interference.
119 snir_ = (ph->Pt * gain_) / (ph->Pn + ph->Pi);
120 } else {
121 snir_ = -INT_MAX;
122 }
124}
125
126double
128 const double &_source_depth, const double &_destination_depth,
129 const double &_destination_distance)
130{
131 assert(_time >= 0);
132 assert(_source_depth <= 0);
135
136 // Quantize the input values
137 int time_filename_ = static_cast<int>(
138 static_cast<int>(floor(_time) / getTimeRoughness()) *
140#if __cplusplus > 199711L
141 // C++11
142 // The column index corresponds to the depth of the receiver
144 static_cast<int>(round((_source_depth * -1) / getDepthRoughness()) *
146 // The line index is relative to the distance of the receiver
147 int line_index_ = static_cast<int>(
149 // The column index corresponds to the depth of the receiver
150 int column_index_ = static_cast<int>(
152#else
153 // Adding 0.5 and cast is equivalent to use round only because we are
154 // dealing with positive integers
156 static_cast<int>((_source_depth * -1) / getDepthRoughness() + 0.5) *
158 int line_index_ = static_cast<int>(
160 int column_index_ = static_cast<int>(
161 (_destination_depth * -1) / getDepthRoughness() + 0.5);
162#endif
163
164 if (source_depth_filename_ == 0) {
166 }
167
171 getTauIndex());
172
174} /* UnderwaterPhysicalfromdb::getGain */
175
176double
178 const double &_source_depth, const double &_destination_depth,
179 const double &_destination_distance)
180{
181 assert(_time >= 0);
182 assert(_source_depth <= 0);
185
186 // Quantize the input values.
187 int time_filename_ = static_cast<int>(
188 static_cast<int>(ceil(_time) / getTimeRoughness()) *
191 static_cast<int>((_source_depth * -1) / getDepthRoughness()) *
193 // The line index is relative to the distance of the receiver
194 int column_index_ =
195 static_cast<int>((_destination_depth * -1) / getDepthRoughness());
196 // The column index corresponds to the depth of the receiver
197 int line_index_ =
198 static_cast<int>(_destination_distance / getDistanceRoughness());
199
200 if (time_filename_ == 0) {
202 }
203
204 if (source_depth_filename_ == 0) {
206 }
207
211 getTauIndex());
212
214} /* UnderwaterPhysicalfromdb::getSelfInterference */
215
216double
218 const int &_row_index, const int &_column_index) const
219{
220 int row_iterator_ = 0;
221 int column_iterator_ = 0;
224 string line_;
225 string token_;
226 string value_;
227 double return_value_;
228
229 char *tmp_ = new char[_file_name.length() + 1];
230 strcpy(tmp_, _file_name.c_str());
231 if (tmp_ == NULL) {
232 std::cerr << "Impossible to open file " << _file_name << std::endl;
233 fprintf(stderr, "Empty string for the file name");
234 }
235
236 input_file_.open(tmp_);
237 if (input_file_.is_open()) {
238 while (std::getline(input_file_, line_)) {
240 if (row_iterator_ == _row_index) {
241 break;
242 }
243 }
244 } else {
245 std::cerr << "Impossible to open file " << _file_name << std::endl;
246 }
247
252 value_ = token_;
253 }
254 }
255
256 stm.str(value_);
258 // std::cout << "file:" << _file_name << ":column:" << _column_index <<
259 // ":row:" << _row_index << ":gain:" << return_value_ << std::endl;
260
261 delete[] tmp_;
262 if (this->isZero(return_value_)) {
263 return (-INT_MAX);
264 } else {
265 return return_value_;
266 }
267} /* UnderwaterPhysicalfromdb::retriveFromFile */
268
269string
270UnderwaterPhysicalfromdb::createNameFile(const char *_path, const int &_time,
271 const int &_source_depth, const int &_tau_index)
272{
273 osstream_.clear();
274 osstream_.str("");
275 osstream_ << _path << "/" << _time << "_" << _source_depth << "_"
276 << _tau_index;
277 return osstream_.str();
278} /* UnderwaterPhysicalfromdb::createNameFile */
const bool isZero(const double &value) const
Evaluates is the number passed as input is equal to zero.
const int & getDepthRoughness() const
Returns the depth_roughness_ parameter.
char * path_
Name of the trace file writter for the current node.
const int & getDistanceRoughness() const
Returns the distance_roughness_ parameter.
const int & getTimeRoughness() const
Returns the time_roughness_ parameter.
virtual int command(int, const char *const *)
TCL command interpreter.
ostringstream osstream_
Used to create strings.
const int & getTotalTime() const
Returns the total_time_ parameter.
char token_separator_
Token used to parse the elements in a line of the database.
virtual double getPER(double snr, int nbits, Packet *)
Returns the packet error rate by using the length of a packet and the information contained in the pa...
TclObject * create(int, const char *const *)
char * path_selfinterference
Name of the trace file writter for the current node.
char * path_gainmaps
Name of the trace file writter for the current node.
virtual double getSelfInterference(const double &_time, const double &_source_depth, const double &_destination_depth, const double &_destination_distance)
Read from the self interference.
virtual double retrieveFromFile(const string &_file_name, const int &_row_index, const int &_column_index) const
Read from a file the value in a specific row - column.
UnderwaterPhysicalfromdb()
Constructor of UnderwaterMPhyBpskDb class.
virtual string createNameFile(const char *_path, const int &_time, const int &_source_depth, const int &_tau_index)
Create the name of the file to read.
virtual double getPER(double _snr, int _nbits, Packet *p)
Return the Packet Error Rate of the packet p passed as input.
const int & getTauIndex() const
Return the value of the tau_index parameter.
int tau_index
Tau index to load in the file.
virtual double getGain(const double &_time, const double &_source_depth, const double &_destination_depth, const double &_destination_distance)
Read from the gain.
virtual int command(int, const char *const *)
TCL command interpreter.
UnderwaterPhysicalfromdbClass class_UnderwaterPhysicalfromdb
Definition of UnderwaterPhysicalfromdb class.