1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
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 <ns3/math.h>
24 #include <ns3/log.h>
25
26 namespace ns3 {
27
28 NS_LOG_COMPONENT_DEFINE ("SpectrumValue");
29
SpectrumValue()30 SpectrumValue::SpectrumValue ()
31 {
32 }
33
SpectrumValue(Ptr<const SpectrumModel> sof)34 SpectrumValue::SpectrumValue (Ptr<const SpectrumModel> sof)
35 : m_spectrumModel (sof),
36 m_values (sof->GetNumBands ())
37 {
38
39 }
40
41 double&
operator [](size_t index)42 SpectrumValue::operator[] (size_t index)
43 {
44 return m_values.at (index);
45 }
46
47 const double&
operator [](size_t index) const48 SpectrumValue::operator[] (size_t index) const
49 {
50 return m_values.at (index);
51 }
52
53
54 SpectrumModelUid_t
GetSpectrumModelUid() const55 SpectrumValue::GetSpectrumModelUid () const
56 {
57 return m_spectrumModel->GetUid ();
58 }
59
60
61 Ptr<const SpectrumModel>
GetSpectrumModel() const62 SpectrumValue::GetSpectrumModel () const
63 {
64 return m_spectrumModel;
65 }
66
67
68 Values::const_iterator
ConstValuesBegin() const69 SpectrumValue::ConstValuesBegin () const
70 {
71 return m_values.begin ();
72 }
73
74 Values::const_iterator
ConstValuesEnd() const75 SpectrumValue::ConstValuesEnd () const
76 {
77 return m_values.end ();
78 }
79
80
81 Values::iterator
ValuesBegin()82 SpectrumValue::ValuesBegin ()
83 {
84 return m_values.begin ();
85 }
86
87 Values::iterator
ValuesEnd()88 SpectrumValue::ValuesEnd ()
89 {
90 return m_values.end ();
91 }
92
93 Bands::const_iterator
ConstBandsBegin() const94 SpectrumValue::ConstBandsBegin () const
95 {
96 return m_spectrumModel->Begin ();
97 }
98
99 Bands::const_iterator
ConstBandsEnd() const100 SpectrumValue::ConstBandsEnd () const
101 {
102 return m_spectrumModel->End ();
103 }
104
105
106 void
Add(const SpectrumValue & x)107 SpectrumValue::Add (const SpectrumValue& x)
108 {
109 Values::iterator it1 = m_values.begin ();
110 Values::const_iterator it2 = x.m_values.begin ();
111
112 NS_ASSERT (m_spectrumModel == x.m_spectrumModel);
113 NS_ASSERT (m_values.size () == x.m_values.size ());
114
115 while (it1 != m_values.end ())
116 {
117 *it1 += *it2;
118 ++it1;
119 ++it2;
120 }
121 }
122
123
124 void
Add(double s)125 SpectrumValue::Add (double s)
126 {
127 Values::iterator it1 = m_values.begin ();
128
129 while (it1 != m_values.end ())
130 {
131 *it1 += s;
132 ++it1;
133 }
134 }
135
136
137
138 void
Subtract(const SpectrumValue & x)139 SpectrumValue::Subtract (const SpectrumValue& x)
140 {
141 Values::iterator it1 = m_values.begin ();
142 Values::const_iterator it2 = x.m_values.begin ();
143
144 NS_ASSERT (m_spectrumModel == x.m_spectrumModel);
145 NS_ASSERT (m_values.size () == x.m_values.size ());
146
147 while (it1 != m_values.end ())
148 {
149 *it1 -= *it2;
150 ++it1;
151 ++it2;
152 }
153 }
154
155
156 void
Subtract(double s)157 SpectrumValue::Subtract (double s)
158 {
159 Add (-s);
160 }
161
162
163
164 void
Multiply(const SpectrumValue & x)165 SpectrumValue::Multiply (const SpectrumValue& x)
166 {
167 Values::iterator it1 = m_values.begin ();
168 Values::const_iterator it2 = x.m_values.begin ();
169
170 NS_ASSERT (m_spectrumModel == x.m_spectrumModel);
171 NS_ASSERT (m_values.size () == x.m_values.size ());
172
173 while (it1 != m_values.end ())
174 {
175 *it1 *= *it2;
176 ++it1;
177 ++it2;
178 }
179 }
180
181
182 void
Multiply(double s)183 SpectrumValue::Multiply (double s)
184 {
185 Values::iterator it1 = m_values.begin ();
186
187 while (it1 != m_values.end ())
188 {
189 *it1 *= s;
190 ++it1;
191 }
192 }
193
194
195
196
197 void
Divide(const SpectrumValue & x)198 SpectrumValue::Divide (const SpectrumValue& x)
199 {
200 Values::iterator it1 = m_values.begin ();
201 Values::const_iterator it2 = x.m_values.begin ();
202
203 NS_ASSERT (m_spectrumModel == x.m_spectrumModel);
204 NS_ASSERT (m_values.size () == x.m_values.size ());
205
206 while (it1 != m_values.end ())
207 {
208 *it1 /= *it2;
209 ++it1;
210 ++it2;
211 }
212 }
213
214
215 void
Divide(double s)216 SpectrumValue::Divide (double s)
217 {
218 NS_LOG_FUNCTION (this << s);
219 Values::iterator it1 = m_values.begin ();
220
221 while (it1 != m_values.end ())
222 {
223 *it1 /= s;
224 ++it1;
225 }
226 }
227
228
229
230
231 void
ChangeSign()232 SpectrumValue::ChangeSign ()
233 {
234 Values::iterator it1 = m_values.begin ();
235
236 while (it1 != m_values.end ())
237 {
238 *it1 = -(*it1);
239 ++it1;
240 }
241 }
242
243
244 void
ShiftLeft(int n)245 SpectrumValue::ShiftLeft (int n)
246 {
247 int i = 0;
248 while (i < (int) m_values.size () - n)
249 {
250 m_values.at (i) = m_values.at (i + n);
251 i++;
252 }
253 while (i < (int)m_values.size ())
254 {
255 m_values.at (i) = 0;
256 i++;
257 }
258 }
259
260
261 void
ShiftRight(int n)262 SpectrumValue::ShiftRight (int n)
263 {
264 int i = m_values.size () - 1;
265 while (i - n >= 0)
266 {
267 m_values.at (i) = m_values.at (i - n);
268 i = i - 1;
269 }
270 while (i >= 0)
271 {
272 m_values.at (i) = 0;
273 --i;
274 }
275 }
276
277
278
279 void
Pow(double exp)280 SpectrumValue::Pow (double exp)
281 {
282 NS_LOG_FUNCTION (this << exp);
283 Values::iterator it1 = m_values.begin ();
284
285 while (it1 != m_values.end ())
286 {
287 *it1 = std::pow (*it1, exp);
288 ++it1;
289 }
290 }
291
292
293 void
Exp(double base)294 SpectrumValue::Exp (double base)
295 {
296 NS_LOG_FUNCTION (this << base);
297 Values::iterator it1 = m_values.begin ();
298
299 while (it1 != m_values.end ())
300 {
301 *it1 = std::pow (base, *it1);
302 ++it1;
303 }
304 }
305
306
307 void
Log10()308 SpectrumValue::Log10 ()
309 {
310 NS_LOG_FUNCTION (this);
311 Values::iterator it1 = m_values.begin ();
312
313 while (it1 != m_values.end ())
314 {
315 *it1 = std::log10 (*it1);
316 ++it1;
317 }
318 }
319
320 void
Log2()321 SpectrumValue::Log2 ()
322 {
323 NS_LOG_FUNCTION (this);
324 Values::iterator it1 = m_values.begin ();
325
326 while (it1 != m_values.end ())
327 {
328 *it1 = log2 (*it1);
329 ++it1;
330 }
331 }
332
333
334 void
Log()335 SpectrumValue::Log ()
336 {
337 NS_LOG_FUNCTION (this);
338 Values::iterator it1 = m_values.begin ();
339
340 while (it1 != m_values.end ())
341 {
342 *it1 = std::log (*it1);
343 ++it1;
344 }
345 }
346
347 double
Norm(const SpectrumValue & x)348 Norm (const SpectrumValue& x)
349 {
350 double s = 0;
351 Values::const_iterator it1 = x.ConstValuesBegin ();
352 while (it1 != x.ConstValuesEnd ())
353 {
354 s += (*it1) * (*it1);
355 ++it1;
356 }
357 return std::sqrt (s);
358 }
359
360
361 double
Sum(const SpectrumValue & x)362 Sum (const SpectrumValue& x)
363 {
364 double s = 0;
365 Values::const_iterator it1 = x.ConstValuesBegin ();
366 while (it1 != x.ConstValuesEnd ())
367 {
368 s += (*it1);
369 ++it1;
370 }
371 return s;
372 }
373
374
375
376 double
Prod(const SpectrumValue & x)377 Prod (const SpectrumValue& x)
378 {
379 double s = 0;
380 Values::const_iterator it1 = x.ConstValuesBegin ();
381 while (it1 != x.ConstValuesEnd ())
382 {
383 s *= (*it1);
384 ++it1;
385 }
386 return s;
387 }
388
389 double
Integral(const SpectrumValue & arg)390 Integral (const SpectrumValue& arg)
391 {
392 double i = 0;
393 Values::const_iterator vit = arg.ConstValuesBegin ();
394 Bands::const_iterator bit = arg.ConstBandsBegin ();
395 while (vit != arg.ConstValuesEnd ())
396 {
397 NS_ASSERT (bit != arg.ConstBandsEnd ());
398 i += (*vit) * (bit->fh - bit->fl);
399 ++vit;
400 ++bit;
401 }
402 NS_ASSERT (bit == arg.ConstBandsEnd ());
403 return i;
404 }
405
406
407
408 Ptr<SpectrumValue>
Copy() const409 SpectrumValue::Copy () const
410 {
411 Ptr<SpectrumValue> p = Create<SpectrumValue> (m_spectrumModel);
412 *p = *this;
413 return p;
414
415 // return Copy<SpectrumValue> (*this)
416 }
417
418
419 /**
420 * \brief Output stream operator
421 * \param os output stream
422 * \param pvf the SpectrumValue to print
423 * \return an output stream
424 */
425 std::ostream&
operator <<(std::ostream & os,const SpectrumValue & pvf)426 operator << (std::ostream& os, const SpectrumValue& pvf)
427 {
428 Values::const_iterator it1 = pvf.ConstValuesBegin ();
429 while (it1 != pvf.ConstValuesEnd ())
430 {
431 os << *it1 << " ";
432 ++it1;
433 }
434 os << std::endl;
435 return os;
436 }
437
438
439
440 SpectrumValue
operator +(const SpectrumValue & lhs,const SpectrumValue & rhs)441 operator+ (const SpectrumValue& lhs, const SpectrumValue& rhs)
442 {
443 SpectrumValue res = lhs;
444 res.Add (rhs);
445 return res;
446 }
447
448
449 SpectrumValue
operator +(const SpectrumValue & lhs,double rhs)450 operator+ (const SpectrumValue& lhs, double rhs)
451 {
452 SpectrumValue res = lhs;
453 res.Add (rhs);
454 return res;
455 }
456
457
458 SpectrumValue
operator +(double lhs,const SpectrumValue & rhs)459 operator+ (double lhs, const SpectrumValue& rhs)
460 {
461 SpectrumValue res = rhs;
462 res.Add (lhs);
463 return res;
464 }
465
466
467 SpectrumValue
operator -(const SpectrumValue & lhs,const SpectrumValue & rhs)468 operator- (const SpectrumValue& lhs, const SpectrumValue& rhs)
469 {
470 SpectrumValue res = rhs;
471 res.ChangeSign ();
472 res.Add (lhs);
473 return res;
474 }
475
476
477
478 SpectrumValue
operator -(const SpectrumValue & lhs,double rhs)479 operator- (const SpectrumValue& lhs, double rhs)
480 {
481 SpectrumValue res = lhs;
482 res.Subtract (rhs);
483 return res;
484 }
485
486
487 SpectrumValue
operator -(double lhs,const SpectrumValue & rhs)488 operator- (double lhs, const SpectrumValue& rhs)
489 {
490 SpectrumValue res = rhs;
491 res.Subtract (lhs);
492 return res;
493 }
494
495 SpectrumValue
operator *(const SpectrumValue & lhs,const SpectrumValue & rhs)496 operator* (const SpectrumValue& lhs, const SpectrumValue& rhs)
497 {
498 SpectrumValue res = lhs;
499 res.Multiply (rhs);
500 return res;
501 }
502
503
504 SpectrumValue
operator *(const SpectrumValue & lhs,double rhs)505 operator* (const SpectrumValue& lhs, double rhs)
506 {
507 SpectrumValue res = lhs;
508 res.Multiply (rhs);
509 return res;
510 }
511
512
513 SpectrumValue
operator *(double lhs,const SpectrumValue & rhs)514 operator* (double lhs, const SpectrumValue& rhs)
515 {
516 SpectrumValue res = rhs;
517 res.Multiply (lhs);
518 return res;
519 }
520
521
522 SpectrumValue
operator /(const SpectrumValue & lhs,const SpectrumValue & rhs)523 operator/ (const SpectrumValue& lhs, const SpectrumValue& rhs)
524 {
525 SpectrumValue res = lhs;
526 res.Divide (rhs);
527 return res;
528 }
529
530
531 SpectrumValue
operator /(const SpectrumValue & lhs,double rhs)532 operator/ (const SpectrumValue& lhs, double rhs)
533 {
534 SpectrumValue res = lhs;
535 res.Divide (rhs);
536 return res;
537 }
538
539
540 SpectrumValue
operator /(double lhs,const SpectrumValue & rhs)541 operator/ (double lhs, const SpectrumValue& rhs)
542 {
543 SpectrumValue res = rhs;
544 res.Divide (lhs);
545 return res;
546 }
547
548
549 SpectrumValue
operator +(const SpectrumValue & rhs)550 operator+ (const SpectrumValue& rhs)
551 {
552 return rhs;
553 }
554
555 SpectrumValue
operator -(const SpectrumValue & rhs)556 operator- (const SpectrumValue& rhs)
557 {
558 SpectrumValue res = rhs;
559 res.ChangeSign ();
560 return res;
561 }
562
563
564 SpectrumValue
Pow(double lhs,const SpectrumValue & rhs)565 Pow (double lhs, const SpectrumValue& rhs)
566 {
567 SpectrumValue res = rhs;
568 res.Exp (lhs);
569 return res;
570 }
571
572
573 SpectrumValue
Pow(const SpectrumValue & lhs,double rhs)574 Pow (const SpectrumValue& lhs, double rhs)
575 {
576 SpectrumValue res = lhs;
577 res.Pow (rhs);
578 return res;
579 }
580
581
582 SpectrumValue
Log10(const SpectrumValue & arg)583 Log10 (const SpectrumValue& arg)
584 {
585 SpectrumValue res = arg;
586 res.Log10 ();
587 return res;
588 }
589
590 SpectrumValue
Log2(const SpectrumValue & arg)591 Log2 (const SpectrumValue& arg)
592 {
593 SpectrumValue res = arg;
594 res.Log2 ();
595 return res;
596 }
597
598 SpectrumValue
Log(const SpectrumValue & arg)599 Log (const SpectrumValue& arg)
600 {
601 SpectrumValue res = arg;
602 res.Log ();
603 return res;
604 }
605
606 SpectrumValue&
operator +=(const SpectrumValue & rhs)607 SpectrumValue::operator+= (const SpectrumValue& rhs)
608 {
609 Add (rhs);
610 return *this;
611 }
612
613 SpectrumValue&
operator -=(const SpectrumValue & rhs)614 SpectrumValue::operator-= (const SpectrumValue& rhs)
615 {
616 Subtract (rhs);
617 return *this;
618 }
619
620 SpectrumValue&
operator *=(const SpectrumValue & rhs)621 SpectrumValue::operator*= (const SpectrumValue& rhs)
622 {
623 Multiply (rhs);
624 return *this;
625 }
626
627 SpectrumValue&
operator /=(const SpectrumValue & rhs)628 SpectrumValue::operator/= (const SpectrumValue& rhs)
629 {
630 Divide (rhs);
631 return *this;
632 }
633
634
635 SpectrumValue&
operator +=(double rhs)636 SpectrumValue::operator+= (double rhs)
637 {
638 Add (rhs);
639 return *this;
640 }
641
642 SpectrumValue&
operator -=(double rhs)643 SpectrumValue::operator-= (double rhs)
644 {
645 Subtract (rhs);
646 return *this;
647 }
648
649 SpectrumValue&
operator *=(double rhs)650 SpectrumValue::operator*= (double rhs)
651 {
652 Multiply (rhs);
653 return *this;
654 }
655
656 SpectrumValue&
operator /=(double rhs)657 SpectrumValue::operator/= (double rhs)
658 {
659 Divide (rhs);
660 return *this;
661 }
662
663
664 SpectrumValue&
operator =(double rhs)665 SpectrumValue::operator= (double rhs)
666 {
667 Values::iterator it1 = m_values.begin ();
668
669 while (it1 != m_values.end ())
670 {
671 *it1 = rhs;
672 ++it1;
673 }
674 return *this;
675 }
676
677
678
679 SpectrumValue
operator <<(int n) const680 SpectrumValue::operator<< (int n) const
681 {
682 SpectrumValue res = *this;
683 res.ShiftLeft (n);
684 return res;
685 }
686
687 SpectrumValue
operator >>(int n) const688 SpectrumValue::operator>> (int n) const
689 {
690 SpectrumValue res = *this;
691 res.ShiftRight (n);
692 return res;
693 }
694
695 uint32_t
GetValuesN() const696 SpectrumValue::GetValuesN () const
697 {
698 return m_values.size ();
699 }
700
701 const double &
ValuesAt(uint32_t pos) const702 SpectrumValue::ValuesAt (uint32_t pos) const
703 {
704 return m_values.at (pos);
705 }
706
707 } // namespace ns3
708
709