logreg_premeasured.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import itertools as itt
  2. import sys
  3. import re
  4. import time
  5. import datetime
  6. import serial
  7. import win32event
  8. import win32file
  9. import win32api
  10. import pywintypes
  11. import subprocess
  12. import matplotlib
  13. import numpy as np
  14. from sklearn.linear_model import LinearRegression
  15. from scipy.signal import butter, filtfilt
  16. freq = 3000000
  17. sample_rate = 2000000
  18. amp = 1
  19. xgain = 35
  20. rf_filename = "test_linreg.bin"
  21. port = 11
  22. adc_filename = "pico_params.xml"
  23. below_trigger = False
  24. sync_filename = "Sync_param.xml"
  25. pico_rate = 80e6
  26. # sample rate - 1st element
  27. # period - 2nd element
  28. # inverse duty cycle - 3rd element
  29. # number - 4th element
  30. def load_csv(file_path):
  31. try:
  32. with open(file_path, 'r', encoding='utf-8') as f:
  33. cleaned_lines = []
  34. for line in f:
  35. line = line.strip()
  36. if line.endswith(','):
  37. line = line[:-1]
  38. cleaned_lines.append(line)
  39. f.close()
  40. loaded_array = np.loadtxt(cleaned_lines, delimiter=',')
  41. if loaded_array.ndim == 1:
  42. loaded_array = loaded_array.reshape(-1, 1)
  43. return loaded_array
  44. except Exception as e:
  45. print(f"Could not load file:\n{e}")
  46. return -1
  47. ls_tup = [
  48. list(item)
  49. for item in itt.product(
  50. range(1, 5),
  51. range(1, 8),
  52. range(1, 6),
  53. range(1, 5)
  54. )
  55. ]
  56. ls_ls = []
  57. for tup in ls_tup:
  58. ls_ls.append(list(tup))
  59. for x in ls_ls:
  60. if x[0] == 1: # sr in MHz
  61. x[0] = 2
  62. elif x[0] == 2:
  63. x[0] = 8
  64. elif x[0] == 3:
  65. x[0] = 10
  66. elif x[0] == 4:
  67. x[0] = 20
  68. if x[1] == 1: # period in ms
  69. x[1] = 0.24
  70. elif x[1] == 2:
  71. x[1] = 0.48
  72. elif x[1] == 3:
  73. x[1] = 0.96
  74. elif x[1] == 4:
  75. x[1] = 1.92
  76. elif x[1] == 5:
  77. x[1] = 3.84
  78. elif x[1] == 6:
  79. x[1] = 7.68
  80. elif x[1] == 7:
  81. x[1] = 11.52
  82. if x[2] == 1: # idc
  83. x[2] = 1.5
  84. elif x[2] == 2:
  85. x[2] = 2
  86. elif x[2] == 3:
  87. x[2] = 4
  88. elif x[2] == 4:
  89. x[2] = 8
  90. elif x[2] == 5:
  91. x[2] = 10
  92. if x[3] == 1: # time in sec
  93. x[3] = 100
  94. elif x[3] == 2:
  95. x[3] = 200
  96. elif x[3] == 3:
  97. x[3] = 400
  98. elif x[3] == 4:
  99. x[3] = 800
  100. x.append(x[1]*x[3])
  101. print(f"Num mes: {len(ls_ls) * 3}")
  102. res = 0
  103. y = np.array([])
  104. with open('delays.txt', 'r', encoding='utf-8') as f:
  105. cleaned_lines = []
  106. for line in f:
  107. res = re.split(r'[=;\n ]', line)
  108. for item in res:
  109. if item == 'delay':
  110. print(f'Delay: {float(res[4]) / 2}')
  111. y = np.append(y, [float(res[4]) / 2])
  112. f.close()
  113. for i in range(0, len(ls_ls)):
  114. with open('delays_updated.txt', 'a', encoding='utf-8') as f:
  115. f.write(f'sr = {ls_ls[i][0]} MHz; period = {ls_ls[i][1]} ms; idc = {ls_ls[i][2]}; time = {ls_ls[i][3]} s;\ndelay = {y[i]} nS;\n')
  116. f.close()
  117. print(np.size(y))
  118. reg = LinearRegression().fit(np.array(ls_ls), y)
  119. scr = reg.score(np.array(ls_ls), y)
  120. print('Coef: ', reg.coef_)
  121. print('Intercept: ', reg.intercept_)
  122. with open('regression5.txt', 'w', encoding='utf-8') as f:
  123. f.write(f'score = {scr};\nsr_coef = {reg.coef_[0]};\nperiod_coef = {reg.coef_[1]} ns/ms;\nidc_coef = {reg.coef_[2]};\nnum_coef = {reg.coef_[3]} ns/num;time_coef = {reg.coef_[4]} ns/ms;\nintercept = {reg.intercept_}\n\n')
  124. f.close()