A Discrete-Event Network Simulator
API
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 namespace ns3 {
27 
28 NS_LOG_COMPONENT_DEFINE ("SpectrumValue");
29 
31 {
32 }
33 
35  : m_spectrumModel (sof),
36  m_values (sof->GetNumBands ())
37 {
38 
39 }
40 
41 double&
43 {
44  return m_values.at (index);
45 }
46 
47 const double&
48 SpectrumValue::operator[] (size_t index) const
49 {
50  return m_values.at (index);
51 }
52 
53 
56 {
57  return m_spectrumModel->GetUid ();
58 }
59 
60 
63 {
64  return m_spectrumModel;
65 }
66 
67 
68 Values::const_iterator
70 {
71  return m_values.begin ();
72 }
73 
74 Values::const_iterator
76 {
77  return m_values.end ();
78 }
79 
80 
81 Values::iterator
83 {
84  return m_values.begin ();
85 }
86 
87 Values::iterator
89 {
90  return m_values.end ();
91 }
92 
93 Bands::const_iterator
95 {
96  return m_spectrumModel->Begin ();
97 }
98 
99 Bands::const_iterator
101 {
102  return m_spectrumModel->End ();
103 }
104 
105 
106 void
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 
114  while (it1 != m_values.end ())
115  {
116  NS_ASSERT ( it2 != x.m_values.end ());
117  *it1 += *it2;
118  ++it1;
119  ++it2;
120  }
121 }
122 
123 
124 void
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
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 
146  while (it1 != m_values.end ())
147  {
148  NS_ASSERT ( it2 != x.m_values.end ());
149  *it1 -= *it2;
150  ++it1;
151  ++it2;
152  }
153 }
154 
155 
156 void
158 {
159  Add (-s);
160 }
161 
162 
163 
164 void
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 
172  while (it1 != m_values.end ())
173  {
174  NS_ASSERT ( it2 != x.m_values.end ());
175  *it1 *= *it2;
176  ++it1;
177  ++it2;
178  }
179 }
180 
181 
182 void
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
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 
205  while (it1 != m_values.end ())
206  {
207  NS_ASSERT ( it2 != x.m_values.end ());
208  *it1 /= *it2;
209  ++it1;
210  ++it2;
211  }
212 }
213 
214 
215 void
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
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
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
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
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
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
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
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
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
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
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
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
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>
410 {
411  Ptr<SpectrumValue> p = Create<SpectrumValue> (m_spectrumModel);
412  *p = *this;
413  return p;
414 
415  // return Copy<SpectrumValue> (*this)
416 }
417 
418 
425 std::ostream&
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
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
450 operator+ (const SpectrumValue& lhs, double rhs)
451 {
452  SpectrumValue res = lhs;
453  res.Add (rhs);
454  return res;
455 }
456 
457 
458 SpectrumValue
459 operator+ (double lhs, const SpectrumValue& rhs)
460 {
461  SpectrumValue res = rhs;
462  res.Add (lhs);
463  return res;
464 }
465 
466 
467 SpectrumValue
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
479 operator- (const SpectrumValue& lhs, double rhs)
480 {
481  SpectrumValue res = lhs;
482  res.Subtract (rhs);
483  return res;
484 }
485 
486 
487 SpectrumValue
488 operator- (double lhs, const SpectrumValue& rhs)
489 {
490  SpectrumValue res = rhs;
491  res.Subtract (lhs);
492  return res;
493 }
494 
495 SpectrumValue
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
505 operator* (const SpectrumValue& lhs, double rhs)
506 {
507  SpectrumValue res = lhs;
508  res.Multiply (rhs);
509  return res;
510 }
511 
512 
513 SpectrumValue
514 operator* (double lhs, const SpectrumValue& rhs)
515 {
516  SpectrumValue res = rhs;
517  res.Multiply (lhs);
518  return res;
519 }
520 
521 
522 SpectrumValue
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
532 operator/ (const SpectrumValue& lhs, double rhs)
533 {
534  SpectrumValue res = lhs;
535  res.Divide (rhs);
536  return res;
537 }
538 
539 
540 SpectrumValue
541 operator/ (double lhs, const SpectrumValue& rhs)
542 {
543  SpectrumValue res = rhs;
544  res.Divide (lhs);
545  return res;
546 }
547 
548 
549 SpectrumValue
551 {
552  return rhs;
553 }
554 
555 SpectrumValue
557 {
558  SpectrumValue res = rhs;
559  res.ChangeSign ();
560  return res;
561 }
562 
563 
564 SpectrumValue
565 Pow (double lhs, const SpectrumValue& rhs)
566 {
567  SpectrumValue res = rhs;
568  res.Exp (lhs);
569  return res;
570 }
571 
572 
573 SpectrumValue
574 Pow (const SpectrumValue& lhs, double rhs)
575 {
576  SpectrumValue res = lhs;
577  res.Pow (rhs);
578  return res;
579 }
580 
581 
582 SpectrumValue
583 Log10 (const SpectrumValue& arg)
584 {
585  SpectrumValue res = arg;
586  res.Log10 ();
587  return res;
588 }
589 
590 SpectrumValue
591 Log2 (const SpectrumValue& arg)
592 {
593  SpectrumValue res = arg;
594  res.Log2 ();
595  return res;
596 }
597 
598 SpectrumValue
599 Log (const SpectrumValue& arg)
600 {
601  SpectrumValue res = arg;
602  res.Log ();
603  return res;
604 }
605 
606 SpectrumValue&
608 {
609  Add (rhs);
610  return *this;
611 }
612 
615 {
616  Subtract (rhs);
617  return *this;
618 }
619 
622 {
623  Multiply (rhs);
624  return *this;
625 }
626 
629 {
630  Divide (rhs);
631  return *this;
632 }
633 
634 
637 {
638  Add (rhs);
639  return *this;
640 }
641 
644 {
645  Subtract (rhs);
646  return *this;
647 }
648 
651 {
652  Multiply (rhs);
653  return *this;
654 }
655 
658 {
659  Divide (rhs);
660  return *this;
661 }
662 
663 
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 
681 {
682  SpectrumValue res = *this;
683  res.ShiftLeft (n);
684  return res;
685 }
686 
689 {
690  SpectrumValue res = *this;
691  res.ShiftRight (n);
692  return res;
693 }
694 
695 
696 
697 
698 } // namespace ns3
699 
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
Uid for SpectrumModels.
void ChangeSign()
Change the values sign.
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:410
void Subtract(const SpectrumValue &x)
Subtracts a SpectrumValue (element by element subtraction)
SpectrumValue & operator+=(const SpectrumValue &rhs)
Add the Right Hand Side of the operator to *this, component by component.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
int64x64_t operator-(const int64x64_t &lhs)
Unary negation operator (change sign operator).
Definition: int64x64-128.h:418
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
SpectrumValue operator>>(int n) const
right shift operator
Values::iterator ValuesEnd()
friend SpectrumValue Log(const SpectrumValue &arg)
void Divide(const SpectrumValue &x)
Divides by a SpectrumValue (element to element division)
Values::const_iterator ConstValuesBegin() const
void Multiply(const SpectrumValue &x)
Multiplies for a SpectrumValue (element to element multiplication)
SpectrumModelUid_t GetUid() const
friend SpectrumValue Log10(const SpectrumValue &arg)
int64x64_t operator*(const int64x64_t &lhs, const int64x64_t &rhs)
Multiplication operator.
Definition: int64x64.h:108
SpectrumValue & operator-=(const SpectrumValue &rhs)
Subtract the Right Hand Side of the operator from *this, component by component.
friend SpectrumValue Pow(const SpectrumValue &lhs, double rhs)
void Add(const SpectrumValue &x)
Add a SpectrumValue (element to element addition)
SpectrumValue Pow(double lhs, const SpectrumValue &rhs)
Ptr< const SpectrumModel > m_spectrumModel
The spectrum model.
void ShiftRight(int n)
Shift the values to the right.
Ptr< SpectrumValue > Copy() const
int64x64_t operator/(const int64x64_t &lhs, const int64x64_t &rhs)
Division operator.
Definition: int64x64.h:119
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:42
Bands::const_iterator Begin() const
Const Iterator to the model Bands container start.
void Log2()
Applies a Log2 to each the elements.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Values::const_iterator ConstValuesEnd() const
Ptr< const SpectrumModel > GetSpectrumModel() const
void Log()
Applies a Log to each the elements.
void Log10()
Applies a Log10 to each the elements.
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.
void ShiftLeft(int n)
Shift the values to the left.
double Sum(const SpectrumValue &x)
Bands::const_iterator ConstBandsEnd() const
SpectrumValue operator<<(int n) const
left shift operator
SpectrumValue Log2(const SpectrumValue &arg)
Set of values corresponding to a given SpectrumModel.
void Exp(double base)
Modifies each element so that it is the base raised to each element value.
SpectrumModelUid_t GetSpectrumModelUid() const
Bands::const_iterator End() const
Const Iterator to the model Bands container end.
Bands::const_iterator ConstBandsBegin() const
double Prod(const SpectrumValue &x)