A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
spectrum-value.cc
Go to the documentation of this file.
1
2/*
3 * Copyright (c) 2009 CTTC
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Nicola Baldo <nbaldo@cttc.es>
19 */
20
21#include "spectrum-value.h"
22
23#include <ns3/log.h>
24#include <ns3/math.h>
25
26namespace ns3
27{
28
29NS_LOG_COMPONENT_DEFINE("SpectrumValue");
30
32{
33}
34
36 : m_spectrumModel(sof),
37 m_values(sof->GetNumBands())
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
55{
56 return m_spectrumModel->GetUid();
57}
58
61{
62 return m_spectrumModel;
63}
64
65Values::const_iterator
67{
68 return m_values.begin();
69}
70
71Values::const_iterator
73{
74 return m_values.end();
75}
76
77Values::iterator
79{
80 return m_values.begin();
81}
82
83Values::iterator
85{
86 return m_values.end();
87}
88
89Bands::const_iterator
91{
92 return m_spectrumModel->Begin();
93}
94
95Bands::const_iterator
97{
98 return m_spectrumModel->End();
99}
100
101void
103{
104 auto it1 = m_values.begin();
105 auto it2 = x.m_values.begin();
106
107 NS_ASSERT(m_spectrumModel == x.m_spectrumModel);
108 NS_ASSERT(m_values.size() == x.m_values.size());
109
110 while (it1 != m_values.end())
111 {
112 *it1 += *it2;
113 ++it1;
114 ++it2;
115 }
116}
117
118void
120{
121 auto it1 = m_values.begin();
122
123 while (it1 != m_values.end())
124 {
125 *it1 += s;
126 ++it1;
127 }
128}
129
130void
132{
133 auto it1 = m_values.begin();
134 auto it2 = x.m_values.begin();
135
136 NS_ASSERT(m_spectrumModel == x.m_spectrumModel);
137 NS_ASSERT(m_values.size() == x.m_values.size());
138
139 while (it1 != m_values.end())
140 {
141 *it1 -= *it2;
142 ++it1;
143 ++it2;
144 }
145}
146
147void
149{
150 Add(-s);
151}
152
153void
155{
156 auto it1 = m_values.begin();
157 auto it2 = x.m_values.begin();
158
159 NS_ASSERT(m_spectrumModel == x.m_spectrumModel);
160 NS_ASSERT(m_values.size() == x.m_values.size());
161
162 while (it1 != m_values.end())
163 {
164 *it1 *= *it2;
165 ++it1;
166 ++it2;
167 }
168}
169
170void
172{
173 auto it1 = m_values.begin();
174
175 while (it1 != m_values.end())
176 {
177 *it1 *= s;
178 ++it1;
179 }
180}
181
182void
184{
185 auto it1 = m_values.begin();
186 auto it2 = x.m_values.begin();
187
188 NS_ASSERT(m_spectrumModel == x.m_spectrumModel);
189 NS_ASSERT(m_values.size() == x.m_values.size());
190
191 while (it1 != m_values.end())
192 {
193 *it1 /= *it2;
194 ++it1;
195 ++it2;
196 }
197}
198
199void
201{
202 NS_LOG_FUNCTION(this << s);
203 auto it1 = m_values.begin();
204
205 while (it1 != m_values.end())
206 {
207 *it1 /= s;
208 ++it1;
209 }
210}
211
212void
214{
215 auto it1 = m_values.begin();
216
217 while (it1 != m_values.end())
218 {
219 *it1 = -(*it1);
220 ++it1;
221 }
222}
223
224void
226{
227 int i = 0;
228 while (i < (int)m_values.size() - n)
229 {
230 m_values.at(i) = m_values.at(i + n);
231 i++;
232 }
233 while (i < (int)m_values.size())
234 {
235 m_values.at(i) = 0;
236 i++;
237 }
238}
239
240void
242{
243 int i = m_values.size() - 1;
244 while (i - n >= 0)
245 {
246 m_values.at(i) = m_values.at(i - n);
247 i = i - 1;
248 }
249 while (i >= 0)
250 {
251 m_values.at(i) = 0;
252 --i;
253 }
254}
255
256void
258{
259 NS_LOG_FUNCTION(this << exp);
260 auto it1 = m_values.begin();
261
262 while (it1 != m_values.end())
263 {
264 *it1 = std::pow(*it1, exp);
265 ++it1;
266 }
267}
268
269void
271{
272 NS_LOG_FUNCTION(this << base);
273 auto it1 = m_values.begin();
274
275 while (it1 != m_values.end())
276 {
277 *it1 = std::pow(base, *it1);
278 ++it1;
279 }
280}
281
282void
284{
285 NS_LOG_FUNCTION(this);
286 auto it1 = m_values.begin();
287
288 while (it1 != m_values.end())
289 {
290 *it1 = std::log10(*it1);
291 ++it1;
292 }
293}
294
295void
297{
298 NS_LOG_FUNCTION(this);
299 auto it1 = m_values.begin();
300
301 while (it1 != m_values.end())
302 {
303 *it1 = log2(*it1);
304 ++it1;
305 }
306}
307
308void
310{
311 NS_LOG_FUNCTION(this);
312 auto it1 = m_values.begin();
313
314 while (it1 != m_values.end())
315 {
316 *it1 = std::log(*it1);
317 ++it1;
318 }
319}
320
321double
323{
324 double s = 0;
325 auto it1 = x.ConstValuesBegin();
326 while (it1 != x.ConstValuesEnd())
327 {
328 s += (*it1) * (*it1);
329 ++it1;
330 }
331 return std::sqrt(s);
332}
333
334double
336{
337 double s = 0;
338 auto it1 = x.ConstValuesBegin();
339 while (it1 != x.ConstValuesEnd())
340 {
341 s += (*it1);
342 ++it1;
343 }
344 return s;
345}
346
347double
349{
350 double s = 0;
351 auto it1 = x.ConstValuesBegin();
352 while (it1 != x.ConstValuesEnd())
353 {
354 s *= (*it1);
355 ++it1;
356 }
357 return s;
358}
359
360double
362{
363 double i = 0;
364 auto vit = arg.ConstValuesBegin();
365 auto bit = arg.ConstBandsBegin();
366 while (vit != arg.ConstValuesEnd())
367 {
368 NS_ASSERT(bit != arg.ConstBandsEnd());
369 i += (*vit) * (bit->fh - bit->fl);
370 ++vit;
371 ++bit;
372 }
373 NS_ASSERT(bit == arg.ConstBandsEnd());
374 return i;
375}
376
379{
380 Ptr<SpectrumValue> p = Create<SpectrumValue>(m_spectrumModel);
381 *p = *this;
382 return p;
383
384 // return Copy<SpectrumValue> (*this)
385}
386
387/**
388 * \brief Output stream operator
389 * \param os output stream
390 * \param pvf the SpectrumValue to print
391 * \return an output stream
392 */
393std::ostream&
394operator<<(std::ostream& os, const SpectrumValue& pvf)
395{
396 auto it1 = pvf.ConstValuesBegin();
397 bool first = true;
398 while (it1 != pvf.ConstValuesEnd())
399 {
400 if (!first)
401 {
402 os << " ";
403 }
404 else
405 {
406 first = false;
407 }
408 os << *it1;
409 ++it1;
410 }
411 return os;
412}
413
414SpectrumValue
415operator+(const SpectrumValue& lhs, const SpectrumValue& rhs)
416{
417 SpectrumValue res = lhs;
418 res.Add(rhs);
419 return res;
420}
421
422bool
424{
425 return (lhs.m_values == rhs.m_values);
426}
427
428bool
430{
431 return (lhs.m_values != rhs.m_values);
432}
433
435operator+(const SpectrumValue& lhs, double rhs)
436{
437 SpectrumValue res = lhs;
438 res.Add(rhs);
439 return res;
440}
441
443operator+(double lhs, const SpectrumValue& rhs)
444{
445 SpectrumValue res = rhs;
446 res.Add(lhs);
447 return res;
448}
449
451operator-(const SpectrumValue& lhs, const SpectrumValue& rhs)
452{
453 SpectrumValue res = rhs;
454 res.ChangeSign();
455 res.Add(lhs);
456 return res;
457}
458
460operator-(const SpectrumValue& lhs, double rhs)
461{
462 SpectrumValue res = lhs;
463 res.Subtract(rhs);
464 return res;
465}
466
468operator-(double lhs, const SpectrumValue& rhs)
469{
470 SpectrumValue res = rhs;
471 res.Subtract(lhs);
472 return res;
473}
474
476operator*(const SpectrumValue& lhs, const SpectrumValue& rhs)
477{
478 SpectrumValue res = lhs;
479 res.Multiply(rhs);
480 return res;
481}
482
484operator*(const SpectrumValue& lhs, double rhs)
485{
486 SpectrumValue res = lhs;
487 res.Multiply(rhs);
488 return res;
489}
490
492operator*(double lhs, const SpectrumValue& rhs)
493{
494 SpectrumValue res = rhs;
495 res.Multiply(lhs);
496 return res;
497}
498
500operator/(const SpectrumValue& lhs, const SpectrumValue& rhs)
501{
502 SpectrumValue res = lhs;
503 res.Divide(rhs);
504 return res;
505}
506
508operator/(const SpectrumValue& lhs, double rhs)
509{
510 SpectrumValue res = lhs;
511 res.Divide(rhs);
512 return res;
513}
514
516operator/(double lhs, const SpectrumValue& rhs)
517{
518 SpectrumValue res = rhs;
519 res.Divide(lhs);
520 return res;
521}
522
525{
526 return rhs;
527}
528
531{
532 SpectrumValue res = rhs;
533 res.ChangeSign();
534 return res;
535}
536
538Pow(double lhs, const SpectrumValue& rhs)
539{
540 SpectrumValue res = rhs;
541 res.Exp(lhs);
542 return res;
543}
544
546Pow(const SpectrumValue& lhs, double rhs)
547{
548 SpectrumValue res = lhs;
549 res.Pow(rhs);
550 return res;
551}
552
555{
556 SpectrumValue res = arg;
557 res.Log10();
558 return res;
559}
560
563{
564 SpectrumValue res = arg;
565 res.Log2();
566 return res;
567}
568
571{
572 SpectrumValue res = arg;
573 res.Log();
574 return res;
575}
576
579{
580 Add(rhs);
581 return *this;
582}
583
586{
587 Subtract(rhs);
588 return *this;
589}
590
593{
594 Multiply(rhs);
595 return *this;
596}
597
600{
601 Divide(rhs);
602 return *this;
603}
604
607{
608 Add(rhs);
609 return *this;
610}
611
614{
615 Subtract(rhs);
616 return *this;
617}
618
621{
622 Multiply(rhs);
623 return *this;
624}
625
628{
629 Divide(rhs);
630 return *this;
631}
632
635{
636 auto it1 = m_values.begin();
637
638 while (it1 != m_values.end())
639 {
640 *it1 = rhs;
641 ++it1;
642 }
643 return *this;
644}
645
647SpectrumValue::operator<<(int n) const
648{
649 SpectrumValue res = *this;
650 res.ShiftLeft(n);
651 return res;
652}
653
656{
657 SpectrumValue res = *this;
658 res.ShiftRight(n);
659 return res;
660}
661
664{
665 return m_values.size();
666}
667
668const double&
670{
671 return m_values.at(pos);
672}
673
674} // namespace ns3
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
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)
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)
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:66
int64x64_t operator/(const int64x64_t &lhs, const int64x64_t &rhs)
Division operator.
Definition: int64x64.h:133
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:88
int64x64_t operator*(const int64x64_t &lhs, const int64x64_t &rhs)
Multiplication operator.
Definition: int64x64.h:118
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Definition: first.py:1
Every class exported by the ns3 library is enclosed in the ns3 namespace.
bool operator!=(Callback< R, Args... > a, Callback< R, Args... > b)
Inequality test.
Definition: callback.h:674
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.h:166
SpectrumValue Pow(double lhs, const SpectrumValue &rhs)
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:159
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)