calc.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. from cmath import atan
  2. import numpy as np
  3. def open_file(path):
  4. """depends on the format of file we open"""
  5. freq, re, im = [], [], []
  6. with open(path) as f:
  7. for line in f:
  8. temp = line[:-1].split(' ')
  9. for i in range(3):
  10. temp[i] = temp[i].replace(" ", "")
  11. freq.append(float(temp[0]))
  12. re.append(float(temp[1]))
  13. im.append(float(temp[2]))
  14. return freq, re, im
  15. def prepare_data(freq, re, im, fl=None):
  16. """the function takes raw data and gives vectors of eq (8)"""
  17. # finding fl from the point with smallest magnitude if argument not provided
  18. if fl is None:
  19. s = abs(np.array(re) + np.array(im)*1j)
  20. # frequency of loaded resonance
  21. fl = freq[list(abs(s)).index(min(abs(s)))]
  22. # frequency of unloaded resonance.
  23. f0 = fl
  24. # f0 = fl does not decrease the accuracy if Q >> 100
  25. e1, e2, e3, gamma, p = [], [], [], [], []
  26. for i in range(0, len(freq)):
  27. # filling vectors
  28. t = 2 * (freq[i] - fl) / f0
  29. g = re[i] + im[i] * 1j
  30. e1.append(t)
  31. e2.append(1)
  32. e3.append(-t * g)
  33. gamma.append(g)
  34. p.append(1 / (1 + t ** 2 * (1 + re[i] ** 2 + im[i] ** 2)))
  35. data = np.array([e1, e2, e3, gamma, p], dtype=np.cdouble)
  36. return data, fl
  37. def solution(data):
  38. """ takes projections of equation (8) on vectors e1, e2, e3 and solves the equations.
  39. It is also gives matrix of equation"""
  40. c = [] # matrix of the system
  41. b = [] # matrix extension
  42. for i in range(3):
  43. c1 = np.vdot(data[i], data[4] * data[0])
  44. c2 = np.vdot(data[i], data[4] * data[1])
  45. c3 = np.vdot(data[i], data[4] * data[2])
  46. c.append([c1, c2, c3])
  47. b.append(np.vdot(data[i], data[4] * data[3]))
  48. c = np.array(c)
  49. a = np.linalg.solve(c, b)
  50. d = np.linalg.inv(c) # inverse of matrix c
  51. return a, c, d
  52. def q_factor(a):
  53. """calculation of result"""
  54. Ql = a[2].imag # Q-factor of loaded resonator
  55. diam = abs(a[1] - a[0] / a[2]) # diameter of circle
  56. k = 1 / (2 / diam - 1)
  57. Q = Ql * (1 + k) # Q-factor = result
  58. return Ql, diam, k, Q
  59. def recalculation_of_data(data, a, c, d, error=False):
  60. """preparation data for the next iteration of solving system"""
  61. # data = np.array([e1, e2, e3, gamma, p], dtype=complex), t = e1, 1 = e2
  62. eps = np.array(a[0]*data[0] + a[1]*data[1] - a[2]*data[0]*data[3] - data[3], dtype=complex)
  63. # eps is eq(7) line's errors
  64. S2 = np.dot(abs(data[4]), abs(eps)*abs(eps)) # the weighted squared sum of errors
  65. sigma2A = [] # the square of standart deviation coefficients a
  66. temp = c[0][0]*d[0][0] + c[1][1]*d[1][1] + c[2][2]*d[2][2]
  67. for i in range(3):
  68. sigma2A.append(d[i][i] * S2 / temp)
  69. for i in range(len(data[4])): # recalculation of weight coefficients P
  70. data[4][i] = 1/(data[0][i]**2 * sigma2A[0] + sigma2A[1] + data[0][i]**2 * sigma2A[2] * (abs(data[3][i])**2))
  71. if error:
  72. return abs(np.array(sigma2A))
  73. else:
  74. return data
  75. def random_deviation(a, sigma2A, diam, k, Ql):
  76. """defines standart deviations of values"""
  77. sigmaQl = sigma2A[2]**0.5
  78. sigmaDiam = (sigma2A[0]/(abs(a[2])**2) + sigma2A[1] + abs(a[0]/a[2]/a[2])**2 * sigma2A[2])**0.5
  79. sigmaK = 2*sigmaDiam/((2-diam)**2)
  80. sigmaQ0 = ((1 + k)**2 * sigma2A[2] + Ql**2 * sigmaK**2)**0.5
  81. return sigmaQ0, sigmaQl
  82. def apply(filename):
  83. freq, re, im = open_file(filename)
  84. data = prepare_data(freq, re, im)
  85. a, c, d = solution(data)
  86. for i in range(2, 10):
  87. data = recalculation_of_data(data, a, c, d)
  88. a, c, d = solution(data)
  89. Ql, diam, k, Q = q_factor(a)
  90. sigma2A = recalculation_of_data(data, a, c, d, error=True)
  91. sigmaQ0, sigmaQl = random_deviation(a, sigma2A, diam, k, Ql)
  92. print(f"Q = {Q} +- {sigmaQ0}, if i == {i}")
  93. def fl_fitting(freq, re, im):
  94. """providing an option to find actual fl"""
  95. data, fl = prepare_data(freq, re, im)
  96. a, c, d = solution(data)
  97. # Repeated curve fitting
  98. # 1.189 of Qfactor Matlab
  99. # fl2 = 0
  100. # g_d=0
  101. # g_c=0
  102. for x in range(0, 3):
  103. g_c = (np.conj(a[2])*a[1]-a[0])/(np.conj(a[2])-a[2])
  104. g_d = a[0]/a[2]
  105. g_2 = 2*g_c-g_d
  106. dt = (a[1]-g_2)/(g_2*a[2]-a[0])
  107. fl2 = fl*(1 + np.real(dt)/2)
  108. data, fl = prepare_data(freq, re, im, fl2)
  109. a, c, d = solution(data)
  110. for i in range(2, 20):
  111. data = recalculation_of_data(data, a, c, d)
  112. a, c, d = solution(data)
  113. Ql, diam, k, Q = q_factor(a)
  114. sigma2A = recalculation_of_data(data, a, c, d, error=True)
  115. sigmaQ0, sigmaQl = random_deviation(a, sigma2A, diam, k, Ql)
  116. # taking into account coupling losses on page 69 of Qfactor Matlab
  117. # to get results similar to example program
  118. if False:
  119. phi1=np.arctan(np.double(g_d.imag/g_d.real)) # 1.239
  120. phi2=np.arctan(np.double((g_c.imag-g_d.imag)/(g_c.real-g_d.real)))
  121. phi=-phi1+phi2
  122. d_s=(1-np.abs(g_d)**2)/(1-np.abs(g_d)*np.cos(phi))
  123. diam = abs(a[1] - a[0] / a[2])
  124. qk=1/(d_s/diam-1)
  125. sigma2A = recalculation_of_data(data, a, c, d, error=True)
  126. sigmaQ0 = random_deviation(a, sigma2A, diam, k, Ql)
  127. Q = Ql * (1 + qk) # Q-factor = result
  128. print(f"Q0 = {Q} +- {sigmaQ0}")
  129. return Q,sigmaQ0, Ql, sigmaQl,a