-
Notifications
You must be signed in to change notification settings - Fork 0
/
xtal.py
151 lines (112 loc) · 3.38 KB
/
xtal.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
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker
from matplotlib.ticker import ScalarFormatter
import math
uH = 1e-6
mH = 1e-3
pF = 1e-12
nF = 1e-9
uF = 1e-6
Hz = 1
MHz = 1e6
kHz = 1e3
ohm = 1
kohm = 1000
Mohm = 1e6
def reson(L, C):
return 1 / (2 * math.pi * math.sqrt(L * C) )
def XL(f, L):
return 2j * math.pi * f * L;
def XC(f, C):
return -1j / (2 * math.pi * f * C)
def Series(lhs, rhs):
return lhs + rhs
def Parallel(lhs, rhs):
return (lhs * rhs) / (lhs + rhs)
def Xtal(f):
s = Rm + XL(f, Lm) + XC(f, Cm)
return Parallel(s, XC(f, Cs) )
def Xtal_imag(f):
return Xtal(f).imag
def Xtal_real(f):
return Xtal(f).real
def Xtal_C(f):
X = Xtal(f).imag
return 1 / (2 * math.pi * f * X)
# Motional
Cm = 0.05 * pF
Lm = 3 * mH
Rm = 10 * ohm
# shunt
Cs = 3 * pF;
print("Xtal")
print(f' Motional series:')
print(f' Lm: {Lm / mH} mH')
print(f' Cm: {Cm / pF} pF')
print(f' Rm: {Rm / ohm} ohm')
print(f' Shunted by:')
print(f' Cs: {Cs / pF} pF')
print("")
Fr = reson(Lm, Cm)
print(f'Series resonance of Lm and Cm, Fr: {Fr / MHz} MHz')
print(f' X_Lm: {XL(Fr, Lm) / kohm} kohm')
print(f' X_Cm: {XC(Fr, Cm) / kohm} kohm')
print("")
print('Vary frequency around the resonance of Lm and Cm and note that')
print('the Cs raises the zero-reactance frequency a very small amount,')
print('and also note that reading off Rm is not very critical with frequency.')
print('')
for offset in (-100, -10, -1, 0, 0.65, 1, 10, 100):
print(f'Xtal @ Fr + {offset} Hz: {Xtal(Fr + offset)} ohm' )
Cp = ((0.05 * 3) / (0.05 + 3)) * pF
print('')
print(f'Cp: {Cp / pF} pF')
Fp = reson(Lm, Cp)
print(f'Fp: {Fp / MHz} MHz')
print('')
print(f'Xtal @ 15 kHz: {Xtal_C(15 * kHz) / pF} pF')
print('')
factor = (Fp / Fr)
factor = factor * factor - 1;
print(f'factor: {factor}')
print(f'factor * Cs: {factor * Cs}')
print(f'actual Cm: {Cm}')
print('')
def plot_it(f_start, f_end, f_step, func, capt=""):
freq = np.arange(f_start, f_end, f_step)
func_v = np.vectorize(func)
y = func_v(freq)
fig, ax = plt.subplots()
ax.plot(freq, y)
ax.set_title(capt)
ax.set_xlabel("Frequency")
ax.grid(True)
ax.ticklabel_format(axis='x', style='sci', scilimits=(6,6), useOffset=False, useMathText=True)
def plot_X_and_C(f_start, f_end, f_step):
freq = np.arange(f_start, f_end, f_step)
func_imag = np.vectorize(Xtal_imag)
y_imag = func_imag(freq)
func_real = np.vectorize(Xtal_C)
y_real = func_real(freq)
fig, ax = plt.subplots()
ax.plot(freq, y_imag)
ax.set_xlabel("Frequency")
ax.set_ylabel("Reactance")
ax.ticklabel_format(axis='x', style='sci', scilimits=(6,6), useOffset=False, useMathText=True)
ax.ticklabel_format(axis='y', style='sci', scilimits=(3,3), useOffset=False, useMathText=True)
ax.set_title("R and C equivalent vs frequency")
ax2 = ax.twinx()
ax2.plot(freq, y_real, 'r-')
ax2.tick_params('y', colors='r')
ax2.set_ylabel("C equiv", color='r')
ax2.ticklabel_format(axis='y', style='sci', scilimits=(-9,-9), useOffset=False, useMathText=True)
ax.grid(True)
f_start = 12.9 * MHz
f_end = 13.2 * MHz
f_step = 10 * Hz
plot_it(f_start, f_end, f_step, Xtal_imag, "Reactance")
plot_it(f_start, f_end, f_step, Xtal_real, "Resistance")
plot_it(f_start, f_end, f_step, Xtal_C, "Series equiv. C")
plot_X_and_C(f_start, f_end, f_step)
plt.show()