DESERT 3.6.0
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#include <climits>
42
43
44
45static class UnderwaterGainFromDbClass : public TclClass
46{
47public:
49 : TclClass("Module/UW/GAINFROMDB")
50 {
51 }
52 TclObject *
53 create(int, const char *const *)
54 {
55 return (new UnderwaterGainFromDb);
56 }
58
60 : time_roughness_(1)
61 , depth_roughness_(1)
62 , distance_roughness_(1)
63 , total_time_(1)
64 , frequency_correction_factor_(1)
65{
66 bind("time_roughness_", &time_roughness_);
67 bind("depth_roughness_", &depth_roughness_);
68 bind("distance_roughness_", &distance_roughness_);
69 bind("total_time_", &total_time_);
70 bind("frequency_correction_factor_", &frequency_correction_factor_);
71 bind_error("token_separator_", &token_separator_);
72 token_separator_ = '\t';
73 path_ = "";
74}
75
76int
77UnderwaterGainFromDb::command(int argc, const char *const *argv)
78{
79 if (argc == 3) {
80 if (strcasecmp(argv[1], "path") == 0) {
81 string tmp_ = ((char *) argv[2]);
82 path_ = new char[tmp_.length() + 1];
83 strcpy(path_, tmp_.c_str());
84 if (path_ == NULL) {
85 fprintf(stderr, "Empty string for the path_ file name");
86 return TCL_ERROR;
87 }
88 return TCL_OK;
89 }
90 }
92} /* UnderwaterGainFromDb::command */
93
94double
95UnderwaterGainFromDb::getPER(double _snr, int _nbits, Packet *p)
96{
97 hdr_MPhy *ph = HDR_MPHY(p);
98
99 double x_ = (ph->srcPosition)->getX();
100 double y_ = (ph->srcPosition)->getY();
101 double z_ = (ph->srcPosition)->getZ();
102 double x_dst_ = (ph->dstPosition)->getX();
103 double y_dst_ = (ph->dstPosition)->getY();
104 double z_dst_ = (ph->dstPosition)->getZ();
105 double depth_src_ = z_;
106 double depth_dst_ = z_dst_;
107 double dist_dst_ =
108 sqrt((x_ - x_dst_) * (x_ - x_dst_) + (y_ - y_dst_) * (y_ - y_dst_));
109
110 double gain_ = pow(
111 10, (this->getGain(NOW, depth_src_, depth_dst_, dist_dst_) / 10));
112 gain_ = gain_ * pow(frequency_correction_factor_, -dist_dst_);
113
114 double snir_;
115 if ((ph->Pn + ph->Pi) != 0) {
116 snir_ = (ph->Pt * gain_) / (ph->Pn + ph->Pi);
117 } else {
118 snir_ = -INT_MAX;
119 cerr << "ph->Pn + ph->Pi = 0!" << endl;
120 }
122} /* UnderwaterGainFromDb::getPER */
123
124void
126{
127 assert(_time >= 0);
129 return;
130} /* UnderwaterGainFromDb::setTimeRoughness */
131
132void
134{
135 assert(_depth >= 0);
137 return;
138} /* UnderwaterGainFromDb::setDepthRoughness */
139
140void
142{
143 assert(_distance >= 0);
145 return;
146} /* UnderwaterGainFromDb::setDistanceRoughness */
147
148void
150{
151 assert(_total_time > 0);
153 return;
154} /* UnderwaterGainFromDb::setTotalTime */
155
156void
158 const double &_frequency_correction_factor)
159{
162 return;
163} /* UnderwaterGainFromDb::setFrequencyCorrectionFactor */
164
165double
166UnderwaterGainFromDb::getGain(const double &_time, const double &_source_depth,
167 const double &_destination_depth, const double &_destination_distance)
168{
169 assert(_time >= 0);
170 assert(_source_depth <= 0);
173
174 int time_filename_ = (int) ((int) ceil(_time) / ((int) time_roughness_) *
176 int source_depth_filename_ = (int) ((int) ceil(_source_depth * -1) /
178 if (source_depth_filename_ == 0)
180 string file_name =
181 this->createNameFile(time_filename_, source_depth_filename_);
182
183 int line_index_ = (int) ((int) ceil(_destination_depth * -1) /
187 double gain_ =
189
190 return gain_;
191} /* UnderwaterGainFromDb::getSnr */
192
193double
195 const int &_row_index, const int &_column_index) const
196{
197 int row_iterator_ = 0;
198 int column_iterator_ = 0;
201 string line_;
202 string token_;
203 string value_;
204 double return_value_;
205
206 char *tmp_ = new char[_file_name.length() + 1];
207 strcpy(tmp_, _file_name.c_str());
208 if (tmp_ == NULL) {
209 fprintf(stderr, "Empty string for the file name");
210 }
211
212 input_file_.open(tmp_);
213 if (input_file_.is_open()) {
214 while (std::getline(input_file_, line_)) {
216 if (row_iterator_ == _row_index) {
217 break;
218 }
219 }
220 } else {
221 cerr << "Impossible to open file " << _file_name << endl;
222 }
223
228 value_ = token_;
229 }
230 }
231
232 stm.str(value_);
234 // cout << "file:" << _file_name << ":column:" << _column_index <<
235 // ":row:" << _row_index << ":gain:" << return_value_ << endl;
236
237 delete[] tmp_;
238 if (this->isZero(return_value_)) {
239 return (-INT_MAX);
240 } else {
241 return return_value_;
242 }
243} /* UnderwaterGainFromDb::retriveSnrFromFile */
244
245string
246UnderwaterGainFromDb::createNameFile(const int &_time, const int &_source_depth)
247{
248 osstream_.clear();
249 osstream_.str("");
250 osstream_ << path_ << "/" << _time << "_" << _source_depth << ".txt";
251 return osstream_.str();
252} /* 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 int command(int, const char *const *) override
TCL command interpreter.
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...
UnderwaterGainFromDbClass class_UnderwaterGainFromDb
Definition of UnderwaterGainFromDb class.
Provides the UWIP packets header description. Definition of the class that define the network layer.