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 <ns3/math.h>
24 #include <ns3/log.h>
25 
26 NS_LOG_COMPONENT_DEFINE ("SpectrumValue");
27 
28 
29 namespace ns3 {
30 
31 
33 {
34 }
35 
37  : m_spectrumModel (sof),
38  m_values (sof->GetNumBands ())
39 {
40 
41 }
42 
43 double&
45 {
46  return m_values.at (index);
47 }
48 
49 const double&
50 SpectrumValue:: operator[] (size_t index) const
51 {
52  return m_values.at (index);
53 }
54 
55 
58 {
59  return m_spectrumModel->GetUid ();
60 }
61 
62 
65 {
66  return m_spectrumModel;
67 }
68 
69 
70 Values::const_iterator
72 {
73  return m_values.begin ();
74 }
75 
76 Values::const_iterator
78 {
79  return m_values.end ();
80 }
81 
82 
83 Values::iterator
85 {
86  return m_values.begin ();
87 }
88 
89 Values::iterator
91 {
92  return m_values.end ();
93 }
94 
95 Bands::const_iterator
97 {
98  return m_spectrumModel->Begin ();
99 }
100 
101 Bands::const_iterator
103 {
104  return m_spectrumModel->End ();
105 }
106 
107 
108 void
110 {
111  Values::iterator it1 = m_values.begin ();
112  Values::const_iterator it2 = x.m_values.begin ();
113 
115 
116  while (it1 != m_values.end ())
117  {
118  NS_ASSERT ( it2 != x.m_values.end ());
119  *it1 += *it2;
120  ++it1;
121  ++it2;
122  }
123 }
124 
125 
126 void
128 {
129  Values::iterator it1 = m_values.begin ();
130 
131  while (it1 != m_values.end ())
132  {
133  *it1 += s;
134  ++it1;
135  }
136 }
137 
138 
139 
140 void
142 {
143  Values::iterator it1 = m_values.begin ();
144  Values::const_iterator it2 = x.m_values.begin ();
145 
147 
148  while (it1 != m_values.end ())
149  {
150  NS_ASSERT ( it2 != x.m_values.end ());
151  *it1 -= *it2;
152  ++it1;
153  ++it2;
154  }
155 }
156 
157 
158 void
160 {
161  Add (-s);
162 }
163 
164 
165 
166 void
168 {
169  Values::iterator it1 = m_values.begin ();
170  Values::const_iterator it2 = x.m_values.begin ();
171 
173 
174  while (it1 != m_values.end ())
175  {
176  NS_ASSERT ( it2 != x.m_values.end ());
177  *it1 *= *it2;
178  ++it1;
179  ++it2;
180  }
181 }
182 
183 
184 void
186 {
187  Values::iterator it1 = m_values.begin ();
188 
189  while (it1 != m_values.end ())
190  {
191  *it1 *= s;
192  ++it1;
193  }
194 }
195 
196 
197 
198 
199 void
201 {
202  Values::iterator it1 = m_values.begin ();
203  Values::const_iterator it2 = x.m_values.begin ();
204 
206 
207  while (it1 != m_values.end ())
208  {
209  NS_ASSERT ( it2 != x.m_values.end ());
210  *it1 /= *it2;
211  ++it1;
212  ++it2;
213  }
214 }
215 
216 
217 void
219 {
220  NS_LOG_FUNCTION (this << s);
221  Values::iterator it1 = m_values.begin ();
222 
223  while (it1 != m_values.end ())
224  {
225  *it1 /= s;
226  ++it1;
227  }
228 }
229 
230 
231 
232 
233 void
235 {
236  Values::iterator it1 = m_values.begin ();
237 
238  while (it1 != m_values.end ())
239  {
240  *it1 = -(*it1);
241  ++it1;
242  }
243 }
244 
245 
246 void
248 {
249  int i = 0;
250  while (i < (int) m_values.size () - n)
251  {
252  m_values.at (i) = m_values.at (i + n);
253  i++;
254  }
255  while (i < (int)m_values.size ())
256  {
257  m_values.at (i) = 0;
258  i++;
259  }
260 }
261 
262 
263 void
265 {
266  int i = m_values.size () - 1;
267  while (i - n >= 0)
268  {
269  m_values.at (i) = m_values.at (i - n);
270  i = i - 1;
271  }
272  while (i >= 0)
273  {
274  m_values.at (i) = 0;
275  --i;
276  }
277 }
278 
279 
280 
281 void
282 SpectrumValue::Pow (double exp)
283 {
284  NS_LOG_FUNCTION (this << exp);
285  Values::iterator it1 = m_values.begin ();
286 
287  while (it1 != m_values.end ())
288  {
289  *it1 = std::pow (*it1, exp);
290  ++it1;
291  }
292 }
293 
294 
295 void
296 SpectrumValue::Exp (double base)
297 {
298  NS_LOG_FUNCTION (this << base);
299  Values::iterator it1 = m_values.begin ();
300 
301  while (it1 != m_values.end ())
302  {
303  *it1 = std::pow (base, *it1);
304  ++it1;
305  }
306 }
307 
308 
309 void
311 {
312  NS_LOG_FUNCTION (this);
313  Values::iterator it1 = m_values.begin ();
314 
315  while (it1 != m_values.end ())
316  {
317  *it1 = std::log10 (*it1);
318  ++it1;
319  }
320 }
321 
322 void
324 {
325  NS_LOG_FUNCTION (this);
326  Values::iterator it1 = m_values.begin ();
327 
328  while (it1 != m_values.end ())
329  {
330  *it1 = log2 (*it1);
331  ++it1;
332  }
333 }
334 
335 
336 void
338 {
339  NS_LOG_FUNCTION (this);
340  Values::iterator it1 = m_values.begin ();
341 
342  while (it1 != m_values.end ())
343  {
344  *it1 = std::log (*it1);
345  ++it1;
346  }
347 }
348 
349 double
351 {
352  double s = 0;
353  Values::const_iterator it1 = x.ConstValuesBegin ();
354  while (it1 != x.ConstValuesEnd ())
355  {
356  s += (*it1) * (*it1);
357  ++it1;
358  }
359  return std::sqrt (s);
360 }
361 
362 
363 double
365 {
366  double s = 0;
367  Values::const_iterator it1 = x.ConstValuesBegin ();
368  while (it1 != x.ConstValuesEnd ())
369  {
370  s += (*it1);
371  ++it1;
372  }
373  return s;
374 }
375 
376 
377 
378 double
380 {
381  double s = 0;
382  Values::const_iterator it1 = x.ConstValuesBegin ();
383  while (it1 != x.ConstValuesEnd ())
384  {
385  s *= (*it1);
386  ++it1;
387  }
388  return s;
389 }
390 
391 double
393 {
394  double i = 0;
395  Values::const_iterator vit = arg.ConstValuesBegin ();
396  Bands::const_iterator bit = arg.ConstBandsBegin ();
397  while (vit != arg.ConstValuesEnd ())
398  {
399  NS_ASSERT (bit != arg.ConstBandsEnd ());
400  i += (*vit) * (bit->fh - bit->fl);
401  ++vit;
402  ++bit;
403  }
404  NS_ASSERT (bit == arg.ConstBandsEnd ());
405  return i;
406 }
407 
408 
409 
410 Ptr<SpectrumValue>
412 {
413  Ptr<SpectrumValue> p = Create<SpectrumValue> (m_spectrumModel);
414  *p = *this;
415  return p;
416 
417  // return Copy<SpectrumValue> (*this)
418 }
419 
420 
421 std::ostream&
422 operator << (std::ostream& os, const SpectrumValue& pvf)
423 {
424  Values::const_iterator it1 = pvf.ConstValuesBegin ();
425  while (it1 != pvf.ConstValuesEnd ())
426  {
427  os << *it1 << " ";
428  ++it1;
429  }
430  os << std::endl;
431  return os;
432 }
433 
434 
435 
436 SpectrumValue
437 operator+ (const SpectrumValue& lhs, const SpectrumValue& rhs)
438 {
439  SpectrumValue res = lhs;
440  res.Add (rhs);
441  return res;
442 }
443 
444 
445 SpectrumValue
446 operator+ (const SpectrumValue& lhs, double rhs)
447 {
448  SpectrumValue res = lhs;
449  res.Add (rhs);
450  return res;
451 }
452 
453 
454 SpectrumValue
455 operator+ (double lhs, const SpectrumValue& rhs)
456 {
457  SpectrumValue res = rhs;
458  res.Add (lhs);
459  return res;
460 }
461 
462 
463 SpectrumValue
464 operator- (const SpectrumValue& lhs, const SpectrumValue& rhs)
465 {
466  SpectrumValue res = rhs;
467  res.ChangeSign ();
468  res.Add (lhs);
469  return res;
470 }
471 
472 
473 
474 SpectrumValue
475 operator- (const SpectrumValue& lhs, double rhs)
476 {
477  SpectrumValue res = lhs;
478  res.Subtract (rhs);
479  return res;
480 }
481 
482 
483 SpectrumValue
484 operator- (double lhs, const SpectrumValue& rhs)
485 {
486  SpectrumValue res = rhs;
487  res.Subtract (lhs);
488  return res;
489 }
490 
491 SpectrumValue
492 operator* (const SpectrumValue& lhs, const SpectrumValue& rhs)
493 {
494  SpectrumValue res = lhs;
495  res.Multiply (rhs);
496  return res;
497 }
498 
499 
500 SpectrumValue
501 operator* (const SpectrumValue& lhs, double rhs)
502 {
503  SpectrumValue res = lhs;
504  res.Multiply (rhs);
505  return res;
506 }
507 
508 
509 SpectrumValue
510 operator* (double lhs, const SpectrumValue& rhs)
511 {
512  SpectrumValue res = rhs;
513  res.Multiply (lhs);
514  return res;
515 }
516 
517 
518 SpectrumValue
519 operator/ (const SpectrumValue& lhs, const SpectrumValue& rhs)
520 {
521  SpectrumValue res = lhs;
522  res.Divide (rhs);
523  return res;
524 }
525 
526 
527 SpectrumValue
528 operator/ (const SpectrumValue& lhs, double rhs)
529 {
530  SpectrumValue res = lhs;
531  res.Divide (rhs);
532  return res;
533 }
534 
535 
536 SpectrumValue
537 operator/ (double lhs, const SpectrumValue& rhs)
538 {
539  SpectrumValue res = rhs;
540  res.Divide (lhs);
541  return res;
542 }
543 
544 
545 SpectrumValue
547 {
548  return rhs;
549 }
550 
551 SpectrumValue
553 {
554  SpectrumValue res = rhs;
555  res.ChangeSign ();
556  return res;
557 }
558 
559 
560 SpectrumValue
561 Pow (double lhs, const SpectrumValue& rhs)
562 {
563  SpectrumValue res = rhs;
564  res.Exp (lhs);
565  return res;
566 }
567 
568 
569 SpectrumValue
570 Pow (const SpectrumValue& lhs, double rhs)
571 {
572  SpectrumValue res = lhs;
573  res.Pow (rhs);
574  return res;
575 }
576 
577 
578 SpectrumValue
579 Log10 (const SpectrumValue& arg)
580 {
581  SpectrumValue res = arg;
582  res.Log10 ();
583  return res;
584 }
585 
586 SpectrumValue
587 Log2 (const SpectrumValue& arg)
588 {
589  SpectrumValue res = arg;
590  res.Log2 ();
591  return res;
592 }
593 
594 SpectrumValue
595 Log (const SpectrumValue& arg)
596 {
597  SpectrumValue res = arg;
598  res.Log ();
599  return res;
600 }
601 
602 SpectrumValue&
604 {
605  Add (rhs);
606  return *this;
607 }
608 
611 {
612  Subtract (rhs);
613  return *this;
614 }
615 
618 {
619  Multiply (rhs);
620  return *this;
621 }
622 
625 {
626  Divide (rhs);
627  return *this;
628 }
629 
630 
633 {
634  Add (rhs);
635  return *this;
636 }
637 
640 {
641  Subtract (rhs);
642  return *this;
643 }
644 
647 {
648  Multiply (rhs);
649  return *this;
650 }
651 
654 {
655  Divide (rhs);
656  return *this;
657 }
658 
659 
662 {
663  Values::iterator it1 = m_values.begin ();
664 
665  while (it1 != m_values.end ())
666  {
667  *it1 = rhs;
668  ++it1;
669  }
670  return *this;
671 }
672 
673 
674 
677 {
678  SpectrumValue res = *this;
679  res.ShiftLeft (n);
680  return res;
681 }
682 
685 {
686  SpectrumValue res = *this;
687  res.ShiftRight (n);
688  return res;
689 }
690 
691 
692 
693 
694 } // namespace ns3
695 
Values::const_iterator ConstValuesEnd() const
SpectrumValue & operator*=(const SpectrumValue &rhs)
Multiply *this by the Right Hand Side of the operator, component by component.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
uint32_t SpectrumModelUid_t
SpectrumValue & operator/=(const SpectrumValue &rhs)
Divide *this by the Right Hand Side of the operator, component by component.
double Integral(const SpectrumValue &arg)
int64x64_t operator+(const int64x64_t &lhs)
Unary plus operator.
Definition: int64x64-128.h:381
void Subtract(const SpectrumValue &x)
SpectrumValue operator>>(int n) const
right shift operator
SpectrumValue & operator+=(const SpectrumValue &rhs)
Add the Right Hand Side of the operator to *this, component by component.
SpectrumModelUid_t GetSpectrumModelUid() const
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:61
int64x64_t operator-(const int64x64_t &lhs)
Unary negation operator (change sign operator)
Definition: int64x64-128.h:389
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
Values::iterator ValuesEnd()
friend SpectrumValue Log(const SpectrumValue &arg)
void Divide(const SpectrumValue &x)
void Multiply(const SpectrumValue &x)
Bands::const_iterator End() const
friend SpectrumValue Log10(const SpectrumValue &arg)
int64x64_t operator*(const int64x64_t &lhs, const int64x64_t &rhs)
Multiplication operator.
Definition: int64x64.h:117
SpectrumValue & operator-=(const SpectrumValue &rhs)
Subtract the Right Hand Side of the operator from *this, component by component.
Bands::const_iterator ConstBandsEnd() const
SpectrumModelUid_t GetUid() const
SpectrumValue operator<<(int n) const
left shift operator
Ptr< const SpectrumModel > GetSpectrumModel() const
friend SpectrumValue Pow(const SpectrumValue &lhs, double rhs)
Ptr< SampleEmitter > s
void Add(const SpectrumValue &x)
SpectrumValue Pow(double lhs, const SpectrumValue &rhs)
Ptr< const SpectrumModel > m_spectrumModel
void ShiftRight(int n)
int64x64_t operator/(const int64x64_t &lhs, const int64x64_t &rhs)
Division operator.
Definition: int64x64.h:128
Bands::const_iterator ConstBandsBegin() const
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:43
SpectrumValue Log10(const SpectrumValue &arg)
Values m_values
Set of values which implement the codomain of the functions in the Function Space defined by Spectrum...
friend SpectrumValue Log2(const SpectrumValue &arg)
double & operator[](size_t index)
Access value at given frequency index.
Values::iterator ValuesBegin()
SpectrumValue Log(const SpectrumValue &arg)
double Norm(const SpectrumValue &x)
SpectrumValue & operator=(double rhs)
Assign each component of *this to the value of the Right Hand Side of the operator.
Bands::const_iterator Begin() const
double Sum(const SpectrumValue &x)
Ptr< SpectrumValue > Copy() const
Values::const_iterator ConstValuesBegin() const
SpectrumValue Log2(const SpectrumValue &arg)
Set of values corresponding to a given SpectrumModel.
void Exp(double base)
double Prod(const SpectrumValue &x)