DESERT 3.5.1
Loading...
Searching...
No Matches
uwphysicalnoise.cpp
Go to the documentation of this file.
1//
2// Copyright (c) 2024 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 "uwphysicalnoise.h"
40#include "underwater-mpropagation.h"
41#include "uwlib.h"
42#include <algorithm>
43#include <phymac-clmsg.h>
44
45static class UwPhysicalNoiseClass : public TclClass
46{
47public:
49 : TclClass("Module/UW/PHYSICALNOISE")
50 {
51 }
52 TclObject *
53 create(int, const char *const *)
54 {
55 return (new UnderwaterPhysicalNoise);
56 }
58
60 : debug_noise(0)
61 , ship_stop(0)
62 , granularity(100)
63 , noise_src()
64{
65 bind("ship_stop", &ship_stop);
66 bind("debug_noise_", &debug_noise);
67 bind("granularity", &granularity);
68}
69
70int
71UnderwaterPhysicalNoise::command(int argc, const char *const *argv)
72{
73 if (argc == 6) {
74 if (strcasecmp(argv[1], "addSource") == 0) {
75
76 int id = std::atoi(argv[2]);
77 double len = std::atof(argv[3]);
78 std::string cat = argv[4];
79 Position *pos =
80 dynamic_cast<Position *>(TclObject::lookup(argv[5]));
81
82 auto cat_it = ship_noise::cat_dict.find(cat);
83
84 if (id > 0 && len > 0 && cat_it != ship_noise::cat_dict.end() &&
85 pos) {
86
87 addNoiseSource(id, len, cat_it->second, pos);
88
89 return TCL_OK;
90 }
91
92 return TCL_ERROR;
93 }
94 } else if (argc == 3) {
95 if (strcasecmp(argv[1], "removeSource") == 0) {
96
97 int id = std::atoi(argv[2]);
98
99 if (id > 0) {
100
102
103 return TCL_OK;
104 }
105
106 return TCL_ERROR;
107 }
108 }
109
110 return UnderwaterPhysical::command(argc, argv);
111}
112
113void
115 size_t id, double len, ship_noise::ShipCategory cat, Position *pos)
116{
117
118 static int mac_addr = -1;
119
120 ClMsgPhy2MacAddr msg;
121 sendSyncClMsg(&msg);
122 mac_addr = msg.getAddr();
123
124 Noisesource src{id, len, cat, pos};
125 noise_src.emplace_back(src);
126
127 if (debug_noise)
128 cout << "MAC: " << mac_addr
129 << ": NOISE ADDED, vector size: " << noise_src.size() << endl;
130}
131
132void
134{
135 noise_src.erase(std::remove_if(noise_src.begin(),
136 noise_src.end(),
137 [&](Noisesource const &ns) { return ns.id == id; }),
138 noise_src.end());
139}
140
141double
143{
144 static int mac_addr = -1;
145
146 ClMsgPhy2MacAddr msg;
147 sendSyncClMsg(&msg);
148 mac_addr = msg.getAddr();
149
150 MSpectralMask *sm = getRxSpectralMask(p);
151 assert(sm);
152
153 double f_c = sm->getFreq();
154 double bw = sm->getBandwidth();
155 double sub_bw = bw / granularity;
156 double total = 0;
157
158 if (debug_noise)
159 cout << NOW << " MAC Addr: " << mac_addr << ", f_c: " << f_c
160 << ", bandwidth: " << bw << ", sub_bandwidth: " << sub_bw
161 << ", granularity: " << granularity << endl;
162
163 for (const auto &elem : noise_src) {
164
165 Position *dstPos = getPosition();
166 double speed = getSpeedKnots(elem.pos);
167
168 assert(dstPos);
169 assert(propagation_);
170
171 UnderwaterMPropagation *uwmp =
172 dynamic_cast<UnderwaterMPropagation *>(propagation_);
173
174 assert(uwmp);
175
176 double dist = elem.pos->getDist(dstPos);
177
178 for (double freq = f_c - (bw / 2); freq < f_c + (bw / 2);
179 freq += sub_bw) {
180
181 double noiseTx = ship_noise::getNoisefromCategory(
182 elem.category, (freq + sub_bw / 2), speed, elem.length);
183
184 if (noiseTx < 0) {
185 if (debug_noise)
186 std::cout << "ERR: Undefined or wrong ship type!"
187 << std::endl;
188
189 continue;
190 }
191
192 double gain = uwlib_AInv(dist / 1000.0,
193 uwmp->uw.practical_spreading,
194 (freq + sub_bw / 2) / 1000.0);
195 double noiseRx = noiseTx * gain * sub_bw;
196
197 total += noiseRx;
198 }
199 }
200
201 if (debug_noise)
202 std::cout << "Total: " << total << ", db: " << 10 * log10(total)
203 << std::endl;
204
205 return total;
206}
207
208double
210{
211 UWSMPosition *smp = dynamic_cast<UWSMPosition *>(p);
212
213 if (smp) {
214 if (ship_stop && smp->isDestReached())
215 return 0;
216
217 return (smp->getSpeed() * MS_TO_KNOTS);
218 }
219
220 return -1;
221}
222
223double
225{
226 double noise = UnderwaterMPhyBpsk::getNoisePower(p);
227
228 if (!noise_src.empty())
229 noise += vesselNoisePower(p);
230
231 return noise;
232}
virtual bool isDestReached() const
double getSpeed() const
Method that return the actual speed.
virtual int command(int argc, const char *const *argv) override
TCL command interpreter.
int ship_stop
If enabled, the speed is set to zero when the ship reaches its destination.
virtual void removeNoiseSourcefromID(size_t id)
It removes the noise emitter corresponding to the given id.
virtual void addNoiseSource(size_t id, double len, ship_noise::ShipCategory cat, Position *pos)
It adds the noise emitter corresponding to the given id.
static constexpr const double MS_TO_KNOTS
Conversion m/s to knots.
virtual double getSpeedKnots(Position *p) const
Compute the speed of the vessel in knots.
UnderwaterPhysicalNoise()
Constructor of UnderwaterPhysicalNoise class.
std::vector< Noisesource > noise_src
Vector that stores all the vessels.
double granularity
Number of step for the integration.
virtual double vesselNoisePower(Packet *p)
It calculates the total noise power, iterating on the vessel map.
virtual double getNoisePower(Packet *p) override
Compute the noise power, considering also vessels noise if needed.
virtual int command(int, const char *const *)
TCL command interpreter.
TclObject * create(int, const char *const *)
double getNoisefromCategory(const ShipCategory &cat, double freq, double speed, double length)
Compute the sound level of a ship given the type.
static const std::unordered_map< std::string, ShipCategory > cat_dict
Dictionary of ship categories.
Definition soundlevels.h:60
ShipCategory
Enum type representing the ship categories.
Definition soundlevels.h:57
Struct that contains the parameters of a vessel.
UwPhysicalNoiseClass class_module_uwphysicalnoise
Definition of UwPhysicalNoise class.