DESERT 3.5.1
Loading...
Searching...
No Matches
uwgainfromdb.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 "uwgainfromdb.h"
40#include "uwip-module.h"
41
42
43
44static class UnderwaterGainFromDbClass : public TclClass
45{
46public:
48 : TclClass("Module/UW/GAINFROMDB")
49 {
50 }
51 TclObject *
52 create(int, const char *const *)
53 {
54 return (new UnderwaterGainFromDb);
55 }
57
59 : time_roughness_(1)
60 , depth_roughness_(1)
61 , distance_roughness_(1)
62 , total_time_(1)
63 , frequency_correction_factor_(1)
64{
65 bind("time_roughness_", &time_roughness_);
66 bind("depth_roughness_", &depth_roughness_);
67 bind("distance_roughness_", &distance_roughness_);
68 bind("total_time_", &total_time_);
69 bind("frequency_correction_factor_", &frequency_correction_factor_);
70 bind_error("token_separator_", &token_separator_);
71 token_separator_ = '\t';
72 path_ = "";
73}
74
75int
76UnderwaterGainFromDb::command(int argc, const char *const *argv)
77{
78 if (argc == 3) {
79 if (strcasecmp(argv[1], "path") == 0) {
80 string tmp_ = ((char *) argv[2]);
81 path_ = new char[tmp_.length() + 1];
82 strcpy(path_, tmp_.c_str());
83 if (path_ == NULL) {
84 fprintf(stderr, "Empty string for the path_ file name");
85 return TCL_ERROR;
86 }
87 return TCL_OK;
88 }
89 }
91} /* UnderwaterGainFromDb::command */
92
93double
94UnderwaterGainFromDb::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_ = pow(
110 10, (this->getGain(NOW, depth_src_, depth_dst_, dist_dst_) / 10));
111 gain_ = gain_ * pow(frequency_correction_factor_, -dist_dst_);
112
113 double snir_;
114 if ((ph->Pn + ph->Pi) != 0) {
115 snir_ = (ph->Pt * gain_) / (ph->Pn + ph->Pi);
116 } else {
117 snir_ = -INT_MAX;
118 cerr << "ph->Pn + ph->Pi = 0!" << endl;
119 }
121} /* UnderwaterGainFromDb::getPER */
122
123void
125{
126 assert(_time >= 0);
128 return;
129} /* UnderwaterGainFromDb::setTimeRoughness */
130
131void
133{
134 assert(_depth >= 0);
136 return;
137} /* UnderwaterGainFromDb::setDepthRoughness */
138
139void
141{
142 assert(_distance >= 0);
144 return;
145} /* UnderwaterGainFromDb::setDistanceRoughness */
146
147void
149{
150 assert(_total_time > 0);
152 return;
153} /* UnderwaterGainFromDb::setTotalTime */
154
155void
157 const double &_frequency_correction_factor)
158{
161 return;
162} /* UnderwaterGainFromDb::setFrequencyCorrectionFactor */
163
164double
165UnderwaterGainFromDb::getGain(const double &_time, const double &_source_depth,
166 const double &_destination_depth, const double &_destination_distance)
167{
168 assert(_time >= 0);
169 assert(_source_depth <= 0);
172
173 int time_filename_ = (int) ((int) ceil(_time) / ((int) time_roughness_) *
175 int source_depth_filename_ = (int) ((int) ceil(_source_depth * -1) /
177 if (source_depth_filename_ == 0)
179 string file_name =
180 this->createNameFile(time_filename_, source_depth_filename_);
181
182 int line_index_ = (int) ((int) ceil(_destination_depth * -1) /
186 double gain_ =
188
189 return gain_;
190} /* UnderwaterGainFromDb::getSnr */
191
192double
194 const int &_row_index, const int &_column_index) const
195{
196 int row_iterator_ = 0;
197 int column_iterator_ = 0;
200 string line_;
201 string token_;
202 string value_;
203 double return_value_;
204
205 char *tmp_ = new char[_file_name.length() + 1];
206 strcpy(tmp_, _file_name.c_str());
207 if (tmp_ == NULL) {
208 fprintf(stderr, "Empty string for the file name");
209 }
210
211 input_file_.open(tmp_);
212 if (input_file_.is_open()) {
213 while (std::getline(input_file_, line_)) {
215 if (row_iterator_ == _row_index) {
216 break;
217 }
218 }
219 } else {
220 cerr << "Impossible to open file " << _file_name << endl;
221 }
222
227 value_ = token_;
228 }
229 }
230
231 stm.str(value_);
233 // cout << "file:" << _file_name << ":column:" << _column_index <<
234 // ":row:" << _row_index << ":gain:" << return_value_ << endl;
235
236 delete[] tmp_;
237 if (this->isZero(return_value_)) {
238 return (-INT_MAX);
239 } else {
240 return return_value_;
241 }
242} /* UnderwaterGainFromDb::retriveSnrFromFile */
243
244string
245UnderwaterGainFromDb::createNameFile(const int &_time, const int &_source_depth)
246{
247 osstream_.clear();
248 osstream_.str("");
249 osstream_ << path_ << "/" << _time << "_" << _source_depth << ".txt";
250 return osstream_.str();
251} /* UnderwaterGainFromDb::createNameFile */
TclObject * create(int, const char *const *)
double frequency_correction_factor_
used to shift from a frequency value to another one.
const bool isZero(const double &value) const
Evaluates is the number passed as input is equal to zero.
int time_roughness_
Roughness of the temporal samples.
virtual void setDistanceRoughness(const int &)
Sets the distance_roughness_ parameter.
char * path_
Name of the trace file writter for the current node.
virtual int command(int, const char *const *)
TCL command interpreter.
virtual void setTotalTime(const int &)
Sets the total_time_ parameter.
ostringstream osstream_
Used to create strings.
virtual void setTimeRoughness(const int &)
Sets the time_roughness_ parameter.
int depth_roughness_
Roughness of the depth samples.
virtual void setDepthRoughness(const int &)
Sets the depth_roughness_ parameter.
virtual void setFrequencyCorrectionFactor(const double &)
Sets the frequency_correction_factor_ parameter.
virtual double retriveGainFromFile(const string &, const int &, const int &) const
virtual double getPER(double snr, int nbits, Packet *p)
Returns the packet error rate by using the length of a packet and the information contained in the pa...
virtual double getGain(const double &, const double &, const double &, const double &)
int total_time_
Maximum value of the temporal samples, after this limit the smilulation time will be reset to zero.
UnderwaterGainFromDb()
Constructor of UnderwaterMPhyBpskDb class.
int distance_roughness_
Roughness of the distance samples.
virtual string createNameFile(const int &, const int &)
Creates the name of the file to load.
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...
virtual int command(int, const char *const *)
TCL command interpreter.
UnderwaterGainFromDbClass class_UnderwaterGainFromDb
Definition of UnderwaterGainFromDb class.
Provides the UWIP packets header description. Definition of the class that define the network layer.