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
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
415operator+(const SpectrumValue& lhs, double rhs)
416{
417 SpectrumValue res = lhs;
418 res.Add(rhs);
419 return res;
420}
421
423operator+(double lhs, const SpectrumValue& rhs)
424{
425 SpectrumValue res = rhs;
426 res.Add(lhs);
427 return res;
428}
429
431operator-(const SpectrumValue& lhs, const SpectrumValue& rhs)
432{
433 SpectrumValue res = rhs;
434 res.ChangeSign();
435 res.Add(lhs);
436 return res;
437}
438
440operator-(const SpectrumValue& lhs, double rhs)
441{
442 SpectrumValue res = lhs;
443 res.Subtract(rhs);
444 return res;
445}
446
448operator-(double lhs, const SpectrumValue& rhs)
449{
450 SpectrumValue res = rhs;
451 res.Subtract(lhs);
452 return res;
453}
454
456operator*(const SpectrumValue& lhs, const SpectrumValue& rhs)
457{
458 SpectrumValue res = lhs;
459 res.Multiply(rhs);
460 return res;
461}
462
464operator*(const SpectrumValue& lhs, double rhs)
465{
466 SpectrumValue res = lhs;
467 res.Multiply(rhs);
468 return res;
469}
470
472operator*(double lhs, const SpectrumValue& rhs)
473{
474 SpectrumValue res = rhs;
475 res.Multiply(lhs);
476 return res;
477}
478
480operator/(const SpectrumValue& lhs, const SpectrumValue& rhs)
481{
482 SpectrumValue res = lhs;
483 res.Divide(rhs);
484 return res;
485}
486
488operator/(const SpectrumValue& lhs, double rhs)
489{
490 SpectrumValue res = lhs;
491 res.Divide(rhs);
492 return res;
493}
494
496operator/(double lhs, const SpectrumValue& rhs)
497{
498 SpectrumValue res = rhs;
499 res.Divide(lhs);
500 return res;
501}
502
505{
506 return rhs;
507}
508
511{
512 SpectrumValue res = rhs;
513 res.ChangeSign();
514 return res;
515}
516
518Pow(double lhs, const SpectrumValue& rhs)
519{
520 SpectrumValue res = rhs;
521 res.Exp(lhs);
522 return res;
523}
524
526Pow(const SpectrumValue& lhs, double rhs)
527{
528 SpectrumValue res = lhs;
529 res.Pow(rhs);
530 return res;
531}
532
535{
536 SpectrumValue res = arg;
537 res.Log10();
538 return res;
539}
540
543{
544 SpectrumValue res = arg;
545 res.Log2();
546 return res;
547}
548
551{
552 SpectrumValue res = arg;
553 res.Log();
554 return res;
555}
556
559{
560 Add(rhs);
561 return *this;
562}
563
566{
567 Subtract(rhs);
568 return *this;
569}
570
573{
574 Multiply(rhs);
575 return *this;
576}
577
580{
581 Divide(rhs);
582 return *this;
583}
584
587{
588 Add(rhs);
589 return *this;
590}
591
594{
595 Subtract(rhs);
596 return *this;
597}
598
601{
602 Multiply(rhs);
603 return *this;
604}
605
608{
609 Divide(rhs);
610 return *this;
611}
612
615{
616 auto it1 = m_values.begin();
617
618 while (it1 != m_values.end())
619 {
620 *it1 = rhs;
621 ++it1;
622 }
623 return *this;
624}
625
627SpectrumValue::operator<<(int n) const
628{
629 SpectrumValue res = *this;
630 res.ShiftLeft(n);
631 return res;
632}
633
636{
637 SpectrumValue res = *this;
638 res.ShiftRight(n);
639 return res;
640}
641
644{
645 return m_values.size();
646}
647
648const double&
650{
651 return m_values.at(pos);
652}
653
654} // 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: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)