forked from bobbyjsmith11/pll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loop_transfer.py
75 lines (59 loc) · 2.16 KB
/
loop_transfer.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
"""
=============
loop_transfer.py
=============
Get the loop transfer function
"""
from pll.phase_noise import load_pn_json
from plot_json import plot_loop_response
from pll.pll_calcs import *
import numpy as np
import json
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--pll_json', type=str,
help='pll json', default='data/pll.json')
parser.add_argument('--fstart', type=int,
help='starting offset in Hz', default=100)
parser.add_argument('--fstop', type=int,
help='ending offset in Hz', default=10e6)
parser.add_argument('--num_points', type=int,
help='number of points in sim', default=100)
parser.add_argument('--title', type=str,
help='plot title', default='add a title')
parser.add_argument("--save", dest="save_json", action='store_true')
parser.add_argument('-o', '--out_file', type=str,
help='file to save as json', default='data/xfer.json')
args = parser.parse_args()
pll = load_pn_json(args.pll_json)
# print(flt)
a = int(np.log10(args.fstart))
b = int(np.log10(args.fstop))
f = np.logspace(a, b, args.num_points)
print(json.dumps(pll, indent=2))
t2 = pll['t2']
a0 = pll['a0']
a1 = pll['a1']
a2 = pll['a2']
a3 = pll['a3']
kphi = pll['kphi']
kvco = pll['kvco']
R = pll['R']
N = pll['N']
cl_g_db = get_closed_loop_transfer_fn(f, kphi, kvco, R, N, t2, a0, a1, a2=a2, a3=a3)
g = get_open_loop_transfer_fn(f, kphi, kvco, N, t2, a0, a1, a2=a2, a3=a3, log=False)
ol_g_db = 20*np.log10(np.absolute(g))
pm = 180 + np.angle(g)*180/np.pi
d = {}
d['f'] = list(f)
d['cl_g_db'] = list(cl_g_db)
d['ol_g_db'] = list(ol_g_db)
d['pm'] = list(pm)
print("{:<14}{:<14}{:<14}{:<14}".format("FREQ", "OPEN_GAIN", "PHASE_MARGIN", "CLOSED"))
for k in range(len(f)):
print("{:<14.0f}{:<14.2f}{:<14.2f}{:<14.2f}".format(f[k], ol_g_db[k], pm[k], cl_g_db[k]))
if args.save_json:
with open(args.out_file, "w") as write_file:
json.dump(d, write_file, indent=2)
plot_loop_response(d, title=args.title)