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
26namespace ns3 {
27
28NS_LOG_COMPONENT_DEFINE ("SpectrumValue");
29
31{
32}
33
35 : m_spectrumModel (sof),
36 m_values (sof->GetNumBands ())
37{
38
39}
40
41double&
43{
44 return m_values.at (index);
45}
46
47const double&
48SpectrumValue::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
68Values::const_iterator
70{
71 return m_values.begin ();
72}
73
74Values::const_iterator
76{
77 return m_values.end ();
78}
79
80
81Values::iterator
83{
84 return m_values.begin ();
85}
86
87Values::iterator
89{
90 return m_values.end ();
91}
92
93Bands::const_iterator
95{
96 return m_spectrumModel->Begin ();
97}
98
99Bands::const_iterator
101{
102 return m_spectrumModel->End ();
103}
104
105
106void
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
124void
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
138void
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
156void
158{
159 Add (-s);
160}
161
162
163
164void
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
182void
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
197void
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
215void
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
231void
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
244void
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
261void
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
279void
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
293void
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
307void
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
320void
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
334void
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
347double
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
361double
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
376double
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
389double
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
410{
411 Ptr<SpectrumValue> p = Create<SpectrumValue> (m_spectrumModel);
412 *p = *this;
413 return p;
414
415 // return Copy<SpectrumValue> (*this)
416}
417
418
425std::ostream&
426operator << (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
440SpectrumValue
442{
443 SpectrumValue res = lhs;
444 res.Add (rhs);
445 return res;
446}
447
448
450operator+ (const SpectrumValue& lhs, double rhs)
451{
452 SpectrumValue res = lhs;
453 res.Add (rhs);
454 return res;
455}
456
457
459operator+ (double lhs, const SpectrumValue& rhs)
460{
461 SpectrumValue res = rhs;
462 res.Add (lhs);
463 return res;
464}
465
466
469{
470 SpectrumValue res = rhs;
471 res.ChangeSign ();
472 res.Add (lhs);
473 return res;
474}
475
476
477
479operator- (const SpectrumValue& lhs, double rhs)
480{
481 SpectrumValue res = lhs;
482 res.Subtract (rhs);
483 return res;
484}
485
486
488operator- (double lhs, const SpectrumValue& rhs)
489{
490 SpectrumValue res = rhs;
491 res.Subtract (lhs);
492 return res;
493}
494
497{
498 SpectrumValue res = lhs;
499 res.Multiply (rhs);
500 return res;
501}
502
503
505operator* (const SpectrumValue& lhs, double rhs)
506{
507 SpectrumValue res = lhs;
508 res.Multiply (rhs);
509 return res;
510}
511
512
514operator* (double lhs, const SpectrumValue& rhs)
515{
516 SpectrumValue res = rhs;
517 res.Multiply (lhs);
518 return res;
519}
520
521
524{
525 SpectrumValue res = lhs;
526 res.Divide (rhs);
527 return res;
528}
529
530
532operator/ (const SpectrumValue& lhs, double rhs)
533{
534 SpectrumValue res = lhs;
535 res.Divide (rhs);
536 return res;
537}
538
539
541operator/ (double lhs, const SpectrumValue& rhs)
542{
543 SpectrumValue res = rhs;
544 res.Divide (lhs);
545 return res;
546}
547
548
551{
552 return rhs;
553}
554
557{
558 SpectrumValue res = rhs;
559 res.ChangeSign ();
560 return res;
561}
562
563
565Pow (double lhs, const SpectrumValue& rhs)
566{
567 SpectrumValue res = rhs;
568 res.Exp (lhs);
569 return res;
570}
571
572
574Pow (const SpectrumValue& lhs, double rhs)
575{
576 SpectrumValue res = lhs;
577 res.Pow (rhs);
578 return res;
579}
580
581
584{
585 SpectrumValue res = arg;
586 res.Log10 ();
587 return res;
588}
589
591Log2 (const SpectrumValue& arg)
592{
593 SpectrumValue res = arg;
594 res.Log2 ();
595 return res;
596}
597
599Log (const SpectrumValue& arg)
600{
601 SpectrumValue res = arg;
602 res.Log ();
603 return res;
604}
605
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
697{
698 return m_values.size ();
699}
700
701const double &
703{
704 return m_values.at (pos);
705}
706
707} // namespace ns3
708
Bands::const_iterator End() const
Const Iterator to the model Bands container end.
Bands::const_iterator Begin() const
Const Iterator to the model Bands container start.
SpectrumModelUid_t GetUid() const
Set of values corresponding to a given SpectrumModel.
double & operator[](size_t index)
Access value at given frequency index.
void Subtract(const SpectrumValue &x)
Subtracts a SpectrumValue (element by element subtraction)
Values::const_iterator ConstValuesBegin() const
Bands::const_iterator ConstBandsEnd() const
void Divide(const SpectrumValue &x)
Divides by a SpectrumValue (element to element division)
SpectrumValue & operator=(double rhs)
Assign each component of *this to the value of the Right Hand Side of the operator.
Values::iterator ValuesBegin()
void Exp(double base)
Modifies each element so that it is the base raised to each element value.
Bands::const_iterator ConstBandsBegin() const
void ChangeSign()
Change the values sign.
friend SpectrumValue Log2(const SpectrumValue &arg)
friend SpectrumValue Log10(const SpectrumValue &arg)
SpectrumValue operator<<(int n) const
left shift operator
void ShiftLeft(int n)
Shift the values to the left.
Ptr< SpectrumValue > Copy() const
friend SpectrumValue Log(const SpectrumValue &arg)
Values m_values
Set of values which implement the codomain of the functions in the Function Space defined by Spectrum...
uint32_t GetValuesN() const
Get the number of values stored in the array.
Values::iterator ValuesEnd()
Ptr< const SpectrumModel > GetSpectrumModel() const
SpectrumValue & operator*=(const SpectrumValue &rhs)
Multiply *this by the Right Hand Side of the operator, component by component.
Ptr< const SpectrumModel > m_spectrumModel
The spectrum model.
void ShiftRight(int n)
Shift the values to the right.
void Add(const SpectrumValue &x)
Add a SpectrumValue (element to element addition)
void Multiply(const SpectrumValue &x)
Multiplies for a SpectrumValue (element to element multiplication)
void Log()
Applies a Log to each the elements.
void Log2()
Applies a Log2 to each the elements.
SpectrumModelUid_t GetSpectrumModelUid() const
SpectrumValue & operator/=(const SpectrumValue &rhs)
Divide *this by the Right Hand Side of the operator, component by component.
SpectrumValue & operator+=(const SpectrumValue &rhs)
Add the Right Hand Side of the operator to *this, component by component.
Values::const_iterator ConstValuesEnd() const
SpectrumValue & operator-=(const SpectrumValue &rhs)
Subtract the Right Hand Side of the operator from *this, component by component.
SpectrumValue operator>>(int n) const
right shift operator
friend SpectrumValue Pow(const SpectrumValue &lhs, double rhs)
void Log10()
Applies a Log10 to each the elements.
const double & ValuesAt(uint32_t pos) const
Get the value element at the position.
#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, const int64x64_t &rhs)
Division operator.
Definition: int64x64.h:131
int64x64_t operator-(const int64x64_t &lhs, const int64x64_t &rhs)
Subtraction operator.
Definition: int64x64.h:103
int64x64_t operator+(const int64x64_t &lhs, const int64x64_t &rhs)
Addition operator.
Definition: int64x64.h:89
int64x64_t operator*(const int64x64_t &lhs, const int64x64_t &rhs)
Multiplication operator.
Definition: int64x64.h:117
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Every class exported by the ns3 library is enclosed in the ns3 namespace.
SpectrumValue Pow(double lhs, const SpectrumValue &rhs)
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:139
double Integral(const SpectrumValue &arg)
uint32_t SpectrumModelUid_t
Uid for SpectrumModels.
SpectrumValue Log2(const SpectrumValue &arg)
SpectrumValue Log10(const SpectrumValue &arg)
double Norm(const SpectrumValue &x)
SpectrumValue Log(const SpectrumValue &arg)
double Prod(const SpectrumValue &x)
double Sum(const SpectrumValue &x)
list x
Random number samples.