-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.py
460 lines (390 loc) · 11.9 KB
/
utility.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# This is all for experiments at the minute and not being called by anything... yet
__author__="bolster"
__date__ ="$02-Dec-2010 18:48:38$"
import cmath, numpy as np, sys#, scipy.special as sps
from math import *
import functools,cPickle
import logging
# Log everything, and send it to stderr.
log = logging.getLogger('multiuserdsm')
log.setLevel(logging.INFO)
h = logging.StreamHandler()
f = logging.Formatter('%(asctime)s:%(levelname)-7s %(module)s %(lineno)d %(message)s')
h.setFormatter(f)
log.addHandler(h)
#Directories
rawdir="raw_results/"
profdir="profiles/"
graphdir="graphs/"
mp=True
material=[{ # awg 26
"r_0c":286.17578, # ohms/km
"a_c":0.14769620,
"l_0":675.36888e-6, # H/km
"l_inf":488.95186e-6, # H/km
"b":0.92930728,
"f_m":806.33863, # kHz
"c_inf":49e-9, # F/km
"c_0":0,
"c_e":0,
"g_0":43e-9, # S/km
"g_e":0.70,
},{
# BT_DWUG
"r_0c":179, # ohms/km
"a_c":35.89e-3,
"l_0":0.695e-3, # H/km
"l_inf":585e-6, # H/km
"b":1.2,
"f_m":1000, # kHz
"c_inf":55e-9, # F/km
"c_0":1e-9,
"c_e":0.1,
"g_0":0.5e-9, # S/km
"g_e":1.033
},{
# awg 24
"r_0c":174.55888, # ohms/km
"a_c":0.053073,
"l_0":617.29e-6, # H/km
"l_inf":478.97e-6, # H/km
"b":1.1529,
"f_m":553.760, # kHz
"c_inf":50e-9, # F/km
"c_0":0.0,
"c_e":0.0,
"g_0":234.87476e-15, # S/km
"g_e":1.38,
}]
t=2 #Type 3 in transferfn.c
tiny= 1e-300
half= 5.00000000000000000000e-01
one = 1.00000000000000000000e+00
two = 2.00000000000000000000e+00
erx = 8.45062911510467529297e-01
## Coefficients for approximation to erf in [0.84375,1.25]
pa0 = -2.36211856075265944077e-03
pa1 = 4.14856118683748331666e-01
pa2 = -3.72207876035701323847e-01
pa3 = 3.18346619901161753674e-01
pa4 = -1.10894694282396677476e-01
pa5 = 3.54783043256182359371e-02
pa6 = -2.16637559486879084300e-03
qa1 = 1.06420880400844228286e-01
qa2 = 5.40397917702171048937e-01
qa3 = 7.18286544141962662868e-02
qa4 = 1.26171219808761642112e-01
qa5 = 1.36370839120290507362e-02
qa6 = 1.19844998467991074170e-02
def erf1(x):
'''erf(x) for x in [0,0.84375]'''
e, i = frexp(x)
if abs(i)>28:
if abs(i)>57:
return 0.125*(8.0*x+efx8*x)
return x + efx*x
z = x*x
r = pp0+z*(pp1+z*(pp2+z*(pp3+z*pp4)))
s = one+z*(qq1+z*(qq2+z*(qq3+z*(qq4+z*qq5))))
y = r/s
return x + x*y
def erfc1(x):
'''erfc(x)for x in [0,0.84375]'''
e,i = frexp(x)
if abs(i)>56:
return one-x
z = x*x
r = pp0+z*(pp1+z*(pp2+z*(pp3+z*pp4)))
s = one+z*(qq1+z*(qq2+z*(qq3+z*(qq4+z*qq5))))
y = r/s
if (x<0.25):
return one-(x+x*y)
else:
r = x*y
r += (x-half)
return half - r
## Coefficients for approximation to erf in [0.84375,1.25]
pa0 = -2.36211856075265944077e-03
pa1 = 4.14856118683748331666e-01
pa2 = -3.72207876035701323847e-01
pa3 = 3.18346619901161753674e-01
pa4 = -1.10894694282396677476e-01
pa5 = 3.54783043256182359371e-02
pa6 = -2.16637559486879084300e-03
qa1 = 1.06420880400844228286e-01
qa2 = 5.40397917702171048937e-01
qa3 = 7.18286544141962662868e-02
qa4 = 1.26171219808761642112e-01
qa5 = 1.36370839120290507362e-02
qa6 = 1.19844998467991074170e-02
def erf2(x):
'''erf(x) for x in [0.84375,1.25]'''
s = fabs(x)-one
P = pa0+s*(pa1+s*(pa2+s*(pa3+s*(pa4+s*(pa5+s*pa6)))))
Q = one+s*(qa1+s*(qa2+s*(qa3+s*(qa4+s*(qa5+s*qa6)))))
if x>=0:
return erx + P/Q
return -erx - P/Q
def erfc2(x):
'''erfc(x) for x in [0.84375, 1.25]'''
return one-erf2(x)
## Coefficients for approximation to erfc in [1.25,1/0.35]
ra0 = -9.86494403484714822705e-03
ra1 = -6.93858572707181764372e-01
ra2 = -1.05586262253232909814e+01
ra3 = -6.23753324503260060396e+01
ra4 = -1.62396669462573470355e+02
ra5 = -1.84605092906711035994e+02
ra6 = -8.12874355063065934246e+01
ra7 = -9.81432934416914548592e+00
sa1 = 1.96512716674392571292e+01
sa2 = 1.37657754143519042600e+02
sa3 = 4.34565877475229228821e+02
sa4 = 6.45387271733267880336e+02
sa5 = 4.29008140027567833386e+02
sa6 = 1.08635005541779435134e+02
sa7 = 6.57024977031928170135e+00
sa8 = -6.04244152148580987438e-02
def erf3(x):
'''erf(x) for x in [1.25,2.857142]'''
x0=x
x = fabs(x)
s = one/(x*x)
R=ra0+s*(ra1+s*(ra2+s*(ra3+s*(ra4+s*(ra5+s*(ra6+s*ra7))))))
S=one+s*(sa1+s*(sa2+s*(sa3+s*(sa4+s*(sa5+s*(sa6+s*(sa7+s*sa8)))))))
z = ldexp(x0,0)
r = exp(-z*z-0.5625)*exp((z-x)*(z+x)+R/S)
if(x0>=0):
return one-r/x
else:
return r/x-one;
def erfc3(x):
'''erfc(x) for x in [1.25,1/0.35]'''
return one-erf3(x)
## Coefficients for approximation to erfc in [1/.35,28]
rb0 = -9.86494292470009928597e-03
rb1 = -7.99283237680523006574e-01
rb2 = -1.77579549177547519889e+01
rb3 = -1.60636384855821916062e+02
rb4 = -6.37566443368389627722e+02
rb5 = -1.02509513161107724954e+03
rb6 = -4.83519191608651397019e+02
sb1 = 3.03380607434824582924e+01
sb2 = 3.25792512996573918826e+02
sb3 = 1.53672958608443695994e+03
sb4 = 3.19985821950859553908e+03
sb5 = 2.55305040643316442583e+03
sb6 = 4.74528541206955367215e+02
sb7 = -2.24409524465858183362e+01
def erf4(x):
'''erf(x) for x in [1/.35,6]'''
x0=x
x = fabs(x)
s = one/(x*x)
R=rb0+s*(rb1+s*(rb2+s*(rb3+s*(rb4+s*(rb5+s*rb6)))))
S=one+s*(sb1+s*(sb2+s*(sb3+s*(sb4+s*(sb5+s*(sb6+s*sb7))))))
z = ldexp(x0,0)
r = exp(-z*z-0.5625)*exp((z-x)*(z+x)+R/S)
if(z>=0):
return one-r/x
else:
return r/x-one;
def erfc4(x):
'''erfc(x) for x in [2.857142,6]'''
return one-erf4(x)
def erf5(x):
'''erf(x) for |x| in [6,inf)'''
if x>0:
return one-tiny
return tiny-one
def erfc5(x):
'''erfc(x) for |x| in [6,inf)'''
if (x>0):
return tiny*tiny
return two-tiny
#############
##inf = float('inf')
##nan = float('nan')
###########
inf = float(9e999)
def erf(x):
'''return the error function of x'''
f = float(x)
if (f == inf):
return 1.0
elif (f == -inf):
return -1.0
## elif (f is nan):
## return nan
else:
if (abs(x)<0.84375):
return erf1(x)
elif (0.84375<=abs(x)<1.25):
return erf2(x)
elif (1.25<=abs(x)<2.857142):
return erf3(x)
elif (2.857142<=abs(x)<6):
return erf4(x)
elif (abs(x)>=6):
return erf5(x)
def erfc(x):
'''return the complementary of error function of x'''
f = float(x)
if (f == inf):
return 0.0
elif (f is -inf):
return 2.0
## elif (f == nan):
## return nan
else:
if (abs(x)<0.84375):
return erfc1(x)
elif (0.84375<=abs(x)<1.25):
return erfc2(x)
elif (1.25<=abs(x)<2.857142):
return erfc3(x)
elif (2.857142<=abs(x)<6):
return erfc4(x)
elif (abs(x)>=6):
return erfc5(x)
CHANNEL_BANDWIDTH = 4312.5 #from include/multiuser_load.h
def do_transfer_function(length,freq,type=3, measure="m"):
'''
Let the transfer function default to type 3;
Allows for easy default change later
Allows for easy 'case-based' changes
'''
'''
Z=impedance/l, Y=admittance/l
Z=R+jwL, Y=G+jwC
Z0=Characteristic Impedence, gamma=propagation constant
Z0=sqrt(Z/Y), gamma=sqrt(Z*Y)
Should Use PyGSL, but start off with cmath
'''
#Length must be in KM
if measure == "m":
length /= 1000
elif measure == "km":
pass
else: raise TypeError('Improper measurement scale used')
w = 2 * pi * freq
#log.debug("Freq=%f,Len=%d",freq,length)
Z = complex(_R(freq),w*_L(freq))
Y = complex(_G(freq),w*_C(freq))
Z0 = cmath.sqrt(Z/Y)
gamma = cmath.sqrt(Z*Y)
gammad=gamma*length
Zs = complex(100,0)
Zl = complex(100,0)
upper = Z0 * ( 1/cmath.cosh(gammad)) #sech=1/cosh
lower = Zs * ( (Z0/Zl) + cmath.tanh(gammad) ) + Z0 * ( 1+ (Z0/Zl)*cmath.tanh(gammad) )
H = upper/lower
H*=H
return cmath.polar(H)[0] #polar returns (abs(h),real(h))
def _R(freq):
'''
Return Resistance Parameter for transfer function
'''
c_partial = pow(material[t]["a_c"]*freq*freq+pow(material[t]["r_0c"],4),(0.25))
try:
if material[t]["r_0s"] > 0:
s_partial = pow(material[t]["a_s"]*pow(freq,2)+pow(material[t]["r_0s"],4),(0.25))
return (c_partial*s_partial)/(c_partial+s_partial)
except KeyError:
return c_partial
def _L(freq):
'''
Return Inductance Parameter for transfer function
'''
upper=(material[t]["l_0"]+material[t]["l_inf"]*pow(freq*1e-3/material[t]["f_m"],material[t]["b"]))
lower=(1+pow(freq*1e-3/material[t]["f_m"],material[t]["b"]))
return (upper/lower)
def _C(freq):
'''
Return Capacitance Parameter for transfer function
'''
return material[t]["c_inf"]+material[t]["c_0"]*pow(freq,-material[t]["c_e"])
def _G(freq):
'''
Return Conductance Parameter for transfer function
'''
return material[t]["g_0"]*pow(freq,material[t]["g_e"])
'''
Mathematical Utilities
'''
def dbmhz_to_watts(psd):
return UndB(psd)*1e-3*CHANNEL_BANDWIDTH
def watts_to_dbmhz(e):
return TodB((e*1e3)/CHANNEL_BANDWIDTH)
def UndB(input):
try:
return pow(10,input/10)
except ValueError:
#log.debug("Caught Exception on UndB(%f)"%input)
raise ValueError
except OverflowError:
log.info("Overflowed on UndB(%f)"%input)
raise OverflowError
def TodB(input):
try:
return 10*log10(input)
except ValueError:
#log.debug("Caught Exception on TodB(%f)"%input)
return -np.inf
def freq_on_tone(K):
'''
Assume ADSL downstream for now
'''
#return K * 4312.5 + 140156.25;
assert False==True, "Someone Tried to use freq_on_tone"
def complex2str(complex):
return '{0:.3f}{1:+.3f}i'.format(complex.real,complex.imag)
def _Q_1(value):
return sqrt(2)*erfc(1-2*value)
def _Ne(M): #from http://www.docstoc.com/docs/21599433/Basics-of-Digital-Modulation
rtM=sqrt(M)
return ((2*rtM)-1)/rtM
#Uncoded SNR Gap (Probability-bit-error:Pe,N Nearest Neighbours:Ne)
def get_GAMMA(Pe,M):
return (pow(_Q_1(Pe/_Ne(M)),2)/3)
#Combination Generator with replacements
def combinations(iterable, r,type=int):
# combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC
pool = tuple(iterable)
n = len(pool)
if not n and r:
return
indices = [0] * r
yield tuple(pool[i] for i in indices)
while True:
for i in reversed(range(r)):
if indices[i] != n - 1:
break
else:
return
indices[i:] = [indices[i] + 1] * (r - i)
yield tuple(pool[i] for i in indices)
#since numpy matrixes are a pain to convert to arrays
def mat2arr(matrix):
return np.squeeze(np.asarray(np.copy(matrix)))
#makes things human
def psd2str(psd):
assert(isinstance(psd,np.ndarray))
return str(map(watts_to_dbmhz,psd))
#Class overload for cost values NOT USED ATM
class CostValue(object):
def __set__(self,obj,val):
self.val= val if val > 0 else sys.maxint
#bitload from int(used for lk max)
def bitload_from_id(id,N,mbpt):
bitload=np.zeros(N)
for i in range(N):
bitload[i]=id%mbpt;
id/=mbpt;
return bitload
if __name__ == "__main__":
test_len=3000
test_f= 140156.25 #first channel for ADSL Downstream
test_result=0.000853
mine=do_transfer_function(test_len,test_f)
assert mine == test_result, "Transfer function isn't right"