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