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 <ns3/log.h>
22#include <ns3/math.h>
23#include <ns3/spectrum-value.h>
24
25namespace ns3
26{
27
28NS_LOG_COMPONENT_DEFINE("SpectrumValue");
29
31{
32}
33
35 : m_spectrumModel(sof),
36 m_values(sof->GetNumBands())
37{
38}
39
40double&
42{
43 return m_values.at(index);
44}
45
46const double&
47SpectrumValue::operator[](size_t index) const
48{
49 return m_values.at(index);
50}
51
54{
55 return m_spectrumModel->GetUid();
56}
57
60{
61 return m_spectrumModel;
62}
63
64Values::const_iterator
66{
67 return m_values.begin();
68}
69
70Values::const_iterator
72{
73 return m_values.end();
74}
75
76Values::iterator
78{
79 return m_values.begin();
80}
81
82Values::iterator
84{
85 return m_values.end();
86}
87
88Bands::const_iterator
90{
91 return m_spectrumModel->Begin();
92}
93
94Bands::const_iterator
96{
97 return m_spectrumModel->End();
98}
99
100void
102{
103 Values::iterator it1 = m_values.begin();
104 Values::const_iterator it2 = x.m_values.begin();
105
106 NS_ASSERT(m_spectrumModel == x.m_spectrumModel);
107 NS_ASSERT(m_values.size() == x.m_values.size());
108
109 while (it1 != m_values.end())
110 {
111 *it1 += *it2;
112 ++it1;
113 ++it2;
114 }
115}
116
117void
119{
120 Values::iterator it1 = m_values.begin();
121
122 while (it1 != m_values.end())
123 {
124 *it1 += s;
125 ++it1;
126 }
127}
128
129void
131{
132 Values::iterator it1 = m_values.begin();
133 Values::const_iterator it2 = x.m_values.begin();
134
135 NS_ASSERT(m_spectrumModel == x.m_spectrumModel);
136 NS_ASSERT(m_values.size() == x.m_values.size());
137
138 while (it1 != m_values.end())
139 {
140 *it1 -= *it2;
141 ++it1;
142 ++it2;
143 }
144}
145
146void
148{
149 Add(-s);
150}
151
152void
154{
155 Values::iterator it1 = m_values.begin();
156 Values::const_iterator it2 = x.m_values.begin();
157
158 NS_ASSERT(m_spectrumModel == x.m_spectrumModel);
159 NS_ASSERT(m_values.size() == x.m_values.size());
160
161 while (it1 != m_values.end())
162 {
163 *it1 *= *it2;
164 ++it1;
165 ++it2;
166 }
167}
168
169void
171{
172 Values::iterator it1 = m_values.begin();
173
174 while (it1 != m_values.end())
175 {
176 *it1 *= s;
177 ++it1;
178 }
179}
180
181void
183{
184 Values::iterator it1 = m_values.begin();
185 Values::const_iterator it2 = x.m_values.begin();
186
187 NS_ASSERT(m_spectrumModel == x.m_spectrumModel);
188 NS_ASSERT(m_values.size() == x.m_values.size());
189
190 while (it1 != m_values.end())
191 {
192 *it1 /= *it2;
193 ++it1;
194 ++it2;
195 }
196}
197
198void
200{
201 NS_LOG_FUNCTION(this << s);
202 Values::iterator it1 = m_values.begin();
203
204 while (it1 != m_values.end())
205 {
206 *it1 /= s;
207 ++it1;
208 }
209}
210
211void
213{
214 Values::iterator it1 = m_values.begin();
215
216 while (it1 != m_values.end())
217 {
218 *it1 = -(*it1);
219 ++it1;
220 }
221}
222
223void
225{
226 int i = 0;
227 while (i < (int)m_values.size() - n)
228 {
229 m_values.at(i) = m_values.at(i + n);
230 i++;
231 }
232 while (i < (int)m_values.size())
233 {
234 m_values.at(i) = 0;
235 i++;
236 }
237}
238
239void
241{
242 int i = m_values.size() - 1;
243 while (i - n >= 0)
244 {
245 m_values.at(i) = m_values.at(i - n);
246 i = i - 1;
247 }
248 while (i >= 0)
249 {
250 m_values.at(i) = 0;
251 --i;
252 }
253}
254
255void
257{
258 NS_LOG_FUNCTION(this << exp);
259 Values::iterator it1 = m_values.begin();
260
261 while (it1 != m_values.end())
262 {
263 *it1 = std::pow(*it1, exp);
264 ++it1;
265 }
266}
267
268void
270{
271 NS_LOG_FUNCTION(this << base);
272 Values::iterator it1 = m_values.begin();
273
274 while (it1 != m_values.end())
275 {
276 *it1 = std::pow(base, *it1);
277 ++it1;
278 }
279}
280
281void
283{
284 NS_LOG_FUNCTION(this);
285 Values::iterator it1 = m_values.begin();
286
287 while (it1 != m_values.end())
288 {
289 *it1 = std::log10(*it1);
290 ++it1;
291 }
292}
293
294void
296{
297 NS_LOG_FUNCTION(this);
298 Values::iterator it1 = m_values.begin();
299
300 while (it1 != m_values.end())
301 {
302 *it1 = log2(*it1);
303 ++it1;
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::log(*it1);
316 ++it1;
317 }
318}
319
320double
322{
323 double s = 0;
324 Values::const_iterator it1 = x.ConstValuesBegin();
325 while (it1 != x.ConstValuesEnd())
326 {
327 s += (*it1) * (*it1);
328 ++it1;
329 }
330 return std::sqrt(s);
331}
332
333double
335{
336 double s = 0;
337 Values::const_iterator it1 = x.ConstValuesBegin();
338 while (it1 != x.ConstValuesEnd())
339 {
340 s += (*it1);
341 ++it1;
342 }
343 return s;
344}
345
346double
348{
349 double s = 0;
350 Values::const_iterator it1 = x.ConstValuesBegin();
351 while (it1 != x.ConstValuesEnd())
352 {
353 s *= (*it1);
354 ++it1;
355 }
356 return s;
357}
358
359double
361{
362 double i = 0;
363 Values::const_iterator vit = arg.ConstValuesBegin();
364 Bands::const_iterator bit = arg.ConstBandsBegin();
365 while (vit != arg.ConstValuesEnd())
366 {
367 NS_ASSERT(bit != arg.ConstBandsEnd());
368 i += (*vit) * (bit->fh - bit->fl);
369 ++vit;
370 ++bit;
371 }
372 NS_ASSERT(bit == arg.ConstBandsEnd());
373 return i;
374}
375
378{
379 Ptr<SpectrumValue> p = Create<SpectrumValue>(m_spectrumModel);
380 *p = *this;
381 return p;
382
383 // return Copy<SpectrumValue> (*this)
384}
385
392std::ostream&
393operator<<(std::ostream& os, const SpectrumValue& pvf)
394{
395 Values::const_iterator it1 = pvf.ConstValuesBegin();
396 while (it1 != pvf.ConstValuesEnd())
397 {
398 os << *it1 << " ";
399 ++it1;
400 }
401 os << std::endl;
402 return os;
403}
404
405SpectrumValue
406operator+(const SpectrumValue& lhs, const SpectrumValue& rhs)
407{
408 SpectrumValue res = lhs;
409 res.Add(rhs);
410 return res;
411}
412
414operator+(const SpectrumValue& lhs, double rhs)
415{
416 SpectrumValue res = lhs;
417 res.Add(rhs);
418 return res;
419}
420
422operator+(double lhs, const SpectrumValue& rhs)
423{
424 SpectrumValue res = rhs;
425 res.Add(lhs);
426 return res;
427}
428
430operator-(const SpectrumValue& lhs, const SpectrumValue& rhs)
431{
432 SpectrumValue res = rhs;
433 res.ChangeSign();
434 res.Add(lhs);
435 return res;
436}
437
439operator-(const SpectrumValue& lhs, double rhs)
440{
441 SpectrumValue res = lhs;
442 res.Subtract(rhs);
443 return res;
444}
445
447operator-(double lhs, const SpectrumValue& rhs)
448{
449 SpectrumValue res = rhs;
450 res.Subtract(lhs);
451 return res;
452}
453
455operator*(const SpectrumValue& lhs, const SpectrumValue& rhs)
456{
457 SpectrumValue res = lhs;
458 res.Multiply(rhs);
459 return res;
460}
461
463operator*(const SpectrumValue& lhs, double rhs)
464{
465 SpectrumValue res = lhs;
466 res.Multiply(rhs);
467 return res;
468}
469
471operator*(double lhs, const SpectrumValue& rhs)
472{
473 SpectrumValue res = rhs;
474 res.Multiply(lhs);
475 return res;
476}
477
479operator/(const SpectrumValue& lhs, const SpectrumValue& rhs)
480{
481 SpectrumValue res = lhs;
482 res.Divide(rhs);
483 return res;
484}
485
487operator/(const SpectrumValue& lhs, double rhs)
488{
489 SpectrumValue res = lhs;
490 res.Divide(rhs);
491 return res;
492}
493
495operator/(double lhs, const SpectrumValue& rhs)
496{
497 SpectrumValue res = rhs;
498 res.Divide(lhs);
499 return res;
500}
501
504{
505 return rhs;
506}
507
510{
511 SpectrumValue res = rhs;
512 res.ChangeSign();
513 return res;
514}
515
517Pow(double lhs, const SpectrumValue& rhs)
518{
519 SpectrumValue res = rhs;
520 res.Exp(lhs);
521 return res;
522}
523
525Pow(const SpectrumValue& lhs, double rhs)
526{
527 SpectrumValue res = lhs;
528 res.Pow(rhs);
529 return res;
530}
531
534{
535 SpectrumValue res = arg;
536 res.Log10();
537 return res;
538}
539
542{
543 SpectrumValue res = arg;
544 res.Log2();
545 return res;
546}
547
550{
551 SpectrumValue res = arg;
552 res.Log();
553 return res;
554}
555
558{
559 Add(rhs);
560 return *this;
561}
562
565{
566 Subtract(rhs);
567 return *this;
568}
569
572{
573 Multiply(rhs);
574 return *this;
575}
576
579{
580 Divide(rhs);
581 return *this;
582}
583
586{
587 Add(rhs);
588 return *this;
589}
590
593{
594 Subtract(rhs);
595 return *this;
596}
597
600{
601 Multiply(rhs);
602 return *this;
603}
604
607{
608 Divide(rhs);
609 return *this;
610}
611
614{
615 Values::iterator it1 = m_values.begin();
616
617 while (it1 != m_values.end())
618 {
619 *it1 = rhs;
620 ++it1;
621 }
622 return *this;
623}
624
626SpectrumValue::operator<<(int n) const
627{
628 SpectrumValue res = *this;
629 res.ShiftLeft(n);
630 return res;
631}
632
635{
636 SpectrumValue res = *this;
637 res.ShiftRight(n);
638 return res;
639}
640
643{
644 return m_values.size();
645}
646
647const double&
649{
650 return m_values.at(pos);
651}
652
653} // namespace ns3
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
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 ",...
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:129
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)