1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 University of Washington
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: Leonard Tracy <lentracy@gmail.com>
19  */
20 
21 #include "uan-prop-model.h"
22 #include "ns3/nstime.h"
23 #include <complex>
24 #include <vector>
25 
26 
27 namespace ns3 {
28 
29 std::ostream &
operator <<(std::ostream & os,const UanPdp & pdp)30 operator<< (std::ostream &os, const UanPdp &pdp)
31 {
32   os << pdp.GetNTaps () << '|';
33   os << pdp.GetResolution ().GetSeconds () << '|';
34 
35   UanPdp::Iterator it = pdp.m_taps.begin ();
36   for (; it != pdp.m_taps.end (); it++)
37     {
38       os << (*it).GetAmp () << '|';
39     }
40   return os;
41 
42 }
43 std::istream &
operator >>(std::istream & is,UanPdp & pdp)44 operator>> (std::istream &is, UanPdp &pdp)
45 {
46   uint32_t ntaps;
47   double resolution;
48   char c1;
49 
50   is >> ntaps >> c1;
51   if (c1 != '|')
52     {
53       NS_FATAL_ERROR ("UanPdp data corrupted at # of taps");
54       return is;
55     }
56   is >> resolution >> c1;
57   if (c1 != '|')
58     {
59       NS_FATAL_ERROR ("UanPdp data corrupted at resolution");
60       return is;
61     }
62   pdp.m_resolution = Seconds (resolution);
63 
64 
65   std::complex<double> amp;
66   pdp.m_taps = std::vector<Tap> (ntaps);
67   for (uint32_t i = 0; i < ntaps && !is.eof (); i++)
68     {
69       is >> amp >> c1;
70       if (c1 != '|')
71         {
72           NS_FATAL_ERROR ("UanPdp data corrupted at tap " << i);
73           return is;
74         }
75       pdp.m_taps[i] = Tap (Seconds (resolution * i), amp);
76     }
77   return is;
78 
79 }
80 
Tap()81 Tap::Tap ()
82   : m_amplitude (0.0),
83     m_delay (Seconds (0))
84 {
85 
86 }
87 
Tap(Time delay,std::complex<double> amp)88 Tap::Tap (Time delay, std::complex<double> amp)
89   : m_amplitude (amp),
90     m_delay (delay)
91 {
92 
93 }
94 
95 std::complex<double>
GetAmp(void) const96 Tap::GetAmp (void) const
97 {
98   return m_amplitude;
99 }
100 
101 Time
GetDelay(void) const102 Tap::GetDelay (void) const
103 {
104   return m_delay;
105 }
106 
107 
UanPdp()108 UanPdp::UanPdp ()
109 {
110 
111 }
112 
UanPdp(std::vector<Tap> taps,Time resolution)113 UanPdp::UanPdp (std::vector<Tap> taps, Time resolution)
114   : m_taps (taps),
115     m_resolution (resolution)
116 {
117 }
118 
UanPdp(std::vector<std::complex<double>> amps,Time resolution)119 UanPdp::UanPdp (std::vector<std::complex<double> > amps, Time resolution)
120   : m_resolution (resolution)
121 {
122   m_taps.resize (amps.size ());
123   Time arrTime = Seconds (0);
124   for (uint32_t index = 0; index < amps.size (); index++)
125     {
126       m_taps[index] = Tap (arrTime, amps[index]);
127       arrTime = arrTime + m_resolution;
128     }
129 }
130 
UanPdp(std::vector<double> amps,Time resolution)131 UanPdp::UanPdp (std::vector<double> amps, Time resolution)
132   : m_resolution (resolution)
133 {
134   m_taps.resize (amps.size ());
135   Time arrTime = Seconds (0);
136   for (uint32_t index = 0; index < amps.size (); index++)
137     {
138       m_taps[index] = Tap (arrTime, amps[index]);
139       arrTime = arrTime + m_resolution;
140     }
141 }
142 
~UanPdp()143 UanPdp::~UanPdp ()
144 {
145   m_taps.clear ();
146 }
147 
148 void
SetTap(std::complex<double> amp,uint32_t index)149 UanPdp::SetTap (std::complex<double> amp, uint32_t index)
150 {
151   if (m_taps.size () <= index)
152     {
153       m_taps.resize (index + 1);
154     }
155 
156   Time delay = index * m_resolution;
157   m_taps[index] = Tap (delay, amp);
158 }
159 const Tap &
GetTap(uint32_t i) const160 UanPdp::GetTap (uint32_t i) const
161 {
162   NS_ASSERT_MSG (i < GetNTaps (), "Call to UanPdp::GetTap with requested tap out of range");
163   return m_taps[i];
164 }
165 void
SetNTaps(uint32_t nTaps)166 UanPdp::SetNTaps (uint32_t nTaps)
167 {
168   m_taps.resize (nTaps);
169 }
170 void
SetResolution(Time resolution)171 UanPdp::SetResolution (Time resolution)
172 {
173   m_resolution = resolution;
174 }
175 UanPdp::Iterator
GetBegin(void) const176 UanPdp::GetBegin (void) const
177 {
178   return m_taps.begin ();
179 }
180 
181 UanPdp::Iterator
GetEnd(void) const182 UanPdp::GetEnd (void) const
183 {
184   return m_taps.end ();
185 }
186 
187 uint32_t
GetNTaps(void) const188 UanPdp::GetNTaps (void) const
189 {
190   return static_cast<uint32_t> (m_taps.size ());
191 }
192 
193 Time
GetResolution(void) const194 UanPdp::GetResolution (void) const
195 {
196   return m_resolution;
197 }
198 
199 std::complex<double>
SumTapsFromMaxC(Time delay,Time duration) const200 UanPdp::SumTapsFromMaxC (Time delay, Time duration) const
201 {
202   if (m_resolution <= Seconds (0))
203     {
204       NS_ASSERT_MSG (GetNTaps () == 1, "Attempted to sum taps over time interval in "
205                      "UanPdp with resolution 0 and multiple taps");
206 
207       if (delay.IsZero ()) return m_taps[0].GetAmp ();
208       return std::complex<double> (0.0, 0.0);
209     }
210 
211   uint32_t numTaps =  (duration / m_resolution + 0.5).GetHigh ();
212   double maxAmp = -1;
213   uint32_t maxTapIndex = 0;
214 
215   for (uint32_t i = 0; i < GetNTaps (); i++)
216     {
217       if (std::abs (m_taps[i].GetAmp ()) > maxAmp)
218         {
219           maxAmp = std::abs (m_taps[i].GetAmp ());
220           maxTapIndex = i;
221         }
222     }
223   uint32_t start = maxTapIndex + (delay / m_resolution).GetHigh ();
224   uint32_t end = std::min (start + numTaps, GetNTaps ());
225   std::complex<double> sum = 0;
226   for (uint32_t i = start; i < end; i++)
227     {
228       sum += m_taps[i].GetAmp ();
229     }
230   return sum;
231 }
232 double
SumTapsFromMaxNc(Time delay,Time duration) const233 UanPdp::SumTapsFromMaxNc (Time delay, Time duration) const
234 {
235   if (m_resolution <= Seconds (0))
236     {
237       NS_ASSERT_MSG (GetNTaps () == 1, "Attempted to sum taps over time interval in "
238                      "UanPdp with resolution 0 and multiple taps");
239 
240       if (delay.IsZero ()) return std::abs (m_taps[0].GetAmp ());
241       return 0;
242     }
243 
244   uint32_t numTaps =  (duration / m_resolution + 0.5).GetHigh ();
245   double maxAmp = -1;
246   uint32_t maxTapIndex = 0;
247 
248   for (uint32_t i = 0; i < GetNTaps (); i++)
249     {
250       if (std::abs (m_taps[i].GetAmp ()) > maxAmp)
251         {
252           maxAmp = std::abs (m_taps[i].GetAmp ());
253           maxTapIndex = i;
254         }
255     }
256 
257 
258   uint32_t start = maxTapIndex + (delay / m_resolution).GetHigh ();
259   uint32_t end = std::min (start + numTaps, GetNTaps ());
260   double sum = 0;
261   for (uint32_t i = start; i < end; i++)
262 
263     {
264       sum += std::abs (m_taps[i].GetAmp ());
265     }
266   return sum;
267 }
268 double
SumTapsNc(Time begin,Time end) const269 UanPdp::SumTapsNc (Time begin, Time end) const
270 {
271   if (m_resolution <= Seconds (0))
272     {
273       NS_ASSERT_MSG (GetNTaps () == 1, "Attempted to sum taps over time interval in "
274                      "UanPdp with resolution 0 and multiple taps");
275 
276       if (begin <= Seconds (0.0) && end >= Seconds (0.0))
277         {
278           return std::abs (m_taps[0].GetAmp ());
279         }
280       else
281         {
282           return 0.0;
283         }
284     }
285 
286   uint32_t stIndex = (begin / m_resolution + 0.5).GetHigh ();
287   uint32_t endIndex = (end / m_resolution + 0.5).GetHigh ();
288 
289   endIndex = std::min (endIndex, GetNTaps ());
290   double sum = 0;
291   for (uint32_t i = stIndex; i < endIndex; i++)
292     {
293       sum += std::abs (m_taps[i].GetAmp ());
294     }
295   return sum;
296 
297 }
298 
299 
300 
301 std::complex<double>
SumTapsC(Time begin,Time end) const302 UanPdp::SumTapsC (Time begin, Time end) const
303 {
304   if (m_resolution <= Seconds (0))
305     {
306       NS_ASSERT_MSG (GetNTaps () == 1, "Attempted to sum taps over time interval in "
307                      "UanPdp with resolution 0 and multiple taps");
308 
309       if (begin <= Seconds (0.0) && end >= Seconds (0.0))
310         {
311           return m_taps[0].GetAmp ();
312         }
313       else
314         {
315           return std::complex<double> (0.0);
316         }
317     }
318 
319   uint32_t stIndex = (begin / m_resolution + 0.5).GetHigh ();
320   uint32_t endIndex = (end / m_resolution + 0.5).GetHigh ();
321 
322   endIndex = std::min (endIndex, GetNTaps ());
323 
324   std::complex<double> sum = 0;
325   for (uint32_t i = stIndex; i < endIndex; i++)
326     {
327       sum += m_taps[i].GetAmp ();
328     }
329   return sum;
330 }
331 
332 UanPdp
NormalizeToSumNc(void) const333 UanPdp::NormalizeToSumNc (void) const
334 {
335   double sumNc = 0.0;
336   std::vector<Tap> newTaps;
337 
338   for (uint32_t i = 0; i < GetNTaps (); i++)
339     {
340       sumNc += std::abs (m_taps[i].GetAmp ());
341     }
342 
343   for (uint32_t i = 0; i < GetNTaps (); i++)
344     {
345       newTaps.push_back ( Tap (m_taps[i].GetDelay (), (m_taps[i].GetAmp () / sumNc)));
346     }
347 
348   return UanPdp (newTaps, m_resolution);
349 }
350 
351 UanPdp
CreateImpulsePdp(void)352 UanPdp::CreateImpulsePdp (void)
353 {
354   UanPdp pdp;
355   pdp.SetResolution (Seconds (0));
356   pdp.SetTap (1.0,0);
357   return pdp;
358 }
359 
360 NS_OBJECT_ENSURE_REGISTERED (UanPropModel);
361 
GetTypeId(void)362 TypeId UanPropModel::GetTypeId (void)
363 {
364   static TypeId tid = TypeId ("ns3::UanPropModel")
365     .SetParent<Object> ()
366     .SetGroupName ("Uan")
367   ;
368   return tid;
369 }
370 
371 void
Clear(void)372 UanPropModel::Clear (void)
373 {
374 }
375 
376 void
DoDispose(void)377 UanPropModel::DoDispose (void)
378 {
379   Clear ();
380   Object::DoDispose ();
381 }
382 
383 }
384