author | Vedran Miletić <rivanvx@gmail.com> |
Tue, 02 Aug 2011 17:42:33 -0400 | |
changeset 7385 | 10beb0e53130 |
parent 7252 | c8200621e252 |
child 7485 | 9bec9af5e3c8 |
permissions | -rw-r--r-- |
7385
10beb0e53130
standardize emacs c++ mode comments
Vedran Miletić <rivanvx@gmail.com>
parents:
7252
diff
changeset
|
1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
6349 | 2 |
|
3 |
/* |
|
4 |
* Copyright (c) 2009 CTTC |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or modify |
|
7 |
* it under the terms of the GNU General Public License version 2 as |
|
8 |
* published by the Free Software Foundation; |
|
9 |
* |
|
10 |
* This program is distributed in the hope that it will be useful, |
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
* GNU General Public License for more details. |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License |
|
16 |
* along with this program; if not, write to the Free Software |
|
17 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
18 |
* |
|
19 |
* Author: Nicola Baldo <nbaldo@cttc.es> |
|
20 |
*/ |
|
21 |
||
22 |
#include <ns3/spectrum-value.h> |
|
23 |
#include <math.h> |
|
24 |
#include <ns3/log.h> |
|
25 |
||
26 |
||
27 |
||
28 |
NS_LOG_COMPONENT_DEFINE ("SpectrumValue"); |
|
29 |
||
30 |
||
31 |
namespace ns3 { |
|
32 |
||
33 |
||
6355
957e0c6ccb29
fixed spectrum code for python bindings as per Gustavo's suggestion
Nicola Baldo <nicola@baldo.biz>
parents:
6349
diff
changeset
|
34 |
SpectrumValue::SpectrumValue () |
7142
89a701fec3a1
run check-style.py on src/spectrum
Nicola Baldo <nicola@baldo.biz>
parents:
6801
diff
changeset
|
35 |
{ |
6355
957e0c6ccb29
fixed spectrum code for python bindings as per Gustavo's suggestion
Nicola Baldo <nicola@baldo.biz>
parents:
6349
diff
changeset
|
36 |
} |
6349 | 37 |
|
38 |
SpectrumValue::SpectrumValue (Ptr<const SpectrumModel> sof) |
|
39 |
: m_spectrumModel (sof), |
|
40 |
m_values (sof->GetNumBands ()) |
|
41 |
{ |
|
42 |
||
43 |
} |
|
44 |
||
45 |
double& |
|
46 |
SpectrumValue:: operator[] (size_t index) |
|
47 |
{ |
|
48 |
return m_values.at (index); |
|
49 |
} |
|
50 |
||
51 |
||
52 |
SpectrumModelUid_t |
|
53 |
SpectrumValue::GetSpectrumModelUid () const |
|
54 |
{ |
|
55 |
return m_spectrumModel->GetUid (); |
|
56 |
} |
|
57 |
||
58 |
||
59 |
Ptr<const SpectrumModel> |
|
60 |
SpectrumValue::GetSpectrumModel () const |
|
61 |
{ |
|
62 |
return m_spectrumModel; |
|
63 |
} |
|
64 |
||
65 |
||
66 |
Values::const_iterator |
|
67 |
SpectrumValue::ConstValuesBegin () const |
|
68 |
{ |
|
69 |
return m_values.begin (); |
|
70 |
} |
|
71 |
||
72 |
Values::const_iterator |
|
73 |
SpectrumValue::ConstValuesEnd () const |
|
74 |
{ |
|
75 |
return m_values.end (); |
|
76 |
} |
|
77 |
||
78 |
||
79 |
Values::iterator |
|
80 |
SpectrumValue::ValuesBegin () |
|
81 |
{ |
|
82 |
return m_values.begin (); |
|
83 |
} |
|
84 |
||
85 |
Values::iterator |
|
86 |
SpectrumValue::ValuesEnd () |
|
87 |
{ |
|
88 |
return m_values.end (); |
|
89 |
} |
|
90 |
||
91 |
Bands::const_iterator |
|
92 |
SpectrumValue::ConstBandsBegin () const |
|
93 |
{ |
|
94 |
return m_spectrumModel->Begin (); |
|
95 |
} |
|
96 |
||
97 |
Bands::const_iterator |
|
98 |
SpectrumValue::ConstBandsEnd () const |
|
99 |
{ |
|
100 |
return m_spectrumModel->End (); |
|
101 |
} |
|
102 |
||
103 |
||
104 |
void |
|
105 |
SpectrumValue::Add (const SpectrumValue& x) |
|
106 |
{ |
|
107 |
Values::iterator it1 = m_values.begin (); |
|
108 |
Values::const_iterator it2 = x.m_values.begin (); |
|
109 |
||
110 |
NS_ASSERT (m_spectrumModel == x.m_spectrumModel); |
|
111 |
||
112 |
while (it1 != m_values.end ()) |
|
113 |
{ |
|
114 |
NS_ASSERT ( it2 != x.m_values.end ()); |
|
115 |
*it1 += *it2; |
|
116 |
++it1; |
|
117 |
++it2; |
|
118 |
} |
|
119 |
} |
|
120 |
||
121 |
||
122 |
void |
|
123 |
SpectrumValue::Add (double s) |
|
124 |
{ |
|
125 |
Values::iterator it1 = m_values.begin (); |
|
126 |
||
127 |
while (it1 != m_values.end ()) |
|
128 |
{ |
|
129 |
*it1 += s; |
|
130 |
++it1; |
|
131 |
} |
|
132 |
} |
|
133 |
||
134 |
||
135 |
||
136 |
void |
|
137 |
SpectrumValue::Subtract (const SpectrumValue& x) |
|
138 |
{ |
|
139 |
Values::iterator it1 = m_values.begin (); |
|
140 |
Values::const_iterator it2 = x.m_values.begin (); |
|
141 |
||
142 |
NS_ASSERT (m_spectrumModel == x.m_spectrumModel); |
|
143 |
||
144 |
while (it1 != m_values.end ()) |
|
145 |
{ |
|
146 |
NS_ASSERT ( it2 != x.m_values.end ()); |
|
147 |
*it1 -= *it2; |
|
148 |
++it1; |
|
149 |
++it2; |
|
150 |
} |
|
151 |
} |
|
152 |
||
153 |
||
154 |
void |
|
155 |
SpectrumValue::Subtract (double s) |
|
156 |
{ |
|
157 |
Add (-s); |
|
158 |
} |
|
159 |
||
160 |
||
161 |
||
162 |
void |
|
163 |
SpectrumValue::Multiply (const SpectrumValue& x) |
|
164 |
{ |
|
165 |
Values::iterator it1 = m_values.begin (); |
|
166 |
Values::const_iterator it2 = x.m_values.begin (); |
|
167 |
||
168 |
NS_ASSERT (m_spectrumModel == x.m_spectrumModel); |
|
169 |
||
170 |
while (it1 != m_values.end ()) |
|
171 |
{ |
|
172 |
NS_ASSERT ( it2 != x.m_values.end ()); |
|
173 |
*it1 *= *it2; |
|
174 |
++it1; |
|
175 |
++it2; |
|
176 |
} |
|
177 |
} |
|
178 |
||
179 |
||
180 |
void |
|
181 |
SpectrumValue::Multiply (double s) |
|
182 |
{ |
|
183 |
Values::iterator it1 = m_values.begin (); |
|
184 |
||
185 |
while (it1 != m_values.end ()) |
|
186 |
{ |
|
187 |
*it1 *= s; |
|
188 |
++it1; |
|
189 |
} |
|
190 |
} |
|
191 |
||
192 |
||
193 |
||
194 |
||
195 |
void |
|
196 |
SpectrumValue::Divide (const SpectrumValue& x) |
|
197 |
{ |
|
198 |
Values::iterator it1 = m_values.begin (); |
|
199 |
Values::const_iterator it2 = x.m_values.begin (); |
|
200 |
||
201 |
NS_ASSERT (m_spectrumModel == x.m_spectrumModel); |
|
202 |
||
203 |
while (it1 != m_values.end ()) |
|
204 |
{ |
|
205 |
NS_ASSERT ( it2 != x.m_values.end ()); |
|
206 |
*it1 /= *it2; |
|
207 |
++it1; |
|
208 |
++it2; |
|
209 |
} |
|
210 |
} |
|
211 |
||
212 |
||
213 |
void |
|
214 |
SpectrumValue::Divide (double s) |
|
215 |
{ |
|
216 |
NS_LOG_FUNCTION (this << s); |
|
217 |
Values::iterator it1 = m_values.begin (); |
|
218 |
||
219 |
while (it1 != m_values.end ()) |
|
220 |
{ |
|
221 |
*it1 /= s; |
|
222 |
++it1; |
|
223 |
} |
|
224 |
} |
|
225 |
||
226 |
||
227 |
||
228 |
||
229 |
void |
|
230 |
SpectrumValue::ChangeSign () |
|
231 |
{ |
|
232 |
Values::iterator it1 = m_values.begin (); |
|
233 |
||
234 |
while (it1 != m_values.end ()) |
|
235 |
{ |
|
236 |
*it1 = -(*it1); |
|
237 |
++it1; |
|
238 |
} |
|
239 |
} |
|
240 |
||
241 |
||
242 |
void |
|
243 |
SpectrumValue::ShiftLeft (int n) |
|
244 |
{ |
|
245 |
int i = 0; |
|
246 |
while (i < (int) m_values.size () - n) |
|
247 |
{ |
|
248 |
m_values.at (i) = m_values.at (i + n); |
|
249 |
i++; |
|
250 |
} |
|
251 |
while (i < (int)m_values.size ()) |
|
252 |
{ |
|
253 |
m_values.at (i) = 0; |
|
254 |
i++; |
|
255 |
} |
|
256 |
} |
|
257 |
||
258 |
||
259 |
void |
|
260 |
SpectrumValue::ShiftRight (int n) |
|
261 |
{ |
|
262 |
int i = m_values.size () - 1; |
|
263 |
while (i - n >= 0) |
|
264 |
{ |
|
265 |
m_values.at (i) = m_values.at (i - n); |
|
266 |
i = i - 1; |
|
267 |
} |
|
268 |
while (i >= 0) |
|
269 |
{ |
|
270 |
m_values.at (i) = 0; |
|
271 |
--i; |
|
272 |
} |
|
273 |
} |
|
274 |
||
275 |
||
276 |
||
277 |
void |
|
278 |
SpectrumValue::Pow (double exp) |
|
279 |
{ |
|
280 |
NS_LOG_FUNCTION (this << exp); |
|
281 |
Values::iterator it1 = m_values.begin (); |
|
282 |
||
283 |
while (it1 != m_values.end ()) |
|
284 |
{ |
|
285 |
*it1 = pow (*it1, exp); |
|
286 |
++it1; |
|
287 |
} |
|
288 |
} |
|
289 |
||
290 |
||
291 |
void |
|
292 |
SpectrumValue::Exp (double base) |
|
293 |
{ |
|
294 |
NS_LOG_FUNCTION (this << base); |
|
295 |
Values::iterator it1 = m_values.begin (); |
|
296 |
||
297 |
while (it1 != m_values.end ()) |
|
298 |
{ |
|
299 |
*it1 = pow (base, *it1); |
|
300 |
++it1; |
|
301 |
} |
|
302 |
} |
|
303 |
||
304 |
||
305 |
void |
|
306 |
SpectrumValue::Log10 () |
|
307 |
{ |
|
308 |
NS_LOG_FUNCTION (this); |
|
309 |
Values::iterator it1 = m_values.begin (); |
|
310 |
||
311 |
while (it1 != m_values.end ()) |
|
312 |
{ |
|
313 |
*it1 = log10 (*it1); |
|
314 |
++it1; |
|
315 |
} |
|
316 |
} |
|
317 |
||
318 |
void |
|
319 |
SpectrumValue::Log2 () |
|
320 |
{ |
|
321 |
NS_LOG_FUNCTION (this); |
|
322 |
Values::iterator it1 = m_values.begin (); |
|
323 |
||
324 |
while (it1 != m_values.end ()) |
|
325 |
{ |
|
326 |
*it1 = log2 (*it1); |
|
327 |
++it1; |
|
328 |
} |
|
329 |
} |
|
330 |
||
331 |
||
332 |
void |
|
333 |
SpectrumValue::Log () |
|
334 |
{ |
|
335 |
NS_LOG_FUNCTION (this); |
|
336 |
Values::iterator it1 = m_values.begin (); |
|
337 |
||
338 |
while (it1 != m_values.end ()) |
|
339 |
{ |
|
340 |
*it1 = log (*it1); |
|
341 |
++it1; |
|
342 |
} |
|
343 |
} |
|
344 |
||
345 |
double |
|
346 |
Norm (const SpectrumValue& x) |
|
347 |
{ |
|
348 |
double s = 0; |
|
349 |
Values::const_iterator it1 = x.ConstValuesBegin (); |
|
350 |
while (it1 != x.ConstValuesEnd ()) |
|
351 |
{ |
|
352 |
s += (*it1) * (*it1); |
|
353 |
++it1; |
|
354 |
} |
|
355 |
return sqrt (s); |
|
356 |
} |
|
357 |
||
358 |
||
359 |
double |
|
360 |
Sum (const SpectrumValue& x) |
|
361 |
{ |
|
362 |
double s = 0; |
|
363 |
Values::const_iterator it1 = x.ConstValuesBegin (); |
|
364 |
while (it1 != x.ConstValuesEnd ()) |
|
365 |
{ |
|
366 |
s += (*it1); |
|
367 |
++it1; |
|
368 |
} |
|
369 |
return s; |
|
370 |
} |
|
371 |
||
372 |
||
373 |
||
374 |
double |
|
375 |
Prod (const SpectrumValue& x) |
|
376 |
{ |
|
377 |
double s = 0; |
|
378 |
Values::const_iterator it1 = x.ConstValuesBegin (); |
|
379 |
while (it1 != x.ConstValuesEnd ()) |
|
380 |
{ |
|
381 |
s *= (*it1); |
|
382 |
++it1; |
|
383 |
} |
|
384 |
return s; |
|
385 |
} |
|
386 |
||
387 |
||
388 |
Ptr<SpectrumValue> |
|
389 |
SpectrumValue::Copy () const |
|
390 |
{ |
|
391 |
Ptr<SpectrumValue> p = Create<SpectrumValue> (m_spectrumModel); |
|
392 |
*p = *this; |
|
393 |
return p; |
|
394 |
||
395 |
// return Copy<SpectrumValue> (*this) |
|
396 |
} |
|
397 |
||
398 |
||
399 |
std::ostream& |
|
400 |
operator << (std::ostream& os, const SpectrumValue& pvf) |
|
401 |
{ |
|
402 |
Values::const_iterator it1 = pvf.ConstValuesBegin (); |
|
403 |
while (it1 != pvf.ConstValuesEnd ()) |
|
404 |
{ |
|
7252
c8200621e252
rerun check-style.py with uncrustify-0.58
Tom Henderson <tomh@tomh.org>
parents:
7142
diff
changeset
|
405 |
os << *it1 << " "; |
6349 | 406 |
++it1; |
407 |
} |
|
408 |
os << std::endl; |
|
409 |
return os; |
|
410 |
} |
|
411 |
||
412 |
||
413 |
||
414 |
SpectrumValue |
|
415 |
operator+ (const SpectrumValue& lhs, const SpectrumValue& rhs) |
|
416 |
{ |
|
417 |
SpectrumValue res = lhs; |
|
418 |
res.Add (rhs); |
|
419 |
return res; |
|
420 |
} |
|
421 |
||
422 |
||
423 |
SpectrumValue |
|
424 |
operator+ (const SpectrumValue& lhs, double rhs) |
|
425 |
{ |
|
426 |
SpectrumValue res = lhs; |
|
427 |
res.Add (rhs); |
|
428 |
return res; |
|
429 |
} |
|
430 |
||
431 |
||
432 |
SpectrumValue |
|
433 |
operator+ (double lhs, const SpectrumValue& rhs) |
|
434 |
{ |
|
435 |
SpectrumValue res = rhs; |
|
436 |
res.Add (lhs); |
|
437 |
return res; |
|
438 |
} |
|
439 |
||
440 |
||
441 |
SpectrumValue |
|
442 |
operator- (const SpectrumValue& lhs, const SpectrumValue& rhs) |
|
443 |
{ |
|
444 |
SpectrumValue res = rhs; |
|
445 |
res.ChangeSign (); |
|
446 |
res.Add (lhs); |
|
447 |
return res; |
|
448 |
} |
|
449 |
||
450 |
||
451 |
||
452 |
SpectrumValue |
|
453 |
operator- (const SpectrumValue& lhs, double rhs) |
|
454 |
{ |
|
455 |
SpectrumValue res = lhs; |
|
456 |
res.Subtract (rhs); |
|
457 |
return res; |
|
458 |
} |
|
459 |
||
460 |
||
461 |
SpectrumValue |
|
462 |
operator- (double lhs, const SpectrumValue& rhs) |
|
463 |
{ |
|
464 |
SpectrumValue res = rhs; |
|
465 |
res.Subtract (lhs); |
|
466 |
return res; |
|
467 |
} |
|
468 |
||
469 |
SpectrumValue |
|
470 |
operator* (const SpectrumValue& lhs, const SpectrumValue& rhs) |
|
471 |
{ |
|
472 |
SpectrumValue res = lhs; |
|
473 |
res.Multiply (rhs); |
|
474 |
return res; |
|
475 |
} |
|
476 |
||
477 |
||
478 |
SpectrumValue |
|
479 |
operator* (const SpectrumValue& lhs, double rhs) |
|
480 |
{ |
|
481 |
SpectrumValue res = lhs; |
|
482 |
res.Multiply (rhs); |
|
483 |
return res; |
|
484 |
} |
|
485 |
||
486 |
||
487 |
SpectrumValue |
|
488 |
operator* (double lhs, const SpectrumValue& rhs) |
|
489 |
{ |
|
490 |
SpectrumValue res = rhs; |
|
491 |
res.Multiply (lhs); |
|
492 |
return res; |
|
493 |
} |
|
494 |
||
495 |
||
496 |
SpectrumValue |
|
497 |
operator/ (const SpectrumValue& lhs, const SpectrumValue& rhs) |
|
498 |
{ |
|
499 |
SpectrumValue res = lhs; |
|
500 |
res.Divide (rhs); |
|
501 |
return res; |
|
502 |
} |
|
503 |
||
504 |
||
505 |
SpectrumValue |
|
506 |
operator/ (const SpectrumValue& lhs, double rhs) |
|
507 |
{ |
|
508 |
SpectrumValue res = lhs; |
|
509 |
res.Divide (rhs); |
|
510 |
return res; |
|
511 |
} |
|
512 |
||
513 |
||
514 |
SpectrumValue |
|
515 |
operator/ (double lhs, const SpectrumValue& rhs) |
|
516 |
{ |
|
517 |
SpectrumValue res = rhs; |
|
518 |
res.Divide (lhs); |
|
519 |
return res; |
|
520 |
} |
|
521 |
||
522 |
||
523 |
SpectrumValue |
|
524 |
operator+ (const SpectrumValue& rhs) |
|
525 |
{ |
|
526 |
return rhs; |
|
527 |
} |
|
528 |
||
529 |
SpectrumValue |
|
530 |
operator- (const SpectrumValue& rhs) |
|
531 |
{ |
|
532 |
SpectrumValue res = rhs; |
|
533 |
res.ChangeSign (); |
|
534 |
return res; |
|
535 |
} |
|
536 |
||
537 |
||
538 |
SpectrumValue |
|
539 |
Pow (double lhs, const SpectrumValue& rhs) |
|
540 |
{ |
|
541 |
SpectrumValue res = rhs; |
|
542 |
res.Exp (lhs); |
|
543 |
return res; |
|
544 |
} |
|
545 |
||
546 |
||
547 |
SpectrumValue |
|
548 |
Pow (const SpectrumValue& lhs, double rhs) |
|
549 |
{ |
|
550 |
SpectrumValue res = lhs; |
|
551 |
res.Pow (rhs); |
|
552 |
return res; |
|
553 |
} |
|
554 |
||
555 |
||
556 |
SpectrumValue |
|
557 |
Log10 (const SpectrumValue& arg) |
|
558 |
{ |
|
559 |
SpectrumValue res = arg; |
|
560 |
res.Log10 (); |
|
561 |
return res; |
|
562 |
} |
|
563 |
||
564 |
SpectrumValue |
|
565 |
Log2 (const SpectrumValue& arg) |
|
566 |
{ |
|
567 |
SpectrumValue res = arg; |
|
568 |
res.Log2 (); |
|
569 |
return res; |
|
570 |
} |
|
571 |
||
572 |
SpectrumValue |
|
573 |
Log (const SpectrumValue& arg) |
|
574 |
{ |
|
575 |
SpectrumValue res = arg; |
|
576 |
res.Log (); |
|
577 |
return res; |
|
578 |
} |
|
579 |
||
580 |
SpectrumValue& |
|
581 |
SpectrumValue:: operator+= (const SpectrumValue& rhs) |
|
582 |
{ |
|
583 |
Add (rhs); |
|
584 |
return *this; |
|
585 |
} |
|
586 |
||
587 |
SpectrumValue& |
|
588 |
SpectrumValue:: operator-= (const SpectrumValue& rhs) |
|
589 |
{ |
|
590 |
Subtract (rhs); |
|
591 |
return *this; |
|
592 |
} |
|
593 |
||
594 |
SpectrumValue& |
|
595 |
SpectrumValue:: operator*= (const SpectrumValue& rhs) |
|
596 |
{ |
|
597 |
Multiply (rhs); |
|
598 |
return *this; |
|
599 |
} |
|
600 |
||
601 |
SpectrumValue& |
|
602 |
SpectrumValue:: operator/= (const SpectrumValue& rhs) |
|
603 |
{ |
|
604 |
Divide (rhs); |
|
605 |
return *this; |
|
606 |
} |
|
607 |
||
608 |
||
609 |
SpectrumValue& |
|
610 |
SpectrumValue:: operator+= (double rhs) |
|
611 |
{ |
|
612 |
Add (rhs); |
|
613 |
return *this; |
|
614 |
} |
|
615 |
||
616 |
SpectrumValue& |
|
617 |
SpectrumValue:: operator-= (double rhs) |
|
618 |
{ |
|
619 |
Subtract (rhs); |
|
620 |
return *this; |
|
621 |
} |
|
622 |
||
623 |
SpectrumValue& |
|
624 |
SpectrumValue:: operator*= (double rhs) |
|
625 |
{ |
|
626 |
Multiply (rhs); |
|
627 |
return *this; |
|
628 |
} |
|
629 |
||
630 |
SpectrumValue& |
|
631 |
SpectrumValue:: operator/= (double rhs) |
|
632 |
{ |
|
633 |
Divide (rhs); |
|
634 |
return *this; |
|
635 |
} |
|
636 |
||
637 |
||
638 |
SpectrumValue& |
|
639 |
SpectrumValue:: operator= (double rhs) |
|
640 |
{ |
|
641 |
Values::iterator it1 = m_values.begin (); |
|
642 |
||
643 |
while (it1 != m_values.end ()) |
|
644 |
{ |
|
645 |
*it1 = rhs; |
|
646 |
++it1; |
|
647 |
} |
|
648 |
return *this; |
|
649 |
} |
|
650 |
||
651 |
||
652 |
||
653 |
SpectrumValue |
|
654 |
SpectrumValue:: operator<< (int n) const |
|
655 |
{ |
|
656 |
SpectrumValue res = *this; |
|
657 |
res.ShiftLeft (n); |
|
658 |
return res; |
|
659 |
} |
|
660 |
||
661 |
SpectrumValue |
|
662 |
SpectrumValue:: operator>> (int n) const |
|
663 |
{ |
|
664 |
SpectrumValue res = *this; |
|
665 |
res.ShiftRight (n); |
|
666 |
return res; |
|
667 |
} |
|
668 |
||
669 |
||
670 |
||
671 |
||
672 |
} // namespace ns3 |
|
673 |