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 while (it1 != pvf.ConstValuesEnd())
398 {
399 os << *it1 << " ";
400 ++it1;
401 }
402 os << std::endl;
403 return os;
404}
405
406SpectrumValue
407operator+(const SpectrumValue& lhs, const SpectrumValue& rhs)
408{
409 SpectrumValue res = lhs;
410 res.Add(rhs);
411 return res;
412}
413
414bool
416{
417 return (lhs.m_values == rhs.m_values);
418}
419
420bool
422{
423 return (lhs.m_values != rhs.m_values);
424}
425
427operator+(const SpectrumValue& lhs, double rhs)
428{
429 SpectrumValue res = lhs;
430 res.Add(rhs);
431 return res;
432}
433
435operator+(double lhs, const SpectrumValue& rhs)
436{
437 SpectrumValue res = rhs;
438 res.Add(lhs);
439 return res;
440}
441
443operator-(const SpectrumValue& lhs, const SpectrumValue& rhs)
444{
445 SpectrumValue res = rhs;
446 res.ChangeSign();
447 res.Add(lhs);
448 return res;
449}
450
452operator-(const SpectrumValue& lhs, double rhs)
453{
454 SpectrumValue res = lhs;
455 res.Subtract(rhs);
456 return res;
457}
458
460operator-(double lhs, const SpectrumValue& rhs)
461{
462 SpectrumValue res = rhs;
463 res.Subtract(lhs);
464 return res;
465}
466
468operator*(const SpectrumValue& lhs, const SpectrumValue& rhs)
469{
470 SpectrumValue res = lhs;
471 res.Multiply(rhs);
472 return res;
473}
474
476operator*(const SpectrumValue& lhs, double rhs)
477{
478 SpectrumValue res = lhs;
479 res.Multiply(rhs);
480 return res;
481}
482
484operator*(double lhs, const SpectrumValue& rhs)
485{
486 SpectrumValue res = rhs;
487 res.Multiply(lhs);
488 return res;
489}
490
492operator/(const SpectrumValue& lhs, const SpectrumValue& rhs)
493{
494 SpectrumValue res = lhs;
495 res.Divide(rhs);
496 return res;
497}
498
500operator/(const SpectrumValue& lhs, double rhs)
501{
502 SpectrumValue res = lhs;
503 res.Divide(rhs);
504 return res;
505}
506
508operator/(double lhs, const SpectrumValue& rhs)
509{
510 SpectrumValue res = rhs;
511 res.Divide(lhs);
512 return res;
513}
514
517{
518 return rhs;
519}
520
523{
524 SpectrumValue res = rhs;
525 res.ChangeSign();
526 return res;
527}
528
530Pow(double lhs, const SpectrumValue& rhs)
531{
532 SpectrumValue res = rhs;
533 res.Exp(lhs);
534 return res;
535}
536
538Pow(const SpectrumValue& lhs, double rhs)
539{
540 SpectrumValue res = lhs;
541 res.Pow(rhs);
542 return res;
543}
544
547{
548 SpectrumValue res = arg;
549 res.Log10();
550 return res;
551}
552
555{
556 SpectrumValue res = arg;
557 res.Log2();
558 return res;
559}
560
563{
564 SpectrumValue res = arg;
565 res.Log();
566 return res;
567}
568
571{
572 Add(rhs);
573 return *this;
574}
575
578{
579 Subtract(rhs);
580 return *this;
581}
582
585{
586 Multiply(rhs);
587 return *this;
588}
589
592{
593 Divide(rhs);
594 return *this;
595}
596
599{
600 Add(rhs);
601 return *this;
602}
603
606{
607 Subtract(rhs);
608 return *this;
609}
610
613{
614 Multiply(rhs);
615 return *this;
616}
617
620{
621 Divide(rhs);
622 return *this;
623}
624
627{
628 auto it1 = m_values.begin();
629
630 while (it1 != m_values.end())
631 {
632 *it1 = rhs;
633 ++it1;
634 }
635 return *this;
636}
637
639SpectrumValue::operator<<(int n) const
640{
641 SpectrumValue res = *this;
642 res.ShiftLeft(n);
643 return res;
644}
645
648{
649 SpectrumValue res = *this;
650 res.ShiftRight(n);
651 return res;
652}
653
656{
657 return m_values.size();
658}
659
660const double&
662{
663 return m_values.at(pos);
664}
665
666} // 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 ",...
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:680
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.h:157
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)