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 
52 {
53  return m_spectrumModel->GetUid ();
54 }
55 
56 
59 {
60  return m_spectrumModel;
61 }
62 
63 
64 Values::const_iterator
66 {
67  return m_values.begin ();
68 }
69 
70 Values::const_iterator
72 {
73  return m_values.end ();
74 }
75 
76 
77 Values::iterator
79 {
80  return m_values.begin ();
81 }
82 
83 Values::iterator
85 {
86  return m_values.end ();
87 }
88 
89 Bands::const_iterator
91 {
92  return m_spectrumModel->Begin ();
93 }
94 
95 Bands::const_iterator
97 {
98  return m_spectrumModel->End ();
99 }
100 
101 
102 void
104 {
105  Values::iterator it1 = m_values.begin ();
106  Values::const_iterator it2 = x.m_values.begin ();
107 
109 
110  while (it1 != m_values.end ())
111  {
112  NS_ASSERT ( it2 != x.m_values.end ());
113  *it1 += *it2;
114  ++it1;
115  ++it2;
116  }
117 }
118 
119 
120 void
122 {
123  Values::iterator it1 = m_values.begin ();
124 
125  while (it1 != m_values.end ())
126  {
127  *it1 += s;
128  ++it1;
129  }
130 }
131 
132 
133 
134 void
136 {
137  Values::iterator it1 = m_values.begin ();
138  Values::const_iterator it2 = x.m_values.begin ();
139 
141 
142  while (it1 != m_values.end ())
143  {
144  NS_ASSERT ( it2 != x.m_values.end ());
145  *it1 -= *it2;
146  ++it1;
147  ++it2;
148  }
149 }
150 
151 
152 void
154 {
155  Add (-s);
156 }
157 
158 
159 
160 void
162 {
163  Values::iterator it1 = m_values.begin ();
164  Values::const_iterator it2 = x.m_values.begin ();
165 
167 
168  while (it1 != m_values.end ())
169  {
170  NS_ASSERT ( it2 != x.m_values.end ());
171  *it1 *= *it2;
172  ++it1;
173  ++it2;
174  }
175 }
176 
177 
178 void
180 {
181  Values::iterator it1 = m_values.begin ();
182 
183  while (it1 != m_values.end ())
184  {
185  *it1 *= s;
186  ++it1;
187  }
188 }
189 
190 
191 
192 
193 void
195 {
196  Values::iterator it1 = m_values.begin ();
197  Values::const_iterator it2 = x.m_values.begin ();
198 
200 
201  while (it1 != m_values.end ())
202  {
203  NS_ASSERT ( it2 != x.m_values.end ());
204  *it1 /= *it2;
205  ++it1;
206  ++it2;
207  }
208 }
209 
210 
211 void
213 {
214  NS_LOG_FUNCTION (this << s);
215  Values::iterator it1 = m_values.begin ();
216 
217  while (it1 != m_values.end ())
218  {
219  *it1 /= s;
220  ++it1;
221  }
222 }
223 
224 
225 
226 
227 void
229 {
230  Values::iterator it1 = m_values.begin ();
231 
232  while (it1 != m_values.end ())
233  {
234  *it1 = -(*it1);
235  ++it1;
236  }
237 }
238 
239 
240 void
242 {
243  int i = 0;
244  while (i < (int) m_values.size () - n)
245  {
246  m_values.at (i) = m_values.at (i + n);
247  i++;
248  }
249  while (i < (int)m_values.size ())
250  {
251  m_values.at (i) = 0;
252  i++;
253  }
254 }
255 
256 
257 void
259 {
260  int i = m_values.size () - 1;
261  while (i - n >= 0)
262  {
263  m_values.at (i) = m_values.at (i - n);
264  i = i - 1;
265  }
266  while (i >= 0)
267  {
268  m_values.at (i) = 0;
269  --i;
270  }
271 }
272 
273 
274 
275 void
276 SpectrumValue::Pow (double exp)
277 {
278  NS_LOG_FUNCTION (this << exp);
279  Values::iterator it1 = m_values.begin ();
280 
281  while (it1 != m_values.end ())
282  {
283  *it1 = std::pow (*it1, exp);
284  ++it1;
285  }
286 }
287 
288 
289 void
290 SpectrumValue::Exp (double base)
291 {
292  NS_LOG_FUNCTION (this << base);
293  Values::iterator it1 = m_values.begin ();
294 
295  while (it1 != m_values.end ())
296  {
297  *it1 = std::pow (base, *it1);
298  ++it1;
299  }
300 }
301 
302 
303 void
305 {
306  NS_LOG_FUNCTION (this);
307  Values::iterator it1 = m_values.begin ();
308 
309  while (it1 != m_values.end ())
310  {
311  *it1 = std::log10 (*it1);
312  ++it1;
313  }
314 }
315 
316 void
318 {
319  NS_LOG_FUNCTION (this);
320  Values::iterator it1 = m_values.begin ();
321 
322  while (it1 != m_values.end ())
323  {
324  *it1 = log2 (*it1);
325  ++it1;
326  }
327 }
328 
329 
330 void
332 {
333  NS_LOG_FUNCTION (this);
334  Values::iterator it1 = m_values.begin ();
335 
336  while (it1 != m_values.end ())
337  {
338  *it1 = std::log (*it1);
339  ++it1;
340  }
341 }
342 
343 double
345 {
346  double s = 0;
347  Values::const_iterator it1 = x.ConstValuesBegin ();
348  while (it1 != x.ConstValuesEnd ())
349  {
350  s += (*it1) * (*it1);
351  ++it1;
352  }
353  return std::sqrt (s);
354 }
355 
356 
357 double
359 {
360  double s = 0;
361  Values::const_iterator it1 = x.ConstValuesBegin ();
362  while (it1 != x.ConstValuesEnd ())
363  {
364  s += (*it1);
365  ++it1;
366  }
367  return s;
368 }
369 
370 
371 
372 double
374 {
375  double s = 0;
376  Values::const_iterator it1 = x.ConstValuesBegin ();
377  while (it1 != x.ConstValuesEnd ())
378  {
379  s *= (*it1);
380  ++it1;
381  }
382  return s;
383 }
384 
385 double
387 {
388  double i = 0;
389  Values::const_iterator vit = arg.ConstValuesBegin ();
390  Bands::const_iterator bit = arg.ConstBandsBegin ();
391  while (vit != arg.ConstValuesEnd ())
392  {
393  NS_ASSERT (bit != arg.ConstBandsEnd ());
394  i += (*vit) * (bit->fh - bit->fl);
395  ++vit;
396  ++bit;
397  }
398  NS_ASSERT (bit == arg.ConstBandsEnd ());
399  return i;
400 }
401 
402 
403 
404 Ptr<SpectrumValue>
406 {
407  Ptr<SpectrumValue> p = Create<SpectrumValue> (m_spectrumModel);
408  *p = *this;
409  return p;
410 
411  // return Copy<SpectrumValue> (*this)
412 }
413 
414 
415 std::ostream&
416 operator << (std::ostream& os, const SpectrumValue& pvf)
417 {
418  Values::const_iterator it1 = pvf.ConstValuesBegin ();
419  while (it1 != pvf.ConstValuesEnd ())
420  {
421  os << *it1 << " ";
422  ++it1;
423  }
424  os << std::endl;
425  return os;
426 }
427 
428 
429 
430 SpectrumValue
431 operator+ (const SpectrumValue& lhs, const SpectrumValue& rhs)
432 {
433  SpectrumValue res = lhs;
434  res.Add (rhs);
435  return res;
436 }
437 
438 
439 SpectrumValue
440 operator+ (const SpectrumValue& lhs, double rhs)
441 {
442  SpectrumValue res = lhs;
443  res.Add (rhs);
444  return res;
445 }
446 
447 
448 SpectrumValue
449 operator+ (double lhs, const SpectrumValue& rhs)
450 {
451  SpectrumValue res = rhs;
452  res.Add (lhs);
453  return res;
454 }
455 
456 
457 SpectrumValue
458 operator- (const SpectrumValue& lhs, const SpectrumValue& rhs)
459 {
460  SpectrumValue res = rhs;
461  res.ChangeSign ();
462  res.Add (lhs);
463  return res;
464 }
465 
466 
467 
468 SpectrumValue
469 operator- (const SpectrumValue& lhs, double rhs)
470 {
471  SpectrumValue res = lhs;
472  res.Subtract (rhs);
473  return res;
474 }
475 
476 
477 SpectrumValue
478 operator- (double lhs, const SpectrumValue& rhs)
479 {
480  SpectrumValue res = rhs;
481  res.Subtract (lhs);
482  return res;
483 }
484 
485 SpectrumValue
486 operator* (const SpectrumValue& lhs, const SpectrumValue& rhs)
487 {
488  SpectrumValue res = lhs;
489  res.Multiply (rhs);
490  return res;
491 }
492 
493 
494 SpectrumValue
495 operator* (const SpectrumValue& lhs, double rhs)
496 {
497  SpectrumValue res = lhs;
498  res.Multiply (rhs);
499  return res;
500 }
501 
502 
503 SpectrumValue
504 operator* (double lhs, const SpectrumValue& rhs)
505 {
506  SpectrumValue res = rhs;
507  res.Multiply (lhs);
508  return res;
509 }
510 
511 
512 SpectrumValue
513 operator/ (const SpectrumValue& lhs, const SpectrumValue& rhs)
514 {
515  SpectrumValue res = lhs;
516  res.Divide (rhs);
517  return res;
518 }
519 
520 
521 SpectrumValue
522 operator/ (const SpectrumValue& lhs, double rhs)
523 {
524  SpectrumValue res = lhs;
525  res.Divide (rhs);
526  return res;
527 }
528 
529 
530 SpectrumValue
531 operator/ (double lhs, const SpectrumValue& rhs)
532 {
533  SpectrumValue res = rhs;
534  res.Divide (lhs);
535  return res;
536 }
537 
538 
539 SpectrumValue
541 {
542  return rhs;
543 }
544 
545 SpectrumValue
547 {
548  SpectrumValue res = rhs;
549  res.ChangeSign ();
550  return res;
551 }
552 
553 
554 SpectrumValue
555 Pow (double lhs, const SpectrumValue& rhs)
556 {
557  SpectrumValue res = rhs;
558  res.Exp (lhs);
559  return res;
560 }
561 
562 
563 SpectrumValue
564 Pow (const SpectrumValue& lhs, double rhs)
565 {
566  SpectrumValue res = lhs;
567  res.Pow (rhs);
568  return res;
569 }
570 
571 
572 SpectrumValue
573 Log10 (const SpectrumValue& arg)
574 {
575  SpectrumValue res = arg;
576  res.Log10 ();
577  return res;
578 }
579 
580 SpectrumValue
581 Log2 (const SpectrumValue& arg)
582 {
583  SpectrumValue res = arg;
584  res.Log2 ();
585  return res;
586 }
587 
588 SpectrumValue
589 Log (const SpectrumValue& arg)
590 {
591  SpectrumValue res = arg;
592  res.Log ();
593  return res;
594 }
595 
596 SpectrumValue&
598 {
599  Add (rhs);
600  return *this;
601 }
602 
605 {
606  Subtract (rhs);
607  return *this;
608 }
609 
612 {
613  Multiply (rhs);
614  return *this;
615 }
616 
619 {
620  Divide (rhs);
621  return *this;
622 }
623 
624 
627 {
628  Add (rhs);
629  return *this;
630 }
631 
634 {
635  Subtract (rhs);
636  return *this;
637 }
638 
641 {
642  Multiply (rhs);
643  return *this;
644 }
645 
648 {
649  Divide (rhs);
650  return *this;
651 }
652 
653 
656 {
657  Values::iterator it1 = m_values.begin ();
658 
659  while (it1 != m_values.end ())
660  {
661  *it1 = rhs;
662  ++it1;
663  }
664  return *this;
665 }
666 
667 
668 
671 {
672  SpectrumValue res = *this;
673  res.ShiftLeft (n);
674  return res;
675 }
676 
679 {
680  SpectrumValue res = *this;
681  res.ShiftRight (n);
682  return res;
683 }
684 
685 
686 
687 
688 } // namespace ns3
689 
Values::const_iterator ConstValuesEnd() const
SpectrumValue & operator*=(const SpectrumValue &rhs)
Multiply *this by the Right Hand Side of the operator, component by component.
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
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)
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)
Definition: assert.h:64
TracedValue< T > operator*(const TracedValue< T > &lhs, const TracedValue< U > &rhs)
Definition: traced-value.h:365
Values::iterator ValuesEnd()
friend SpectrumValue Log(const SpectrumValue &arg)
void Divide(const SpectrumValue &x)
void Multiply(const SpectrumValue &x)
friend SpectrumValue Log10(const SpectrumValue &arg)
SpectrumValue & operator-=(const SpectrumValue &rhs)
Subtract the Right Hand Side of the operator from *this, component by component.
Bands::const_iterator ConstBandsEnd() const
SpectrumValue operator<<(int n) const
left shift operator
NS_LOG_COMPONENT_DEFINE("SpectrumValue")
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
Time operator+(const Time &lhs, const Time &rhs)
Definition: nstime.h:649
void ShiftRight(int n)
TracedValue< T > operator/(const TracedValue< T > &lhs, const TracedValue< U > &rhs)
Definition: traced-value.h:381
Bands::const_iterator ConstBandsBegin() const
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:43
Time operator-(const Time &lhs, const Time &rhs)
Definition: nstime.h:653
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.
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)